Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Great Code Syntax Highlighter for Blog Posting

1.80/5 (2 votes)
13 Jun 2014CPOL1 min read 8.7K  
Great Code Syntax Highlighter for Blog Posting

How do you get nicely formatted code into your blog that looks as good as Stack Overflow or at least as good as your IDE?

You need an online syntax highlighter that converts your source code into pretty HTML code. I liked the one at http://markup.su/highlighter/ and found the Slush and Poppies theme to be the most attractive.

  1. Paste code into the Online Syntax Highlighter
  2. Paste that code back into your blog as HTML (use HTML view in Blogger)
  3. Add a scrollbar using the "overflow: auto;" CSS setting to handle longer lines of text

Blogger Notes: If you don't want to see scrollbars, you might want to manually wrap columns at 70 characters to keep it looking nice, because Blogger doesn't mind letting lines extend beyond their section.

C#
public class CTGTablePool {

    /**
    *
    * @param tableClass
    * @param c - Use application context if your process lives longer
    * than the activity
    * @return
    */
    public synchronized static < T > T getTable(Class < T > 
        tableClass, Context c) {
        
        self().checkInit();
        String hashKey = self().dbName + tableClass.getName();
        if (self().tableMap.get(hashKey) == null) {
            Utils.LogD(TablePool.class.getName(), 
                "Creating table connection for: " +
                tableClass.getName() + " in db: " + self().dbName);

            Constructor < T > construct;
            T tableInstance = null;
            try {
                construct = tableClass.getConstructor(Context.class,
                    String.class);
                tableInstance = (T)construct.newInstance(
                    c.getApplicationContext(), self().dbName);
                self().tableMap.put(hashKey, tableInstance);
            } catch (Exception e) {
                throw new RuntimeException(
                    "Could not create the table: " + 
                    tableClass.getName() + " in db: " + 
                    self().dbName + ", because of: " + e.toString());
            }
            return tableInstance;
        } else {
            Utils.LogD(TablePool.class.getName(), 
                "Reusing existing table connection for: " + 
                tableClass.getName() + " in db: " + self().dbName);
            return (T)self().tableMap.get(hashKey);
        }
    }

    /**
    * Reset all the database connections
    */
    public synchronized static void invalidate() {
        Utils.LogD(CTGTablePool.class.getName(), 
            "Invalidating database connections");
        self().tableMap.clear();
        self().tableVersionTable = null;
        TableManager.invalidate();
    }
}

Ok, now that looks ok, but it is a pain to manually wrap the lines to 70 chars. Help! Is there a better blogging tool that is more friendly to programmers?

Note: If you don't manually wrap it before pasting the code into the syntax highlighter, you get an odd looking line extending beyond the sample area.

C#
private void checkInit() {
    Assert.assertNotNull(CTGTablePool.class.getName()+
        ".setDbName() needs to be called first!", self().dbName);
}

You can modify the <pre> style to add a scroll bar instead which looks a little better. Although I don't know why Blogger seems to think all I should get is this narrow space. I don't think I am asking for too much, am I?

HTML
<pre style="background: #f1f1f1; color: black; overflow: auto;">

 

 

C#
private void checkInit() {
        Assert.assertNotNull(CTGTablePool.class.getName()+
             ".setDbName() needs to be called first!", self().dbName);
    }

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)