Click here to Skip to main content
11,639,765 members (60,325 online)
Click here to Skip to main content

Tagged as

Great Code Syntax Highlighter for Blog Posting

, 13 Jun 2014 CPOL 3.3K 5
Rate this:
Please Sign up or sign in to vote.
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);
    }

 

License

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

Share

About the Author

Right Handed Monkey
Chief Technology Officer WorxForUs
United States United States
I am a programmer who posts rambling on about java, Android, PHP, or whatever I am motivated to type on my charcoal colored Kinesis Freestyle2 keyboard. Please send +1's, shared links, warm thoughts of encouragement, or emasculating flames of internet fury to my blog. Why not? In fact, say anything... but please don't say 'thank'. I don't know, but something about it makes my skin crawl like an electric eel is asking to give me a kiss. No, thanks. (See there's an 's' in there. Was that really so hard?

righthandedmonkey.com

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 13 Jun 2014
Article Copyright 2014 by Right Handed Monkey
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid