-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrivprocess.class.php
More file actions
362 lines (296 loc) · 9.76 KB
/
scrivprocess.class.php
File metadata and controls
362 lines (296 loc) · 9.76 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/**
* @file scrivprocess.class.php
* Handle parsing and processing for exported files.
* Convert the files to Twitter Bootstrap based site.
*
* @author akempler
*
*/
class ScrivProcess {
/**
* The name of the original uploaded file.
* @var string
*/
protected $filename = '';
/**
* The path to the directory where converted files will be placed.
* For example ./converted/
* @var string
* @access protected
*/
protected $converted_path = './converted/';
/**
* The path to the converted file.
* The file's body has been isolated and the templates have been applied before and after the body.
* @var string
* @access protected
*/
protected $converted_filepath = '';
/**
* The path to the html template files.
* These are prepended and appended to the <body> and </body> tags.
* @var string
* @access protected
*/
protected $template_path = './templates/';
/**
* The path to the file passed to this class. TODO change to source_filepath.
* @var string
* @access protected
*/
protected $original_filepath = '';
/**
* The title to use for the converted document.
* This is parsed out of the document.
* @var string
* @access protected
*/
public $doc_title = '';
/**
* The author to use for the converted document.
* This is parsed out of the document if available.
* @var string
* @access protected
*/
public $doc_author = '';
/**
* The SimpleXML represenation of the file.
* @var SimpleXML Object
* @access protected
*/
protected $sxml = NULL;
/**
* A DOMDocument created via loadHtml() based on the file.
* @var DOMDocument
* @access protected
*/
protected $doc = NULL;
/**
* The type of file being parsed.
* Allowable types are: 'word', 'scrivener'
* @var string
* @access protected
*/
protected $exporttype = NULL;
/**
* Constructor
* @param string $type - 'word' or 'scrivener'
* @todo handle no type passed?
* Could look for it in the head:
* <meta name=Generator content="Microsoft Word 12 (filtered)">
*/
function __construct($type) {
$this->exporttype = $type;
}
/**
* Parse and convert a file to a Twitter Bootstrap based html document.
*
* @access public
* @return string - The name of the file if successful. Otherwise NULL.
*/
public function convert_file($filepath) {
$this->original_filepath = $filepath;
if (isset($_SERVER['WINDIR']) || isset($_SERVER['windir'])) {
$pieces = explode('\\', $filepath);
} else {
$pieces = explode('/', $filepath);
}
$this->filename = array_pop($pieces);
//print" filename=".$this->filename." ";
$this->import_file();
$this->strip_file();
// Wrap the scrivener export in the template
$this->add_template();
return $this->converted_filepath;
}
/**
* Set the path to the directory that will contain the converted files.
* @param string $path
* @access public
*/
public function set_converted_path($path) {
if(!is_dir($path)) {
if (PHP_SAPI === 'cli') {
print"The target directory does not exist: ".$path."\n\n";
die();
}
}
$this->converted_path = $path;
}
/**
* Get the path to the converted file.
* This is the path to the fully converted Twiiter Boostrap-ized file.
* @return string
* @access public
*/
public function get_converted_filepath() {
return $this->converted_filepath;
}
/**
* Creates simplexml and domDocument objects based on the provided file.
* Also does some preliminary cleaning of the file,
* for example, it converts Windows carriage returns to /n to avoid a bunch of entities being generated.
* @access protected
*/
protected function import_file() {
// Read in the file
if(file_exists($this->original_filepath)) {
$html = file_get_contents($this->original_filepath);
if (!$html) {
ScrivMsg::set_message('error', "There was an error processing the file. The errors reported were:");
foreach(libxml_get_errors() as $error) {
ScrivMsg::set_message('error', $error->message);
}
return FALSE;
}
} else {
if (PHP_SAPI === 'cli') {
print"File not found at: ".$this->original_filepath."\n\n";
die();
}
ScrivMsg::set_message('error', "Sorry. There was an error processing the file. Please try again.");
return FALSE;
}
// TODO poor placement of this
$html = $this->raw_cleanup($html);
$html_dom = new DOMDocument();
$html_dom->preserveWhiteSpace = false;
libxml_use_internal_errors(true);
$html_dom->loadHTML($html);
$this->doc = $html_dom;
$xml = $html_dom->saveXML();
//$doc = new DOMDocument();
//@$doc->loadXML($xml, LIBXML_NOXMLDECL);
//$this->sxml = simplexml_import_dom($doc);
$this->sxml = simplexml_load_string($xml);
if (!$this->sxml) {
ScrivMsg::set_message('error', "There was an error processing the file. The errors reported were:");
foreach(libxml_get_errors() as $error) {
ScrivMsg::set_message('error', $error->message);
}
return FALSE;
} else {
return TRUE;
}
}
/**
* Some initial cleanup of the raw html document when first imported.
* @param string - raw html.
*/
protected function raw_cleanup($html) {
// Get rid of Windows style carriage returns.
// domdocument will convert them to
$html = preg_replace('/\r\n/', "\n", $html);
$html = str_replace('<img src="', '<img src="./img/', $html);
//$html = str_replace('<meta charset="utf-8"/>', '', $html);
return $html;
}
/**
* Load the file and resave with only the content inside the body tags.
* @return boolean - true if the file was converted successfully, otherwise false.
* @access protected
*/
protected function strip_file() {
$this->converted_filepath = $this->converted_path . $this->filename;
if($this->exporttype == 'word') {
$this->clean_word();
}
$this->sxml = simplexml_import_dom($this->doc);
// Save just the content in the body. We'll add our own html head.
$this->sxml->body->asXML($this->converted_filepath);
$this->remove_elements();
return TRUE;
}
/**
* Remove the body tag and possibly other elements.
*
* @access protected
* @todo this will fail on a body with attributes since it just looks for <body>
* For example: <body lang="EN-US" link="blue" vlink="purple">
* use a regex instead.
*/
protected function remove_elements() {
$source = $this->converted_filepath;
$data = file_get_contents($source);
$data = str_replace("<body>", "", $data);
$data = str_replace("</body>", "", $data);
file_put_contents($source, $data);
}
/**
* Cleanup a Word file
*
* @access protected
* @todo Break into multiple methods that can be overridden.
*/
protected function clean_word() {
$sxml = simplexml_import_dom($this->doc);
// Remove the table of contents
$nodes = $sxml->xpath('//p[@class="MsoToc1"]');
$nodes = array_merge($nodes, $sxml->xpath('//p[@class="MsoToc2"]'));
$nodes = array_merge($nodes, $sxml->xpath('//p[@class="MsoToc3"]'));
$nodes = array_merge($nodes, $sxml->xpath('//p[@class="MsoTocHeading"]'));
// NOTE, doing this updates the sxml object.
foreach($nodes as $toc) {
$dom = dom_import_simplexml($toc);
if(!$dom) {
// TODO set appropriate msg
echo"Error converting xml";
} else {
$dom->parentNode->removeChild($dom);
}
}
// Add ids to the h1 tags so they can serve as navigation.
// This is done by creating a new h1 element and replacing the existing one.
// That way we remove all the sub elements like spans, etc that Word adds. For example:
// <h1><a name="_Toc320541121"><span style="font-variant:normal !important; text-transform:uppercase">Overview of Steps</span></a></h1>
// We populate the h1 with the text value of the original h1.
$h1s = $this->doc->getElementsByTagName('h1');
$i = $h1s->length;
while ($i >= 0) {
$h1 = $h1s->item($i);
if($h1) {
$newh1 = $this->doc->createElement('h1', $h1->nodeValue);
// manually create ids for the h1 elements since Word does not provide them.
$h1Attribute = $this->doc->createAttribute('id');
$h1Attribute->value = 'h1_'.$i;
$newh1->appendChild($h1Attribute);
$h1->parentNode->replaceChild($newh1, $h1);
}
$i--;
}
$h2s = $this->doc->getElementsByTagName('h2');
$i = $h2s->length;
while ($i >= 0) {
$h2 = $h2s->item($i);
if($h2) {
$newh2 = $this->doc->createElement('h2', $h2->nodeValue);
$h2Attribute = $this->doc->createAttribute('id');
$h2Attribute->value = 'h2_'.$i;
$newh2->appendChild($h2Attribute);
$h2->parentNode->replaceChild($newh2, $h2);
}
$i--;
}
$bodies = $this->doc->getElementsByTagName("body");
foreach($bodies as $body) {
$body->removeAttribute('lang');
$body->removeAttribute('link');
$body->removeAttribute('vlink');
}
} // END clean_word()
/**
* Wrap the exported scrivener file in the template.
* @param string $template
* @access protected
*/
protected function add_template($template='template1') {
$html = file_get_contents($this->template_path.$template.'_top.html');
$html = str_replace("%page_heading%", $this->doc_title, $html);
$html = str_replace("%document_title%", $this->doc_title, $html);
$html = str_replace("%author%", $this->doc_author, $html);
$html .= file_get_contents($this->converted_filepath);
$html .= file_get_contents($this->template_path.$template.'_bottom.html');
file_put_contents($this->converted_filepath, $html);
}
} // END class