-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-nanowrimo-anthologizer.php
308 lines (240 loc) · 9.49 KB
/
class-nanowrimo-anthologizer.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
/*
* append* is for structural data
* write* is for content (html)
*/
abstract class NaNoWriMoAnthologizer {
public $api;
public $useItems = array();
public $badgeURL = '';
public $output;
public function __construct($api, $useItems = false) {
$this->api = $api;
$this->setUseItems($useItems);
$this->init();
$this->appendFront();
$this->appendBody();
$this->finish();
}
public function setUseItems($itemsArray = false) {
if ($itemsArray) {
$this->useItems = $itemsArray;
} else {
$this->useItems = $_SESSION['useItems'];
}
}
abstract function init();
abstract function appendFront();
abstract function appendBody();
abstract function appendPart($section, $partNumber);
abstract function appendItem($section, $partNumber, $itemNumber);
abstract function output();
public function writePartHead($section, $partNumber) {
$html = '';
return $html;
}
public function writeItemHead($section, $partNumber, $itemNumber) {
$html = '';
return $html;
}
public function writeItemContent($section, $partNumber, $itemNumber) {
$html = '';
return $html;
}
public function useItem($guid) {
return (in_array($guid, $this->useItems));
}
public function countItemWords($itemContent) {
return round(str_word_count($itemContent), -1);
}
public function countTotalWords() {
//get the content of all items
//concatenate them together
//count the words
$contentString = '';
$contentNodes = $this->api->getNodeListByXPath("//tei:div[@type='libraryItem']/div");
foreach($contentNodes as $contentNode) {
$contentString .= ' ' . $contentNode->textContent;
}
return round(str_word_count($contentString), -2);
}
public function finish() {
//override me to do any final tasks
}
}
class NaNoWriMoHTMLer extends NaNoWriMoAnthologizer {
public $xpath;
public $totalWords = 0;
public function __construct($api, $useItems = false) {
parent::__construct($api, $useItems);
}
public function init() {
//we'll be building the HTML as a DOMDocument
$this->output = new DOMDocument();
$fontFamily = $this->api->getProjectOutputParams('font-face');
$fontSize = $this->api->getProjectOutputParams('font-size');
$title = $this->api->getProjectTitle(true);
if($fontSize == '') {
$fontSize = "12pt";
}
$html = "<html><head><title>$title</title>
";
$html .= "<style type='text/css'>
@page {margin: 3cm }
body {
font-family:$fontFamily;
font-size :$fontSize;
}
";
$css = "body {font-family: $fontFamily ;
font-size : $fontSize ;
}\n";
$css .= "div.title-container {text-align: center; }\n";
$css .= "ul, li {list-style: none;}\n";
//$html .= $css;
$html .= "</style>";
//echo $html;
//die();
$html .= "</head><body><div id='front'></div><div id='body'></body></html>";
$this->output->loadHTML($html);
$this->xpath = new DOMXPath($this->output);
$this->badgeURL = WP_PLUGIN_URL . "/nanowrimo/images/nanowrimo_participant_09_120x240.png";
}
public function appendFront() {
$frontNode = $this->xpath->query("//div[@id='front']")->item(0);
$titleContainer = $this->output->createElement('div');
$titleContainer->setAttribute('class', 'title-container');
$badge = $this->output->createElement('img');
$badge->setAttribute('src', $this->badgeURL);
$badge->setAttribute('class', 'nnwm-badge');
$anth = $this->output->createElement('img');
$anth->setAttribute('src', WP_PLUGIN_URL . "/nanowrimo/images/web_badge_square4.jpg");
$anth->setAttribute('class', 'anth-badge');
$projectTitle = $this->api->getProjectTitle(true);
$titleH = $this->output->createElement('h1', $projectTitle);
$subtitleH = $this->output->createElement('h2', $this->api->getProjectSubTitle(true));
$subtitleH->setAttribute('class', 'subtitle');
$titleContainer->appendChild($badge);
$titleContainer->appendChild($anth);
$titleContainer->appendChild($titleH);
$titleContainer->appendChild($subtitleH);
$frontNode->appendChild($titleContainer);
$cr = $this->api->getProjectCopyright();
//TODO: fix this in api
//api returns text span
//this substrings just the content
$cr = substr($cr, 43, -7);
$creator = $this->api->getProjectCreator();
$creator = substr($creator, 43, -7);
$frontNode->appendChild($this->output->createElement('p', $creator));
$frontNode->appendChild($this->output->createElement('p', $cr . ", 2010"));
//dedication
$ded = $this->api->getSectionPartItemContent('front', 0, 0, true);
$frontNode->appendChild($this->output->importNode($ded, true));
//acknowledgements
$ack = $this->api->getSectionPartItemContent('front', 0, 1, true);
$frontNode->appendChild($this->output->importNode($ack, true));
$frontNode->appendChild($this->writeTOC());
}
public function appendBody() {
$bodyNode = $this->xpath->query("//div[@id = 'body']")->item(0);
for($partN = 0; $partN < $this->api->getSectionPartCount('body'); $partN++) {
$partDiv = $this->output->createElement('div');
$partDiv->setAttribute('id', "body-$partN");
$partDiv->setAttribute('class', 'part');
$partDiv->appendChild($this->writePartHead('body', $partN));
for($itemN = 0; $itemN < $this->api->getSectionPartItemCount('body', $partN); $itemN++ ) {
$itemDiv = $this->output->createElement('div');
$itemDiv->setAttribute('id', "body-$partN-$itemN");
$itemDiv->setAttribute('class', 'item');
$itemDiv->appendChild($this->writeItemHead('body', $partN, $itemN));
$itemDiv->appendChild($this->writeItemContent('body', $partN, $itemN));
$partDiv->appendChild($itemDiv);
}
$bodyNode->appendChild($partDiv);
}
}
public function appendItem($section, $partNumber, $itemNumber) {
//the HTML output should be nested in divs, rather that linearly as with PDF, so skip this and handle in appendBody
}
public function appendPart($section, $partNumber) {
//the HTML output should be nested in divs, rather that linearly as with PDF, so skip this and handle in appendBody
}
public function output() {
return $this->output->saveHTML();
}
public function writePartHead($section, $partN) {
$partHeaderDiv = $this->output->createElement('div');
$partHeaderDiv->setAttribute('class', 'part-header');
$titleH = $this->output->createElement('h2');
$titleH->setAttribute('class', 'part-title');
$title = $this->output->importNode($this->api->getSectionPartTitle('body', $partN, true), true );
$titleH->appendChild($title->firstChild);
$partHeaderDiv->appendChild($titleH);
return $partHeaderDiv;
}
public function writeItemHead($section, $partN, $itemN) {
$itemHeaderDiv = $this->output->createElement('div');
$itemHeaderDiv->setAttribute('class', 'item-header');
$titleH = $this->output->createElement('h3');
$titleH->setAttribute('class', 'item-title');
$title = $this->output->importNode($this->api->getSectionPartItemTitle($section, $partN, $itemN, true), true );
$titleH->appendChild($title->firstChild);
$itemHeaderDiv->appendChild($titleH);
if( ($this->api->getProjectOutputParams('item-count') == 'on') ||
($this->api->getProjectOutputParams('total-count') == 'on') ) {
$wordCount = $this->countItemWords($this->api->getSectionPartItemContent($section, $partN, $itemN));
$this->totalWords = $this->totalWords + $wordCount;
}
if($this->api->getProjectOutputParams('item-count') == 'on') {
$wcP = $this->output->createElement('p', "About $wordCount words");
$itemHeaderDiv->appendChild($wcP);
}
return $itemHeaderDiv;
}
public function writeItemContent($section, $partN, $itemN) {
$contentDiv = $this->output->importNode($this->api->getSectionPartItemContent($section, $partN, $itemN, true), true);
return $contentDiv;
}
public function writeTOC() {
//TODO: also make changes to epub toc.ncx while I'm looping for same data
$tocDiv = $this->output->createElement('div');
$tocDiv->setAttribute('class', 'toc');
$tocDiv->appendChild($this->output->createElement('h2', 'Table of Contents'));
$partsUL = $this->output->createElement('ul');
$partsUL->setAttribute('class', 'toc-parts');
for($partN = 0; $partN < $this->api->getSectionPartCount('body'); $partN++) {
$partLI = $this->output->createElement('li');
$partLI->setAttribute('class', 'toc-part');
$titleH = $this->output->createElement('h3');
$title = $this->output->importNode($this->api->getSectionPartTitle('body', $partN, true), true );
$titleH->appendChild($title->firstChild);
$partLI->appendChild($titleH);
$itemsUL = $this->output->createElement('ul');
$itemsUL->setAttribute('class', 'toc-items');
$partLI->appendChild($itemsUL);
for($itemN = 0; $itemN < $this->api->getSectionPartItemCount('body', $partN); $itemN++ ) {
$itemLI = $this->output->createElement('li');
$itemLI->setAttribute('class', 'toc-item');
$itemTitleH = $this->output->createElement('h4');
$itemTitle = $this->output->importNode($this->api->getSectionPartItemTitle('body', $partN, $itemN, true), true );
$titleA = $this->output->createElement('a');
$titleA->appendChild($itemTitle->firstChild);
$titleA->setAttribute('href', "#body-$partN-$itemN");
$itemTitleH->appendChild($titleA);
$itemLI->appendChild($itemTitleH);
$itemsUL->appendChild($itemLI);
}
$partsUL->appendChild($partLI);
}
$tocDiv->appendChild($partsUL);
return $tocDiv;
}
public function finish() {
if ( $this->api->getProjectOutputParams('total-count') == 'on') {
$frontNode = $this->xpath->query("//div[@id='front']")->item(0);
$words = $this->output->createElement('p', "About " . $this->totalWords . " words");
$frontNode->firstChild->appendChild($words);
}
}
}