[PHP] Delphi Sytax Higlighting Script
Hallo,
ich suche ein Script, dem als $_GET Element eine Dateiname übergeben werden kann. Diese Datei soll das Script dann mit dem in Delphi / Pascal üblichen Systax Highlighting ausgeben. Kennt jemand so ein Script? Florian |
Re: [PHP] Delphi Sytax Higlighting Script
Das hier kannst du benutzen.
Code:
Mit include in dein Script einbinden und dann etwa wie folgt benutzen:
<?php
$syntax = array( 'delphi.dfm' => array( 'formats' => array( 'keyword' => 'font-weight:bold;', 'string' => 'color:#33f', 'number' => 'color:#f06', 'binary' => 'color:#093', ), 'groups' => array( '@' => array( "'" => array( 's' => "'", 'o' => "string" ), "#[0-9]+|#\\$[0-9a-fA-F]+" => array( 's' => "", 'o' => "string" ), "[-+]([0-9]+|[0-9]*\.[0-9]+|[0-9]*\.[0-9]+[eE][-+]?[0-9]+|\\$[0-9a-fA-F]+)[[:>:]]" => array( 's' => "", 'o' => "number" ), "[[:<:]]([0-9]+|[0-9]*\.[0-9]+|[0-9]*\.[0-9]+[eE][-+]?[0-9]+)[[:>:]]" => array( 's' => "", 'o' => "number" ), "\\$[0-9a-fA-F]+[[:>:]]" => array( 's' => "", 'o' => "number" ), "{" => array( 's' => "}", 'o' => "binary" ), ), ), 'keywords' => array( '@' => array( 'begin', 'end', 'object', ), ), ), 'pascal' => array( 'formats' => array( 'keyword' => 'font-weight:bold;', 'comment' => 'color:#090;', 'directive' => 'color:#099;', 'string' => 'color:#33f;', 'number' => 'color:#f06;', 'assembler' => 'color:#000;background-color:#999;', ), 'groups' => array( '@' => array( "{\\$" => array( 's' => "}", 'o' => "directive" ), "\\(\\*\\$" => array( 's' => "\\*\)", 'o' => "directive" ), "{" => array( 's' => "}", 'o' => "comment" ), "\\(\\*" => array( 's' => "\\*\\)", 'o' => "comment" ), "//" => array( 's' => "\n", 'o' => "comment" ), "'" => array( 's' => "'", 'o' => "string" ), "#[0-9]+|#\\$[0-9a-fA-F]+" => array( 's' => "", 'o' => "string" ), "[-+]([0-9]+|[0-9]*\.[0-9]+|[0-9]*\.[0-9]+[eE][-+]?[0-9]+|\\$[0-9a-fA-F]+)[[:>:]]" => array( 's' => "", 'o' => "number" ), "[[:<:]]([0-9]+|[0-9]*\.[0-9]+|[0-9]*\.[0-9]+[eE][-+]?[0-9]+)[[:>:]]" => array( 's' => "", 'o' => "number" ), "\\$[0-9a-fA-F]+[[:>:]]" => array( 's' => "", 'o' => "number" ), "[[:<:]]asm[[:>:]]" => array( 's' => "[[:<:]]end[[:>:]]", 'i' => "assembler" ), ), 'assembler' => array( "{" => array( 's' => "}", 'o' => "comment" ), "\\(\\*" => array( 's' => "\\*\\)", 'o' => "comment" ), "//" => array( 's' => "\n", 'o' => "comment" ), "'" => array( 's' => "'", 'o' => "string" ), '"' => array( 's' => '"', 'o' => "string" ), ), ), 'keywords' => array( '@' => array( 'and', 'array', 'as', 'asm', 'assembler', 'automated', 'begin', 'case', 'cdecl', 'class', 'const', 'constructor', 'default', 'destructor', 'dispinterface', 'div', 'do', 'downto', 'else', 'end', 'except', 'export', 'exports', 'external', 'far', 'file', 'final', 'finalization', 'finally', 'for', 'forward', 'function', 'goto', 'if', 'implementation', 'in', 'inherited', 'initialization', 'inline', 'interface', 'is', 'label', 'library', 'message', 'mod', 'name', 'near', 'nil', 'not', 'object', 'of', 'or', 'out', 'overload', 'override', 'packed', 'pascal', 'private', 'procedure', 'program', 'property', 'protected', 'public', 'published', 'raise', 'record', 'read', 'register', 'reintroduce', 'repeat', 'resourcestring', 'safecall', 'sealed', 'set', 'shl', 'shr', 'static', 'stdcall', 'string', 'then', 'threadvar', 'to', 'try', 'type', 'unit', 'unsafe', 'until', 'uses', 'var', 'varargs', 'virtual', 'while', 'with', 'write', 'xor', ), ), ), ); function _splitAtPattern($flat, $regex, $merge = TRUE) { $parts = spliti($regex, $flat); $result = array(); $result[] = $parts[0]; $flat = substr($flat, strlen($parts[0])); reset($parts); next($parts); while (list(,$part) = each($parts)) { if (eregi('^' . $regex, $flat, $regs)) $taglen = strlen($regs[0]); else $taglen = strcspn($flat, ">") + 1; $result[] = substr($flat, 0, $taglen); $result[] = $part; $flat = substr($flat, $taglen + strlen($part)); } return $result; } function do_keywords($text, $language, $context) { global $syntax; if (!isset($syntax[$language])) return htmlspecialchars($text); if (!isset($syntax[$language]['keywords'][$context])) return htmlspecialchars($text); if (!isset($syntax[$language]['formats']['keyword'])) return htmlspecialchars($text); $fmt = $syntax[$language]['formats']['keyword']; $rx = "[[:<:]](" . join('|', $syntax[$language]['keywords'][$context]) . ")[[:>:]]"; $all = _splitAtPattern($text, $rx, false); $res = ''; $cnt = count($all); for ($k = 0; $k < $cnt; $k++) { if ($k & 1) $res .= '<span style="' . $fmt . '">'; $res .= htmlspecialchars($all[$k]); if ($k & 1) $res .= '</span>'; } return $res; } function _isFullMatch($match, $string) { if (substr($match, 0, 7) == '[[:<:]]') $match = substr($match, 7); if (substr($match, -7, 7) == '[[:>:]]') $match = substr($match, 0, strlen($match) - 7); return eregi("^" . $match . "$", $string); } function do_highlight($text, $language, $context) { global $syntax; if (!isset($syntax[$language])) return $text; $res = ''; if (isset($syntax[$language]['formats'][$context])) $res .= '<span style="' . $syntax[$language]['formats'][$context] . '">'; if (!isset($syntax[$language]['groups'][$context])) $res .= do_keywords($text, $language, $context); else { while ($text != '') { $rx = join('|', array_keys($syntax[$language]['groups'][$context])); $splat = split($rx, $text, 2); if (count($splat) < 2) { $res .= do_keywords($text, $language, $context); break; } $res .= do_keywords($splat[0], $language, $context); $start = substr($text, strlen($splat[0]), strlen($text) - strlen($splat[0]) - strlen($splat[1])); $text = $splat[1]; $k = null; reset($syntax[$language]['groups'][$context]); while (list($key, $val) = each($syntax[$language]['groups'][$context])) if (_isFullMatch($key, $start)) { $k = $key; break; } if (empty($k)) { Debug('Internal error for "' . $start . '"'); $res .= do_keywords($text, $language, $context); break; } $val = $syntax[$language]['groups'][$context][$k]; if (empty($val['s'])) $res .= do_highlight($start, $language, $val['o']); else { $rx = $val['s']; $splat = split($rx, $text, 2); if (count($splat) < 2) { $splat[0] = $text; $splat[1] = ''; } $stop = substr($text, strlen($splat[0]), strlen($text) - strlen($splat[0]) - strlen($splat[1])); $text = $splat[1]; if (isset($val['o'])) $res .= do_highlight($start . $splat[0] . $stop, $language, $val['o']); elseif ($val['i']) $res .= do_keywords($start, $language, $context) . do_highlight($splat[0], $language, $val['i']) . do_keywords($stop, $language, $context); else $res .= do_keywords($start . $splat[0] . $stop, $language, '@'); } } } if (isset($syntax[$language]['formats'][$context])) $res .= '</span>'; return $res; } function highlight(&$text, $file) { if (eregi("\.(pas|dpr)$", $file)) return do_highlight(rtrim($text), 'pascal', '@'); if (eregi("\.(dfm)$", $file)) return do_highlight(rtrim($text), 'delphi.dfm', '@'); return htmlspecialchars($text); } ?>
Code:
(nicht meckern, ist'n schnell hingehackter Code für meine Garage gewesen)
$file = $_GET['file'];
echo "<pre>" . highlight(file_get_contents($file), $file) . "</pre>"; |
Re: [PHP] Delphi Sytax Higlighting Script
Also ein Dateiname zu übergeben erscheint mir komisch. Du willst doch wahrscheinlich eher eine ganze Datei hochladen, oder?
Also hier steht, wie man eine Datei hochläd. Wie man gut syntax-highlightet weiß ich alleridng auch nicht. Eventuell einfach mit preg_replace. Damit kannst du dann alle reservierten Wörter erstmal durch das Wort mit umklammern usw. Aber wenn da jemand ein besseres Konzept hat würde mich das mal interessieren... |
Re: [PHP] Delphi Sytax Higlighting Script
Danke!
|
Re: [PHP] Delphi Syntax Highlighting Script
Liste der Anhänge anzeigen (Anzahl: 1)
Kleiner Nachtrag:
Ich hab' aus dem Fragment oben 'ne kleine Bibliothek gemacht und noch mehr Sprachen hinzugefügt (ja, ich weiß: Syntax-Highlighter gibt's schon wie Sand am Meer ..., trotzdem hab' ich selbst einen geschrieben), zu finden in meiner Garage oder im Anhang. Angepasst an das letzte Beispiel wär' der Gebrauch so:
Code:
Formatierung anpassen kann man in "syntag.php" in den Zeilen 517-528.
include_once("syntag.php");
$file = $_GET['file']; echo "<pre>" . SynTag::Highlight(file_get_contents($file), $file) . "</pre>"; |
Re: [PHP] Delphi Sytax Higlighting Script
Ich empfehle GeSHi (Generic Syntax Highlighter).
|
Re: [PHP] Delphi Sytax Higlighting Script
Zitat:
Machte mir dabei aber etwas "zu viel", so dass das Ergebnis immer recht groß wurde (Dateigröße), und ich wollte eigentlich nicht '<span style="color:#000; font-weight:bold">(</span>' um jede Klammer gemacht haben. Sicher hätte ich einiges einstellen können, aber vieles ließ sich nicht "auf die Schnelle" konfigurieren, darum hab' ich mir lieber "auf die Schnelle" was eigenes geschrieben. |
Re: [PHP] Delphi Sytax Higlighting Script
Du kannst GeShi auf CSS-Dateien trimmen. Die werden dann nur einmal für jede Sprache erstellt.
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 19:53 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz