moderate) || ($node->uid == $user->uid && user_access('edit own book pages'))) {
return TRUE;
}
else {
// do nothing. node-access() will determine further access
}
}
}
/**
* Menu callback; Generates various representation of a book page with
* all descendants and prints the requested representation to output.
*
* Notes:
*
* The user must have both 'access content' permissions (checked
* when the menu item for export is invoked in book.module) and
* 'export books as dxml' permissions to export a book.
*
* DocBook XML will embed a node in a parent book
* For DocBook output, the exported node will be a document fragment
* unless the node is a level 0 node (book), specifically
*
* - a <chapter> for level 1 elements,
* - a <section> for levels 2 and deeper.
*
*
* @param nid
* - an integer representing the node id (nid) of the node to export
*
*/
function book_export_docbook($nid = 0, $depth) {
$type = drupal_strtolower($type);
$depth = count(book_location($node)) + 1;
if (user_access('export books as DocBook XML')) {
drupal_set_header('Content-Type: text/xml; charset=utf-8');
$xml = "\n";
if ($depth == 1) {
$xml .= "\n";
}
else if ($depth > 1) {
$xml .= "\n";
}
$xml .= book_recurse($nid, $depth, 'export_docbook_node_visitor_pre', 'export_docbook_node_visitor_post');
return $xml;
}
else {
drupal_access_denied();
}
}
function _export_tidy($html) {
// Initialize Tidy
ob_start();
// Set up some configuration options for Tidy
$config = Array(
'Indent' => True,
'Numeric-Entities' => True,
'Output-Xhtml' => True,
'Wrap' => 80);
$tidy = new tidy;
$tidy->parseString($html, $config, 'utf8');
$tidy->cleanRepair();
$content = $tidy;
$newdom = new DOMDocument('1.0');
$dom = DOMDocument::LoadXML($content);
$body = $dom->getElementsByTagname('body');
$body = $body->item(0);
$children = $body->childNodes;
foreach ($children as $child) {
// echo $child->nodeValue;
// echo "
\n";
$domnode = $newdom->importNode($child, true);
$newdom->appendChild($domnode);
}
$content = $newdom->saveXML();
// Strip out XML directives
$content = preg_replace('/<\?xml version=.*\?>\s*/i', '', $content);
return "\n\n$content\n\n";
}
/**
* Generates DocBook XML for a given node. This function is a
* 'pre-node' visitor function for book_recurse(). The generated XML
* * is valid DocBook, but each node's HTML content is wrapped in a
* CDATA * section, and put inside a <literallayout> element.
* The node * body has an md5-hash applied; the value of this is
* stored as node * metadata to allow importing code to determine if
* contents have * changed. The weight of a node is also stored as
* metadata to allow * the node to be properly re-imported.
*
* @param $node
* - the node to generate output for.
* @param $depth
* - the depth of the given node in the hierarchy. This
* is currently not used.
* @param $nid
* - the node id (nid) of the given node. This
* is used only for generating output (e.g., id attribute)
* @return
* - the generated XML for the given node.
*/
function export_docbook_node_visitor_pre($node, $depth, $nid) {
// Output the content:
if (node_hook($node, 'content')) {
$node = node_invoke($node, 'content');
}
// Allow modules to change $node->body before viewing.
node_invoke_nodeapi($node, 'export_xml', $node->body, false);
$title = "". $node->title ."\n";
$html = $node->body;
// clean up the HTML content using W3C Tidy
$content = _export_tidy($html);
// grab the XSL stylesheet from the node where it's stored
// and initialize the XSLT processor
$xsl = new DOMDocument('1.0', 'iso-8859-1');
try {
$xsl->load(drupal_get_path('module', 'export_docbook').'/h2db.xsl');
}
catch (DOMException $e) {
echo '';
print_r ($e);
echo '';
}
// make a new input DOM document
$xml = new DOMDocument('1.0', 'iso-8859-1');
if (!$xml->loadXML($content)) {
drupal_set_message(t("There was an error while parsing the content as XML\n"));
}
$xsltproc = new XSLTProcessor();
$xsltproc->importStylesheet($xsl);
// Strip out XML directives
$content = preg_replace('/<\?xml version=.*\?>\s*/i', '', $content);
// Perform the XSLT transformation
$content = $xsltproc->transformToXML($xml);
// Strip out XML directives
$content = preg_replace('/<\?xml version=.*\?>\s*/i', '', $content);
if ($depth == 1) {
$output .= "nid ."\">\n";
$output .= $title;
$output .= "\n";
$output .= "Preface\n";
$output .= $content;
$output .= "\n";
}
else if ($depth == 2) {
$output .= "nid ."\">\n";
$output .= $title;
$output .= $content;
}
else {
$output .= "nid ."\">\n";
$output .= $title;
$output .= $content;
}
return $output;
}
/**
* Completes the XML generation for the node. This function is a
* 'post-node' visitor function for book_recurse().
*/
function export_docbook_node_visitor_post($node, $depth) {
if ($depth == 1) {
return "\n";
}
else if ($depth == 2) {
return "\n";
}
else {
return "\n";
}
}
/**
* @file
* Allows authorized users to export a book's structure as DocBook.
*/
/**
* Implementation of hook_link().
*/
function export_docbook_link($type, $node = 0, $main = 0) {
$links = array();
if ($type == 'node' && isset($node->parent)) {
if (!$main) {
if (user_access('export books as DocBook XML')) {
$links[] = l(t('export DocBook'),
'book/export/docbook/'. $node->nid,
array('title' => t('Export this book page and its sub-pages as DocBook.')));
}
}
}
return $links;
}
/**
* Implementation of hook_help().
*/
function export_docbook_help($section) {
switch ($section) {
case 'admin/help#export_docbook':
return t(
"Users can choose to export 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.
You can:
For more information, visit the online documentation.
",
array(
'%permissions' => url('admin/access/permissions'),
'%book-module-help' => url('http://drupal.org/handbook/modules/book')
)
);
case 'admin/modules#description':
return t('Allows authorized users to export a book in DocBook XML format.');
}
if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'outline') {
return t('The outline feature allows you to include posts in the book hierarchy.', array('%book' => url('book')));
}
}