fun.regex_replace.php

Go to the documentation of this file.
00001 <?php
00002   /*
00003    This  file is part  of WikiConverter.   WikiConverter is  a program
00004    that  converts   text/wiki  into   other  formats  (like   html  or
00005    xml/docbook).
00006 
00007    Copyright (c) 2005 Dashamir Hoxha, dhoxha@inima.al
00008 
00009    WikiConverter  is free  software;  you can  redistribute it  and/or
00010    modify  it under the  terms of  the GNU  General Public  License as
00011    published by the Free Software  Foundation; either version 2 of the
00012    License, or (at your option) any later version.
00013 
00014    WikiConverter is  distributed in the  hope that it will  be useful,
00015    but  WITHOUT ANY  WARRANTY; without  even the  implied  warranty of
00016    MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
00017    General Public License for more details.
00018 
00019    You should have  received a copy of the  GNU General Public License
00020    along  with  WikiConverter; if  not,  write  to  the Free  Software
00021    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00022    USA
00023   */
00024 
00054 function regex_replace($line)
00055 {
00056   $patterns = 
00057     array(
00058           // [menu->item->subitem (Ctrl-C)]
00059           "#(?<!\\\\|\[)\[(([^\]]*->[^\]]*)?\s*(\(.*\))?)\s*\]#e",
00060 
00061           // [> xref]
00062           "#(?<!\\\\|\[)\[>([^\]]*)\]#",
00063 
00064           // [< imagename < width < alt]
00065           "#(?<!\\\\|\[)\[<([^\]<]*)(<([^<]*))?(<([^\]]*))?\]#e",
00066 
00067           // [label > url]
00068           "#(?<!\\\\|\[)\[([^\]]+)>([^\]]*)\]#",
00069 
00070           // [/footnote]
00071           "#(?<!\\\\|\[)\[/([^\]]+)\]#",
00072 
00073           // [url]
00074           "#(?<!\\\\|\[)\[([^\]]+)\]#",
00075 
00076           // `command`
00077           "#(?<!\\\\)`(.+?)(?<!\\\\)`#",
00078 
00079           // ~filename~
00080           "#(?<!\\\\)~(.+?)(?<!\\\\)~#",
00081 
00082           // _emphasized_
00083           "#(?<!\\\\)_(.+?)(?<!\\\\)_#",
00084 
00085           /* prompt!# command */
00086           "/^(\s*)(.*?)\s*(?<!\\\\)!#\s*(.*?)(\s*)\$/",
00087 
00088           /* bash$ cmd   or bash# cmd */
00089           "/^(\s*)(?<!\\\\)(bash\\\$|bash#)\s+(.*?)(\s*)\$/",
00090 
00091           // \[escaped]
00092           "#\\\\\[([^\]]+)\]#",
00093 
00094           /* < -> &lt; */
00095           "/\\\\</",
00096 
00097           /* > -> &gt; */
00098           "/\\\\>/",
00099 
00100           /* \!# -> !# */
00101           "/\\\\!#/",
00102 
00103           /* '\bash$' and '\bash#' at the begining of a line */
00104           "/^(\s*)\\\\(bash\\\$|bash#)/",
00105 
00106           /* remove the slash (\) before the escaped chars */
00107           "/\\\\(_|`|~)/",
00108           );
00109 
00110   $replacements = 
00111     array(
00112           // [menu->item->subitem]
00113           "convert_menu_items('\\1')",
00114 
00115           // [> (xref)]
00116           '<xref linkend="\\1"/>',
00117 
00118           // [< (imagename) (< (width))? (< (alt))?]
00119           "mediaobject('\\1', '\\3', '\\5')",
00120 
00121           // [(label)>(url)]
00122           '<ulink url="\\2">\\1</ulink>',
00123 
00124           // [/(footnote)]
00125           '<footnote><para>\\1</para></footnote>',
00126 
00127           // [(url)]
00128           '<ulink url="\\1"/>',
00129 
00130           // `(command)`
00131           '<command>\\1</command>',
00132 
00133           // ~(filename)~
00134           '<filename>\\1</filename>',
00135 
00136           // _(emphasized)_
00137           '<emphasis>\\1</emphasis>',
00138 
00139           /* prompt!# command */
00140           '\\1<prompt>\\2</prompt> <command>\\3</command>\\4',
00141 
00142           /* bash$ cmd   or bash# cmd */
00143           '\\1<prompt>\\2</prompt> <command>\\3</command>\\4',
00144 
00145           // \[(escaped)]
00146           '[\\1]',
00147 
00148           /* < -> &amp;lt; */
00149           '&amp;lt;',
00150 
00151           /* > -> &amp;gt; */
00152           '&amp;gt;',
00153 
00154           /* \!# -> !# */
00155           '!#',
00156 
00157           /* '\bash$' and '\bash#' at the begining of a line */
00158           '\\1\\2',
00159 
00160           /* remove the slash (\) before the escaped chars */
00161           '\\1',
00162           );
00163 
00164   $line = preg_replace($patterns, $replacements, $line);
00165   return $line;
00166 }
00167 
00182 function convert_menu_items($str)
00183 {
00184   $pos = strpos($str, '(');
00185   if ($pos===false)
00186     {
00187       $menu_path = $str;
00188       $shortcut = '';
00189     }
00190   else
00191     {
00192       $menu_path = substr($str, 0, $pos);
00193       $shortcut = substr($str, $pos + 1);
00194 
00195       $pos = strpos($shortcut, ')');
00196       if ($pos)  $shortcut = substr($shortcut, 0, $pos);
00197     }
00198   $menu_path = trim($menu_path);
00199   $shortcut = trim($shortcut);
00200 
00201   $arr_menu = explode('->', $menu_path);
00202   $arr_keys = explode('-', $shortcut);
00203 
00204   //count the number of nonempty menuitems
00205   for ($i=0; $i < sizeof($arr_menu); $i++)
00206     {
00207       $item = trim($arr_menu[$i]);
00208       if ($item!='') $item_cnt++;
00209     }
00210 
00211   //decide whether to output a <menuchoice> element
00212   if ($menu_path=='')  $mchoice = false;
00213   else if ($item_cnt==1 and $shortcut=='')  $mchoice = false;
00214   else $mchoice = true;
00215 
00216   $xml = '';
00217   if ($mchoice)  $xml .= '<menuchoice>';
00218   if ($shortcut!='')
00219     {
00220       if ($mchoice)  $xml .= '<shortcut>';
00221       $xml .= '<keycombo>';
00222       for ($i=0; $i < sizeof($arr_keys); $i++)
00223         {
00224           $key = trim($arr_keys[$i]);
00225           $xml .= '<keycap>'.$key.'</keycap>';          
00226         }
00227       $xml .= '</keycombo>';
00228       if ($mchoice)  $xml .= '</shortcut>';
00229     }
00230   if ($menu_path!='')
00231     {
00232       for ($i=0; $i < sizeof($arr_menu); $i++)
00233         {
00234           $item = $arr_menu[$i];
00235           $item = preg_replace('/(.)_/', '<accel>\\1</accel>', $item);
00236           $item = trim($item);
00237           if ($item=='')  continue;
00238 
00239           if ($i==0)
00240             $xml .= '<guimenu>'.$item.'</guimenu>';
00241           else if ($i==(sizeof($arr_menu)-1))
00242             $xml .= '<guimenuitem>'.$item.'</guimenuitem>';
00243           else
00244             $xml .= '<guisubmenu>'.$item.'</guisubmenu>';
00245         }
00246     }
00247   if ($mchoice)  $xml .= '</menuchoice>';
00248 
00249   return $xml;
00250 }
00251 
00253 function mediaobject($filename, $width ='', $alt ='')
00254 {
00255   //print "'$filename' '$width' '$alt'"; //debug
00256   $widthparam = ($width=='' ? '' : "width=\"$width\"");
00257   $str = "
00258 <mediaobject>
00259   <imageobject><imagedata fileref=\"$filename\" $widthparam align=\"center\"/></imageobject>
00260   <textobject><phrase>$alt</phrase></textobject>
00261 </mediaobject>
00262 ";
00263   return $str;
00264 }
00265 ?>

Generated on Wed Jan 9 08:27:32 2008 for DokBookWiki by  doxygen 1.5.2