From c8c374d6859ef272b342f9375dda7da4ec029876 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Tue, 19 Aug 2008 21:57:11 -0400 Subject: [PATCH] Add convert_txt tool to convert a text file into a HTML document * docs/tools/convert_txt.php: New file --- docs/tools/convert_txt.php | 127 +++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100755 docs/tools/convert_txt.php diff --git a/docs/tools/convert_txt.php b/docs/tools/convert_txt.php new file mode 100755 index 0000000..162d50d --- /dev/null +++ b/docs/tools/convert_txt.php @@ -0,0 +1,127 @@ +#!/usr/bin/env php +'; + 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

$section[header]

\n"; + + if (is_array($section['data'])) + { + $sections .= "\n"; + } + else + { + $sections .= "

$text

\n"; + } +} + +echo << + + $TABLE[subject] - $TABLE[title] + + + +

$TABLE[title]

+ +

$TABLE[preface]

+ +$sections + + + + +HTML; + +?> \ No newline at end of file -- 2.43.5