-
Notifications
You must be signed in to change notification settings - Fork 1
/
epub-output.php
96 lines (77 loc) · 3.09 KB
/
epub-output.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
include_once(ANTHOLOGIZE_TEIDOM_PATH);
include_once(ANTHOLOGIZE_TEIDOMAPI_PATH);
include_once(WP_PLUGIN_DIR . '/nanowrimo/includes/class-nanowrimo-anthologizer.php');
include_once(WP_PLUGIN_DIR . '/nanowrimo/includes/class-epub-builder.php');
global $tocDOM;
$ops = array('includeStructuredSubjects' => false, //Include structured data about tags and categories
'includeItemSubjects' => false, // Include basic data about tags and categories
'includeCreatorData' => false, // Include basic data about creators
'includeStructuredCreatorData' => false, //include structured data about creators
'includeOriginalPostData' => true, //include data about the original post (true to use tags and categories)
'checkImgSrcs' => false, //whether to check availability of image sources
'linkToEmbeddedObjects' => false,
'indexSubjects' => false,
'indexCategories' => false,
'indexTags' => false,
'indexAuthors' => false,
'indexImages' => false,
);
$ops['outputParams'] = $_SESSION['outputParams'];
$tei = new TeiDom($_SESSION, $ops);
$api = new TeiApi($tei);
$htmler = new NaNoWriMoHTMLer($api, true);
$epub = new EpubBuilder($tei, $htmler->output);
//Hack the toc.ncx file epub writes to make it conform to the ids produced.
$tocDOM = new DOMDocument();
$tocDOM->load($epub->oebpsDir . "toc.ncx");
rewriteTOC($htmler->output);
$tocDOM->save($epub->oebpsDir . "toc.ncx");
$epub->output();
function rewriteTOC($htmlDOM) {
global $tocDOM;
//remove <navMap>
//change depth?
$htmlXPath = new DOMXPath($htmlDOM);
$navMap = $tocDOM->getElementsByTagName('navMap')->item(0);
while($navMap->childNodes->length != 0 ) {
$navMap->removeChild($navMap->firstChild);
}
$parts = $htmlXPath->query("//div[@id='body']/div[@class='part']");
for($partN = 0; $partN < $parts->length; $partN++) {
$part = $parts->item($partN);
$title = $part->firstChild->firstChild->textContent; //shitty practice, I know
$partNavPoint = newNavPoint("body-$partN", $title);
$partNavPoint = $navMap->appendChild($partNavPoint);
$partNavPoint->setAttribute('playOrder', $partN);
//set playorder on $partNavPoint
$navMap->appendChild($partNavPoint);
$items = $htmlXPath->query("div[@class='item']", $part);
for($itemN = 0; $itemN < $items->length; $itemN++) {
$item = $items->item($itemN);
$itemTitle = $item->firstChild->firstChild->textContent; //shitty practice, I know
$itemNavPoint = newNavPoint("body-$partN-$itemN", $itemTitle);
//set playOrder
//append where it goes
//lets try this
$itemNavPoint->setAttribute('playOrder', $itemN);
$partNavPoint->appendChild($itemNavPoint);
}
}
}
function newNavPoint($id, $label) {
global $tocDOM;
$label = htmlspecialchars($label);
$navPoint = $tocDOM->createElement('navPoint');
$navPoint->setAttribute('id', $id);
$navLabelNode = $tocDOM->createElement('navLabel');
$text = $tocDOM->createElement('text', $label);
$navLabelNode->appendChild($text);
$navPoint->appendChild($navLabelNode);
$content = $tocDOM->createElement('content');
$content->setAttribute('src', "main_content.html#$id");
$navPoint->appendChild($content);
return $navPoint;
}
die();
?>