process_content.php

Go to the documentation of this file.
00001 <?php
00002   /*
00003    This file is part of DocBookWiki.  DocBookWiki is a web application
00004    that displays and edits DocBook documents.
00005 
00006    Copyright (C) 2004, 2005, 2006, 2007
00007    Dashamir Hoxha, dashohoxha@users.sourceforge.net
00008 
00009    DocBookWiki is free software; you can redistribute it and/or modify
00010    it under the  terms of the GNU General  Public License as published
00011    by the Free  Software Foundation; either version 2  of the License,
00012    or (at your option) any later version.
00013 
00014    DocBookWiki is distributed in the  hope that it will be useful, but
00015    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  DocBookWiki;  if  not,  write  to  the  Free  Software
00021    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00022    USA
00023   */
00024 
00033 include_once dirname(__FILE__).'/convert_from_xml.php';
00034 
00039 function get_node_content($xml_file, $mode)
00040 {
00041   //get the content of the xml file
00042   $xml_content = implode('', file($xml_file));
00043 
00044   //strip <![CDATA[...]]> and <!--comment-->
00045   $xml_content = strip_comments($xml_content);
00046   $xml_content = strip_cdata($xml_content);
00047 
00048   //write the modified xml content to a temporary file
00049   $tmpfile = write_tmp_file($xml_content);
00050 
00051   //generate the html content by processing the temporary file
00052   if ($mode=='html' or $mode=='text')
00053     {
00054       $converter = "xml_to_$mode";
00055       $html_content = $converter($tmpfile);
00056     }
00057   else 
00058     {
00059       $xsl_file = XSLT."edit_content/xml2${mode}.xsl";
00060       $html_content = shell("xsltproc $xsl_file $tmpfile");
00061     } 
00062   unlink($tmpfile);
00063 
00064   //put back the <![CDATA[...]]> and <!--comment--> parts
00065   $html_content = putback_cdata($html_content, 'html');
00066   if ($mode=='text')  $html_content = compact_cdata($html_content);
00067   $html_content = putback_comments($html_content);
00068 
00069   //comment any {{variables}} so that they are not substituted
00070   $html_content = str_replace('{{', '{{#', $html_content);
00071 
00072   return $html_content;
00073 }
00074 
00076 function strip_cdata($str)
00077 {
00078   global $arr_cdata;
00079 
00080   preg_match_all('#<!\[CDATA\[.*?]]>#s', $str, $matches);
00081   $arr_cdata = $matches[0];
00082 
00083   for ($i=0; $i < sizeof($arr_cdata); $i++)
00084     {
00085       $str = str_replace($arr_cdata[$i], "<cdata>$i</cdata>", $str);
00086       $arr_cdata[$i] = str_replace("\r\n", "\n", $arr_cdata[$i]);
00087     }
00088       
00089   return $str;
00090 }
00091 
00093 function strip_comments($str)
00094 {
00095   global $arr_comments;
00096 
00097   preg_match_all('#<!--.*?-->#s', $str, $matches);
00098   $arr_comments = $matches[0];
00099 
00100   for ($i=0; $i < sizeof($arr_comments); $i++)
00101     {
00102       $str = str_replace($arr_comments[$i], "<comment>$i</comment>", $str);
00103       $arr_comments[$i] = str_replace("\r\n", "\n", $arr_comments[$i]);
00104     }
00105       
00106   return $str;
00107 }
00108 
00110 function putback_comments($str)
00111 {
00112   global $arr_comments;
00113   for ($i=0; $i < sizeof($arr_comments); $i++)
00114     {
00115       $str = str_replace("<comment>$i</comment>", $arr_comments[$i], $str);
00116     }
00117   return $str;
00118 }
00119 
00121 function putback_cdata($str, $mode ='xml')
00122 {
00123   global $arr_cdata;
00124   for ($i=0; $i < sizeof($arr_cdata); $i++)
00125     {
00126       $cdata = $arr_cdata[$i];
00127       if ($mode=='html')
00128         {
00129           $cdata = preg_replace('#&(\w+);#', '&amp;$1;', $cdata);
00130           $cdata = preg_replace('#^<#', '&lt;', $cdata);
00131         }
00132       $str = str_replace("<cdata>$i</cdata>", $cdata, $str);
00133     }
00134   return $str;
00135 }
00136 
00151 function expand_cdata($content)
00152 {
00153   $content = preg_replace('#\r\n#', "\n", $content);
00154 
00155   $patterns = 
00156     array(
00157           // --tag-- ==> --tag <![CDATA[
00158           '#--(code|scr|screen|ll)-- *\n(.*?)\n---- *\n?#s',
00159 
00160           // [[xyz]] ==> <![CDATA[xyz]]>
00161           "#(?<!\\\\)\\[\\[(.*?)\\]\\]#s",
00162           );
00163   $replacements = 
00164     array(
00165           //  --tag-- ==> --tag <![CDATA[
00166           "--\\1\n<![CDATA[\\2]]>\n----\n",
00167 
00168           // [[xyz]] ==> <![CDATA[xyz]]>
00169           '<![CDATA[\\1]]>',
00170           );
00171   $content = preg_replace($patterns, $replacements, $content);
00172 
00173   return $content;
00174 }
00175 
00190 function compact_cdata($content)
00191 {
00192   // <![CDATA[xyz]]> ==> [[xyz]] 
00193   $pattern1 = '#&lt;!\[CDATA\[(.*?)\]\]>#s';
00194   $replacement1 = '[[\\1]]';
00195   $content = preg_replace($pattern1, $replacement1, $content);
00196 
00197   // --tag-- ==> --tag [[ ==> --tag--
00198   $pattern2 = '#--(code|scr|screen|ll)\s+\[\[(.*?)\]\]\s*\n---- *\n#s';
00199   $replacement2 = "--\\1--\n\\2\n----\n";
00200   $content = preg_replace($pattern2, $replacement2, $content);
00201 
00202   return $content;
00203 }
00204 ?>

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