#!/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; ?>