incolla sul sito, evidenziazione della sintassi
-
08-07-2019 - |
Domanda
Sto ottenendo un host web e ho progetti con teammats. Ho pensato che sarebbe stata una buona idea avere il mio sito incolla che non ha una data di scadenza per incollare (lo so http: // pastie. org / esiste) e altre cose. Volevo sapere. Qual è un semplice lib evidenziatore che posso usare sul codice? vorrei usare solo C / C ++.
Soluzione
La domanda è taggata " php " ma tu " useresti solo C / C ++ " ;?
Una soluzione PHP è GeSHi .
Altri suggerimenti
Costruire un evidenziatore per una sola lingua (senza contesto, con lessici regolari come il C ++) è in realtà abbastanza facile perché in pratica puoi avvolgere tutti i tuoi lessemi in un'unica grande espressione regolare:
$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>";
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow