[Go] SyntaxHighlighter

개발/Go 2016. 6. 28. 10:32
반응형

최신버전의 SyntaxHighlighter가 golang을 지원하지 않았다.


하지만, 역시 누군가 만들어둔게 있어서 이걸 스크립트 부분에 붙여넣으니 잘 동작을 했다.


/**
 * SyntaxHighlighter
 * http://alexgorbatchev.com/SyntaxHighlighter
 *
 * SyntaxHighlighter is donationware. If you are using it, please donate.
 * http://alexgorbatchev.com/SyntaxHighlighter/donate.html
 *
 * @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()
    {
        var funcs   =   'make';
 
        var keywords =  'break case chan const continue default defer else fallthrough for func go ' +
                        'goto if import interface map package range return select struct switch type var ';
 
        this.regexList = [
            { regex: SyntaxHighlighter.regexLib.singleLineCComments,    css: 'comments' },          // one line comments
            { regex: SyntaxHighlighter.regexLib.multiLineCComments,     css: 'comments' },          // multiline comments
            { regex: SyntaxHighlighter.regexLib.doubleQuotedString,     css: 'string' },            // double quoted strings
            { regex: SyntaxHighlighter.regexLib.singleQuotedString,     css: 'string' },            // single quoted strings
            { regex: /^var \w+/g,                                       css: 'variable' },          // variables
            { regex: /\b([\d]+(\.[\d]+)?|0x[a-f0-9]+)\b/gi,             css: 'value' },         // numbers
            { regex: new RegExp(this.getKeywords(funcs), 'gmi'),        css: 'functions' },         // common functions
            { regex: new RegExp(this.getKeywords(keywords), 'gm'),      css: 'keyword' }            // keyword
            ];
 
        this.forHtmlScript(SyntaxHighlighter.regexLib.phpScriptTags);
    };
 
    Brush.prototype = new SyntaxHighlighter.Highlighter();
    Brush.aliases   = ['golang'];
 
    SyntaxHighlighter.brushes.Golang = Brush;
 
    // CommonJS
    typeof(exports) != 'undefined' ? exports.Brush = Brush : null;
})();


위 구문을 <script></script> 부분에 추가한 후,

<pre class="brush:golang"></pre> 이런식으로 사용을 하면 된다.


물론 위 구문을 그대로 복사해서 .js 파일로 만든 후, 업로드하여 script부분에서는 src에 파일 경로를 넣어 참조하게끔하는 방법이 훨씬 간단하다.

(회사라 파일 업로드가 안되는 관계로...)


반응형

'개발 > Go' 카테고리의 다른 글

[Go] func 가변인자(dynamic arguments) 전달  (0) 2016.06.28
[Go] XML Marshal / UnMarshal  (0) 2016.06.28
[Go] LumberJack for Logging  (0) 2016.06.28
[개발환경] Go + SubLimeText 3 + GoSublime  (0) 2016.06.11
[Go] 간단한 채팅 서버  (0) 2016.06.11
,