]> src.bluestatic.org Git - isso.git/blob - docs/tools/convert_txt.php
Update version number
[isso.git] / docs / tools / convert_txt.php
1 #!/usr/bin/env php
2 <?php
3
4 define('ISSO', dirname(dirname(dirname(__FILE__))));
5 require_once ISSO . '/App.php';
6 require_once ISSO . '/Functions.php';
7
8 if ($argc != 2)
9 {
10 echo 'Useage: convert_txt.php <file.txt>';
11 exit;
12 }
13
14 $data = @file_get_contents($argv[1]);
15 if ($data === false)
16 {
17 echo 'Unable to locate ' . $argv[1];
18 exit;
19 }
20
21 // ###################################################################
22
23 $data = BSFunctions::convert_line_breaks($data);
24 $data = explode("\n", $data);
25
26 define('COLUMN_WIDTH', 80);
27 $line = 0;
28
29 $TABLE = array(
30 'subject' => null,
31 'title' => null,
32 'preface' => null,
33 'sections' => array(), // array(header => X, items => array(list items))
34 );
35
36 // ###################################################################
37
38 $matches = array();
39 if (!preg_match('/(\w+)\s+(.*)/i', $data[$line++], $matches))
40 {
41 throw new Exception('No title line');
42 }
43
44 $TABLE['subject'] = $matches[1];
45 $TABLE['title'] = ucwords(strtolower($matches[2]));
46
47 if ($data[$line++] != str_repeat('=', COLUMN_WIDTH))
48 {
49 throw new Exception('No header separator');
50 }
51
52 // read the preface
53 while (trim($data[++$line]) != '')
54 {
55 $TABLE['preface'] .= trim($data[$line]) . ' ';
56 }
57 $TABLE['preface'] = trim($TABLE['preface']);
58
59 // read the sections
60 for ($section = 0; $line < sizeof($data); $line++)
61 {
62 // empty lines signal a new section
63 if (trim($data[$line]) == '')
64 {
65 $section++;
66 }
67
68 // look for the heading
69 if (preg_match('/#{5,}/', $data[$line]))
70 {
71 $TABLE['sections'][$section]['header'] = ucwords(strtolower($data[$line - 1]));
72 }
73 // we've read the heading, start reading data
74 else if (isset($TABLE['sections'][$section]['header']))
75 {
76 if ($data[$line][0] == '-')
77 {
78 $TABLE['sections'][$section]['data'][] = trim(substr($data[$line], 1));
79 }
80 else
81 {
82 $TABLE['sections'][$section]['data'][sizeof($TABLE['sections'][$section]['data']) - 1] .= ' ' . trim($data[$line]);
83 }
84 }
85 }
86
87 // ###################################################################
88
89 $sections = '';
90 foreach ($TABLE['sections'] as $section)
91 {
92 $sections .= "\n<h2>$section[header]</h2>\n";
93
94 if (is_array($section['data']))
95 {
96 $sections .= "<ul>\n";
97 foreach ($section['data'] as $item)
98 {
99 $sections .= "\t<li>$item</li>\n";
100 }
101 $sections .= "</ul>\n";
102 }
103 else
104 {
105 $sections .= "<p>$text</p>\n";
106 }
107 }
108
109 echo <<<HTML
110 <html>
111 <head>
112 <title>$TABLE[subject] - $TABLE[title]</title>
113 </head>
114 <body>
115
116 <h1>$TABLE[title]</h1>
117
118 <p>$TABLE[preface]</p>
119
120 $sections
121
122 </body>
123 </html>
124
125 HTML;
126
127 ?>