convert_to_xml.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 
00025 
00032 function get_id_title()
00033 {
00034   $node_path = WebApp::getSVar('docbook->node_path');
00035   $navig = get_arr_navigation($node_path);
00036   $title = $navig['this_title'];
00037   if ($node_path=='./')
00038     {
00039       $id = WebApp::getSVar('docbook->book_id');
00040     }
00041   else
00042     {
00043       ereg('([^/]+)/$', $node_path, $regs);
00044       $id = $regs[1];
00045     }
00046 
00047   return array($id,$title);
00048 }
00049 
00050 function xml_to_xml($content)
00051 {
00052   //complete the content by adding the title etc.
00053   list($id, $title) = get_id_title();
00054   $node_types = process_index_node('get_node_type');
00055   list($node_type, $subnode_type) = explode(':', $node_types);
00056 
00057   $content = str_replace("\r\n", "\n", $content);
00058   $content = chop($content)."\n";
00059   $content = "<?xml version='1.0' encoding='utf-8'?>
00060 <$node_type id='$id'>
00061 <title>$title</title>$content</$node_type>
00062 ";
00063 
00064   return (is_wellformed($content) ? $content : 'ERROR');
00065 }
00066 
00071 function is_wellformed($xml_content)
00072 {
00073   //write the xml content to a temporary file
00074   $tmpfile = write_tmp_file($xml_content);
00075 
00076   //process it with a transformer which makes no changes
00077   //just to check for any errors
00078   $xsl_file = XSLT."edit_content/xml2xml1.xsl";
00079   $output = shell("xsltproc $xsl_file $tmpfile");
00080 
00081   unlink($tmpfile);
00082 
00083   //check for any errors
00084   if (ereg("^$tmpfile:", $output))
00085     {
00086       //WebApp::message($output);
00087       popup_window('Transformation Error', "<xmp>$output</xmp>");
00088       return false;
00089     }
00090   else
00091     return true;
00092 }
00093 
00094 function html_to_xml($content)
00095 {
00096   //complete the content by adding the title etc.
00097   list($id, $title) = get_id_title();
00098   $node_types = process_index_node('get_node_type');
00099   list($node_type, $subnode_type) = explode(':', $node_types);
00100 
00101   $content = "<?xml version='1.0' encoding='utf-8'?>
00102 <$node_type id='$id'>
00103 <title>$title</title>
00104 
00105 $content
00106 </$node_type>
00107 ";
00108 
00109   //write the modified content to a temporary file
00110   $tmpfile = write_tmp_file($content);
00111 
00112   //process it with a transformer
00113   $normalize = WebApp::getSVar('edit_content->normalize');
00114   $norm = ($normalize=='true' ? '_normalize' : '');
00115   $xsl_file = XSLT."edit_content/html2xml${norm}.xsl";
00116   $xml_content = shell("xsltproc $xsl_file $tmpfile");
00117 
00118   //check for any errors during conversion
00119   if (ereg("^$tmpfile:", $xml_content))
00120     {
00121       //WebApp::message($xml_content);
00122       popup_window('Transformation Error', "<xmp>$xml_content</xmp>");
00123       return 'ERROR';
00124     }
00125 
00126   unlink($tmpfile);
00127 
00128   return $xml_content;
00129 }
00130 
00131 function text_to_xml($content)
00132 {
00133   //include the converter from Text/Wiki to XML
00134   include_once dirname(__FILE__)."/text_to_xml/package.WikiConverter.php";
00135 
00136   //get id, title, node_type, etc.
00137   list($id, $title) = get_id_title();
00138   $node_types = process_index_node('get_node_type');
00139   list($node_type, $subnode_type) = explode(':', $node_types);
00140 
00141   //convert to XML
00142   if ($node_type=='bookinfo' or $node_type=='articleinfo')
00143     {
00144       $info = new Info();
00145       $info->parse($content);
00146       $xml_content = $info->to_xml();
00147     }
00148   else
00149     {
00150       $parser = new WikiParser;
00151       $tpl = $parser->parse_string($content);
00152       $xml_content = $tpl->to_xml();
00153       if (trim($xml_content)=='')  $xml_content = '<para/>';
00154     }
00155 
00156   //complete the xml_content by adding the title etc.
00157   if ($node_type=='book' or $node_type=='article')
00158     {
00159       $xml_content = "<?xml version='1.0' encoding='utf-8' standalone='no'?>
00160 <$node_type id='$id'><title>$title</title></$node_type>";
00161     }
00162   else if ($node_type=='bookinfo' or $node_type=='articleinfo')
00163     {
00164       $xml_content = "<?xml version='1.0' encoding='utf-8' standalone='no'?>
00165 <$node_type id='$id'>$xml_content</$node_type>";
00166     }
00167   else
00168     {
00169       $xml_content = "<?xml version='1.0' encoding='utf-8' standalone='no'?>
00170 <$node_type id='$id'><title>$title</title>$xml_content</$node_type>";
00171     }
00172 
00173   return (is_wellformed($xml_content) ? $xml_content : 'ERROR');
00174 }
00175 
00176 function latex_to_xml($content)
00177 {
00178   $msg = "Converting from LATEX to XML is not implemented yet,\n"
00179     . "so, any modifications are not saved.\n";
00180   WebApp::message($msg);
00181 
00182   $xml_content = 'ERROR';
00183   return $xml_content;
00184 }
00185 
00186 function texi_to_xml($content)
00187 {
00188   $msg = "Converting from TEXI to XML is not implemented yet,\n"
00189     . "so, any modifications are not saved.\n";
00190   WebApp::message($msg);
00191 
00192   $xml_content = 'ERROR';
00193   return $xml_content;
00194 }
00195 ?>

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