01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
2009-09-29 Tue
■ google-code-prettify を読む [javascript]
google-code-prettify は JavaScript で書かれた、
pre 要素をシンタックスハイライトする。
D言語は対応していない。
このサイトでは、prettify.jsにキーワードを追加して対応している。
言語を追加する場合、prettify.jsを修正せずに
PR.registerLangHandlerで追加するのが本来のやり方。
PR.registerLangHandleを使ってD言語に対応させてみる。
PR.registerLangHandlerで特定の言語に対応するには、
PR.registerLangHandler(handler, ['d'])
これでクラス属性に"prettify lang-d"を持つpre要素に適用される、
ハンドラーを追加する。
google-code-prettify には、ハンドラーを出力する関数
PR.createSimpleLexer
PR.sourceDecorator
の2つが用意されている。
C言語に似ている言語の場合
PR.sourceDecorator を使うといい。
var D_KEYWORDS = "abstract alias align asm assert auto " + "body bool break byte " + "case cast catch cdouble cent cfloat char class const continue creal " + "dchar debug default delegate delete deprecated do double " + "else enum export extern " + "false final finally float for foreach function foreach_reverse " + "goto " + "idouble if ifloat import in inout int interface invariant ireal is " + "lazy long " + "macro mixin module " + "new nothrow null " + "out override " + "package pragma private protected public pure " + "real ref return scope shared short static struct super switch synchronized " + "template this throw true try typedef typeid typeof " + "ubyte ucent uint ulong union unittest ushort " + "version void volatile " + "wchar while with " + "__FILE__ __LINE__ __gshared __thread __traits "; PR.registerLangHandler(PR.sourceDecorator({ 'keywords': D_KEYWORDS, 'cStyleComments': true }), ['d']);
キーワードのハイライトが出来る。
しかし、この場合D言語特有の構文に対応できない。
- ネストできるブロックコメント
- WYSIWYG文字列
- 16進文字列
- デリミタ指定文字列
- トークン文字列
対応するためには、ハンドラーを自作する必要がある。