PHP语法高亮

Kon*_*lph 38 php syntax-highlighting

我正在寻找可以自定义的PHP语法高亮引擎(即我可以为新语言提供自己的标记器)并且可以同时处理多种语言(即在同一输出页面上).这个引擎必须与CSS类一起很好地工作,即它应该通过插入<span>class属性装饰的元素来格式化输出.可扩展模式的奖励点.

搜索客户端语法高亮脚本(JavaScript).

到目前为止,我一直坚持使用GeSHi.不幸的是,由于几个原因,GeSHi失败了.主要原因是不同的语言文件定义了完全不同的,不一致的样式.我已经花了好几个小时试图将不同的语言定义重构为一个共同点,但由于大多数定义文件本身都非常糟糕,我最终还是希望切换.

理想情况下,我想要一个类似于CodeRay,Pygments或JavaScript dp.SyntaxHighlighter的API.

澄清:

我在找编写的代码高亮软件 PHP中,没有 PHP(因为我需要用它从内PHP).

Kon*_*lph 48

由于没有现有工具满足我的需求,我自己写了.瞧,看哪:

的HyperLight

使用非常简单:只需使用

 <?php hyperlight($code, 'php'); ?>
Run Code Online (Sandbox Code Playgroud)

突出代码.编写新的语言定义也相对容易 - 使用正则表达式和功能强大但简单的状态机.顺便说一句,我仍然需要很多定义,所以请随时贡献.

  • 添加并不难.奖励![这里](http://pastebin.com/A7A7zrED)是我的通用代码荧光笔,可以合理地处理多种语言.测试:Java,C#,JavaScript,AS3,C,C++,Lua (3认同)

mic*_*man 10

[我将此答案标记为社区Wiki,因为您特别寻找Javascript]

http://softwaremaniacs.org/soft/highlight/是一个PHP(加上以下支持的其他语言列表)语法高亮库:

Python,Ruby,Perl,PHP,XML,HTML,CSS,Django,Javascript,VBScript,Delphi,Java,C++,C#,Lisp,RenderMan(RSL和RIB),Maya嵌入式语言,SQL,SmallTalk,Axapta,1C,Ini ,Diff,DOS .bat,Bash

它使用<span class ="keyword">样式标记.

它也已集成在dojo工具包中(作为dojox项目:dojox.lang.highlight)

虽然不是最常用的运行网络服务器的方式,严格来说,Javascript不仅在客户端实现,而且还有服务器端Javascript引擎/平台组合.


Tau*_*man 9

我在这里找到了用PHP编写的这个简单的通用语法高亮显示并修改了一下:

<?php

/**
 * Original => http://phoboslab.org/log/2007/08/generic-syntax-highlighting-with-regular-expressions
 * Usage => `echo SyntaxHighlight::process('source code here');`
 */

class SyntaxHighlight {
    public static function process($s) {
        $s = htmlspecialchars($s);

        // Workaround for escaped backslashes
        $s = str_replace('\\\\','\\\\<e>', $s); 

        $regexp = array(

            // Comments/Strings
            '/(
                \/\*.*?\*\/|
                \/\/.*?\n|
                \#.[^a-fA-F0-9]+?\n|
                \&lt;\!\-\-[\s\S]+\-\-\&gt;|
                (?<!\\\)&quot;.*?(?<!\\\)&quot;|
                (?<!\\\)\'(.*?)(?<!\\\)\'
            )/isex' 
            => 'self::replaceId($tokens,\'$1\')',

            // Punctuations
            '/([\-\!\%\^\*\(\)\+\|\~\=\`\{\}\[\]\:\"\'<>\?\,\.\/]+)/'
            => '<span class="P">$1</span>',

            // Numbers (also look for Hex)
            '/(?<!\w)(
                (0x|\#)[\da-f]+|
                \d+|
                \d+(px|em|cm|mm|rem|s|\%)
            )(?!\w)/ix'
            => '<span class="N">$1</span>',

            // Make the bold assumption that an
            // all uppercase word has a special meaning
            '/(?<!\w|>|\#)(
                [A-Z_0-9]{2,}
            )(?!\w)/x'
            => '<span class="D">$1</span>',

            // Keywords
            '/(?<!\w|\$|\%|\@|>)(
                and|or|xor|for|do|while|foreach|as|return|die|exit|if|then|else|
                elseif|new|delete|try|throw|catch|finally|class|function|string|
                array|object|resource|var|bool|boolean|int|integer|float|double|
                real|string|array|global|const|static|public|private|protected|
                published|extends|switch|true|false|null|void|this|self|struct|
                char|signed|unsigned|short|long
            )(?!\w|=")/ix'
            => '<span class="K">$1</span>',

            // PHP/Perl-Style Vars: $var, %var, @var
            '/(?<!\w)(
                (\$|\%|\@)(\-&gt;|\w)+
            )(?!\w)/ix'
            => '<span class="V">$1</span>'

        );

        $tokens = array(); // This array will be filled from the regexp-callback

        $s = preg_replace(array_keys($regexp), array_values($regexp), $s);

        // Paste the comments and strings back in again
        $s = str_replace(array_keys($tokens), array_values($tokens), $s);

        // Delete the "Escaped Backslash Workaround Token" (TM)
        // and replace tabs with four spaces.
        $s = str_replace(array('<e>', "\t"), array('', '    '), $s);

        return '<pre><code>' . $s . '</code></pre>';
    }

    // Regexp-Callback to replace every comment or string with a uniqid and save
    // the matched text in an array
    // This way, strings and comments will be stripped out and wont be processed
    // by the other expressions searching for keywords etc.
    private static function replaceId(&$a, $match) {
        $id = "##r" . uniqid() . "##";

        // String or Comment?
        if(substr($match, 0, 2) == '//' || substr($match, 0, 2) == '/*' || substr($match, 0, 2) == '##' || substr($match, 0, 7) == '&lt;!--') {
            $a[$id] = '<span class="C">' . $match . '</span>';
        } else {
            $a[$id] = '<span class="S">' . $match . '</span>';
        }
        return $id;
    }
}

?>
Run Code Online (Sandbox Code Playgroud)

演示: http ://phpfiddle.org/lite/code/1sf-htn


更新

我刚刚在这里创建了我自己的JavaScript通用语法荧光笔的PHP端口→ https://github.com/tovic/generic-syntax-highlighter/blob/master/generic-syntax-highlighter.php

如何使用:

<?php require 'generic-syntax-highlighter.php'; ?>
<pre><code><?php echo SH('&lt;div class="foo"&gt;&lt;/div&gt;'); ?></code></pre>
Run Code Online (Sandbox Code Playgroud)