summaryrefslogtreecommitdiff
path: root/buildscripts/chmbuilder/ChmQuickstartBuilder.php
blob: 43b55d12391c83c4c162819c35ae143471a22088 (plain)
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php

class ChmQuickstartBuilder
{
	private $base;
	const DEMO_URL = 'http://www.pradosoft.com/demos/quickstart/';
	const CSS_URL = 'assets/chm_style.css';
	private $output_dir;
	private $app;

	private $_viewed=array();

	public function __construct($base,$output)
	{
		$this->base = $base;
		$this->output_dir = $output;

		if(!is_dir($this->output_dir) || !is_dir($this->output_dir.'/assets'))
		{
			@mkdir($this->output_dir);
			@mkdir($this->output_dir.'/assets/');
			copy(dirname(__FILE__).'/chm_style.css', $this->output_dir.'/assets/chm_style.css');
		}

		Prado::setPathOfAlias('Output', realpath($this->output_dir));
	}

	public function buildDoc($pages)
	{
		foreach($pages as $section)
		{
			foreach($section as $page)
			{
				$this->parsePage($page);
			}
		}
	}

	protected function initApp()
	{
		$this->app = new TApplication($this->base);
		$response = new THttpResponse();
		$response->setBufferOutput(false);
		$this->app->setResponse($response);
		$assets = new TAssetManager();
		$assets->setBasePath('Output.assets.*');
		$this->app->setAssetManager($assets);
	}

	public function parsePage($page)
	{
		$_GET['page'] = str_replace(array('/','.page'),array('.',''),$page);
		$_GET['notheme'] = 'true';

		$html = $this->parseHtmlContent($this->getApplicationContent());
		$file = str_replace(array('/','.page'), array('_','.html'),$page);
//		echo 'writing file '.$file."\n";
		file_put_contents($this->output_dir.'/'.$file, $html);
	}

	public function getApplicationContent()
	{
		ob_start();
		$this->initApp();
		$this->app->run();
		$content = ob_get_contents();
		ob_end_clean();
		return $content;
	}

	public function parseHtmlContent($content)
	{
		$html = preg_replace('/<input.*name="PRADO_PAGESTATE" [^>]+\/>/m', '', $content);
$html = str_replace('<div id="header">
<div class="title">Prado QuickStart Tutorial</div>
<div class="image"></div>
</div>', '', $html);
$html = preg_replace('/<div id="footer">.*?<\/div>/ms', '<div id="footer">
Copyright &copy; 2005-2007 <a href="http://www.pradosoft.com">PradoSoft</a>.</div>', $html);


		$html = str_replace('</head>', '<link rel="stylesheet" type="text/css" href="'.self::CSS_URL.'" /></head>', $html);

		$html = preg_replace_callback('/(?<!RunButton" )href=".*\?page=([a-zA-Z0-9\.#]+)"/',
			array($this, 'update_page_url'), $html);
		$html = preg_replace_callback('/(?<=RunButton" )href=".*\?page=([a-zA-Z0-9\.#]+)"/',
			array($this, 'update_run_url'), $html);

		$html = preg_replace('/(src|href)=("?)\//', '$1=$2assets/',$html);
		$html = str_replace('http://www.pradosoft.com/docs/manual', '../manual/CHMdefaultConverter', $html);
		$html = str_replace('target="_blank">View Source', '>View Source', $html);
		$html = preg_replace('/action="[^"]+"/', '', $html);
		$html = preg_replace('/<script[^>]+><\/script>/', '', $html); //remove js
		$html = preg_replace('/href="C:[^"]+"/', 'href="#"', $html);

		$html = preg_replace_callback('/href="\?page=ViewSource&(amp;){0,1}path=([a-zA-z0-9\.\/]+)"/',
			array($this, 'update_source_url'), $html);

		return $html;
	}

	protected function update_source_url($matches)
	{
		$page = $matches[2];
		$file = str_replace('/', '_',$page).'.html';

		if(!isset($this->_viewed[$page]))
		{
			$this->_viewed[$page]=true;
			$this->view_source_page($page);
		}
		return 'href="'.$file.'"';
	}

	protected function view_source_page($page)
	{
		$_GET['page'] = 'ViewSource';
		$_GET['path'] = $page;
		$_GET['lines'] = 'false';

		$html = $this->parseHtmlContent($this->getApplicationContent());
		$file = str_replace('/', '_',$page).'.html';
//		echo 'writing file '.$file."\n";
		file_put_contents($this->output_dir.'/'.$file, $html);
	}

	protected function update_page_url($matches)
	{
		$bits = explode('#',str_replace('.','_',$matches[1]));
		$anchor = isset($bits[1]) ? '#'.$bits[1] : '';
		return 'href="'.$bits[0].'.html'.$anchor.'"';
	}

	protected function update_run_url($matches)
	{
		return 'href="'.self::DEMO_URL.'?page='.$matches[1].'"';
	}
}

class HTMLHelpTOCBuilder
{

	public function buildToc($file,$output,$classes)
	{
		$contents = file_get_contents($file);
		$content = $this->prepareContent($contents);
		$ul = $this->parseUl($content);
		$toc = $this->header();
		$toc .= $this->to_string($ul);
		$toc .= $this->footer();
		$toc = $this->appendApiToc($output,$toc);
		$toc = $this->appendClassesToc($classes,$toc);
		file_put_contents($output.'/toc.hhc', $toc);
		file_put_contents($output.'/prado3_manual.hhp', $this->getHHP());
		file_put_contents($output.'/manual.html', $this->getIndexPage());
		$index = $output.'/manual/CHMdefaultConverter/index.hhk';
		file_put_contents($index, $this->updateIndex($index));
	}

	protected function updateIndex($file)
	{
		$content = file_get_contents($file);
		return preg_replace('/"Local" value="/', '"Local" value="manual\\CHMdefaultConverter\\', $content);
	}

	protected function appendClassesToc($classes, $toc)
	{
		$version = Prado::getVersion();
		$ul['classes']['params'][] = array('Name' => "Prado {$version} Class Index");
		foreach($classes as $class)
		{
			$ul['classes']['ul'][0]['params'][] =
				array('Name'=>$class, 'Local'=>'classdoc/'.$class.'.html');
		}
		$ul['wiki']['params'][] = array('Name' => "Prado Wiki", 'Local'=>'wiki\\index.html');
		$content = $this->to_string($ul);
		$toc = preg_replace('!(</BODY></HTML>)!', $content.'$1', $toc);
		return $toc;
	}

	protected function appendApiToc($output,$toc)
	{
		$content = file_get_contents($output.'/manual/CHMdefaultConverter/contents.hhc');
		$content = preg_replace('/"Local" value="/', '"Local" value="manual\\CHMdefaultConverter\\', $content);
		$toc = preg_replace('!(API Manual">\s*</OBJECT>)\s*(</UL>\s*</BODY></HTML>)!', '$1'."\n".$content.'$2', $toc);
		return preg_replace("/\r/","\n",$toc);
	}

	protected function getIndexPage()
	{
		$version = Prado::getVersion();
		$date = date('d M Y', time());
		$year = date('Y',time());
$content = <<<EOD
<!doctype html public "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<title>Prado Manual</title>
	<style type="text/css">
	/*<![CDATA[*/
	body
	{
	font-family: 'Lucida Grande', Verdana, Geneva, Lucida, Helvetica, Arial, sans-serif;
	font-weight:normal;
	}
	h1
	{
		color: #600;
	}
	/*]]>*/
	</style>
</head>

<body style="text-align:center">

<h1>Prado {$version} Manual</h1>

<div>Created On: {$date}</div>

<div style="margin-top:3em;margin-bottom:0.75em"><strong>Written By:</strong> Qiang Xue, Wei Zhuo</div>
<div style="margin-bottom:3em;"><strong>Edited By:</strong> Wei Zhuo</div>

<div id="footer">
Copyright &copy; 2005-{$year} <a href="http://www.pradosoft.com">PradoSoft</a>.</div>

</body>
</html>
EOD;
		return $content;
	}

	protected function getHHP()
	{
		$version = Prado::getVersion();
$content = <<<EOD
[OPTIONS]
Binary TOC=Yes
Compatibility=1.1 or later
Compiled File=prado3_manual.chm
Contents File=toc.hhc
Default Window=main
Default Topic=manual.html
Display compile progress=Yes
Error log file=_errorlog.txt
Full-text search=Yes
Language=0x409 English (United States)
Title=Prado {$version} Manual
Binary Index=Yes
Index file=manual\CHMdefaultConverter\index.hhk
Default Font=
Auto Index=Yes
Create CHI file=No
Full text search stop list file=
Display compile notes=Yes

[WINDOWS]
main="Prado {$version} Manual","toc.hhc","manual\CHMdefaultConverter\index.hhk","manual.html","manual.html",,,,,0x63520,250,0x104e,[10,10,900,700],0xb0000,,,,,,0

EOD;
		return $content;
	}

	protected function parseUl($content)
	{
		$ul = array();
		$current = null;
		$ul['index']['params'][] = array('Name'=>'Prado Manual', 'Local'=>'manual.html');

		foreach(explode("\n", $content) as $line)
		{
			$line = trim($line);
			if(strlen($line) > 0)
			{
				if(strpos($line,'^')===false)
				{
					$current = $line;
					$ul[$current]['params'][]['Name'] = $current;
				}
				else
				{
					list($page,$title) = explode('^', $line);
					$ul[$current]['ul'][0]['params'][] = array('Name'=>$title, 'Local'=>$this->getFileName($page));
				}
			}
		}
		$version = Prado::getVersion();
		$ul['api']['params'][] = array('Name' => "Prado {$version} API Manual");

		return $ul;
	}

	protected function getFileName($page)
	{
		return 'quickstart\\'.str_replace('.', '_',$page).'.html';
	}

	protected function prepareContent($content)
	{
		$content = preg_replace('/<\/?div[^>]*>/','', $content);
		$content = preg_replace('/<\/?ul>|<\/?li>|<\/a>/ms', '', $content);
		$content = str_replace('<a href="?page=', '', $content);
		$content = str_replace('">', '^', $content);
		return $content;
	}

	public function to_string($ul)
	{
		$contents = "<UL>\n";
		foreach($ul as $li)
		{
				if(isset($li['params']))
				{
					$contents .= $this->li_to_string($li);
				}
				if(isset($li['ul']))
				{
					$contents .= $this->to_string($li['ul']);
				}
		}
		$contents .= "</UL>\n";
		return $contents;
	}

	protected function li_to_string($li)
	{
		$contents = '';
		foreach($li['params'] as $param)
		{
			$contents .= "\t<LI>";
			$contents .= "<OBJECT type=\"text/sitemap\">\n";
			foreach($param as $name => $value)
				$contents .= "\t\t\t<param name=\"$name\" value=\"$value\">\n";
			$contents .= "\t\t</OBJECT>\n";
		}
		return $contents;
	}

	public function header()
	{
		$content = <<<EOD
<HTML>
<HEAD>
</HEAD>
<BODY>
   <OBJECT type="text/site properties">
     <param name="Window Styles" value="0x800025">
     <param name="FrameName" value="right">
     <param name="ImageType" value="Folder">
     <param name="comment" value="title:Online Help">
     <param name="comment" value="base:index.htm">
   </OBJECT>

EOD;
		return $content;
	}

	public function footer()
	{
		return '</BODY></HTML>';
	}
}

class ClassDocBuilder
{
	private $output;
	private $base;

	function __construct($base, $output)
	{
		$this->base = $base;
		$this->output = $output.'/classdoc';
		if(!is_dir($this->output))
		{
			mkdir($this->output);
			mkdir($this->output.'/assets/');
		}
		Prado::setPathOfAlias('Output', $this->output);
	}

	protected function initApp()
	{
		$this->app = new TApplication($this->base);
		$response = new THttpResponse();
		$response->setBufferOutput(false);
		$this->app->setResponse($response);
		$assets = new TAssetManager();
		$assets->setBasePath('Output.assets.*');
		$this->app->setAssetManager($assets);
	}

	public function buildDoc($class)
	{
		$this->parsePage($class);
	}

	public function parseBasePage()
	{
		$_GET['page'] = 'Classes';

		$html = $this->parseHtmlContent($this->getApplicationContent());
		$file = 'Classes.html';
//		echo 'writing file '.$file."\n";
		file_put_contents($this->output.'/'.$file, $html);
	}

	public function parsePage($class)
	{
		$_GET['page'] = 'ClassDoc';
		$_GET['class'] = $class;

		$html = $this->parseHtmlContent($this->getApplicationContent());
		$file = $class.'.html';
//		echo 'writing file '.$file."\n";
		file_put_contents($this->output.'/'.$file, $html);
	}

	protected function getApplicationContent()
	{
		ob_start();
		$this->initApp();
		$this->app->run();
		$content = ob_get_contents();
		ob_end_clean();
		$this->app->completeRequest();
		$this->app=null;
		return $content;
	}

	protected function parseHtmlContent($content)
	{
		$html = preg_replace('/<input.*name="PRADO_PAGESTATE" [^>]+\/>/m', '', $content);
		$html = preg_replace('!href="/(\w+)/style.css"!', 'href="assets/$1/style.css"', $html);
		return $html;
	}
}


?>