Rover12421's Blog

The End.

Wp-codebox插件修改

        wp-codebox同是代码高亮插件,用的GeSHi-1.0.8.3的核心。在其基础上增加了"Copy to clipboard",“View Code"和显示语种三个功能。但是看到后台的那个捐赠真的很不爽,汉化一下,增加几个功能就来个捐赠,真好意思…很怀疑作者又没有仔细看geshi.php中的代码,GeSHi-1.0.8.3的原geshi.php就有4619行,里面有很多内容,可惜我不懂php,看不明白。但是我要的一个功能没有,就是语法高亮,但语言有多种写法,比如php,我可以写 lang="php”,lang=“php4”,lang=php5 等等,syntaxhighlighter的核心做到了,原本以为codebox也有,后来发现没得,看geshi.php的源码中提供了这功能,但是为什么就不去实现呢?估计作者根本就没想这事儿,甚至geshi.php都没仔细瞧一瞧。你把版权写上就行了,还搞个什么捐赠,哪个啥。
        不废话了,看看这么实现这功能。geshi.php提供了这功能,我们找到get_language_name_from_extension()这个函数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    /**
     * Given a file extension, this method returns either a valid geshi language
     * name, or the empty string if it couldn't be found
     *
     * @param string The extension to get a language name for
     * @param array  A lookup array to use instead of the default one
     * @since 1.0.5
     * @todo Re-think about how this method works (maybe make it private and/or make it
     *       a extension->lang lookup?)
     * @todo static?
     */
    function get_language_name_from_extension( $extension, $lookup = array() ) {
        if ( !is_array($lookup) || empty($lookup)) {
            $lookup = array(
                'actionscript' => array('as'),
                'ada' => array('a', 'ada', 'adb', 'ads'),
                'apache' => array('conf'),
                'asm' => array('ash', 'asm', 'inc'),
                'asp' => array('asp'),
                'bash' => array('sh'),
                'bf' => array('bf'),
                'c' => array('c', 'h'),
                'c_mac' => array('c', 'h'),
                'caddcl' => array(),
                'cadlisp' => array(),
                'cdfg' => array('cdfg'),
                'cobol' => array('cbl'),
                'cpp' => array('cpp', 'hpp', 'C', 'H', 'CPP', 'HPP'),
                'csharp' => array('cs'),
                'css' => array('css'),
                'd' => array('d'),
                'delphi' => array('dpk', 'dpr', 'pp', 'pas'),
                'diff' => array('diff', 'patch'),
                'dos' => array('bat', 'cmd'),
                'gettext' => array('po', 'pot'),
                'gml' => array('gml'),
                'gnuplot' => array('plt'),
                'groovy' => array('groovy'),
                'haskell' => array('hs'),
                'html4strict' => array('html', 'htm'),
                'ini' => array('ini', 'desktop'),
                'java' => array('java'),
                'javascript' => array('js'),
                'klonec' => array('kl1'),
                'klonecpp' => array('klx'),
                'latex' => array('tex'),
                'lisp' => array('lisp'),
                'lua' => array('lua'),
                'matlab' => array('m'),
                'mpasm' => array(),
                'mysql' => array('sql'),
                'nsis' => array(),
                'objc' => array(),
                'oobas' => array(),
                'oracle8' => array(),
                'oracle10' => array(),
                'pascal' => array('pas'),
                'perl' => array('pl', 'pm'),
                'php' => array('php', 'php5', 'phtml', 'phps'),
                'povray' => array('pov'),
                'providex' => array('pvc', 'pvx'),
                'prolog' => array('pl'),
                'python' => array('py'),
                'qbasic' => array('bi'),
                'reg' => array('reg'),
                'ruby' => array('rb'),
                'sas' => array('sas'),
                'scala' => array('scala'),
                'scheme' => array('scm'),
                'scilab' => array('sci'),
                'smalltalk' => array('st'),
                'smarty' => array(),
                'tcl' => array('tcl'),
                'vb' => array('bas'),
                'vbnet' => array(),
                'visualfoxpro' => array(),
                'whitespace' => array('ws'),
                'xml' => array('xml', 'svg'),
                'z80' => array('z80', 'asm', 'inc')
            );
        }
 
        foreach ($lookup as $lang => $extensions) {
            if (in_array($extension, $extensions)) {
                return $lang;
            }
        }
        return '';
    }

通过get_language_name_from_extension()把lang=“#"中的语言过滤,找到对应的高亮语法文件。最后的返回空,我觉得应该改下,改成 return $extension;因为很多语法多写这并没完全写出来,当然可以根据自己需要修改。下面是我自己的修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    /**
     * Given a file extension, this method returns either a valid geshi language
     * name, or the empty string if it couldn't be found
     *
     * @param string The extension to get a language name for
     * @param array  A lookup array to use instead of the default one
     * @since 1.0.5
     * @todo Re-think about how this method works (maybe make it private and/or make it
     *       a extension->lang lookup?)
     * @todo static?
     *
     * @ Rover12421 Amend.
     */
    function get_language_name_from_extension( $extension, $lookup = array() ) {
        if ( !is_array($lookup) || empty($lookup)) {
            $lookup = array(
                'abap' => array('abap'),
                'actionscript' => array('as', 'actionscript'),          
                'ada' => array('a', 'ada', 'adb', 'ads'),
                'apache' => array('conf', 'apache', 'cnf'),
                'applescript' => array('applescript'),
                'asm' => array('ash', 'asm'),
                'asp' => array('asp'),
                'autoit' => array('autoit', 'au', 'au3'),
                'bash' => array('sh', 'bash', 'shell'),
                'basic4gl' => array('basic4gl'),
                'bf' => array('bf'),
                'c' => array('c', 'h'),
                'cpp-qt' => array('cpp-qt', 'qt'),
                'caddcl' => array('caddcl'),
                'cadlisp' => array('cadlisp'),
                'cdfg' => array('cdfg'),
                'cobol' => array('cbl', 'cobol'),
                'cpp' => array('cpp', 'hpp'),
                'csharp' => array('cs', 'csharp', 'c-sharp'),
                'css' => array('css'),
                'd' => array('d'),
                'delphi' => array('dpk', 'dpr', 'pp','delphi'),
                'diff' => array('diff', 'patch'),
                'dos' => array('dos', 'bat', 'cmd'),
                'gettext' => array('po', 'pot', 'gettext'),
                'gml' => array('gml'),
                'gnuplot' => array('plt', 'gnuplot'),
                'groovy' => array('groovy'),
                'haskell' => array('hs', 'haskell'),
                'html4strict' => array('html', 'htm', 'html4strict'),
                'ini' => array('ini', 'desktop'),
                'java' => array('java'),
                'javascript' => array('js', 'javascript', 'jscript'),
                'klonec' => array('kl1', 'klonec'),
                'klonecpp' => array('klx', 'klonecpp'),
                'latex' => array('tex', 'latex'),
                'lisp' => array('lisp'),
                'lua' => array('lua'),
                'matlab' => array('m', 'matlab'),
                'mpasm' => array('mpasm'),
                'mysql' => array('mysql','mysqli'),
                'nsis' => array('nsis'),
                'objc' => array('objc'),
                'oobas' => array('oobas'),
                'oracle8' => array('oracle8'),
                'oracle10' => array('oracle10', 'oracle'),
                'pascal' => array('pas', 'pascal'),
                'perl' => array('pl', 'pm', 'perl'),
                'php' => array('php', 'php4', 'php5', 'phtml', 'phps'),
                'povray' => array('pov', 'povray'),
                'providex' => array('pvc', 'pvx', 'providex'),
                'prolog' => array('pl', 'prolog'),
                'python' => array('py', 'python'),
                'qbasic' => array('bi', 'qbasic'),
                'reg' => array('reg'),
                'ruby' => array('rb', 'ruby', 'rails', 'ror'),
                'robots' => array('robots'),
                'sas' => array('sas'),
                'scala' => array('scala'),
                'scheme' => array('scm', 'scheme'),
                'scilab' => array('sci', 'scilab'),
                'smalltalk' => array('st', 'smalltalk'),
                'smarty' => array('smarty'),
                'sql' => array('sql'),
                'tcl' => array('tcl'),
                'tsql' => array('tsql', 't-sql'),
                'vb' => array('bas', 'vb'),
                'vbnet' => array('vbnet', 'vb.net'),
                'visualfoxpro' => array('visualfoxpro', 'foxpro', 'vfp'),
                'whitespace' => array('ws', 'whitespace'),
                'xml' => array('xml', 'svg', 'xhtml', 'xslt'),
                'z80' => array('z80', 'inc')
            );
        }
 
        foreach ($lookup as $lang => $extensions) {
            if (in_array($extension, $extensions)) {
                return $lang;
            }
        }
        return $extension;
    }

出来完后,我们在哪使用呢?就是在判断高亮出来文件是否存在的时候使用,就是function set_language()这个函数,只需要修改两句话就行了:

1
2
3
4
5
6
7
$language = strtolower($language);
 
        //Retreive the full filename
        $file_name = $this->language_path . $language . '.php';
        if ($file_name == $this->loaded_language) {
            // this language is already loaded!
            return;

把这段修改成下面这段

1
2
3
4
5
6
7
8
9
10
        $language = strtolower($language);
 
        $this->language = $this->get_language_name_from_extension($language);
 
        //Retreive the full filename
        /*$file_name = $this->language_path . $language . '.php';*/
        $file_name = $this->language_path . $this->language . '.php';
        if ($file_name == $this->loaded_language) {
            // this language is already loaded!
            return;

就这么简单的修改完成了。
虽然在这儿说很简单,但实际却花了我很长时间,因为自己不懂PHP。

Comments