amiga-news ENGLISH VERSION
.
Links| Forum| Kommentare| News melden
.
Chat| Umfragen| Newsticker| Archiv
.

amiga-news.de Forum > Forum und Interna > [ code ] Test-Thread [ - Suche - Neue Beiträge - Registrieren - Login - ]

1 2 -3- [ - Beitrag schreiben - ]

07.05.2006, 22:25 Uhr

Holger
Posts: 8116
Nutzer
Ja, aber was, wenn's zufällig ein passendes öffnendes gibt?

so ala [ i] [ /i] [ /i]

Also so: test test [/i]

mfg

PS: das mit den doppelten x1A hatte seine Tücken, scheint jetzt aber zu funktionieren. Werde noch ein paar Testläufe machen...


--
Good coders do not comment. What was hard to write should be hard to read too.

[ - Antworten - Zitieren - Direktlink - ]

08.05.2006, 14:15 Uhr

_PAB_
Posts: 3016
Nutzer
@Holger:
> Ja, aber was, wenn's zufällig ein passendes öffnendes gibt?

Das ist dann halt Pech und der Autor des Beitrages wird es merken, wenn er sich seinen eigenen Beitrag ansieht.
Wie gesagt, Du kannst escapen soviel Du willst. Ich würde es aber besser finden, wenn Du nur das notwendige escapest, also nur gültige Tags (öffnend und schließend meinetwegen).

[ - Antworten - Zitieren - Direktlink - ]

21.05.2006, 16:07 Uhr

Holger
Posts: 8116
Nutzer
php code:
<?
#global
$keyword = '[blue][b]${0}[/b][/blue]';
$libfunc = '[blue]${0}[/blue]';
$literal = '[green]${0}[/green]';
$escape  = '[blue]${0}[/blue]';
$preproc = '[red]${0}[/red]';
$include = '${1}[green]${2}[/green]';
$comment = '[gray]${0}[/gray]';
$op_sep  = '[black]${0}[/black]';


#...

while(preg_match("/^(.*?)\s*[code language="?([^"]*)"?\]\s*(.*?)\s*\[\/code\]\r?\n?(.*?)$/is", $out, $matches)) {
  $orig_lang = $matches[2];
  $lang = strtolower($orig_lang);
  $code = $matches[3];
  $code = preg_replace("/x1Ax1A/", "\[", $code);
  $code = preg_replace("/\[(/?+(?:[biu]|black|red|green|blue|yellow|gray|code|img|url))b/", "x1Ax1A$1", $code);
  $code = html_entity_decode($code);
  highlight($code, $lang);
  $code = htmlentities($code);
  $out = $matches[1].'</font><blockquote>'.get_last_font().$orig_lang.' code:</font><hr><font face="Courier New, Courier, monospace" color="#400080"><pre>'.$code.'</pre></font><hr></blockquote>'.get_last_font().$matches[4];
}
// ---- Ohne Syntax-Highlighting
$out = preg_replace("/[code\]\s*(.*?)\s*\[\/code\]\r?\n?/is", '</font><blockquote>'.get_last_font().'code:</font><hr><font face="Courier New, Courier, monospace" color="#400080"><pre>${1}</pre></font><hr></blockquote>'.get_last_font(), $out);

#...

function highlight(&$code, $lang)
{
  global $keyword;
  global $libfunc;
  global $literal;
  global $escape;
  global $preproc;
  global $include;
  global $comment;
  global $op_sep;

  //sgml document & html declaration tags
  if(preg_match("/^(x?html|php|asp|hsc|jsp)$/", $lang)) {
    $comments="<!--.*?-->";
    $decl="<![^!>]*+>";
    $word="(?:\w[\w:\-_]*+\b)";
    $tags="(<\/\s*+$word\s*+>)|(<\s*+$word)((?:\s++$word(?:=(?:"[^"]*+"|$word))?+)*+\s*+>)";
    $script="<%(?:[^%]*+(?:%(?!>))*+)*+%>|<\?(?:[^?]*+(?:\?(?!>))*+)*+\?".">";
    $entity="\&(?:\#(?:[xX][0-9A-Fa-f]++|[0-9]++)|[a-zA-Z][a-zA-Z\-0-9]*+);";
    preg_match_all("/($comments)|($decl)|($script)|$tags|(<?+[^<]*+)/", $code, $chunks);
    $chunks[1]=preg_replace("/.+/s", $comment, $chunks[1]);
    $chunks[2]=preg_replace("/.+/s", $preproc, $chunks[2]);
    $keywords=&getKeywords('html');
    $myKeyword=preg_replace("/\$\{0\}/", '\${1}', $op_sep)
      .preg_replace("/\$\{0\}/", '\${2}', $keyword);
    $chunks[5]=preg_replace("/^(<\s*\/?\s*)($keywords)\b/", $myKeyword, $chunks[5]);
    $myKeyword.='${3}'.preg_replace("/\$\{0\}/", '>', $op_sep);
    $chunks[4]=preg_replace("/^(<\s*\/?\s*)($keywords)\b(.*)>/", $myKeyword, $chunks[4]);
    $chunks[6]=preg_replace("/[^>]++/", $libfunc, $chunks[6]);
    $chunks[6]=preg_replace("/(?<==)(?:"[^"]*+"|[^\s\r\n=">]+)/", $literal, $chunks[6]);
    $chunks[6]=preg_replace("/>$/", $op_sep, $chunks[6]);
    $chunks[6]=preg_replace("/$entity/", $escape, $chunks[6]);
    $chunks[7]=preg_replace("/$entity/", $escape, $chunks[7]);
    $sublang=preg_match("/^(x?html)$/", $lang)? "javascript":
                               $sublang=='jsp'? 'java' :$lang.'1';
    foreach($chunks[3] as &$subcode)
      if($subcode) highlight($subcode, $sublang);
    $chunks=array_map(NULL, $chunks[1], $chunks[2],
      $chunks[3], $chunks[4],$chunks[5], $chunks[6], $chunks[7]);
    $code=""; foreach($chunks as $part) $code.=join("", $part);
  }
  // c-style syntax, comments, literals
  if(preg_match("/^(?:c(?:\+\+)?|java(?:script)?|perl|php1|asp1)$/", $lang)) {
    $comments ="\/\/[^\r\n]*";
    $comments.="|\/\*(?:[^*]*+(?:\*(?!\/))*+)*+\*\/";
    if("php1"==$lang) {
      $comments.="|\#[^rn]*";
    }
    // string literals include " escapes, single line only
    $literals =""(?:[^"\n\r\\]*+(?:\\[^\r\n\\]|\\\\)?+)*+"";
    // 'x' char literals or 'xyz' string literals, depending on language
    $literals.="|'(?:\\'|[^'\n\r])*+'";
    // numerical values, e.g. 0 1.2 .3 4.5E6 78f 9D 0.12d 3.4E5F 6789L
    $literals.="|-?+(?:\.\d++|\d++(?:\.\d++)?+)(?:[eE]\d++|[Ll]\b)?+[dDfF]?";
    $literals.="|0[xX][0-9a-fA-F]++[Ll]?";
    // now everything not a comment, literal or preprocessor instruction
    $keywords=&getKeywords($lang);
    $preprocs=&getPreprocessor($lang);
    $libfuncs=&getLibFuncs($lang);
    if("c++"==$lang) {
      $literals.="|(?:\b(?:true|false)\b)";
    } elseif(preg_match("/^java(?:script)?$/", $lang)) {
      $literals.="|(?:\b(?:true|false|null)\b)";
    }
    preg_match_all("/($preprocs)|($comments)|($literals)|([\w_$]+)|(x1Ax1A|\S)|([\n\r\s]+)/", $code, $chunks);
    $chunks[1]=preg_replace("/.+/s", $preproc, $chunks[1]);
    $chunks[1]=preg_replace("/(#include\s*)(<[^>]+>|"[^"]*")/s", $include, $chunks[1]);
    $chunks[2]=preg_replace("/.+/s", $comment, $chunks[2]);
    $chunks[3]=preg_replace("/.+/", $literal, $chunks[3]);
    $chunks[3]=preg_replace("/(?:\\(?:x1Ax1A|.))+/", $escape, $chunks[3]);
    $chunks[4]=preg_replace("/^(?:$keywords)$/", $keyword, $chunks[4]);
    $chunks[4]=preg_replace("/^(?:$libfuncs)$/", $libfunc, $chunks[4]);
    if("php1"==$lang) { // case insensitive keywords
      $keywords=&getKeywords("php2");
      $chunks[4]=preg_replace("/^(?:$keywords)$/i", $keyword, $chunks[4]);
    }
    $chunks[5]=preg_replace("/.+/", $op_sep, $chunks[5]);
    $chunks=array_map(NULL, $chunks[1], $chunks[2],
      $chunks[3], $chunks[4],$chunks[5],  $chunks[6]);
    $code=""; foreach($chunks as $part) $code.=join("", $part);
  } elseif("basic"==$lang) {
    $comments ="(?:\bREM\b|'|x60)[^\r\n]*";
    $literals =""[^"\n\r]*+"";
    // 'x' char literals or 'xyz' string literals, depending on language
    $literals.="|'(?:\\'|[^'\n\r])*+'";
    // numerical values, e.g. 0 1.2 .3 4.5E6 78f 9D 0.12d 3.4E5F 6789L
    $literals.="|-?+(?:\.\d++|\d++(?:\.\d++)?+)(?:[eE]\d++|[%&]\b)?+!?";
    $literals.="|[&]h[0-9a-fA-F]++\b[%&]?|%[01]++\b[%&]?";
    // now everything not a comment, literal or preprocessor instruction
    $keywords=&getKeywords($lang);
    $preprocs="(?:\bREM\b|'|x60)s*\$"."i(?:nclude)?+\b[^\r\n]*|^[a-z]*:";
    preg_match_all("/($preprocs)|($comments)|($literals)|([\w_]+[%!&$]?)|(x1Ax1A|\S)|([\n\r\s]+)/i", $code, $chunks);
    $chunks[1]=preg_replace("/.+/s", $preproc, $chunks[1]);
    $chunks[1]=preg_replace("/(\x24i(?:nclude)?+\s*+)("[^"]*"|.*)/is", $include, $chunks[1]);
    $chunks[2]=preg_replace("/.+/s", $comment, $chunks[2]);
    $chunks[3]=preg_replace("/.+/", $literal, $chunks[3]);
    $chunks[4]=preg_replace("/^(?:$keywords)[%!&$]?$/i", $keyword, $chunks[4]);
    $chunks[5]=preg_replace("/.+/", $op_sep, $chunks[5]);
    $chunks=array_map(NULL, $chunks[1], $chunks[2],
      $chunks[3], $chunks[4],$chunks[5],  $chunks[6]);
    $code=""; foreach($chunks as $part) $code.=join("", $part);
  }
}
function &getKeywords($lang)
{
  if($lang=="html") {
    $keywords ="a|abbr|acronym|address|applet|area|b|base|basefont";
    $keywords.="|bdo|big|blockquote|body|br|button|caption|center";
    $keywords.="|cite|code|col|colgroup|dd|del|dfn|dir|div|dl|dt|em";
    $keywords.="|fieldset|font|form|frame|frameset|h[1-6]|head|hr";
    $keywords.="|html|i|iframe|img|input|ins|isindex|kbd|label";
    $keywords.="|legend|li|link|map|menu|meta|noframes|noscript";
    $keywords.="|object|ol|optgroup|option|p|param|pre|q|s|samp";
    $keywords.="|script|select|small|span|strike|strong|style|sub";
    $keywords.="|sup|table|tbody|td|textarea|tfoot|th|thead|title";
    $keywords.="|tr|tt|u|ul|var";
  } elseif(preg_match("/^c(\+\+)?$/", $lang)) {
    $keywords ="auto|break|case|char|const|continue|default|do|double|else";
    $keywords.="|enum|extern|float|for|goto|if|int|long|register|return";
    $keywords.="|short|signed|sizeof|static|struct|switch|typedef|union";
    $keywords.="|unsigned|void|volatile|while";
    if("c++"==$lang) // C++ only
    {
      $keywords.="|asm|bool|catch|class|const_cast|delete|dynamic_cast";
      $keywords.="|explicit|friend|inline|mutable|namespace|new|operator";
      $keywords.="|private|protected|public|reinterpret_cast|static_cast";
      $keywords.="|template|this|throw|try|typeid|typename|using|virtual";
      $keywords.="|wchar_t";
    }
  } elseif("java"==$lang) {
    $keywords ="abstract|boolean|break|byte|case|catch|char|class";
    $keywords.="|continue|default|do|double|else|extends|final|finally";
    $keywords.="|float|for|if|implements|import|instanceof|int|interface";
    $keywords.="|long|native|new|package|private|protected|public|return";
    $keywords.="|short|static|strictfp|super|switch|synchronized|this|throw";
    $keywords.="|throws|transient|try|void|volatile|while|assert";
    // the following are reserved but unused
    $keywords.="|goto|const";
  } elseif("php1"==$lang) {
    // case insensitive keywords
    $keywords ="array|as|bool|boolean|break|case|compact|continue|count";
    $keywords.="|current|default|define|defined|die|do|double|doubleval";
    $keywords.="|each|echo|else|elseif|empty|end|eregi?(?:_replace)?+";
    $keywords.="|eval|exit|extract|float|floatval|for|foreach|function";
    $keywords.="|gettype|if|in_array|include(?:_once)?+|int|integer|intval";
    $keywords.="|isset|key|leak|list|list|mail|next|object|pos|prev";
    $keywords.="|print(?:f|_r)?+|range|readfile|real|require(?:_once)?+";
    $keywords.="|reset|return|serialize|settype|shuffle|sizeof|sleep";
    $keywords.="|(?:nat(?:case)?|u?[ak]?r?)?sort|spliti?|sql_regcase";
    $keywords.="|string|strval|switch|uniqid|unserialize|unset|usleep";
    $keywords.="|var_dump|var_export|virtual|while|preg_(?:match(?:_all)?+|replace)";
  } elseif("php2"==$lang) {
    // case sensitive keywords (constants)
    $keywords ="NULL|PHP_OS|PHP_VERSION|TRUE|FALSE|E_ERROR|E_WARNING";
    $keywords.="|E_PARSE|E_NOTICE|E_CORE_ERROR|E_CORE_WARNING|E_STRICT";
  } elseif("perl"==$lang) {
    $keywords ="abs|atan2|bless|caller|chomp|chop|chr|continue";
    $keywords.="|cos|crypt|dbmclose|dbmopen|defined|delete|die";
    $keywords.="|do|dump|each|eval|exists|exit|exp|formline";
    $keywords.="|gmtime|goto|grep|hex|import|index|int|join";
    $keywords.="|keys|last|lc|lcfirst|length|local|localtime|log";
    $keywords.="|map|my|next|no|oct|ord|our|pack|package|pop|pos";
    $keywords.="|push|quotemeta|rand|redo|ref|require|reset";
    $keywords.="|return|reverse|rindex|scalar|shift|sin|sort";
    $keywords.="|splice|split|sprintf|sqrt|srand|study|sub|substr";
    $keywords.="|tie|tied|time|times|uc|ucfirst|undef|unpack";
    $keywords.="|unshift|untie|use|values|wantarray";
  } elseif("javascript"==$lang) {
    $keywords ="as|break|case|catch|class|const|continue|default|delete|";
    $keywords.="do|else|export|extends|finally|for|function|if|import";
    $keywords.="|in|instanceof|is|namespace|new|package|private|public";
    $keywords.="|return|super|switch|this|throw|try|typeof|use|var";
    $keywords.="|void|while|with";
    // the following are reserved but unused
    $keywords.="|abstract|debugger|enum|goto|implements|interface|native";
    $keywords.="|protected|synchronized|throws|transient|volatile";
  } elseif("basic"==$lang) {
    $keywords ="if|then|else|elsif|end|for|to|step|next|loop|repeat|until";
    $keywords.="|do|while|wend|declare|sub|shared|common|call|return";
    $keywords.="|print|at|using|locate|screen|color|palette|input|timer|sleep";
    $keywords.="|dim|as|integer|string|float|open|input|output|close|seek";
    $keywords.="|function|let|on|error|goto|gosub|on|off|or|and|xor|not|mod";
    $keywords.="|str|chr|asc|bin|oct|hex|mid|left|right|peek[bwl]?+|poke[bwl]?";
    $keywords.="|csrlin|pos|cls|line|circle|paint|inkey|beep|varptr|sadd";
    $keywords.="|randomize|int|rnd|resume|library|data|read|restore|val";
  } else {//unknown... most likely keywords in every language
    $keywords ="break|case|continue|default|do|else|for|if|return|switch|while";
  }
  return $keywords;
}
function &getPreprocessor($lang) {
  if(preg_match("/^c(\+\+)?$/", $lang)) {
    $preprocs="\s*+\#(?:include|define|ifdef|endif|else|pragma)\b";
    // macros may span multiple lines if linebreak is escaped with  
    $preprocs.="(?:(?:[^\n\r\\]*+(?:\\[^\n\r])*+)*+(?:\\\r?\n?)?+)*+";
    return $preprocs;
  } else return "(?!X)X";//no preprocessor
}
function &getLibFuncs($lang) {
  if(preg_match("/^c(\+\+)?$/", $lang)) {
    $libfuncs="U?(?:BYTE|WORD|LONG)|[AB]PTR|BOOL|TRUE|FALSE|NULL";
    $libfuncs.="|TAG_(?:END|IGNORE|MORE)";
    $libfuncs.="|(?:Open|Close)(?:Library|Device)";
    $libfuncs.="|f?+printf|fopen|fclose|fwrite";
    if("c++"==$lang) {
      $libfuncs.="|std|cout|cin|endl|map|vector|pair";
    }
    return $libfuncs;
  } elseif("java"==$lang) {
    return "Object|String|Class(?:Loader)?+";
  }
  return "(?!X)X";//no libfuncs yet
}
?>


--
Good coders do not comment. What was hard to write should be hard to read too.

[ - Antworten - Zitieren - Direktlink - ]

21.05.2006, 16:18 Uhr

Holger
Posts: 8116
Nutzer
So, das war's erstmal. Die Zeile

$code = preg_replace("/\[(/?+(?:[biu]|black|red|green|blue|yellow|gray|code|img|url))b/", "x1Ax1A$1", $code);

müßte noch geändert werden in

$code = preg_replace("/\[(/?+(?:[iub]|f[1-5]|url|email|img|center|code"
."|verdana|arial|courier|comic|red|blue|green|yellow|white|gray|black|list))\b/", "x1Ax1A$1", $code);

(mußte erst mal nachsehen, welche Formatierungen es gibt.)

Viel Spaß beim Testen, Einbauen, etc...

mfg

--
Good coders do not comment. What was hard to write should be hard to read too.

[ - Antworten - Zitieren - Direktlink - ]


1 2 -3- [ - Beitrag schreiben - ]


amiga-news.de Forum > Forum und Interna > [ code ] Test-Thread [ - Suche - Neue Beiträge - Registrieren - Login - ]


.
Impressum | Datenschutzerklärung | Netiquette | Werbung | Kontakt
Copyright © 1998-2024 by amiga-news.de - alle Rechte vorbehalten.
.