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.
- Paste code into the Online Syntax Highlighter
- Paste that code back into your blog as HTML (use HTML view in Blogger)
- 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 {
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);
}
}
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);
}