幫SyntaxHighlighter-Plus加上新的語言樣版

WordPress 的 SyntaxHighlighter-Plus 外掛預設支援下列的語言

* Bash — `bash`, `sh`
* C++ — `cpp`, `c`, `c++`
* C# — `c#`, `c-sharp`, `csharp`
* CSS — `css`
* Delphi — `delphi`, `pascal`
* Diff — `diff`, `patch`
* Groovy — `groovy`
* Java — `java`
* JavaScript — `js`, `jscript`, `javascript`
* PHP — `php`
* Plain text — `plain`, `text`
* Python — `py`, `python`
* Ruby — `rb`, `ruby`, `rails`, `ror`
* SQL — `sql`
* VB — `vb`, `vbnet`, `vb.net`
* XML/HTML — `xml`, `html`, `xhtml`, `xslt`

如果今天想要加入一個新的語言該怎麼怎做呢?

只要2個步驟就可以完成了

1.把適用於SyntaxHighlighter-Plus的語言樣版(JavaScript)放到
/wp-content/plugins/syntaxhighlighter-plus/syntaxhighlighter/scripts

2.編輯/wp-content/plugins/syntaxhighlighter-plus/syntaxhighlighter.php
大約在在112行的位置加入樣版的名子

$this->aliases = apply_filters( ‘agsyntaxhighlighter_aliases’, array(
‘Bash’ => array(‘bash’, ‘sh’, ‘shell’),
‘Cpp’ => array(‘cpp’, ‘c’, ‘c++’),

例如要加入一個批次檔(bat)的樣版,把下面這行加入

‘Batch’ => array(‘batch’,’dos’,’bat’),

加入後變成

$this->aliases = apply_filters( ‘agsyntaxhighlighter_aliases’, array(
‘Bash’ => array(‘bash’, ‘sh’, ‘shell’),
‘Batch’ => array(‘batch’,’dos’,’bat’),
‘Cpp’ => array(‘cpp’, ‘c’, ‘c++’),

下面我們用例子來看看。

如果要加入Dos批次檔與Perl的語法樣版

首先,先把下面2個JavaScrip放到

/wp-content/plugins/syntaxhighlighter-plus/syntaxhighlighter/scripts

程式碼另存為shBrushBatch.js

/*shBrushBatch.js*/

SyntaxHighlighter.brushes.Batch = function()
{

var keywords =  'do else for in call choice goto shift pause errorlevel ' +
'if not exist lfnfor start setlocal endlocal echo set';

var commands =  'append attrib cd chdir chkdsk choice cls copy del erase deltree ' +
'dir exit fc comp fdisk find format fsutil help join ' +
'label loadfix md mkdir mem memmaker more move msd pcpark ' +
'print rd rmdir ren scandisk share sort subst sys ' +
'time date tree truename type undelete ver xcopy';

this.regexList = [
{ regex: SyntaxHighlighter.regexLib.singleLinePerlComments,             css: 'comments' },              // one line comments
{ regex: SyntaxHighlighter.regexLib.doubleQuotedString,                 css: 'string' },                // double quoted strings
{ regex: new RegExp(this.getKeywords(keywords), 'gm'),                  css: 'keyword' },               // keywords
{ regex: new RegExp(this.getKeywords(commands), 'gm'),                  css: 'functions' }              // commands
];
}

SyntaxHighlighter.brushes.Batch.prototype       = new SyntaxHighlighter.Highlighter();
SyntaxHighlighter.brushes.Batch.aliases         = ['dos', 'batch', 'bat'];

程式碼另存為shBrushPerl.js

</span></span>

程式碼另存為shBrushPerl.js



/*shBrushPerl.js*/

SyntaxHighlighter.brushes.Perl = function()
{
var funcs = 'abs accept alarm atan2 bind binmode bless caller chdir chmod chomp chop chown chr chroot close closedir connect cos crypt dbmclose dbmopen defined delete dump each endgrent endhostent endnetent endprotoent endpwent endservent eof exec exists exp fcntl fileno flock fork format formline getc getgrent getgrgid getgrnam gethostbyaddr gethostbyname gethostent getlogin getnetbyaddr getnetbyname getnetent getpeername getpgrp getppid getpriority getprotobyname getprotobynumber getprotoent getpwent getpwnam getpwuid getservbyname getservbyport getservent getsockname getsockopt glob gmtime grep hex import index int ioctl join keys kill lc lcfirst length link listen localtime lock log lstat m map mkdir msgctl msgget msgrcv msgsnd no oct open opendir ord pack pipe pop pos print printf prototype push q qq quotemeta qw qx rand read readdir readline readlink readpipe recv ref rename reset reverse rewinddir rindex rmdir scalar seek seekdir semctl semget semop send setgrent sethostent setnetent setpgrp setpriority setprotoent setpwent setservent setsockopt shift shmctl shmget shmread shmwrite shutdown sin sleep socket socketpair sort splice split sprintf sqrt srand stat study sub substr symlink syscall sysopen sysread sysseek system syswrite tell telldir tie tied time times tr truncate uc ucfirst umask undef unlink unpack unshift untie utime values vec waitpid wantarray warn write qr';

var keywords =  's select goto die do package redo require return continue for foreach last next wait while use if else elsif eval exit unless switch case';

var constants   = 'my our local';

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: /\$\w+/g,                                                                                      css: 'variable' },                      // variables
{ regex: new RegExp(this.getKeywords(funcs), 'gmi'),            css: 'functions' },                     // common functions
{ regex: new RegExp(this.getKeywords(constants), 'gmi'),        css: 'constants' },                     // constants
{ regex: new RegExp(this.getKeywords(keywords), 'gm'),          css: 'keyword' }                        // keyword
];

this.forHtmlScript(SyntaxHighlighter.regexLib.phpScriptTags);
};
SyntaxHighlighter.brushes.Perl.prototype = new SyntaxHighlighter.Highlighter();
SyntaxHighlighter.brushes.Perl.aliases  = ['perl','pl'];

最後在 /wp-content/plugins/syntaxhighlighter-plus/syntaxhighlighter.php 加入

‘Batch’ => array(‘batch’,’dos’,’bat’),
‘Perl’ => array(‘perl’, ‘pl’),

大功告成

參考資料:
shBrushBatch for Google SyntaxHighlighter
Perl Syntax Highlighter

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *