最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】尝试给wordpress的SyntaxHighlighter插件添加支持更多(brush)语法:make(makefile),(Linux)终端(terminal)输出

WordPress crifan 3436浏览 0评论

【背景】

之前一直使用

SyntaxHighlighter

这个wordpress的插件,去支持语法高亮。

其对于常见的语言,比如C#,Python等等,支持的还是不错的。

但是最近遇到一些问题:

当遇到:

(1)Linux终端内容,当做bash/shell去格式化时,显示效果乱,且内容显示错误

想要语法高亮,Linux类(包括Cygwin)终端terminal的输出的信息的时候

由于是设置的brush是bash或shell,结果貌似由于语法本身不匹配,而导致高亮出来的效果,明显很乱。

这还是小问题,大问题是:

不少的时候,是把原先正常输出的信息,显示有误,比如某些字符被吞掉了,空格去去掉了

当然,查看网页源码时,此时之前输入的Linux终端输出的内容,还是正常的,是当时输入的内容。

只是显示出来的内容却都变了。

(2)不支持make,即makefile的语法

有时候,想要输入makefile的内容时

结果无法找到合适的语法高亮

所以也很不爽。

所以决定:

抽空,去为SyntaxHighlighter,增加:

(1)linux terminal,比如缩写为terminal

(2)makefile比如缩写为make

的语法高亮的支持。

【折腾过程】

1.抽取去主页:

http://alexgorbatchev.com/SyntaxHighlighter/download/

下载了其源码:

syntaxhighlighter_3.0.83.zip

解压后,随便去看了看,比如Python的brush的代码:

D:\tmp\tmp_dev_root\wordpress\syntaxhighlighter\syntaxhighlighter_3.0.83\syntaxhighlighter_3.0.83\scripts\shBrushPython.js

内容是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
 * SyntaxHighlighter
 *
 * SyntaxHighlighter is donationware. If you are using it, please donate.
 *
 * @version
 * 3.0.83 (July 02 2010)
 *
 * @copyright
 * Copyright (C) 2004-2010 Alex Gorbatchev.
 *
 * @license
 * Dual licensed under the MIT and GPL licenses.
 */
;(function()
{
    // CommonJS
    typeof(require) != 'undefined' ? SyntaxHighlighter = require('shCore').SyntaxHighlighter : null;
 
    function Brush()
    {
        // Contributed by Gheorghe Milas and Ahmad Sherif
     
        var keywords =  'and assert break class continue def del elif else ' +
                        'except exec finally for from global if import in is ' +
                        'lambda not or pass print raise return try yield while';
 
        var funcs = '__import__ abs all any apply basestring bin bool buffer callable ' +
                    'chr classmethod cmp coerce compile complex delattr dict dir ' +
                    'divmod enumerate eval execfile file filter float format frozenset ' +
                    'getattr globals hasattr hash help hex id input int intern ' +
                    'isinstance issubclass iter len list locals long map max min next ' +
                    'object oct open ord pow print property range raw_input reduce ' +
                    'reload repr reversed round set setattr slice sorted staticmethod ' +
                    'str sum super tuple type type unichr unicode vars xrange zip';
 
        var special =  'None True False self cls class_';
 
        this.regexList = [
                { regex: SyntaxHighlighter.regexLib.singleLinePerlComments, css: 'comments' },
                { regex: /^\s*@\w+/gm,                                      css: 'decorator' },
                { regex: /(['\"]{3})([^\1])*?\1/gm,                         css: 'comments' },
                { regex: /"(?!")(?:\.|\\\"|[^\""\n])*"/gm,                  css: 'string' },
                { regex: /'(?!')(?:\.|(\\\')|[^\''\n])*'/gm,                css: 'string' },
                { regex: /\+|\-|\*|\/|\%|=|==/gm,                           css: 'keyword' },
                { regex: /\b\d+\.?\w*/g,                                    css: 'value' },
                { regex: new RegExp(this.getKeywords(funcs), 'gmi'),        css: 'functions' },
                { regex: new RegExp(this.getKeywords(keywords), 'gm'),      css: 'keyword' },
                { regex: new RegExp(this.getKeywords(special), 'gm'),       css: 'color1' }
                ];
             
        this.forHtmlScript(SyntaxHighlighter.regexLib.aspScriptTags);
    };
 
    Brush.prototype = new SyntaxHighlighter.Highlighter();
    Brush.aliases   = ['py', 'python'];
 
    SyntaxHighlighter.brushes.Python = Brush;
 
    // CommonJS
    typeof(exports) != 'undefined' ? exports.Brush = Brush : null;
})();

很明显,貌似也不复杂,而且主要就是写合适的正则表达式,就可以搞定了。

2.所以,看来,抽空去给terminal和make添加语法支持,应该不是太复杂的事情,有空可以去试试了。

 

【总结】

剩下就是抽时间,去实现了。

转载请注明:在路上 » 【记录】尝试给wordpress的SyntaxHighlighter插件添加支持更多(brush)语法:make(makefile),(Linux)终端(terminal)输出

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.209 seconds, using 22.16MB memory