| 1 |
<?php
|
| 2 |
// $Id: export_docbook.module,v 1.4 2005/11/30 17:45:28 puregin Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Allows users to export Drupal books as DocBook XML.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_perm().
|
| 11 |
*/
|
| 12 |
function export_docbook_perm() {
|
| 13 |
return array('export books as DocBook XML');
|
| 14 |
}
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Implementation of hook_access().
|
| 18 |
*/
|
| 19 |
function export_docbook_access($op, $node) {
|
| 20 |
global $user;
|
| 21 |
|
| 22 |
if ($op == 'update') {
|
| 23 |
if ((user_access('maintain books') && !$node->moderate) || ($node->uid == $user->uid && user_access('edit own book pages'))) {
|
| 24 |
return TRUE;
|
| 25 |
}
|
| 26 |
else {
|
| 27 |
// do nothing. node-access() will determine further access
|
| 28 |
}
|
| 29 |
}
|
| 30 |
}
|
| 31 |
|
| 32 |
|
| 33 |
/**
|
| 34 |
* Menu callback; Generates various representation of a book page with
|
| 35 |
* all descendants and prints the requested representation to output.
|
| 36 |
*
|
| 37 |
* Notes:
|
| 38 |
*
|
| 39 |
* The user must have both 'access content' permissions (checked
|
| 40 |
* when the menu item for export is invoked in book.module) and
|
| 41 |
* 'export books as dxml' permissions to export a book.
|
| 42 |
*
|
| 43 |
* DocBook XML will embed a node in a parent book
|
| 44 |
|
| 45 |
* For DocBook output, the exported node will be a document fragment
|
| 46 |
* unless the node is a level 0 node (book), specifically
|
| 47 |
* <ul>
|
| 48 |
* <li>a <chapter> for level 1 elements, </li>
|
| 49 |
* <li>a <section> for levels 2 and deeper.</li>
|
| 50 |
* </ul>
|
| 51 |
*
|
| 52 |
* @param nid
|
| 53 |
* - an integer representing the node id (nid) of the node to export
|
| 54 |
*
|
| 55 |
*/
|
| 56 |
function book_export_docbook($nid = 0, $depth) {
|
| 57 |
$node = node_load(array('nid' => $nid));
|
| 58 |
$depth = count(book_location($node)) + 1;
|
| 59 |
|
| 60 |
if (user_access('export books as DocBook XML')) {
|
| 61 |
drupal_set_header('Content-Type: text/xml; charset=utf-8');
|
| 62 |
$xml = '<?xml version="1.0"?>'."\n";
|
| 63 |
$root_element = '';
|
| 64 |
if ($depth == 1)
|
| 65 |
$root_element = 'book';
|
| 66 |
else if ($depth == 2)
|
| 67 |
$root_element = 'chapter';
|
| 68 |
else
|
| 69 |
$root_element = 'section';
|
| 70 |
$xml .= '<!DOCTYPE '.$root_element.' PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"'.
|
| 71 |
' "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">'."\n";
|
| 72 |
$xml .= book_recurse($nid, $depth, 'export_docbook_node_visitor_pre', 'export_docbook_node_visitor_post');
|
| 73 |
return $xml;
|
| 74 |
}
|
| 75 |
else {
|
| 76 |
drupal_access_denied();
|
| 77 |
}
|
| 78 |
}
|
| 79 |
|
| 80 |
function _export_tidy($html) {
|
| 81 |
// Initialize Tidy
|
| 82 |
ob_start();
|
| 83 |
|
| 84 |
// Set up some configuration options for Tidy
|
| 85 |
$config = array(
|
| 86 |
'indent' => true,
|
| 87 |
'numeric-entities' => true,
|
| 88 |
'output-xhtml' => true,
|
| 89 |
'input-encoding' => 'utf8',
|
| 90 |
'output-encoding' => 'utf8',
|
| 91 |
// 'add-xml-decl' => true,
|
| 92 |
// 'add-xml-space' => true,
|
| 93 |
'drop-proprietary-attributes' => true,
|
| 94 |
'clean' => true,
|
| 95 |
'wrap' => 80);
|
| 96 |
|
| 97 |
$tidy = new tidy;
|
| 98 |
$tidy->parseString($html, $config, 'utf8');
|
| 99 |
$tidy->cleanRepair();
|
| 100 |
$content = tidy_get_output($tidy);
|
| 101 |
|
| 102 |
return $content;
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Generates DocBook XML for a given node. This function is a
|
| 107 |
* 'pre-node' visitor function for book_recurse(). The generated XML
|
| 108 |
* is valid DocBook, but each node's HTML content is wrapped in a
|
| 109 |
* CDATA section, and put inside a <literallayout> element.
|
| 110 |
* The node body has an md5-hash applied; the value of this is
|
| 111 |
* stored as node metadata to allow importing code to determine if
|
| 112 |
* contents have changed. The weight of a node is also stored as
|
| 113 |
* metadata to allow the node to be properly re-imported.
|
| 114 |
*
|
| 115 |
* @param $node
|
| 116 |
* - the node to generate output for.
|
| 117 |
* @param $depth
|
| 118 |
* - the depth of the given node in the hierarchy. This
|
| 119 |
* is currently not used.
|
| 120 |
* @param $nid
|
| 121 |
* - the node id (nid) of the given node. This
|
| 122 |
* is used only for generating output (e.g., id attribute)
|
| 123 |
* @return
|
| 124 |
* - the generated XML for the given node.
|
| 125 |
*/
|
| 126 |
function export_docbook_node_visitor_pre($node, $depth, $nid) {
|
| 127 |
global $locale;
|
| 128 |
|
| 129 |
// Output the content:
|
| 130 |
if (node_hook($node, 'content')) {
|
| 131 |
$node = node_invoke($node, 'content');
|
| 132 |
}
|
| 133 |
// Allow modules to change $node->body before viewing.
|
| 134 |
node_invoke_nodeapi($node, 'export_xml', $node->body, false);
|
| 135 |
|
| 136 |
$html = $node->body;
|
| 137 |
drupal_set_message(print_r($html,true));
|
| 138 |
// clean up the HTML content using W3C Tidy
|
| 139 |
$content = _export_tidy($html);
|
| 140 |
|
| 141 |
// grab the XSL stylesheet from the node where it's stored
|
| 142 |
// and initialize the XSLT processor
|
| 143 |
$xsl = new DOMDocument();
|
| 144 |
try {
|
| 145 |
$xsl->load(dirname(__FILE__).'/h2db.xsl');
|
| 146 |
}
|
| 147 |
catch (DOMException $e) {
|
| 148 |
drupal_set_message(t("There was an error while parsing stylesheet: ").e.getMessage()."\n");
|
| 149 |
}
|
| 150 |
|
| 151 |
$xsltproc = new XSLTProcessor();
|
| 152 |
$xsltproc->importStylesheet($xsl);
|
| 153 |
|
| 154 |
// make a new input DOM document
|
| 155 |
$inputdoc = new DOMDocument();
|
| 156 |
if (!$inputdoc->loadXML($content)) {
|
| 157 |
drupal_set_message(t("There was an error while parsing the content as XML."));
|
| 158 |
}
|
| 159 |
|
| 160 |
$xsltproc->setParameter('', 'graphics_location', 'http://'.$_SERVER['HTTP_HOST']);
|
| 161 |
|
| 162 |
// Perform the XSLT transformation
|
| 163 |
$content = $xsltproc->transformToXML($inputdoc);
|
| 164 |
|
| 165 |
$title = "<title>".$node->title."</title>\n";
|
| 166 |
if ($depth == 1) {
|
| 167 |
$root_element = 'book';
|
| 168 |
$output .= "<book id=\"node-".$node->nid ."\" lang=\"$locale\">\n";
|
| 169 |
$output .= $title;
|
| 170 |
$output .= "<preface>\n";
|
| 171 |
$output .= "<title>".t('Preface')."</title>\n";
|
| 172 |
$output .= $content;
|
| 173 |
$output .= "</preface>\n";
|
| 174 |
}
|
| 175 |
else if ($depth == 2) {
|
| 176 |
$output .= "<chapter id=\"node-".$node->nid ."\" lang=\"$locale\">\n";
|
| 177 |
$output .= $title;
|
| 178 |
$output .= $content;
|
| 179 |
}
|
| 180 |
else {
|
| 181 |
$output .= "<section id=\"node-".$node->nid ."\" lang=\"$locale\">\n";
|
| 182 |
$output .= $title;
|
| 183 |
$output .= $content;
|
| 184 |
}
|
| 185 |
|
| 186 |
return $output;
|
| 187 |
}
|
| 188 |
|
| 189 |
/**
|
| 190 |
* Completes the XML generation for the node. This function is a
|
| 191 |
* 'post-node' visitor function for book_recurse().
|
| 192 |
*/
|
| 193 |
function export_docbook_node_visitor_post($node, $depth) {
|
| 194 |
if ($depth == 1) {
|
| 195 |
return "</book>\n";
|
| 196 |
}
|
| 197 |
else if ($depth == 2) {
|
| 198 |
return "</chapter>\n";
|
| 199 |
}
|
| 200 |
else {
|
| 201 |
return "</section>\n";
|
| 202 |
}
|
| 203 |
}
|
| 204 |
|
| 205 |
/**
|
| 206 |
* @file
|
| 207 |
* Allows authorized users to export a book's structure as DocBook.
|
| 208 |
*/
|
| 209 |
|
| 210 |
/**
|
| 211 |
* Implementation of hook_link().
|
| 212 |
*/
|
| 213 |
function export_docbook_link($type, $node = 0, $main = 0) {
|
| 214 |
$links = array();
|
| 215 |
if ($type == 'node' && isset($node->parent)) {
|
| 216 |
if (!$main) {
|
| 217 |
if (user_access('export books as DocBook XML')) {
|
| 218 |
$links['export_docbook_link'] = array( 'title' => t('export DocBook'),
|
| 219 |
'href'=> 'book/export/docbook/'. $node->nid,
|
| 220 |
'attributes' => array('title' => t('Export this book page and its sub-pages as DocBook.')));
|
| 221 |
}
|
| 222 |
}
|
| 223 |
}
|
| 224 |
return $links;
|
| 225 |
}
|
| 226 |
|
| 227 |
|
| 228 |
/**
|
| 229 |
* Implementation of hook_help().
|
| 230 |
*/
|
| 231 |
function export_docbook_help($section) {
|
| 232 |
switch ($section) {
|
| 233 |
case 'admin/help#export_docbook':
|
| 234 |
return t(
|
| 235 |
"<p>Users can choose to <em>export</em> any book page and its subsections as DocBook XML (for offline editing, or production of print or other electronic publication formats) by selecting the \"export DocBook XML\" link. Note: it may be neccessary to shift-click on the link to save the results to a file on the local computer.</p>
|
| 236 |
|
| 237 |
<p>You can:</p><ul>
|
| 238 |
<li>control who can create, edit, and maintain book pages by setting access permissions:
|
| 239 |
<a href = \"%permissions\" title = \"access permissions\">administer » access control</a>
|
| 240 |
</li></ul>
|
| 241 |
|
| 242 |
<p>For more information, visit the <a href = \"%book-module-help\" title = \"book module online help\">online documentation</a>.
|
| 243 |
",
|
| 244 |
array(
|
| 245 |
'%permissions' => url('admin/access/permissions'),
|
| 246 |
'%book-module-help' => url('http://drupal.org/handbook/modules/book')
|
| 247 |
)
|
| 248 |
);
|
| 249 |
}
|
| 250 |
|
| 251 |
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'outline') {
|
| 252 |
return t('The outline feature allows you to include posts in the <a href="%book">book hierarchy</a>.', array('%book' => url('book')));
|
| 253 |
}
|
| 254 |
}
|