使用 SyntaxHighlighter 3.0.83 對包含多行注釋(/* .... */) 的 Java 代碼進行語法著色時會將其后的所有代碼當成多行注釋進行著色。
修改 shBrushJava.js 可以解決這個問題。
找到:
this.regexList = [ { regex: SyntaxHighlighter.regexLib.singleLineCComments, css: 'comments' }, // one line comments { regex: /\/\*([^\*][\s\S]*)?\*\//gm, css: 'comments' }, // multiline comments { regex: /\/\*(?!\*\/)\*[\s\S]*?\*\//gm, css: 'preprocessor' }, // documentation comments { regex: SyntaxHighlighter.regexLib.doubleQuotedString, css: 'string' }, // strings { regex: SyntaxHighlighter.regexLib.singleQuotedString, css: 'string' }, // strings { regex: /\b([\d]+(\.[\d]+)?|0x[a-f0-9]+)\b/gi, css: 'value' }, // numbers { regex: /(?!\@interface\b)\@[\$\w]+\b/g, css: 'color1' }, // annotation @anno { regex: /\@interface\b/g, css: 'color2' }, // @interface keyword { regex: new RegExp(this.getKeywords(keywords), 'gm'), css: 'keyword' } // java keyword ];
修改為:
this.regexList = [ { regex: SyntaxHighlighter.regexLib.singleLineCComments, css: 'comments' }, // one line comments { regex: /\/\*(?!\*\/)\*[\s\S]*?\*\//gm, css: 'preprocessor' }, // documentation comments { regex: SyntaxHighlighter.regexLib.multiLineCComments, css: 'comments' }, // multiline comments { regex: SyntaxHighlighter.regexLib.doubleQuotedString, css: 'string' }, // strings { regex: SyntaxHighlighter.regexLib.singleQuotedString, css: 'string' }, // strings { regex: /\b([\d]+(\.[\d]+)?|0x[a-f0-9]+)\b/gi, css: 'value' }, // numbers { regex: /(?!\@interface\b)\@[\$\w]+\b/g, css: 'color1' }, // annotation @anno { regex: /\@interface\b/g, css: 'color2' }, // @interface keyword { regex: new RegExp(this.getKeywords(keywords), 'gm'), css: 'keyword' } // java keyword ];注意把處理文檔注釋的正則式放到處理多行注釋的正則式前。
本文是使用 B3log Solo 從 簡約設計の藝術(shù) 進行同步發(fā)布的