home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / modules / wiki / parse / transforms.php < prev   
PHP Script  |  2004-03-08  |  20KB  |  701 lines

  1. <?php
  2. // $Id: transforms.php,v 1.1 2004/01/12 22:14:05 comsubvie Exp $
  3.  
  4. // The main parser components.  Each of these takes a line of text and scans it
  5. //   for particular wiki markup.  It converts markup elements to
  6. //   $FlgChr . x . $FlgChr, where x is an index into the global array $Entity,
  7. //   which contains descriptions of each markup entity.  Later, these will be
  8. //   converted back into HTML  (or, in the future, perhaps some other
  9. //   representation such as XML).
  10.  
  11. function parse_noop($text)
  12. {
  13.   return $text;
  14. }
  15.  
  16. // The following function "corrects" for PHP's odd preg_replace behavior.
  17. // Back-references have backslashes inserted before certain quotes
  18. // (specifically, whichever quote was used around the backreference); this
  19. // function removes remove those backslashes.
  20.  
  21. function q1($text)
  22.   { return str_replace('\\"', '"', $text); }
  23.  
  24. function validate_page($page)
  25. {
  26.   global $FlgChr;
  27.  
  28.   $p = parse_wikiname($page, 1);
  29.   if(preg_match('/^' . $FlgChr .'!?'. '\\d+' . $FlgChr . '$/', $p))
  30.     { return 1; }
  31.   $p = parse_freelink('((' . $page . '))', 1);
  32.   if(preg_match('/^' . $FlgChr . '!?'. '\\d+' . $FlgChr . '$/', $p))
  33.     { return 2; }
  34.   return 0;
  35. }
  36.  
  37. function pre_parser($text)
  38. {
  39. // Before parsing the whole text, do check for line continuation and forced
  40. // line breaks. To achieve this, and still have code-sections, code-sections
  41. // need to be parsed in this section as well
  42.  
  43.   // Parse the code sections, to escape them from the line control
  44.   $text = preg_replace("/\n\s*<((?:php)?code)>\s*\n(.*\n)\s*<\\/\\1>\s*(?=\n|$)/Usei", 
  45.                        "q1('\n').code_token('\\1',q1('\\2'))", $text); 
  46.  
  47.   // Concatenate lines ending in a single \
  48.   $text = preg_replace("/([^\\\])\\\\\n\s*/s", " ", $text);
  49.  
  50.   // Insert page breaks to lines ending in a double \
  51.   $text = preg_replace("/\\\\\\\\\n\s*/se", "new_entity(array('newline'))", 
  52.                        $text);
  53.   return $text;
  54. }
  55.  
  56. function code_token($codetype, $code) 
  57. {
  58.   global $FlgChr, $Entity;
  59.  
  60.   if (stristr("code", $codetype))
  61.     { $Entity[count($Entity)] = array('code', parse_htmlisms($code)); } 
  62.   else if (stristr("phpcode", $codetype))
  63.     { $Entity[count($Entity)] = array('phpcode', $code); }
  64.  
  65.   return $FlgChr . (count($Entity)-1) . $FlgChr; //Is a blockelement
  66. }
  67.  
  68. function parse_elem_flag($text)
  69. {
  70.   global $FlgChr;
  71.  
  72.   // Hide element flags (0xFF) from view.
  73.   return preg_replace('/' . $FlgChr . '/e', "new_entity(array('raw', '$FlgChr'))", $text, -1);
  74. }
  75.  
  76. function new_entity($array,$blockElem=true)
  77. {
  78.   global $Entity, $FlgChr;
  79.  
  80.   $Entity[count($Entity)] = $array;
  81.   return $FlgChr . ($blockElem ? '' : '!') . (count($Entity) - 1) . $FlgChr;
  82. }
  83.  
  84. function parse_wikiname($text, $validate = 0)
  85. {
  86.   global $LinkPtn, $EnableWikiLinks;
  87.  
  88.   if(!$EnableWikiLinks) { return $text; }
  89.  
  90.   if($validate)
  91.     { $ptn = "/(^|[^A-Za-z])(\\/?$LinkPtn)(())(\"\")?/e"; }
  92.   else
  93.     { $ptn = "/(^|[^A-Za-z])(!?\\/?$LinkPtn)((\#[A-Za-z]([-A-Za-z0-9_:.]*[-A-Za-z0-9_])?)?)(\"\")?/e"; }
  94.  
  95.   return preg_replace($ptn,
  96.                       "q1('\\1').wikiname_token(q1('\\2'),'\\3')",
  97.                       $text, -1);
  98. }
  99.  
  100. function wikiname_token($name, $anchor)
  101. {
  102.   global $ParseObject;
  103.   if($name[0] == '!')                   // No-link escape sequence.
  104.     { return substr($name, 1); }        // Trim leading '!'.
  105.   $link = $name;
  106.   // translate sub-page markup into a qualified wikiword
  107.   if ($name[0] == '/')
  108.     { 
  109.       if (preg_match("|(.*)\\/[^\\/]*|", $ParseObject, $path)) 
  110.         { $link = $path[1] . $name; }
  111.       else
  112.         { $link = substr($name,1); } 
  113.     }
  114.  
  115.   return new_entity(array('ref', $link, $name, '', $anchor, $anchor),false);
  116. }
  117.  
  118. function parse_freelink($text, $validate = 0)
  119. {
  120.   global $EnableFreeLinks;
  121.  
  122.   if(!$EnableFreeLinks) { return $text; }
  123.  
  124.   if($validate)
  125.   {
  126.     $ptn = "/\\(\\(([^\\|\\)]+)()()\\)\\)/e";
  127.   }
  128.   else
  129.   {
  130.     $ptn = "/(!?\\(\\(([^\\|\\)]+)(\\|[^\\)#]+)?(\\#[A-Za-z][-A-Za-z0-9_:.]*)?()\\)\\))/e";
  131.   }
  132.  
  133.   return preg_replace($ptn,
  134.                       "freelink_token(q1('\\2'), q1('\\3'), '\\4', '', '\\1')",
  135.                       $text, -1);
  136. }
  137.  
  138. function freelink_token($link, $appearance, $anchor, $anchor_appearance, $nolink)
  139. {
  140.   global $ParseObject, $FlgChr;
  141.   if($nolink[0] == '!')                              // No-link escape sequence.
  142.     { return new_entity(array('raw', substr($nolink, 1))); } // Trim leading '!'
  143.   
  144.   if($appearance == '')
  145.     { $appearance = $link; }
  146.   else
  147.   { 
  148.     $appearance = substr($appearance, 1);    // Trim leading '|'.
  149.     if (preg_match("/$FlgChr/", $appearance)) 
  150.       { $appearance = parse_elements($appearance); }
  151.   }
  152.  
  153.   // translate sub-page markup into a qualified wikiword
  154.   if ($link[0] == '/')
  155.     { 
  156.       if (preg_match("|(.*)\\/[^\\/]*|", $ParseObject, $path)) 
  157.         { $link = $path[1] . $link; }
  158.       else
  159.         { $link = substr($link,1); } 
  160.     }
  161.   if (preg_match("/$FlgChr/", $link)) 
  162.     { return $nolink; }
  163.   else 
  164.   {
  165.     return new_entity(array('ref', $link, $appearance, '',
  166.                           $anchor, $anchor_appearance), false);
  167.   }
  168. }
  169.  
  170. function parse_interwiki($text)
  171. {
  172.   global $InterwikiPtn;
  173.  
  174.   return preg_replace("/(^|[^A-Za-z])($InterwikiPtn)(?=\$|[^\\/=&~A-Za-z0-9])/e",
  175.                       "q1('\\1').interwiki_token(q1('\\3'),q1('\\4')).q1('\\5')",
  176.                       $text, -1);
  177. }
  178.  
  179. function interwiki_token($prefix, $ref)
  180. {
  181.   global $pagestore;
  182.  
  183.   if(($url = $pagestore->interwiki($prefix)) != '')
  184.   {
  185.     return new_entity(array('interwiki', $url . $ref, $prefix . ':' . $ref), false);
  186.   }
  187.  
  188.   return $prefix . ':' . $ref;
  189. }
  190.  
  191. function parse_hyperlink_ref($text)
  192. {
  193.   global $UrlPtn,$InterwikiPtn;
  194.  
  195.   return preg_replace("/\\[($UrlPtn|$InterwikiPtn)]/Ue",
  196.                       "url_token(q1('\\1'), '')", $text, -1);
  197. }
  198. function image_search($text) 
  199. {
  200.   global $ImgPtn, $ExtRef;
  201.   if (preg_match("/$ImgPtn$/", $text))
  202.     { return parse_elements(parse_hyperlink($text)); }
  203.   else 
  204.     { return $ExtRef[0] . $text . $ExtRef[1]; }
  205. }
  206. function parse_hyperlink_description($text)
  207. {
  208.   global $UrlPtn, $InterwikiPtn;
  209.   return preg_replace("/\\[($UrlPtn|$InterwikiPtn) ([^]]+)]/e",
  210.                       "url_token(q1('\\1'),image_search('\\4'))", 
  211.                       $text, -1);
  212. }
  213.  
  214. function parse_hyperlink($text)
  215. {
  216.   global $UrlPtn, $InterwikiPtn;
  217.  
  218.   return preg_replace("/(^|[^A-Za-z])($UrlPtn|$InterwikiPtn)(?=\$|[^\\/?=&~A-Za-z0-9])/e",
  219.                       "q1('\\1').url_token(q1('\\2'), q1('\\2')).q1('\\5')", $text, -1);
  220. }
  221.  
  222. function url_token($value, $display)
  223. {
  224.   global $pagestore, $InterwikiPtn, $UrlPtn, $RefList, $ImgPtn;
  225.   static $count = 1;
  226.   // Expand interwiki-entry, if necessary
  227.   if ((!preg_match("/$UrlPtn/", $value)) and
  228.       preg_match("/$InterwikiPtn/", $value, $match))  
  229.   {
  230.      $couldBeImage=($display==$value);
  231.      if (($url=$pagestore->interwiki($match[1])) != '') 
  232.        { $value = $url . $match[2];
  233.          if ($couldBeImage and preg_match("/$ImgPtn$/", $value)) 
  234.            { $display = $value; }
  235.        }
  236.      else
  237.        { $value = "[$match[1]:$match[2] $display]"; }
  238.   }
  239.  
  240.   if($display == '')
  241.     { $display = '[' . $count++ . ']';
  242.       $RefList[] = $value; }
  243.  
  244.   return new_entity(array('url', $value, $display), false);
  245. }
  246.  
  247. function parse_macros($text)
  248. {
  249.   return preg_replace('/\\[\\[([^] ]+( [^]]+)?)]]/e',
  250.                       "macro_token(q1('\\1'), q1('\\3'))", $text, -1);
  251.  
  252. }
  253.  
  254. function parse_no_macros($text)
  255. {
  256.   return preg_replace('/\\[\\[([^] ]+( [^]]+)?)]]/e',
  257.                       "", $text, -1);
  258.  
  259. }
  260.  
  261. function macro_token($macro, $trail)
  262. {
  263.   global $ViewMacroEngine;
  264.  
  265.   $cmd  = strtok($macro, ' ');
  266.   $args = strtok('');
  267.  
  268.   if($ViewMacroEngine[$cmd] != '')
  269.     { return new_entity(array('raw', $ViewMacroEngine[$cmd]($args))); }
  270.   else
  271.     { return '[[' . $macro . ']]' . ($trail == "\n" ? $trail : ''); }
  272. }
  273.  
  274. function parse_transclude($text)
  275. {
  276.   $text2 = preg_replace('/%%([^%]+)%%/e',
  277.                         "transclude_token(q1('\\1'))", $text, -1);
  278.   if($text2 != $text)
  279.     { $text2 = str_replace("\n", '', $text2); }
  280.   return $text2;
  281. }
  282.  
  283. function transclude_token($text)
  284. {
  285.   global $pagestore, $ParseEngine, $ParseObject;
  286.   static $visited_array = array();
  287.   static $visited_count = 0;
  288.  
  289.   if(!validate_page($text))
  290.     { return '%%' . $text . '%%'; }
  291.  
  292.   $visited_array[$visited_count++] = $ParseObject;
  293.   for($i = 0; $i < $visited_count; $i++)
  294.   {
  295.     if($visited_array[$i] == $text)
  296.     {
  297.       $visited_count--;
  298.       return '%%' . $text . '%%';
  299.     }
  300.   }
  301.  
  302.   $pg = $pagestore->page($text);
  303.   $pg->read();
  304.   if(!$pg->exists)
  305.   {
  306.     $visited_count--;
  307.     return '%%' . $text . '%%';
  308.   }
  309.  
  310.   $result = new_entity(array('raw', parseText($pg->text, $ParseEngine, $text)));
  311.   $visited_count--;
  312.   return $result;
  313. }
  314.  
  315. function parse_textenhance($text) 
  316. {
  317.   global $EnableTextEnhance;
  318.  
  319.   if ($EnableTextEnhance) 
  320.   {
  321.     if (preg_match("/^(\*+)([^*].*)$/", $text, $match)) 
  322.     {
  323.        // Special case, since *'s at start of line is markup for lists
  324.        $return = $match[1] . 
  325.                  preg_replace("/(\*\*)([^*]+)\\1/e", 
  326.                               "pair_tokens('bold', q1('\\2'))", $match[2], -1);
  327.     } 
  328.     else 
  329.     {
  330.        $return = preg_replace("/(\*\*)([^*]+)\\1/e", 
  331.                               "pair_tokens('bold', q1('\\2'))", $text, -1);
  332.     }
  333.     $return = preg_replace( "/(\/\/)([^\/]+)\\1/e", 
  334.                              "pair_tokens('italic', q1('\\2'))", $return, -1);
  335.     $return = preg_replace( "/(--)([^-]+)\\1/e", 
  336.                              "pair_tokens('del', q1('\\2'))", $return, -1);
  337.     $return = preg_replace( "/(\+\+)([^\+]+)\\1/e", 
  338.                              "pair_tokens('ins', q1('\\2'))", $return, -1);
  339.     $return = preg_replace( "/(\^\^)([^^]+)\\1/e", 
  340.                              "pair_tokens('superscript', q1('\\2'))", $return, -1);
  341.     $return = preg_replace( "/(,,)([^,]+)\\1/e", 
  342.                             "pair_tokens('subscript', q1('\\2'))", $return, -1);
  343.     return $return; 
  344.   } else { 
  345.     return $text; 
  346.   }
  347.  
  348. function parse_bold($text)
  349. {
  350.   return preg_replace("/'''(()|[^'].*)'''/Ue", "pair_tokens('bold', q1('\\1'))",
  351.                       $text, -1);
  352. }
  353.  
  354. function parse_italic($text)
  355. {
  356.   return preg_replace("/''(()|[^'].*)''/Ue", "pair_tokens('italic', q1('\\1'))",
  357.                       $text, -1);
  358. }
  359.  
  360. function parse_teletype($text)
  361. {
  362.   return preg_replace("/{{({*?.*}*?)}}/Ue",
  363.                       "pair_tokens('tt', q1('\\1'))", $text, -1);
  364. }
  365.  
  366. function pair_tokens($type, $text, $blockElem=false)
  367. {
  368.   global $Entity, $FlgChr;
  369.  
  370.   $Entity[count($Entity)] = array($type . '_start');
  371.   $Entity[count($Entity)] = array($type . '_end');
  372.  
  373.   return $FlgChr . ($blockElem ? '' : '!') . (count($Entity) - 2) . $FlgChr . $text .
  374.          $FlgChr . ($blockElem ? '' : '!') . (count($Entity) - 1) . $FlgChr;
  375. }
  376.  
  377. function parse_newline($text)
  378. {
  379.   global $FlgChr;
  380.   static $last = array('', '');
  381.  
  382.   // More than two consecutive newlines fold into two newlines.
  383.   if($last[0] == "\n" && $last[1] == "\n" && $text == "\n")
  384.     { return ''; }
  385.   $last[0] = $last[1];
  386.   $last[1] = $text;
  387.  
  388.   // Lines not beginning with $FlgChr or beginning with $FlgChr! are paragraps
  389.   return preg_replace("/^(([^$FlgChr]|$FlgChr!).+)$/e",
  390.                       "pair_tokens('paragraph', q1('\\1'), true)", $text, -1);
  391. }
  392.  
  393. function parse_horiz($text)
  394. {
  395.   return preg_replace("/-{4,}(\\n(\\r)?)?/e", "new_entity(array('hr'))",
  396.                       $text, -1);
  397. }
  398.  
  399. function parse_nowiki($text)
  400. {
  401.   return preg_replace("/```(.*)```/Ue",
  402.                       "new_entity(array('nowiki', parse_elements(q1('\\1'))))",
  403.                       $text, -1);
  404. }
  405.  
  406. function parse_raw_html($text)
  407. {
  408.   global $Entity, $FlgChr;
  409.   static $in_html = 0;
  410.   static $buffer  = '';
  411.  
  412.   if($in_html)
  413.   {
  414.     if(strtolower($text) == "</html>\n")
  415.     {
  416.       $Entity[count($Entity)] = array('raw', $buffer);
  417.       $buffer  = '';
  418.       $in_html = 0;
  419.       return $FlgChr . (count($Entity) - 1) . $FlgChr; //$blockElem=true
  420.     }
  421.  
  422.     $buffer = $buffer . parse_elements($text);
  423.     return '';
  424.   }
  425.   else
  426.   {
  427.     if(strtolower($text) == "<html>\n")
  428.     {
  429.       $in_html = 1;
  430.       return '';
  431.     }
  432.  
  433.     return $text;
  434.   }
  435. }
  436.  
  437. function parse_indents($text)
  438. {
  439.   global $MaxNesting;
  440.   static $last_prefix = '';             // Last line's prefix.
  441.  
  442. // Locate the indent prefix characters.
  443.  
  444.   preg_match('/^([:\\*#]*)(;([^:]*):)?(.*\\n?$)/', $text, $result);
  445.  
  446.   if($result[2] != '')                  // Definition list.
  447.     { $result[1] = $result[1] . ';'; }
  448.  
  449. // No list on last line, no list on this line.  Bail out:
  450.  
  451.   if($last_prefix == '' && $result[1] == '') 
  452.     { return $text; }                   // Common case fast.
  453.  
  454. // Remember lengths of strings.
  455.  
  456.   $last_len   = strlen($last_prefix);
  457.   $prefix_len = strlen($result[1]);
  458.  
  459.   $text = $result[4];
  460.  
  461.   $fixup = '';
  462.  
  463. // Loop through and look for prefix characters in common with the
  464. // previous line.
  465.  
  466.   for($i = 0;
  467.       $i < $MaxNesting && ($i < $last_len || $i < $prefix_len);
  468.       $i++)
  469.   {
  470.     // If equal, continue.
  471.     if($i < $last_len && $i < $prefix_len     // Neither past end.
  472.        && $last_prefix[$i] == $result[1][$i]) // Equal content.
  473.       { continue; }
  474.  
  475.     // If we've gone deeper than the previous line, we're done.
  476.     if($i >= $last_len)
  477.       { break; }
  478.  
  479.     // If last line goes further than us, end its dangling lists.
  480.     if($i >= $prefix_len                      // Current line ended.
  481.        || $last_prefix[$i] != $result[1][$i]) // Or not, but they differ.
  482.     {
  483.       for($j = $i; $j < $MaxNesting && $j < $last_len; $j++)
  484.       {
  485.         $fixup = entity_listitem($last_prefix[$j], 'end')
  486.                  . entity_list($last_prefix[$j], 'end')
  487.                  . $fixup;
  488.       }
  489.       break;
  490.     }
  491.   }
  492.  
  493. // End the preceding line's list item if we're starting another one
  494. // at the same level.
  495.  
  496.   if($i > 0 && $i >= $prefix_len)
  497.     { $fixup = $fixup . entity_listitem($last_prefix[$i - 1], 'end'); }
  498.  
  499. // Start fresh new lists for this line as needed.
  500. // We start all but the last one as *indents* (definition lists)
  501. // instead of what they really may appear as, since their function is
  502. // really just to indent.
  503.  
  504.   for(; $i < $MaxNesting - 1 && $i + 1 < $prefix_len; $i++)
  505.   {
  506.     $result[1][$i] = ':';             // Pretend to be an indent.
  507.     $fixup = $fixup
  508.              . entity_list(':', 'start')
  509.              . entity_listitem(':', 'start');
  510.   }
  511.   if($i < $prefix_len)                // Start the list itself.
  512.   {
  513.     $fixup = $fixup
  514.              . entity_list($result[1][$i], 'start');
  515.   }
  516.  
  517. // Start the list *item*.
  518.  
  519.   if($result[2] != '')                // Definition list.
  520.   {
  521.     $fixup = $fixup
  522.              . new_entity(array('term_item_start'))
  523.              . $result[3]
  524.              . new_entity(array('term_item_end'));
  525.   }
  526.  
  527.   if($result[1] != '')
  528.     { $text = entity_listitem(substr($result[1], -1), 'start') . $text; }
  529.  
  530.   $text = $fixup . $text;
  531.  
  532.   $last_prefix = $result[1];
  533.  
  534.   return $text;
  535. }
  536.  
  537. function entity_list($type, $fn, $attr='')
  538. {
  539.   if($type == '*')
  540.     { return new_entity(array('bullet_list_' . $fn, $attr)); }
  541.   else if($type == ':' || $type == ';')
  542.     { return new_entity(array('indent_list_' . $fn)); }
  543.   else if($type == '#')
  544.     { return new_entity(array('numbered_list_' . $fn)); }
  545. }
  546.  
  547. function entity_listitem($type, $fn)
  548. {
  549.   if($type == '*')
  550.     { return new_entity(array('bullet_item_' . $fn)); }
  551.   else if($type == ':' || $type == ';')
  552.     { return new_entity(array('indent_item_' . $fn)); }
  553.   else if($type == '#')
  554.     { return new_entity(array('numbered_item_' . $fn)); }
  555. }
  556.  
  557. function parse_heading($text)
  558. {
  559.   global $MaxHeading;
  560.  
  561.   if(!preg_match('/^\s*(=+) (.*) (=+)\s*$/', $text, $result))
  562.     { return $text; }
  563.  
  564.   if(strlen($result[1]) != strlen($result[3]))
  565.     { return $text; }
  566.  
  567.   if(($level = strlen($result[1])) > $MaxHeading)
  568.     { $level = $MaxHeading; }
  569.  
  570.   return new_entity(array('head_start', $level)) .
  571.          $result[2] .
  572.          new_entity(array('head_end', $level));
  573. }
  574.  
  575. function parse_htmlisms($text)
  576. {
  577.   $text = str_replace('&', '&', $text);
  578.   $text = str_replace('<', '<', $text);
  579.   return $text;
  580. }
  581.  
  582. function parse_elements($text)
  583. {
  584.   global $FlgChr;
  585.   return preg_replace("/$FlgChr!?(\\d+)$FlgChr/e", "generate_element(q1('\\1'))", $text, -1);
  586. }
  587.  
  588. function generate_element($text)
  589. {
  590.   global $Entity, $DisplayEngine;
  591.  
  592.   if(function_exists('call_user_func_array'))
  593.   {
  594.     return call_user_func_array($DisplayEngine[$Entity[$text][0]],
  595.                                 array_slice($Entity[$text], 1));
  596.   }
  597.   else
  598.   {
  599.     return $DisplayEngine[$Entity[$text][0]]($Entity[$text][1],
  600.                                              $Entity[$text][2],
  601.                                              $Entity[$text][3],
  602.                                              $Entity[$text][4],
  603.                                              $Entity[$text][5]);
  604.   }
  605. }
  606.  
  607. function parse_diff_skip($text)
  608. {
  609.   if(preg_match('/^--+/', $text))
  610.     { return ''; }
  611.   if(preg_match('/^\\\\ No newline/', $text))
  612.     { return ''; }
  613.   return $text;
  614. }
  615.  
  616. function parse_diff_color($text)
  617. {
  618.   static $in_old = 0;
  619.   static $in_new = 0;
  620.  
  621.   if(strlen($text) == 0)
  622.     { $this_old = $this_new = 0; }
  623.   else
  624.   {
  625.     $this_old = ($text[0] == '<');
  626.     $this_new = ($text[0] == '>');
  627.   }
  628.  
  629.   if($this_old || $this_new)
  630.     { $text = substr($text, 2); }
  631.  
  632.   if($this_old && !$in_old)
  633.     { $text = new_entity(array('diff_old_start')) . $text; }
  634.   else if($this_new && !$in_new)
  635.     { $text = new_entity(array('diff_new_start')) . $text; }
  636.  
  637.   if($in_old && !$this_old)
  638.     { $text = new_entity(array('diff_old_end')) . $text; }
  639.   else if($in_new && !$this_new)
  640.     { $text = new_entity(array('diff_new_end')) . $text; }
  641.  
  642.   $in_old = $this_old;
  643.   $in_new = $this_new;
  644.  
  645.   return $text;
  646. }
  647.  
  648. function parse_diff_message($text)
  649. {
  650.   global $FlgChr;
  651.  
  652.   $text = preg_replace('/(^(' . $FlgChr . '\\d+' . $FlgChr . ')?)\\d[0-9,]*c[0-9,]*$/e',
  653.                        "q1('\\1').new_entity(array('diff_change'))", $text, -1);
  654.   $text = preg_replace('/(^(' . $FlgChr . '\\d+' . $FlgChr . ')?)\\d[0-9,]*a[0-9,]*$/e',
  655.                        "q1('\\1').new_entity(array('diff_add'))", $text, -1);
  656.   $text = preg_replace('/(^(' . $FlgChr . '\\d+' . $FlgChr . ')?)\\d[0-9,]*d[0-9,]*$/e',
  657.                        "q1('\\1').new_entity(array('diff_delete'))", $text, -1);
  658.  
  659.   return $text;
  660. }
  661.  
  662. function parse_table($text)
  663. {
  664.   static $in_table = 0;
  665.  
  666.   $pre = '';
  667.   $post = '';
  668.   if(preg_match('/^(\|\|)+(\{([^{}]+)\})?.*(\|\|)\s*$/', $text, $args))  // Table.
  669.   {
  670.     if(!$in_table)
  671.     {
  672.       $pre = html_table_start($args[3]);
  673.       $in_table = 1;
  674.     }
  675.     $text = preg_replace('/\|\s+\|/e',
  676.                          "q1('|').new_entity(array('raw',' ')).q1('|')", 
  677.                          $text);
  678.     $text = preg_replace('/^((\|\|+)(\{([^{}]+)\})?)(.*)\|\|\s*$/e',
  679.                          "new_entity(array('raw',html_table_row_start('\\4').
  680.                                                  html_table_cell_start(strlen('\\2')/2, '\\4'))).".
  681.                          "q1('\\5').new_entity(array('raw',html_table_cell_end().html_table_row_end()))",
  682.                          $text, -1);
  683.     $text = preg_replace('/((\|\|+)(\{([^{}]+)\})?)/e',
  684.                          "new_entity(array('raw',html_table_cell_end().html_table_cell_start(strlen('\\2')/2, '\\4')))",
  685.                          $text, -1);
  686.   }
  687.   else if($in_table)                    // Have exited table.
  688.   {
  689.     $in_table = 0;
  690.     $pre = html_table_end();
  691.   }
  692.  
  693.   if($pre != '')
  694.     { $text = new_entity(array('raw', $pre)) . $text; }
  695.   if($post != '')
  696.     { $text = $text . new_entity(array('raw', $post)); }
  697.  
  698.   return $text;
  699. }
  700. ?>