65.9K
CodeProject 正在变化。 阅读更多。
Home

博客发文的优秀代码语法高亮器

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.80/5 (2投票s)

2014年6月13日

CPOL

1分钟阅读

viewsIcon

8911

博客发文的优秀代码语法高亮器

如何将格式良好的代码放入你的博客,使其看起来像 Stack Overflow 一样好,或者至少像你的 IDE 一样好?

你需要一个在线语法高亮器,将你的源代码转换为漂亮的 HTML 代码。我喜欢 http://markup.su/highlighter/ 上的那个,并且觉得 Slush 和 Poppies 主题最吸引人。

  1. 将代码粘贴到 在线语法高亮器
  2. 将这段代码粘贴回你的博客,作为 HTML (在 Blogger 中使用 HTML 视图)
  3. 使用 CSS 设置 “overflow: auto;” 添加滚动条,以处理较长的文本行

Blogger 注意:如果你不想看到滚动条,你可能需要手动将列在 70 个字符处换行,以保持良好的外观,因为 Blogger 不介意让行超出其部分范围。

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();
    }
}

好的,现在看起来还不错,但是手动将行换行到 70 个字符很麻烦。有没有更好的博客工具,对程序员更友好?

注意:如果你在粘贴到语法高亮器之前没有手动换行,你会得到一个看起来很奇怪的行,超出示例区域。

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

你可以修改 <pre> 样式来添加滚动条,这看起来会更好一些。虽然我不知道为什么 Blogger 认为我应该得到的只是这个狭窄的空间。我不认为我要求太多,对吧?

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

 

 

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

 

© . All rights reserved.