C++ 类模板和多文件编译 NetHack
Jul 23

一个在 Wordpress 中使代码加亮的办法,需要激活 MyCSS 插件,并且保证 WP Lightbox 2 插件是禁用的。

写一段程序代码(我是写的 C++),尽量包含最多的语法元素,然后把代码给 htmlize 了(可以用 Emacs 的 htmlize.el 或者 Vim),这里有一个要求,就是生成的 html 必须是 CSS 与 <pre> 分开的,CSS 在 <head> 里, <pre><body> 中,然后把 CSS copy 到 MyCSS 里。

以后贴代码的时候,只要用同样的方法生成 html,然后把那个 <pre> 贴到 post 里就行了。还有一点需要注意,MyCSS 会生成一个 my.css 放在 blog 的 <head> 里,位置是比较靠前的,在我这里比 Coolcode 和 SyntaxHighlighter 都要考前,所以 my.css 里的内容可能会被覆盖掉。

测试:

;; =============== Unfill-buffer ===============>
(defun unfill-buffer ()
  “Unfill current buffer.”
  (interactive “”)
  (setq m (point-marker))
  (beginning-of-buffer)
  (while (re-search-forward \\\\([^ ]+\\\\) *
 *\\\\([^ ]\\\\) nil t)
    (replace-match “\\\\1\\\\2″))
  (set-marker m 0 (current-buffer)))

顺便把我的 CSS 贴在这里,是 Emacs 的 htmlize 生成的,我做了一些改动:

.comment {
  /* font-lock-comment-face */
  color: #b22222;
  font-style: italic;
}
.comment-delimiter {
  /* font-lock-comment-delimiter-face */
  color: #b22222;
}
.constant {
  /* font-lock-constant-face */
  color: #5f9ea0;
}
.function-name {
  /* font-lock-function-name-face */
  color: #0000ff;
}
.keyword {
  /* font-lock-keyword-face */
  color: #a020f0;
  font-weight: bold;
}
.preprocessor {
  /* font-lock-preprocessor-face */
  color: #5f9ea0;
}
.string {
  /* font-lock-string-face */
  color: #ab7e7e;
}
.type {
  /* font-lock-type-face */
  color: #228b22;
}
.variable-name {
  /* font-lock-variable-name-face */
  color: #b8860b;
}

One Response to “Wordpress 中的代码加亮”

  1. Cheng Meng Says:

    不错,emacs的着色比vim要好。我几乎都忘了emacs该怎么用了

Leave a Reply

Close
E-mail It