AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Programmieren allgemein [PHP] Delphi Sytax Higlighting Script
Thema durchsuchen
Ansicht
Themen-Optionen

[PHP] Delphi Sytax Higlighting Script

Ein Thema von Zacherl · begonnen am 8. Aug 2005 · letzter Beitrag vom 15. Aug 2005
Antwort Antwort
Benutzerbild von Zacherl
Zacherl

Registriert seit: 3. Sep 2004
4.629 Beiträge
 
Delphi 10.2 Tokyo Starter
 
#1

[PHP] Delphi Sytax Higlighting Script

  Alt 8. Aug 2005, 10:21
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
Projekte:
- GitHub (Profil, zyantific)
- zYan Disassembler Engine ( Zydis Online, Zydis GitHub)
  Mit Zitat antworten Zitat
Benutzerbild von Flocke
Flocke

Registriert seit: 9. Jun 2005
Ort: Unna
1.172 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#2

Re: [PHP] Delphi Sytax Higlighting Script

  Alt 8. Aug 2005, 10:35
Das hier kannst du benutzen.

Code:
<?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);
}

?>
Mit include in dein Script einbinden und dann etwa wie folgt benutzen:

Code:
$file = $_GET['file'];
echo "<pre>" . highlight(file_get_contents($file), $file) . "</pre>";
(nicht meckern, ist'n schnell hingehackter Code für meine Garage gewesen)
Volker
Besucht meine Garage
Aktuell: RtfLabel 1.3d, PrintToFile 1.4
  Mit Zitat antworten Zitat
Benutzerbild von yankee
yankee

Registriert seit: 10. Mär 2004
1.134 Beiträge
 
Lazarus
 
#3

Re: [PHP] Delphi Sytax Higlighting Script

  Alt 8. Aug 2005, 10:37
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...
Letzter Tipp: Drogen. Machen zwar nicht glücklich, geben einem aber wenigstens das Gefühl glücklich zu sein.

Have a lot of fun!
  Mit Zitat antworten Zitat
Benutzerbild von Zacherl
Zacherl

Registriert seit: 3. Sep 2004
4.629 Beiträge
 
Delphi 10.2 Tokyo Starter
 
#4

Re: [PHP] Delphi Sytax Higlighting Script

  Alt 8. Aug 2005, 12:34
Danke!
Projekte:
- GitHub (Profil, zyantific)
- zYan Disassembler Engine ( Zydis Online, Zydis GitHub)
  Mit Zitat antworten Zitat
Benutzerbild von Flocke
Flocke

Registriert seit: 9. Jun 2005
Ort: Unna
1.172 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#5

Re: [PHP] Delphi Syntax Highlighting Script

  Alt 15. Aug 2005, 10:01
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:
include_once("syntag.php");
$file = $_GET['file'];
echo "<pre>" . SynTag::Highlight(file_get_contents($file), $file) . "</pre>";
Formatierung anpassen kann man in "syntag.php" in den Zeilen 517-528.
Angehängte Dateien
Dateityp: zip syntag_0.2_602.zip (22,5 KB, 4x aufgerufen)
Volker
Besucht meine Garage
Aktuell: RtfLabel 1.3d, PrintToFile 1.4
  Mit Zitat antworten Zitat
Olli
(Gast)

n/a Beiträge
 
#6

Re: [PHP] Delphi Sytax Higlighting Script

  Alt 15. Aug 2005, 12:38
Ich empfehle Bei Google suchenGeSHi (Generic Syntax Highlighter).
  Mit Zitat antworten Zitat
Benutzerbild von Flocke
Flocke

Registriert seit: 9. Jun 2005
Ort: Unna
1.172 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#7

Re: [PHP] Delphi Sytax Higlighting Script

  Alt 15. Aug 2005, 12:48
Zitat von Olli:
Ich empfehle Bei Google suchenGeSHi (Generic Syntax Highlighter).
Hatte ich für meine Seite auch in Erwägung gezogen und ausprobiert, formatiert auch wirklich ansprechend und ist durchaus empfehlenswert.

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.
Volker
Besucht meine Garage
Aktuell: RtfLabel 1.3d, PrintToFile 1.4
  Mit Zitat antworten Zitat
Olli
(Gast)

n/a Beiträge
 
#8

Re: [PHP] Delphi Sytax Higlighting Script

  Alt 15. Aug 2005, 14:11
Du kannst GeShi auf CSS-Dateien trimmen. Die werden dann nur einmal für jede Sprache erstellt.
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 14:03 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