网站粘贴,语法高亮

时间:2008-11-19 07:31:19

标签: c++ c syntax-highlighting paste

我正在获得一个网络主机,我有团队合作项目。我认为让我自己的粘贴网站在粘贴(我知道http://pastie.org/存在)和其他东西上没有失效日期是一个不错的主意。我想知道。什么是一个简单的亮点lib我可以在代码上使用?我只会使用C / C ++。

2 个答案:

答案 0 :(得分:2)

问题是标记为“php”,但你“只使用C / C ++”?

PHP解决方案是GeSHi

答案 1 :(得分:0)

仅为一种语言构建荧光笔(无上下文,使用常规词法,例如C ++)实际上非常简单,因为您基本上可以将所有词汇包装成一个大的正则表达式:

$cpplex = '/
    (?<string>"(?:\\\\"|.)*?")|
    (?<char>\'(?:\\\\\'|.)*?\')|
    (?<comment>\\/\\/.*?\n|\\/\*.*?\*\\/)|
    (?<preprocessor>#\w+(?:\\\\\n|[^\\\\])*?\n)| # This one is not perfect!
    (?<number>
        (?: # Integer followed by optional fractional part.
            (?:0(?:
                    x[0-9a-f]+|[0-7]*)|\d+)
            (?:\.\d*)?(?:e[+-]\d+)?)
        |(?: # Just the fractional part.
            (?:\.\d*)(?:e[+-]\d+)?))|
    (?<keyword>asm|auto|break|case…)|            # TODO Complete. Include ciso646!
    (?<identifier>\\w(?:\\w|\\d)*)
    /xs';

$matches = preg_match_all($cpplex, $input, $matches, PREG_OFFSET_CAPTURE);

foreach ($matches as $match) {
    // TODO: determine which group was matched.
    // Don't forget lexemes that are *not* part of the expression:
    // i.e. whitespaces and operators. These are between the matches.
    echo "<span class=\"$keyword\">$token</span>";
}