曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 4115|回复: 0
打印 上一主题 下一主题

自定义 syntaxhighlighter 语言格式刷文件

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34387
跳转到指定楼层
楼主
发表于 2011-7-1 11:07:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
yntaxhighlighter 版本:3.0.83

其实为新语言扩展语法高亮还是比较简单的,所要做的工作只要是增添一个格式刷文件,各种语法格式刷文件放在 code 文件夹下,并且是 js 文件格式。为了节省时间提高工作效率,可以复制一个与你所要添加的语言语法结构相近的格式刷文件到另一处,只要对其修改即可。比如这里以新增 Makefile 语言的格式刷为例。

在格式刷文件中,语法构成比较简单,修改的主要有 function Brush() 这个函数里的内容。在其中添加你要高亮的关键字,命令,函数等内容。比如我为 Makefile 我添加了下面内容:
var keywords =    'ifdef ifndef endif define endef ifeq ifneq else include -include sinclude override export unexport vpath';
        
        
        var commands =  'alias apropos awk basename bash bc bg builtin bzip2 cal cat cd cfdisk chgrp chmod chown chroot' +
                        'cksum clear cmp comm command cp cron crontab csplit cut date dc dd ddrescue declare df ' +
                        'diff diff3 dig dir dircolors dirname dirs du echo egrep eject enable env ethtool eval ' +
                        'exec exit expand export expr false fdformat fdisk fg fgrep fi file find fmt fold format ' +
                        'free fsck ftp gawk getopts grep groups gzip hash history hostname id ifconfig ' +
                        'import install join kill less let ln local locate logname logout look lpc lpr lprint ' +
                        'lprintd lprintq lprm ls lsof make man mkdir mkfifo mkisofs mknod more mount mtools ' +
                        'mv netstat nice nl nohup nslookup open op passwd paste pathchk ping popd pr printcap ' +
                        'printenv printf ps pushd pwd quota quotacheck quotactl ram rcp read readonly renice ' +
                        'remsync rm rmdir rsync screen scp sdiff sed select seq set sftp shift shopt shutdown ' +
                        'sleep sort source split ssh strace su sudo sum symlink sync tail tar tee test time ' +
                        'times touch top traceroute trap tr true tsort tty type ulimit umask umount unalias ' +
                        'uname unexpand uniq units unset unshar useradd usermod users uuencode uudecode v vdir ' +
                        'vi watch wc whereis which who whoami Wget xargs yes'
                        ;
        var functions =
                        'subst patsubst strip findstring filter filter-out sort word wordlist words firstword dir' +
                        'notdir suffix basename addsuffix addprefix join wildcard foreach if call shell error warning' +
                        'origin foreach eval value join'
var keywords 表示 Makefile 里的关键字;
var commands 表示 shell 命令,这部分其实不属于 Makefile 本身的东西,添加进去,是为了更美观些。
var functions  表示 Makefile 的内置函数。

接着要修改 this.regexList 这个东东里面的内容,这里面一般有两种格式,内置高亮 API 调用,以及自定义的正则表达式。

比如我要对关键字和函数进行高亮,我可以采用下面两条语句:
regex: new RegExp(this.getKeywords(keywords), 'gm'),        css: 'keyword bold' },
regex: new RegExp(this.getKeywords(commands), 'gm'),            css: 'functions' },
注意后半段 css: 中的信息定义,这里的定义只是调用了具有某种高亮效果的 css 文件,比如我希望对 Makefile 里的命令使用和 Bash 里的函数一样颜色的效果,我就定义 css: 'functions' 。

但是,有些特殊的符号并不能简单的使用上面的 css: 进行高亮,需要正则表达式。比如在 Makefile 中的 filter-out 函数,使用 css:functions 或别的什么效果是无法识别 filter 和 out 中间的 '-' 符号的,所以这时就需要使用正则表达式来识别。

需要采用正则表达式的有几个地方:
regex: /\$[@#$%^*<?]/gm,                                    css: 'preprocessor bold'
regex: /\$\(\w+\-\w+\)/g,                css: 'variable' },
regex: /\$\(\w+\)/g,                css: 'variable' },
regex: /filter\-out/g,            css: 'functions' },
第 1 条正则识别了 Makefile 里的 $@, $%, $< 等特殊变量。
第 2 和第 3 条识别普通变量,Makefile 里的变量引用是 $(变量名) 这样的格式的。注意有些变量是由 '-' 符号连接起来的,如果只是使用第 3 条,那么就无法识别这种情况。
第 4 条单独处理了 filter-out 这个函数。

最后该底下的一句:
Brush.aliases    = ['Makefile', 'MK'];
为你的格式刷添加一个别名。

到此整个改动已经完成了。我对 JS 虽然不了解,对正则也不是很熟练,但是这并不影响修改这格式刷文件:)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2024-6-17 10:03 , Processed in 0.072909 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表