Einzelnen Beitrag anzeigen

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