/Articles/Forth/PHP/GenerateHTML.php/GenerateHTML.php
3,8 → 3,10
// project. Script should be run once (offline) when new versions of sorces are
// available. It creates new subdirectory with HTML documentation tree.
//
// Configuration file is GenerateHTML.cfg
// Default configuration is in file GenerateHTML.cfg
//
// Command Line paramaters are available via php GenerateHTML.php --help
//
// (c)miho 2007 / http://www.mlab.cz
 
// **********************************************************************
11,6 → 13,7
// History
// **********************************************************************
// 0.00 work wersion
// 0.01 basic functionality
 
 
// **********************************************************************
30,18 → 33,30
// **********************************************************************
 
 
function Error($ErrorStr)
// If ErrorStr is not empty print it and die
{
if ($ErrorStr=="")
return;
print "\n";
print " ERROR: -->".$ErrorStr."\n";
die(1);
}
 
 
function MyReadFile($FileName, &$FileContent)
// Reads file, returns file as a single string
{
//
if ($FileName=="")
return "No File Name";
 
// Read file
$FileContent=@file($FileName);
 
// Test if file contains any content
if ($FileContent==FALSE)
{
unset($FileContent);
return "No Data in File $FileName";
}
return "No Data in File: $FileName";
 
// Remove CR or LF or CR/LF
foreach($FileContent as $Key => $Line)
134,8 → 149,102
// **********************************************************************
 
 
function Help()
// Display help
{
print "\n";
print "This script takes default parameters from GenerateHTML.cfg.\n";
print "\n";
print "Parameters can be entered directly on command line as well:\n";
print " php GenerateHTML.php par1=val1 par2=val2\n";
print "\n";
print "Sometimes (in Windows) it is necessary to use quotation marks this way:\n";
print " php GenerateHTML.php \"par1=val1\" \"par2=val2\"\n";
print "\n";
print "Parameters are: \n";
print " EnableApache={0|1} - Enable to run in Apache\n";
print " SourceDir=path - path to asm source directory\n";
print " SourceAsm=file - name of top level asm source file\n";
print " TemplateDir=path - path to HTML template\n";
print " DestinationDir=path - path to destination HTML\n";
Error("\n");
}
 
 
function Parameters(&$CFG)
// Function process Command Line parameters
{
// Info
print "Parameters\n";
// Dummy values
$CFG["EnableApache"] = 0; // Do not allow run in Apache
$CFG["SourceDir"] = "";
$CFG["SourceAsm"] = "";
$CFG["TemplateDir"] = "";
$CFG["DestinationDir"] = "";
// Default values (in cfg file)
@include_once("GenerateHTML.cfg");
// Command Line parameters
if ($_SERVER["argc"]>1)
{
// Help
if ($_SERVER["argv"][1]=="--help")
Help();
// Drop
unset($_SERVER["argv"][0]);
// Go through arguments
foreach($_SERVER["argv"] as $Key => $Value)
{
$Left=substr($Value,0,strpos($Value,"="));
$Right=substr($Value,strpos($Value,"=")+1);
if (isset($CFG[$Left]) && $Right!="" )
{
$CFG[$Left]=$Right;
}
else
{
return "Invalid Parameter: $Value";
}
}
}
 
// Correct paths and existence
foreach($CFG as $Key => $Value)
{
if (stripos($Key,"Dir"))
{
// Correct / at the end of path
if (substr($Value,strlen($Value)-1)!="/")
{
$CFG[$Key]=$Value."/";
}
// Check existence
#print "DIR".$CFG[$Key]."\n";
if ( ! is_dir($CFG[$Key]))
{
return "Directory does not exist: ".$Key."=".$CFG[$Key];
}
}
}
 
// Print info
if (count(CFG))
foreach($CFG as $Key => $Value)
print " $Key=$Value\n";
print "\n";
 
// Check if alowed to run in Apache
if ($_SERVER["argc"]==0 & ! $CFG["Apache"])
return "This Script is configured so that it will not run in Apache.";
 
// No Error
return "";
}
 
 
function SourceAsm($SourceDir, $FileName, &$SourceAsmFiles, &$LabelList )
// Process ASM source file, recurse all includes
// Returns Error String
// Stores file names and labels into two arrays
// IN $SourceDir - base directory (not printed)
// IN $FileName - file to process
149,11 → 258,8
// Read file
$Error=MyReadFile($SourceDir.$FileName, $FileContent);
if ($Error!="")
{
print $Error."\n";
return 1;
}
return $Error;
 
// Remember filename
$SourceAsmFiles[]=$FileName;
 
193,13 → 299,15
{
$Dir="";
}
SourceAsm($SourceDir, $Dir.$Value, $SourceAsmFiles, $LabelList);
$Error=SourceAsm($SourceDir, $Dir.$Value, $SourceAsmFiles, $LabelList);
if ($Error!="")
return $Error;
}
unset($Includes);
}
 
// End
return 0;
return "";
}
 
 
207,6 → 315,7
// Prints all procesed ASM files
{
print "Asm Source File List\n";
if (count($SourceAsmFiles))
foreach($SourceAsmFiles as $Key => $Value)
{
print " ".$Value."\n";
219,6 → 328,7
// Prints all found labels
{
print "Label List\n";
if (count($LabelList))
foreach($LabelList as $Key => $Value)
{
print " ".$Value["Label"]." ".$Value["FileName"]." ".$Value["LineNumber"]."\n";
229,6 → 339,7
 
function CreateWordList($SourceDir, &$LabelList, &$WordList)
// Goes through LabelList and looks for word definitions
// Returns Error String
// IN $LabelList - Found labels
// OUT $WordList - Word List (ShortLabel, Word, Comment, Label, FileName)
{
235,7 → 346,7
 
print "Word List\n";
if ($LabelList)
if (count($LabelList))
foreach ($LabelList as $Value)
{
// Take all VE definitions
252,10 → 363,7
$FileName=$SourceDir.$Value["FileName"];
$Error=MyReadFileString($FileName, $FileContent);
if ($Error!="")
{
print " ".$Error."\n";
return 1;
}
return $Error;
$FileContent="\n".$FileContent;
// Find label
316,11 → 424,13
}
}
print "\n";
return "";
}
 
 
function GenerateWordList($TemplateDir, $DestinationDir, &$LabelList, &$WordList)
// Creates HTML pages with Word List
// Returns Error String
// IN $TemplateDir - template directory (file WordList.*.html)
// IN $LabelList - list of labels (Label, FileName, LineNumber)
// IN $WordList - list of words (ShortLabel, Word, Comment, Label, FileName)
328,6 → 438,7
{
// Find all templates
print "Word List in HTML\n";
if (count(glob($TemplateDir."WordList.*.html")))
foreach (glob($TemplateDir."WordList.*.html") as $FileName)
{
// Process template file
336,17 → 447,13
// Read template file
$Error=MyReadFileString($FileName, $FileContent);
if ($Error!="")
{
print " ".$Error."\n";
return 1;
}
return $Error;
 
// Find <<WordList>>
if (!preg_match("/( *)(<<WordList>>)/i",$FileContent,$Matches))
{
print " Missing <<WordList>> in template file\n";
unset($FileContent);
return -1;
return "Missing <<WordList>> in template file";
}
$Indent=$Matches[1];
359,6 → 466,7
$WordListHTML[]=" </tr>";
// Create HTML code - table lines
if (count($WordList))
foreach($WordList as $Key => $Value)
{
// Prepare (just for readibility)
390,10 → 498,7
// Create Output File
$Error=MyWriteFile($DestinationDir.basename($FileName), $FileContent);
if ($Error!="")
{
print " ".$Error."\n";
return -1;
}
return $Error;
 
// Clear memory
unset($FileContent);
402,13 → 507,17
 
// Delimiter
print "\n";
return "";
}
 
 
function GenerateAsmFiles($TemplateDir, $SourceDir, &$SourceAsmFiles, $DestinationDir)
// Cretaes HTML files from all processed ASM files
// IN
//
// Returns Error String
// IN $TemplateDir - directory with template files
// IN $SourceDir - directory with asm source files
// OUT $SourceAsmFiles -
// IN $DestinationDir - directory for generated HTML files
{
// Info
print "Copy ASM Files\n";
416,21 → 525,13
// Destination directory exists
$DestinationDir.=ASMFILES;
if (!is_dir($DestinationDir))
{
if (!@mkdir($DestinationDir))
{
print " Unable Create Dir ".$DestinationDir."\n";
return -1;
}
}
return "Unable Create Dir ".$DestinationDir;
 
// Read template
$Error=MyReadFileString($TemplateDir."FileAsm.en.html", $Template);
if ($Error!="")
{
print " ".$Error."\n";
return -1;
}
return $Error;
 
// Copy all source files
foreach($SourceAsmFiles as $Key => $FileName)
440,10 → 541,7
// Read ASM file
$Error=MyReadFileString($SourceDir.$FileName, $FileContent);
if ($Error!="")
{
print " ".$Error."\n";
return 1;
}
return $Error;
 
// Prepare HTML
$FileContent=htmlspecialchars($FileContent);
457,14 → 555,12
// Write ASM file in HTML
$Error=MyWriteFile($DestinationDir.FileName2HTML($FileName), $TemplateWork);
if ($Error!="")
{
print " ".$Error."\n";
return -1;
}
return $Error;
}
// Delimiter
print "\n";
return "";
}
 
 
472,39 → 568,36
// Main Block
// **********************************************************************
 
// Global Like Variables (arrays)
// $CFG - Config parameters
// $SourceAsmFiles - All processed ASM files (filenames)
// $LabelList - All label definitions (Label, FileName, LineNumber)
// $WordList - Word List (ShortLabel, Word, Comment, Label, FileName)
 
// This file contains configurations for this script
require_once("GenerateHTML.cfg");
// Process Command Line Parameters
Error(Parameters($CFG));
 
 
// Global Like Variables
//$SourceAsmFiles - All processed ASM files (filenames)
//$LabelList - All label definitions (Label, FileName, LineNumber)
//$WordList - Word List (ShortLabel, Word, Comment, Label, FileName)
 
 
// Process all ASM files from the root level
SourceAsm($CFG_SourceDir, $CFG_SourceAsm, $SourceAsmFiles, $LabelList);
Error(SourceAsm($CFG["SourceDir"], $CFG["SourceAsm"], $SourceAsmFiles, $LabelList));
PrintSourceAsm($SourceAsmFiles);
PrintLabels($LabelList);
 
// Destilate Labels and Words
CreateWordList($CFG_SourceDir, $LabelList, $WordList);
Error(CreateWordList($CFG["SourceDir"], $LabelList, $WordList));
 
// Create HTML WordList
GenerateWordList($CFG_TemplateDir, $CFG_DestinationDir, $LabelList, $WordList);
Error(GenerateWordList($CFG["TemplateDir"], $CFG["DestinationDir"], $LabelList, $WordList));
 
// Copy ASM files and convert them into HTML
GenerateAsmFiles($CFG_TemplateDir, $CFG_SourceDir, $SourceAsmFiles, $CFG_DestinationDir);
Error(GenerateAsmFiles($CFG["TemplateDir"], $CFG["SourceDir"], $SourceAsmFiles, $CFG["DestinationDir"]));
 
// Finish
print "O.K.\n";
return
 
// Zpracování readme autora + verze
// Dodělat kontroly vstupní CFG parametrů (existence souborů a adresářů)
// Osetreni chyb - die(1) zpusobi chybu (v shellu a da se tak poznat, ze to nedopadlo)
 
// Zpracování templejtů do samostatného podprogramu (vyřešit indent...)
// tím se vyřeší i en/cs verze Asm souboru
 
// Generovat log do souboru místo printu (zvážit) oddělit chyby a varování
// Vyčistit cílový adresář
// Process all FORTH files