Website insertion, syntax highlighting
I am getting web hosting and I have projects with teammats. I thought it would be nice to have my own paste site that doesn't have a paste expiration date (I know http://pastie.org/ ) and other things. I wanted to know. What is a simple lib highlighting that I can use for code? I would only use C / C ++.
+1
user34537
source
to share
2 answers
Making a token for just one language (no context, with regexes like C ++) is actually pretty straightforward, because you can basically wrap all your tokens in one big regex:
$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>";
}
0
source to share