-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopf_generator.php
128 lines (115 loc) · 4.4 KB
/
opf_generator.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
include dirname(__FILE__).'/include.php';
$root = require 'directory_saved.php';
$nav_levels = array();
for($i=0; $i<10; $i++) $nav_levels[$i] = isset($settings['levels'][$i])&&$settings['levels'][$i];
$docs_used = array();
function dfs(node $node, $depth = 0) {
global $manifest, $mtc, $order, $navs, $tul, $nav_levels, $docs_used;
if(isset($docs_used[$node->hash])) return;
$docs_used[$node->hash] = true;
$manifest.='<item id="' . $node->hash . '" media-type="application/xhtml+xml" href="docs/' . $node->hash .'.html"></item>';
if ($nav_levels[$depth]) {
$mtc .= ' <itemref idref="' . $node->hash. '"/>';
$navs.='
<navPoint class="h' . $depth . '" id="' . $node->hash . '" playOrder="' . ($order++) . '">
<navLabel>
<text>' . $node->name . '</text>
</navLabel>
<content src="docs/' . $node->hash . '.html"/>';
$tul.='<li class="h' . $depth . '"><a href="' . $node->hash . '.html">' . $node->name . '</a></li>';
if (count($node->children) > 0)
$tul.='<ul>';
}
foreach ($node->children as $child)
dfs($child, $depth + 1);
if ($nav_levels[$depth]) {
$navs.="</navPoint>";
if (count($node->children) > 0)
$tul.='</ul>';
}
}
$manifest = $mtc = $navs = $tul = '';
$order = 2;
dfs($root);
$objects = scandir("docs/img");
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
$manifest .= '<item id="' . $object . '" media-type="image/jpeg" href="docs/img/' . $object . '"/>';
}
}
$ncx = '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN"
"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
<!--
For a detailed description of NCX usage please refer to:
http://www.idpf.org/2007/opf/OPF_2.0_final_spec.html#Section2.4.1
-->
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1" xml:lang="en-US">
<head>
<meta name="dtb:uid" content="BookId"/>
<meta name="dtb:depth" content="2"/>
<meta name="dtb:totalPageCount" content="0"/>
<meta name="dtb:maxPageNumber" content="0"/>
</head>
<docTitle>
<text>' . $root->name . '</text>
</docTitle>
<docAuthor>
<text>Georgeee</text>
</docAuthor>
<navMap>
<navPoint class="toc" id="toc" playOrder="1">
<navLabel>
<text>Содержание</text>
</navLabel>
<content src="docs/toc.html"/>
</navPoint>
' . $navs . '
</navMap>
</ncx>';
$opf = '<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="BookId">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:title>' . $root->name . '</dc:title>
<dc:language>'.$settings['book']['lang'].'</dc:language>
'.(empty($settings['creator'])?'':'<dc:creator>'.implode('</dc:creator><dc:creator>', $settings['creator']).'</dc:creator>').'
<dc:publisher>Georgeee</dc:publisher>
<dc:creator>Georgeee</dc:creator>
<dc:subject>Reference</dc:subject>
<dc:date>' . date('Y-m-d') . '</dc:date>
<dc:description>'.(empty($settings['book']['desc'])?'':$settings['book']['desc']).'</dc:description>
</metadata>
<manifest>
<item id="toc" media-type="application/xhtml+xml" href="docs/toc.html"></item>
' . $manifest . '
<item id="My_Table_of_Contents" media-type="application/x-dtbncx+xml" href="book.ncx"/>
</manifest>
<spine toc="My_Table_of_Contents">
<itemref idref="toc"/>
' . $mtc . '
</spine>
<guide>
<reference type="toc" title="Содержание" href="docs/toc.html"></reference>
</guide>
</package>';
$toc = '<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Содержание</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>
<div>
<h2>Содержание</h2>
<ul>
' . $tul . '
</ul>
</div>
</body>
</html>';
file_put_contents("book.opf", $opf);
file_put_contents("book.ncx", $ncx);
file_put_contents('docs/toc.html', $toc);
//exec('./kindlegen/kindlegen book.opf');