Monday, May 19, 2014

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.

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.

    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?
 <pre style="background: #f1f1f1; color: black; overflow: auto;"> 

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


No comments:

Post a Comment