2012-10-20 17 views
2

我使用JekyllPygmentsMarkdown轉換爲靜態html頁面。內容準備爲GitHub pages。要顯示的代碼示例(在該示例外殼命令)我添加以下部分,將文件:如何擴展pygments.lexers.shell的關鍵字列表

{% highlight sh %} 
$ ls -1a 
. 
.. 
README 
{% endhighlight %} 

參數sh是指殼其構成詞法分析器。您也可以選擇其他詞法分析器,例如console以突出顯示文本。

我注意到一些基本命令如ls沒有被shell詞法分析器突出顯示。這也可以在source code of the lexer中看到。以下摘錄顯示shell詞法分析器的關鍵字定義(可在BashLexer類中找到)。

... 
'basic': [ 
    (r'\b(if|fi|else|while|do|done|for|then|return|function|case|' 
    r'select|continue|until|esac|elif)\s*\b', 
    Keyword), 
    (r'\b(alias|bg|bind|break|builtin|caller|cd|command|compgen|' 
    r'complete|declare|dirs|disown|echo|enable|eval|exec|exit|' 
    r'export|false|fc|fg|getopts|hash|help|history|jobs|kill|let|' 
    r'local|logout|popd|printf|pushd|pwd|read|readonly|set|shift|' 
    r'shopt|source|suspend|test|time|times|trap|true|type|typeset|' 
    r'ulimit|umask|unalias|unset|wait)\s*\b(?!\.)', 
    Name.Builtin), 
    (r'#.*\n', Comment), 
    (r'\\[\w\W]', String.Escape), 
    (r'(\b\w+)(\s*)(=)', bygroups(Name.Variable, Text, Operator)), 
    (r'[\[\]{}()=]', Operator), 
    (r'<<-?\s*(\'?)\\?(\w+)[\w\W]+?\2', String), 
    (r'&&|\|\|', Operator), 
], 
... 

有什麼方法可以擴展關鍵字列表,或者您可以推薦另一個詞法分析器嗎?

+1

你可以看看也很受歡迎的[彩虹語法熒光筆](https://github.com/ccampbell/rainbow)。它的外殼模式演示[這裏](http://htmlpreview.github.com/?https://raw.github.com/ccampbell/rainbow/master/demos/shell.html)。它突出了我嘗試過的'ls -1a',應該很容易整合。 –

回答

2

您可以通過編寫自己的詞法分析器來添加關鍵字到Pygments詞法分析器,該詞法分析器通過繼承另一個詞法分析器來添加新的關鍵字。請參閱pygments doc瞭解更多信息。我做了一個子類化的詞法分析器,在C++詞法分析器中增加了幾個關鍵字,可以在this Github repo中看到。

我不確定如何添加除EXTRA_KEYWORDS以外的其他類型的關鍵字。