【发布时间】:2008-11-19 07:31:19
【问题描述】:
我正在获得一个虚拟主机,并且我有团队垫的项目。我认为拥有自己的粘贴站点是一个不错的主意,该站点没有粘贴到期日期(我知道http://pastie.org/ 存在)和其他内容。我想知道。什么是我可以在代码上使用的简单高亮库?我只会使用 C/C++。
【问题讨论】:
标签: c++ c syntax-highlighting paste
我正在获得一个虚拟主机,并且我有团队垫的项目。我认为拥有自己的粘贴站点是一个不错的主意,该站点没有粘贴到期日期(我知道http://pastie.org/ 存在)和其他内容。我想知道。什么是我可以在代码上使用的简单高亮库?我只会使用 C/C++。
【问题讨论】:
标签: c++ c syntax-highlighting paste
问题被标记为“php”,但您“只会使用 C/C++”?
PHP 解决方案是GeSHi。
【讨论】:
只为一种语言(上下文无关,使用 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>";
}
【讨论】: