Add convert_txt tool to convert a text file into a HTML document
authorRobert Sesek <rsesek@bluestatic.org>
Wed, 20 Aug 2008 01:57:11 +0000 (21:57 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Wed, 20 Aug 2008 01:57:11 +0000 (21:57 -0400)
* docs/tools/convert_txt.php: New file

docs/tools/convert_txt.php [new file with mode: 0755]

diff --git a/docs/tools/convert_txt.php b/docs/tools/convert_txt.php
new file mode 100755 (executable)
index 0000000..162d50d
--- /dev/null
@@ -0,0 +1,127 @@
+#!/usr/bin/env php
+<?php
+
+define('ISSO', dirname(dirname(dirname(__FILE__))));
+require_once ISSO . '/App.php';
+require_once ISSO . '/Functions.php';
+
+if ($argc != 2)
+{
+       echo 'Useage: convert_txt.php <file.txt>';
+       exit;
+}
+
+$data = @file_get_contents($argv[1]);
+if ($data === false)
+{
+       echo 'Unable to locate ' . $argv[1];
+       exit;
+}
+
+// ###################################################################
+
+$data = BSFunctions::convert_line_breaks($data);
+$data = explode("\n", $data);
+
+define('COLUMN_WIDTH', 80);
+$line = 0;
+
+$TABLE = array(
+       'subject'               => null,
+       'title'                 => null,
+       'preface'               => null,
+       'sections'              => array(), // array(header => X, items => array(list items))
+);
+
+// ###################################################################
+
+$matches = array();
+if (!preg_match('/(\w+)\s+(.*)/i', $data[$line++], $matches))
+{
+       throw new Exception('No title line');
+}
+
+$TABLE['subject']      = $matches[1];
+$TABLE['title']                = ucwords(strtolower($matches[2]));
+
+if ($data[$line++] != str_repeat('=', COLUMN_WIDTH))
+{
+       throw new Exception('No header separator');
+}
+
+// read the preface
+while (trim($data[++$line]) != '')
+{
+       $TABLE['preface'] .= trim($data[$line]) . ' ';
+}
+$TABLE['preface'] = trim($TABLE['preface']);
+
+// read the sections
+for ($section = 0; $line < sizeof($data); $line++)
+{
+       // empty lines signal a new section
+       if (trim($data[$line]) == '')
+       {
+               $section++;
+       }
+       
+       // look for the heading
+       if (preg_match('/#{5,}/', $data[$line]))
+       {
+               $TABLE['sections'][$section]['header'] = ucwords(strtolower($data[$line - 1]));
+       }
+       // we've read the heading, start reading data
+       else if (isset($TABLE['sections'][$section]['header']))
+       {
+               if ($data[$line][0] == '-')
+               {
+                       $TABLE['sections'][$section]['data'][] = trim(substr($data[$line], 1));
+               }
+               else
+               {
+                       $TABLE['sections'][$section]['data'][sizeof($TABLE['sections'][$section]['data']) - 1] .= ' ' . trim($data[$line]);
+               }
+       }
+}
+
+// ###################################################################
+
+$sections = '';
+foreach ($TABLE['sections'] as $section)
+{
+       $sections .= "\n<h2>$section[header]</h2>\n";
+       
+       if (is_array($section['data']))
+       {
+               $sections .= "<ul>\n";
+               foreach ($section['data'] as $item)
+               {
+                       $sections .= "\t<li>$item</li>\n";
+               }
+               $sections .= "</ul>\n";
+       }
+       else
+       {
+               $sections .= "<p>$text</p>\n";
+       }
+}
+
+echo <<<HTML
+<html>
+<head>
+       <title>$TABLE[subject] - $TABLE[title]</title>
+</head>
+<body>
+
+<h1>$TABLE[title]</h1>
+
+<p>$TABLE[preface]</p>
+
+$sections
+
+</body>
+</html>
+
+HTML;
+
+?>
\ No newline at end of file