Objective-c Brush For SyntaxHighlighter

不知道为什么,SyntaxHighlighter 不带Objective-C的刷子,很多高亮插件都不带。。。 没办法只有自己写了,修修改改,凑合用吧。 暂时还有Bug,后面我会发完整包出来。

新增文件:【shBrushObjc.js】 - (scripts目录下)

/**
* Objective-c Brush For SyntaxHighlighter
*
* @version
* 0.9
*
* @copyright
* Copyright (C) 2011 http://isaced.blog.163.com
*
*/
;(function()
{
    // CommonJS
    typeof(require) != 'undefined' ? SyntaxHighlighter = require('shCore').SyntaxHighlighter : null;

    function Brush()
    {
        var datatypes = 'char bool BOOL double float int long short id void ';

        var keywords = 'IBAction IBOutlet SEL YES NO readwrite readonly nonatomic ' +
                        'retain assign readonly getter setter nil NULL ' +
                        'super self copy ' +
                        'break case catch class const copy __finally __exception __try ' +
                        'const_cast continue private public protected __declspec ' +
                        'default delete deprecated dllexport dllimport do dynamic_cast ' +
                        'else enum explicit extern if for friend goto inline ' +
                        'mutable naked namespace new noinline noreturn nothrow ' +
                        'register reinterpret_cast return selectany ' +
                        'sizeof static static_cast struct switch template this ' +
                        'thread throw true false try typedef typeid typename union ' +
                        'using uuid virtual volatile whcar_t while '
      
        //顺序很重要
        this.regexList = [
                        { regex: SyntaxHighlighter.regexLib.singleLineCComments,        css: 'comments' },              // one line comments
                        { regex: SyntaxHighlighter.regexLib.multiLineCComments,         css: 'comments' },              // multiline comments
                        { regex: SyntaxHighlighter.regexLib.doubleQuotedString,         css: 'color3' },                // double quoted strings
                        { regex: SyntaxHighlighter.regexLib.singleQuotedString,         css: 'color3' },                // single quoted strings
                        { regex: new RegExp('@\\w+\\b', 'g'),                           css: 'color2' },                // keyword pink 以@开头的可能是关键字
                        { regex: new RegExp('@', 'g'),                                  css: 'color3' },                // nsstring @"fsafds" red 以@开头的可能是nsstring
                        { regex: new RegExp('\.?\\b\\d+[a-z]?\\b', 'g'),                css: 'color6' },                // number blue 数字可以这样 23 23.4 23.4f 3l
                        { regex: new RegExp('^ *#.*', 'gm'),                            css: 'color5' },                // preprocessor brown
                        { regex: new RegExp('\\b(NS[A-Z]|UI[A-Z]|CG[A-Z])\\w+\\b', 'g'),css: 'color4' },                // builtInType purple
                        { regex: new RegExp(this.getKeywords(datatypes), 'gm'),      css: 'color2' },          // datatypes pink
                        { regex: new RegExp(this.getKeywords(keywords), 'gm'),          css: 'color2' },                // keyword pink
                        { regex: new RegExp('\\s+\\w+\\b\\s*(?=(:|\\]))', 'g'),         css: 'color7' },                // function call dark green 函数调用可能和三元操作符冲突
                        ];
    };

    Brush.prototype = new SyntaxHighlighter.Highlighter();
    Brush.aliases = ['objc', 'obj-c', 'objective-c'];

    SyntaxHighlighter.brushes.Objc = Brush;
    // CommonJS
    typeof(exports) != 'undefined' ? exports.Brush = Brush : null;
})();

修改文件:【shCoreDefault.css】 - (styles目录下)

/*---Objective-c---*/
/*shrimp purple*/
.syntaxhighlighter .color4, .syntaxhighlighter .color4 a {
  color: #663399 !important;
}
/*shrimp brown*/
.syntaxhighlighter .color5, .syntaxhighlighter .color5 a {
  color: #993300 !important;
}
/*shrimp blue*/
.syntaxhighlighter .color6, .syntaxhighlighter .color6 a {
  color: blue !important;
}
/*shrimp dark green*/
.syntaxhighlighter .color7, .syntaxhighlighter .color7 a {
  color: #005500 !important;
}

调用示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
 <title>Objective-c Brush For SyntaxHighlighter</title>
 <script type="text/javascript" src="scripts/shCore.js"></script>
 <script type="text/javascript" src="scripts/shBrushObjc.js"></script>
 <link type="text/css" rel="stylesheet" href="styles/shCoreDefault.css"/>
 <script type="text/javascript">SyntaxHighlighter.all();</script>
</head>

<body style="background: white; font-family: Helvetica">

<h1>Objective-c Brush For SyntaxHighlighter</h1>
<pre class="brush: obj-c;">
//Fraction.h
#import<Foundation/NSObject.h>
@interfaceFraction: NSObject{
    int numerator;
    int denominator;
}
    -(void) print;
    -(void) setNumerator: (int) n;
    -(void) setDenominator: (int) d;
    -(int) numerator;
    -(int) denominator;
@end
</pre>

</html>

另一种:

/**
 * Wordpress SyntaxHighlighter brush for Objective-C
 * By Matej Bukovinski, www.bukovinski.com
 *
 * Copyright (C) 2009 Matej Bukovinski
 * 
 * Adapted from:
 * SyntaxHighlighter - Objective-C Brush, version 1.0.0
 * http://codepirate.seaandco.com/
 * Copyright (C) 2009 Geoffrey Byers.
 * 
 * Licensed under a GNU Lesser General Public License.
 * http://creativecommons.org/licenses/LGPL/2.1/
 * 
 */

SyntaxHighlighter.brushes.ObjC = function() {
 
 var datatypes = 'char bool BOOL double float int long short id void';
 
 var keywords = 'IBAction IBOutlet SEL YES NO readwrite readonly nonatomic nil NULL ';
 keywords += 'super self copy ';
 keywords += 'break case catch class const copy __finally __exception __try ';
 keywords += 'const_cast continue private public protected __declspec ';
 keywords += 'default delete deprecated dllexport dllimport do dynamic_cast ';
 keywords += 'else enum explicit extern if for friend goto inline ';
 keywords += 'mutable naked namespace new noinline noreturn nothrow ';
 keywords += 'register reinterpret_cast return selectany ';
 keywords += 'sizeof static static_cast struct switch template this ';
 keywords += 'thread throw true false try typedef typeid typename union ';
 keywords += 'using uuid virtual volatile whcar_t while';
 
 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: new RegExp('^ *#.*', 'gm'),      css: 'preprocessor' }, // preprocessor
  { regex: new RegExp(this.getKeywords(datatypes), 'gm'),  css: 'color1 bold' },  // datatypes
  { regex: new RegExp(this.getKeywords(keywords), 'gm'),  css: 'keyword' },  // keyword
  { regex: new RegExp('\\bNS\\w+\\b', 'g'),     css: 'keyword' },  // keyword
  { regex: new RegExp('@\\w+\\b', 'g'),      css: 'keyword' },  // keyword
  { regex: new RegExp('@"(?:\\.|(\\\\\\")|[^\\""\\n])*"', 'g'), css: 'string' } // objc string  
  ];
 
}

SyntaxHighlighter.brushes.ObjC.prototype = new SyntaxHighlighter.Highlighter();
SyntaxHighlighter.brushes.ObjC.aliases = ['objc', 'obj-c'];