サイトに貼り付け、構文の強調表示
-
08-07-2019 - |
質問
ウェブホストを取得しています。チームマットを使用したプロジェクトがあります。貼り付けの有効期限がない独自の貼り付けサイトを作成するのは良い考えだと思いました( http:// pastieを知っています。 org / が存在します)など。知りたかった。コードで使用できるシンプルなハイライトライブラリは何ですか?私はC / C ++のみを使用します。
解決
質問には" php"というタグが付けられますしかし、「C / C ++のみを使用する」ということですか?
PHPソリューションは GeSHi です。
他のヒント
基本的にすべての語彙素を1つの大きな正規表現にラップできるので、1つの言語(コンテキスト無料、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>";
}
所属していません StackOverflow