Rev 543 Rev 545
1 <?php 1 <?php
2 // This PHP script generates clickable HTML documentation files for amforth 2 // This PHP script generates clickable HTML documentation files for amforth
3 // project. Script should be run once (offline) when new versions of sorces are 3 // project. Script should be run once (offline) when new versions of sorces are
4 // available. It creates new subdirectory with HTML documentation tree. 4 // available. It creates new subdirectory with HTML documentation tree.
5 // 5 //
6 // Configuration file is GenerateHTML.cfg 6 // Default configuration is in file GenerateHTML.cfg
-   7 //
-   8 // Command Line paramaters are available via php GenerateHTML.php --help
7 // 9 //
8 // (c)miho 2007 / http://www.mlab.cz 10 // (c)miho 2007 / http://www.mlab.cz
9   11  
10 // ********************************************************************** 12 // **********************************************************************
11 // History 13 // History
12 // ********************************************************************** 14 // **********************************************************************
13 // 0.00 work wersion 15 // 0.00 work wersion
-   16 // 0.01 basic functionality
14   17  
15   18  
16 // ********************************************************************** 19 // **********************************************************************
17 // Definitions/parameters 20 // Definitions/parameters
18 // ********************************************************************** 21 // **********************************************************************
19   22  
20   23  
21 define ('INCLUDE_PATTERN', "/^ *\.include +\"(\S+)\"/i"); 24 define ('INCLUDE_PATTERN', "/^ *\.include +\"(\S+)\"/i");
22 define ('LABEL_PREFIX', "(VE_|XT_|PFA)"); 25 define ('LABEL_PREFIX', "(VE_|XT_|PFA)");
23 define ('LABEL_PATTERN', "/^ *(".LABEL_PREFIX."\S+):/i"); 26 define ('LABEL_PATTERN', "/^ *(".LABEL_PREFIX."\S+):/i");
24 define ('WORD_PATTERN', "/((?:\\n *;.*)*)\\n *VE_Q:[^\\n]*\\n? *.db +[^,]* *, *(\S[^\\n|;]+)/im"); // Q is used instead of Word 27 define ('WORD_PATTERN', "/((?:\\n *;.*)*)\\n *VE_Q:[^\\n]*\\n? *.db +[^,]* *, *(\S[^\\n|;]+)/im"); // Q is used instead of Word
25 define ('ASMFILES', "CodeAsm/"); 28 define ('ASMFILES', "CodeAsm/");
26   29  
27   30  
28 // ********************************************************************** 31 // **********************************************************************
29 // Generic Funcions 32 // Generic Funcions
30 // ********************************************************************** 33 // **********************************************************************
31   34  
32   35  
-   36 function Error($ErrorStr)
-   37 // If ErrorStr is not empty print it and die
-   38 {
-   39 if ($ErrorStr=="")
-   40 return;
-   41 print "\n";
-   42 print " ERROR: -->".$ErrorStr."\n";
-   43 die(1);
-   44 }
-   45  
-   46  
33 function MyReadFile($FileName, &$FileContent) 47 function MyReadFile($FileName, &$FileContent)
34 // Reads file, returns file as a single string 48 // Reads file, returns file as a single string
35 { 49 {
-   50 //
-   51 if ($FileName=="")
-   52 return "No File Name";
-   53  
36 // Read file 54 // Read file
37 $FileContent=@file($FileName); 55 $FileContent=@file($FileName);
38   56  
39 // Test if file contains any content 57 // Test if file contains any content
40 if ($FileContent==FALSE) 58 if ($FileContent==FALSE)
41 { -  
42 unset($FileContent); -  
43 return "No Data in File $FileName"; 59 return "No Data in File: $FileName";
44 } -  
45   60  
46 // Remove CR or LF or CR/LF 61 // Remove CR or LF or CR/LF
47 foreach($FileContent as $Key => $Line) 62 foreach($FileContent as $Key => $Line)
48 { 63 {
49 $FileContent[$Key]=rtrim($Line); 64 $FileContent[$Key]=rtrim($Line);
50 } 65 }
51   66  
52 // No error 67 // No error
53 return ""; 68 return "";
54 } 69 }
55   70  
56   71  
57 function MyReadFileString($FileName, &$FileContent) 72 function MyReadFileString($FileName, &$FileContent)
58 // Reads file and returns its content as a single string 73 // Reads file and returns its content as a single string
59 { 74 {
60 // No Error 75 // No Error
61 $Error=""; 76 $Error="";
62 77
63 // Read file 78 // Read file
64 $Error=MyReadFile($FileName, &$FileContent); 79 $Error=MyReadFile($FileName, &$FileContent);
65   80  
66 // Convert to a single string 81 // Convert to a single string
67 if ($Error=="") $FileContent=implode("\n",$FileContent); 82 if ($Error=="") $FileContent=implode("\n",$FileContent);
68   83  
69 // Finished 84 // Finished
70 return $Error; 85 return $Error;
71 } 86 }
72   87  
73   88  
74 function MyWriteFile($FileName, $FileContent) 89 function MyWriteFile($FileName, $FileContent)
75 // Creates and writes file 90 // Creates and writes file
76 { 91 {
77 // Create Output File 92 // Create Output File
78 $File=@fopen($FileName,"w"); 93 $File=@fopen($FileName,"w");
79 if ($File==FALSE) 94 if ($File==FALSE)
80 { 95 {
81 return "Unable Write File ".$FileName; 96 return "Unable Write File ".$FileName;
82 } 97 }
83 98
84 // Write content 99 // Write content
85 fwrite($File,$FileContent); 100 fwrite($File,$FileContent);
86 fclose($File); 101 fclose($File);
87 102
88 // No Error 103 // No Error
89 return ""; 104 return "";
90 } 105 }
91   106  
92   107  
93 function FileName2HTML($FileName) 108 function FileName2HTML($FileName)
94 // Converts FileName to FileName of HTML file 109 // Converts FileName to FileName of HTML file
95 { 110 {
96 // Remove path 111 // Remove path
97 $FileName=basename($FileName); 112 $FileName=basename($FileName);
98 // Change suffix 113 // Change suffix
99 $FileName=preg_replace("/\.asm$/i",".html",$FileName); 114 $FileName=preg_replace("/\.asm$/i",".html",$FileName);
100 // Finished 115 // Finished
101 #print $FileName; 116 #print $FileName;
102 return $FileName; 117 return $FileName;
103 } 118 }
104   119  
105   120  
106 function Label2Link($Link, $href, $title, &$LabelList) 121 function Label2Link($Link, $href, $title, &$LabelList)
107 // Converts Label to Link 122 // Converts Label to Link
108 // If label not found returns original text 123 // If label not found returns original text
109 // IN $Link - label to find 124 // IN $Link - label to find
110 // IN $Word - word to show (on mouse) 125 // IN $Word - word to show (on mouse)
111 // IN $LabelList - list of all labels (Label, FileName, LineNumber) 126 // IN $LabelList - list of all labels (Label, FileName, LineNumber)
112 { 127 {
113 // Find label in $LabelList 128 // Find label in $LabelList
114 foreach($LabelList as $Value) 129 foreach($LabelList as $Value)
115 { 130 {
116 if ($Link===$Value["Label"]) 131 if ($Link===$Value["Label"])
117 { 132 {
118 #$LabelListItem=$Value ///////////////////////////// 133 #$LabelListItem=$Value /////////////////////////////
119 print "Found ".$Value["Label"]."\n"; 134 print "Found ".$Value["Label"]."\n";
120 } 135 }
121 } 136 }
122   137  
123 $FileName=$Value["FileName"]."#".$Link; 138 $FileName=$Value["FileName"]."#".$Link;
124 139
125 // Create link 140 // Create link
126 $Link="<a href=\"".$FileName."\" title=\" ".$Word."\">".$Link.'</a>'; 141 $Link="<a href=\"".$FileName."\" title=\" ".$Word."\">".$Link.'</a>';
127   142  
128 return $Link; 143 return $Link;
129 } 144 }
130   145  
131   146  
132 // ********************************************************************** 147 // **********************************************************************
133 // Function for processing 148 // Function for processing
134 // ********************************************************************** 149 // **********************************************************************
135   150  
136   151  
-   152 function Help()
-   153 // Display help
-   154 {
-   155 print "\n";
-   156 print "This script takes default parameters from GenerateHTML.cfg.\n";
-   157 print "\n";
-   158 print "Parameters can be entered directly on command line as well:\n";
-   159 print " php GenerateHTML.php par1=val1 par2=val2\n";
-   160 print "\n";
-   161 print "Sometimes (in Windows) it is necessary to use quotation marks this way:\n";
-   162 print " php GenerateHTML.php \"par1=val1\" \"par2=val2\"\n";
-   163 print "\n";
-   164 print "Parameters are: \n";
-   165 print " EnableApache={0|1} - Enable to run in Apache\n";
-   166 print " SourceDir=path - path to asm source directory\n";
-   167 print " SourceAsm=file - name of top level asm source file\n";
-   168 print " TemplateDir=path - path to HTML template\n";
-   169 print " DestinationDir=path - path to destination HTML\n";
-   170 Error("\n");
-   171 }
-   172  
-   173  
-   174 function Parameters(&$CFG)
-   175 // Function process Command Line parameters
-   176 {
-   177 // Info
-   178 print "Parameters\n";
-   179 // Dummy values
-   180 $CFG["EnableApache"] = 0; // Do not allow run in Apache
-   181 $CFG["SourceDir"] = "";
-   182 $CFG["SourceAsm"] = "";
-   183 $CFG["TemplateDir"] = "";
-   184 $CFG["DestinationDir"] = "";
-   185 // Default values (in cfg file)
-   186 @include_once("GenerateHTML.cfg");
-   187 // Command Line parameters
-   188 if ($_SERVER["argc"]>1)
-   189 {
-   190 // Help
-   191 if ($_SERVER["argv"][1]=="--help")
-   192 Help();
-   193 // Drop
-   194 unset($_SERVER["argv"][0]);
-   195 // Go through arguments
-   196 foreach($_SERVER["argv"] as $Key => $Value)
-   197 {
-   198 $Left=substr($Value,0,strpos($Value,"="));
-   199 $Right=substr($Value,strpos($Value,"=")+1);
-   200 if (isset($CFG[$Left]) && $Right!="" )
-   201 {
-   202 $CFG[$Left]=$Right;
-   203 }
-   204 else
-   205 {
-   206 return "Invalid Parameter: $Value";
-   207 }
-   208 }
-   209 }
-   210  
-   211 // Correct paths and existence
-   212 foreach($CFG as $Key => $Value)
-   213 {
-   214 if (stripos($Key,"Dir"))
-   215 {
-   216 // Correct / at the end of path
-   217 if (substr($Value,strlen($Value)-1)!="/")
-   218 {
-   219 $CFG[$Key]=$Value."/";
-   220 }
-   221 // Check existence
-   222 #print "DIR".$CFG[$Key]."\n";
-   223 if ( ! is_dir($CFG[$Key]))
-   224 {
-   225 return "Directory does not exist: ".$Key."=".$CFG[$Key];
-   226 }
-   227 }
-   228 }
-   229  
-   230 // Print info
-   231 if (count(CFG))
-   232 foreach($CFG as $Key => $Value)
-   233 print " $Key=$Value\n";
-   234 print "\n";
-   235  
-   236 // Check if alowed to run in Apache
-   237 if ($_SERVER["argc"]==0 & ! $CFG["Apache"])
-   238 return "This Script is configured so that it will not run in Apache.";
-   239  
-   240 // No Error
-   241 return "";
-   242 }
-   243  
-   244  
137 function SourceAsm($SourceDir, $FileName, &$SourceAsmFiles, &$LabelList ) 245 function SourceAsm($SourceDir, $FileName, &$SourceAsmFiles, &$LabelList )
138 // Process ASM source file, recurse all includes 246 // Process ASM source file, recurse all includes
-   247 // Returns Error String
139 // Stores file names and labels into two arrays 248 // Stores file names and labels into two arrays
140 // IN $SourceDir - base directory (not printed) 249 // IN $SourceDir - base directory (not printed)
141 // IN $FileName - file to process 250 // IN $FileName - file to process
142 // OUT $SourceAsmFiles - list of all processed files (Filename) 251 // OUT $SourceAsmFiles - list of all processed files (Filename)
143 // OUT $LabelList - list of all labels (Label, FileName, LineNumber) 252 // OUT $LabelList - list of all labels (Label, FileName, LineNumber)
144 { 253 {
145   254  
146 // Start 255 // Start
147 print "Read Asm: $FileName\n"; 256 print "Read Asm: $FileName\n";
148   257  
149 // Read file 258 // Read file
150 $Error=MyReadFile($SourceDir.$FileName, $FileContent); 259 $Error=MyReadFile($SourceDir.$FileName, $FileContent);
151 if ($Error!="") 260 if ($Error!="")
152 { -  
153 print $Error."\n"; -  
154 return 1; 261 return $Error;
155 } -  
156 262  
157 // Remember filename 263 // Remember filename
158 $SourceAsmFiles[]=$FileName; 264 $SourceAsmFiles[]=$FileName;
159   265  
160 // Filter source file line by line - find labels 266 // Filter source file line by line - find labels
161 foreach($FileContent as $Key => $Value) 267 foreach($FileContent as $Key => $Value)
162 { 268 {
163 // Find label definitions 269 // Find label definitions
164 if (preg_match(LABEL_PATTERN,$Value,$Matches)) 270 if (preg_match(LABEL_PATTERN,$Value,$Matches))
165 { 271 {
166 print " label @line ".($Key+1)." ".$Matches[1]."\n"; 272 print " label @line ".($Key+1)." ".$Matches[1]."\n";
167 $LabelList[]=array("Label"=>$Matches[1],"FileName"=>$FileName,"LineNumber"=>($Key+1)); 273 $LabelList[]=array("Label"=>$Matches[1],"FileName"=>$FileName,"LineNumber"=>($Key+1));
168 } 274 }
169 } 275 }
170   276  
171 // Filter source file line by line - find includes 277 // Filter source file line by line - find includes
172 foreach($FileContent as $Key => $Value) 278 foreach($FileContent as $Key => $Value)
173 { 279 {
174 // Find .include "filename" lines 280 // Find .include "filename" lines
175 if (preg_match(INCLUDE_PATTERN,$Value,$Matches)) 281 if (preg_match(INCLUDE_PATTERN,$Value,$Matches))
176 { 282 {
177 print " include @line ".($Key+1)." --> ".$Matches[1]."\n"; 283 print " include @line ".($Key+1)." --> ".$Matches[1]."\n";
178 // Remember links 284 // Remember links
179 $Includes[]=$Matches[1]; 285 $Includes[]=$Matches[1];
180 } 286 }
181 } 287 }
182   288  
183 // Print delimiter 289 // Print delimiter
184 print "\n"; 290 print "\n";
185 291
186 // Recurse includes 292 // Recurse includes
187 if ($Includes) 293 if ($Includes)
188 { 294 {
189 foreach($Includes as $Value) 295 foreach($Includes as $Value)
190 { 296 {
191 $Dir=dirname($FileName)."/"; 297 $Dir=dirname($FileName)."/";
192 if ($Dir=="./") 298 if ($Dir=="./")
193 { 299 {
194 $Dir=""; 300 $Dir="";
195 } 301 }
196 SourceAsm($SourceDir, $Dir.$Value, $SourceAsmFiles, $LabelList); 302 $Error=SourceAsm($SourceDir, $Dir.$Value, $SourceAsmFiles, $LabelList);
-   303 if ($Error!="")
-   304 return $Error;
197 } 305 }
198 unset($Includes); 306 unset($Includes);
199 } 307 }
200   308  
201 // End 309 // End
202 return 0; 310 return "";
203 } 311 }
204   312  
205   313  
206 function PrintSourceAsm(&$SourceAsmFiles) 314 function PrintSourceAsm(&$SourceAsmFiles)
207 // Prints all procesed ASM files 315 // Prints all procesed ASM files
208 { 316 {
209 print "Asm Source File List\n"; 317 print "Asm Source File List\n";
-   318 if (count($SourceAsmFiles))
210 foreach($SourceAsmFiles as $Key => $Value) 319 foreach($SourceAsmFiles as $Key => $Value)
211 { 320 {
212 print " ".$Value."\n"; 321 print " ".$Value."\n";
213 } 322 }
214 print "\n"; 323 print "\n";
215 } 324 }
216   325  
217   326  
218 function PrintLabels(&$LabelList) 327 function PrintLabels(&$LabelList)
219 // Prints all found labels 328 // Prints all found labels
220 { 329 {
221 print "Label List\n"; 330 print "Label List\n";
-   331 if (count($LabelList))
222 foreach($LabelList as $Key => $Value) 332 foreach($LabelList as $Key => $Value)
223 { 333 {
224 print " ".$Value["Label"]." ".$Value["FileName"]." ".$Value["LineNumber"]."\n"; 334 print " ".$Value["Label"]." ".$Value["FileName"]." ".$Value["LineNumber"]."\n";
225 } 335 }
226 print "\n"; 336 print "\n";
227 } 337 }
228   338  
229   339  
230 function CreateWordList($SourceDir, &$LabelList, &$WordList) 340 function CreateWordList($SourceDir, &$LabelList, &$WordList)
231 // Goes through LabelList and looks for word definitions 341 // Goes through LabelList and looks for word definitions
-   342 // Returns Error String
232 // IN $LabelList - Found labels 343 // IN $LabelList - Found labels
233 // OUT $WordList - Word List (ShortLabel, Word, Comment, Label, FileName) 344 // OUT $WordList - Word List (ShortLabel, Word, Comment, Label, FileName)
234 { 345 {
235   346  
236 print "Word List\n"; 347 print "Word List\n";
237 348
238 if ($LabelList) 349 if (count($LabelList))
239 foreach ($LabelList as $Value) 350 foreach ($LabelList as $Value)
240 { 351 {
241 // Take all VE definitions 352 // Take all VE definitions
242 if (stristr(substr($Value["Label"],0,3),"VE_")) 353 if (stristr(substr($Value["Label"],0,3),"VE_"))
243 { 354 {
244 // Prepare Label without VE_ 355 // Prepare Label without VE_
245 $ShortLabel=substr($Value["Label"],3); 356 $ShortLabel=substr($Value["Label"],3);
246   357  
247 // Prepare search pattern 358 // Prepare search pattern
248 $Pattern=str_replace("Q",$ShortLabel,WORD_PATTERN); 359 $Pattern=str_replace("Q",$ShortLabel,WORD_PATTERN);
249 #print "Pattern: ".$Pattern." ".$Value["FileName"]."\n"; 360 #print "Pattern: ".$Pattern." ".$Value["FileName"]."\n";
250   361  
251 // Read source file 362 // Read source file
252 $FileName=$SourceDir.$Value["FileName"]; 363 $FileName=$SourceDir.$Value["FileName"];
253 $Error=MyReadFileString($FileName, $FileContent); 364 $Error=MyReadFileString($FileName, $FileContent);
254 if ($Error!="") 365 if ($Error!="")
255 { -  
256 print " ".$Error."\n"; -  
257 return 1; 366 return $Error;
258 } -  
259 $FileContent="\n".$FileContent; 367 $FileContent="\n".$FileContent;
260 368
261 // Find label 369 // Find label
262 if (preg_match($Pattern,$FileContent,$Matches)) 370 if (preg_match($Pattern,$FileContent,$Matches))
263 { 371 {
264 // Coments - remove semiculomn 372 // Coments - remove semiculomn
265 $Comment = rtrim(preg_replace("/\\n; ?(.*)/","$1\n",$Matches[1])); 373 $Comment = rtrim(preg_replace("/\\n; ?(.*)/","$1\n",$Matches[1]));
266   374  
267 // Convert .db parameters into string 375 // Convert .db parameters into string
268 $Word=""; 376 $Word="";
269 #$Word=$Matches[2]." : "; 377 #$Word=$Matches[2]." : ";
270   378  
271 foreach(explode(",",$Matches[2]) as $Val) 379 foreach(explode(",",$Matches[2]) as $Val)
272 { 380 {
273 // String element 381 // String element
274 preg_match("/^ *\"([^\"]*)(\" *)/",$Val,$Tmp); 382 preg_match("/^ *\"([^\"]*)(\" *)/",$Val,$Tmp);
275 #print "S:".$Tmp[1]."\n"; 383 #print "S:".$Tmp[1]."\n";
276 if ($Tmp[1]!="") 384 if ($Tmp[1]!="")
277 { 385 {
278 $Word.=$Tmp[1]; 386 $Word.=$Tmp[1];
279 } 387 }
280   388  
281 // Hexa number 389 // Hexa number
282 preg_match("/\\$([0-9A-F]+)/i",$Val,$Tmp); 390 preg_match("/\\$([0-9A-F]+)/i",$Val,$Tmp);
283 #print "H:".$Tmp[1]."\n"; 391 #print "H:".$Tmp[1]."\n";
284 if ($Tmp[1]!="") 392 if ($Tmp[1]!="")
285 { 393 {
286 $Number=hexdec($Tmp[1]); 394 $Number=hexdec($Tmp[1]);
287 if ($Number!=0) 395 if ($Number!=0)
288 $Word.=chr($Number); 396 $Word.=chr($Number);
289 } 397 }
290 398
291 // Decimal number 399 // Decimal number
292 preg_match("/^([0-9]+)/i",$Val,$Tmp); 400 preg_match("/^([0-9]+)/i",$Val,$Tmp);
293 #print "D:".$Tmp[1]."\n"; 401 #print "D:".$Tmp[1]."\n";
294 if ($Tmp[1]!="") 402 if ($Tmp[1]!="")
295 { 403 {
296 $Number=$Tmp[1]; 404 $Number=$Tmp[1];
297 if ($Number!=0) 405 if ($Number!=0)
298 $Word.=chr($Number); 406 $Word.=chr($Number);
299 } 407 }
300 408
301 } 409 }
302   410  
303 // Store label into array 411 // Store label into array
304 $WordList[]=array("Word"=>$Word, "ShortLabel"=>$ShortLabel, "Comment"=>$Comment, 412 $WordList[]=array("Word"=>$Word, "ShortLabel"=>$ShortLabel, "Comment"=>$Comment,
305 "Label"=>$Value["Label"] , "FileName"=>$Value["FileName"] 413 "Label"=>$Value["Label"] , "FileName"=>$Value["FileName"]
306 ); 414 );
307 print " ".$Word." = ".$ShortLabel."\n"; 415 print " ".$Word." = ".$ShortLabel."\n";
308 if($Comment) print " ".preg_replace("/\\n/","\n ",$Comment)."\n"; 416 if($Comment) print " ".preg_replace("/\\n/","\n ",$Comment)."\n";
309 } 417 }
310   418  
311 // Sort Words 419 // Sort Words
312 array_multisort($WordList); 420 array_multisort($WordList);
313   421  
314 // Clean Up 422 // Clean Up
315 unset($FileContent); 423 unset($FileContent);
316 } 424 }
317 } 425 }
318 print "\n"; 426 print "\n";
-   427 return "";
319 } 428 }
320   429  
321   430  
322 function GenerateWordList($TemplateDir, $DestinationDir, &$LabelList, &$WordList) 431 function GenerateWordList($TemplateDir, $DestinationDir, &$LabelList, &$WordList)
323 // Creates HTML pages with Word List 432 // Creates HTML pages with Word List
-   433 // Returns Error String
324 // IN $TemplateDir - template directory (file WordList.*.html) 434 // IN $TemplateDir - template directory (file WordList.*.html)
325 // IN $LabelList - list of labels (Label, FileName, LineNumber) 435 // IN $LabelList - list of labels (Label, FileName, LineNumber)
326 // IN $WordList - list of words (ShortLabel, Word, Comment, Label, FileName) 436 // IN $WordList - list of words (ShortLabel, Word, Comment, Label, FileName)
327 // OUT WordList.*.html - create HTML pages (file WordList.*.html) 437 // OUT WordList.*.html - create HTML pages (file WordList.*.html)
328 { 438 {
329 // Find all templates 439 // Find all templates
330 print "Word List in HTML\n"; 440 print "Word List in HTML\n";
-   441 if (count(glob($TemplateDir."WordList.*.html")))
331 foreach (glob($TemplateDir."WordList.*.html") as $FileName) 442 foreach (glob($TemplateDir."WordList.*.html") as $FileName)
332 { 443 {
333 // Process template file 444 // Process template file
334 print " Temlate $FileName\n"; 445 print " Temlate $FileName\n";
335   446  
336 // Read template file 447 // Read template file
337 $Error=MyReadFileString($FileName, $FileContent); 448 $Error=MyReadFileString($FileName, $FileContent);
338 if ($Error!="") 449 if ($Error!="")
339 { -  
340 print " ".$Error."\n"; -  
341 return 1; 450 return $Error;
342 } -  
343   451  
344 // Find <<WordList>> 452 // Find <<WordList>>
345 if (!preg_match("/( *)(<<WordList>>)/i",$FileContent,$Matches)) 453 if (!preg_match("/( *)(<<WordList>>)/i",$FileContent,$Matches))
346 { 454 {
347 print " Missing <<WordList>> in template file\n"; -  
348 unset($FileContent); 455 unset($FileContent);
349 return -1; 456 return "Missing <<WordList>> in template file";
350 } 457 }
351 $Indent=$Matches[1]; 458 $Indent=$Matches[1];
352 459
353 // Create HTML code - table header 460 // Create HTML code - table header
354 $WordListHTML[]="<table>"; 461 $WordListHTML[]="<table>";
355 $WordListHTML[]=" <tr>"; 462 $WordListHTML[]=" <tr>";
356 $WordListHTML[]=" <th>Word</th>"; 463 $WordListHTML[]=" <th>Word</th>";
357 $WordListHTML[]=" <th>Label</th>"; 464 $WordListHTML[]=" <th>Label</th>";
358 $WordListHTML[]=" <th>Definition</th>"; 465 $WordListHTML[]=" <th>Definition</th>";
359 $WordListHTML[]=" </tr>"; 466 $WordListHTML[]=" </tr>";
360 467
361 // Create HTML code - table lines 468 // Create HTML code - table lines
-   469 if (count($WordList))
362 foreach($WordList as $Key => $Value) 470 foreach($WordList as $Key => $Value)
363 { 471 {
364 // Prepare (just for readibility) 472 // Prepare (just for readibility)
365 $Word=$Value["Word"]; 473 $Word=$Value["Word"];
366 $Link="<a href=\"".ASMFILES.FileName2HTML($Value["FileName"])."#".$Value["ShortLabel"]. 474 $Link="<a href=\"".ASMFILES.FileName2HTML($Value["FileName"])."#".$Value["ShortLabel"].
367 "\" title=\"".$Value["Label"]."\">".$Value["ShortLabel"]."</a>"; 475 "\" title=\"".$Value["Label"]."\">".$Value["ShortLabel"]."</a>";
368 $Comment=$Value["Comment"]; 476 $Comment=$Value["Comment"];
369 // Generate HTML 477 // Generate HTML
370 $WordListHTML[]=" <tr>"; 478 $WordListHTML[]=" <tr>";
371 $WordListHTML[]=" <td>".htmlspecialchars($Word)."</td>"; 479 $WordListHTML[]=" <td>".htmlspecialchars($Word)."</td>";
372 $WordListHTML[]=" <td>".$Link."</td>"; 480 $WordListHTML[]=" <td>".$Link."</td>";
373 $WordListHTML[]=" <td>".htmlspecialchars($Comment)."</td>"; 481 $WordListHTML[]=" <td>".htmlspecialchars($Comment)."</td>";
374 $WordListHTML[]=" </tr>"; 482 $WordListHTML[]=" </tr>";
375 } 483 }
376 // Create HTML code - table end 484 // Create HTML code - table end
377 $WordListHTML[]="</table>"; 485 $WordListHTML[]="</table>";
378 486
379 // Indent and Concatenate lines 487 // Indent and Concatenate lines
380 foreach($WordListHTML as $Key => $Value) 488 foreach($WordListHTML as $Key => $Value)
381 { 489 {
382 $WordListHTML[$Key]=$Indent.preg_replace("/\n/","<br>",$Value); 490 $WordListHTML[$Key]=$Indent.preg_replace("/\n/","<br>",$Value);
383 } 491 }
384 $WordListHTML=implode("\n",$WordListHTML); 492 $WordListHTML=implode("\n",$WordListHTML);
385 493
386 // Put it into HTML template 494 // Put it into HTML template
387 $FileContent=str_ireplace("<<WordList>>", $WordListHTML, $FileContent); 495 $FileContent=str_ireplace("<<WordList>>", $WordListHTML, $FileContent);
388 #print $FileContent; 496 #print $FileContent;
389 497
390 // Create Output File 498 // Create Output File
391 $Error=MyWriteFile($DestinationDir.basename($FileName), $FileContent); 499 $Error=MyWriteFile($DestinationDir.basename($FileName), $FileContent);
392 if ($Error!="") 500 if ($Error!="")
393 { -  
394 print " ".$Error."\n"; -  
395 return -1; 501 return $Error;
396 } -  
397   502  
398 // Clear memory 503 // Clear memory
399 unset($FileContent); 504 unset($FileContent);
400 unset($WordListHTML); 505 unset($WordListHTML);
401 } 506 }
402   507  
403 // Delimiter 508 // Delimiter
404 print "\n"; 509 print "\n";
-   510 return "";
405 } 511 }
406   512  
407   513  
408 function GenerateAsmFiles($TemplateDir, $SourceDir, &$SourceAsmFiles, $DestinationDir) 514 function GenerateAsmFiles($TemplateDir, $SourceDir, &$SourceAsmFiles, $DestinationDir)
409 // Cretaes HTML files from all processed ASM files 515 // Cretaes HTML files from all processed ASM files
410 // IN 516 // Returns Error String
-   517 // IN $TemplateDir - directory with template files
-   518 // IN $SourceDir - directory with asm source files
411 // 519 // OUT $SourceAsmFiles -
-   520 // IN $DestinationDir - directory for generated HTML files
412 { 521 {
413 // Info 522 // Info
414 print "Copy ASM Files\n"; 523 print "Copy ASM Files\n";
415   524  
416 // Destination directory exists 525 // Destination directory exists
417 $DestinationDir.=ASMFILES; 526 $DestinationDir.=ASMFILES;
418 if (!is_dir($DestinationDir)) 527 if (!is_dir($DestinationDir))
419 { -  
420 if (!@mkdir($DestinationDir)) 528 if (!@mkdir($DestinationDir))
421 { -  
422 print " Unable Create Dir ".$DestinationDir."\n"; 529 return "Unable Create Dir ".$DestinationDir;
423 return -1; -  
424 } -  
425 } -  
426 530  
427 // Read template 531 // Read template
428 $Error=MyReadFileString($TemplateDir."FileAsm.en.html", $Template); 532 $Error=MyReadFileString($TemplateDir."FileAsm.en.html", $Template);
429 if ($Error!="") 533 if ($Error!="")
430 { -  
431 print " ".$Error."\n"; -  
432 return -1; 534 return $Error;
433 } -  
434   535  
435 // Copy all source files 536 // Copy all source files
436 foreach($SourceAsmFiles as $Key => $FileName) 537 foreach($SourceAsmFiles as $Key => $FileName)
437 { 538 {
438 print " ".$FileName."\n"; 539 print " ".$FileName."\n";
439   540  
440 // Read ASM file 541 // Read ASM file
441 $Error=MyReadFileString($SourceDir.$FileName, $FileContent); 542 $Error=MyReadFileString($SourceDir.$FileName, $FileContent);
442 if ($Error!="") 543 if ($Error!="")
443 { -  
444 print " ".$Error."\n"; -  
445 return 1; 544 return $Error;
446 } -  
447   545  
448 // Prepare HTML 546 // Prepare HTML
449 $FileContent=htmlspecialchars($FileContent); 547 $FileContent=htmlspecialchars($FileContent);
450 $FileContent="<pre>\n".$FileContent."\n</pre>"; 548 $FileContent="<pre>\n".$FileContent."\n</pre>";
451   549  
452 // Use Template 550 // Use Template
453 $TemplateWork=$Template; 551 $TemplateWork=$Template;
454 $TemplateWork=str_ireplace("<<FileName>>", $FileName, $TemplateWork); 552 $TemplateWork=str_ireplace("<<FileName>>", $FileName, $TemplateWork);
455 $TemplateWork=str_ireplace("<<FileContent>>", $FileContent, $TemplateWork); 553 $TemplateWork=str_ireplace("<<FileContent>>", $FileContent, $TemplateWork);
456   554  
457 // Write ASM file in HTML 555 // Write ASM file in HTML
458 $Error=MyWriteFile($DestinationDir.FileName2HTML($FileName), $TemplateWork); 556 $Error=MyWriteFile($DestinationDir.FileName2HTML($FileName), $TemplateWork);
459 if ($Error!="") 557 if ($Error!="")
460 { -  
461 print " ".$Error."\n"; -  
462 return -1; 558 return $Error;
463 } -  
464 } 559 }
465 560
466 // Delimiter 561 // Delimiter
467 print "\n"; 562 print "\n";
-   563 return "";
468 } 564 }
469   565  
470   566  
471 // ********************************************************************** 567 // **********************************************************************
472 // Main Block 568 // Main Block
473 // ********************************************************************** 569 // **********************************************************************
474   570  
-   571 // Global Like Variables (arrays)
-   572 // $CFG - Config parameters
-   573 // $SourceAsmFiles - All processed ASM files (filenames)
-   574 // $LabelList - All label definitions (Label, FileName, LineNumber)
-   575 // $WordList - Word List (ShortLabel, Word, Comment, Label, FileName)
475   576  
476 // This file contains configurations for this script 577 // Process Command Line Parameters
477 require_once("GenerateHTML.cfg"); -  
478   -  
479   -  
480 // Global Like Variables 578 Error(Parameters($CFG));
481 //$SourceAsmFiles - All processed ASM files (filenames) -  
482 //$LabelList - All label definitions (Label, FileName, LineNumber) -  
483 //$WordList - Word List (ShortLabel, Word, Comment, Label, FileName) -  
484   -  
485   579  
486 // Process all ASM files from the root level 580 // Process all ASM files from the root level
487 SourceAsm($CFG_SourceDir, $CFG_SourceAsm, $SourceAsmFiles, $LabelList); 581 Error(SourceAsm($CFG["SourceDir"], $CFG["SourceAsm"], $SourceAsmFiles, $LabelList));
488 PrintSourceAsm($SourceAsmFiles); 582 PrintSourceAsm($SourceAsmFiles);
489 PrintLabels($LabelList); 583 PrintLabels($LabelList);
490   584  
491 // Destilate Labels and Words 585 // Destilate Labels and Words
492 CreateWordList($CFG_SourceDir, $LabelList, $WordList); 586 Error(CreateWordList($CFG["SourceDir"], $LabelList, $WordList));
493   587  
494 // Create HTML WordList 588 // Create HTML WordList
495 GenerateWordList($CFG_TemplateDir, $CFG_DestinationDir, $LabelList, $WordList); 589 Error(GenerateWordList($CFG["TemplateDir"], $CFG["DestinationDir"], $LabelList, $WordList));
496   590  
497 // Copy ASM files and convert them into HTML 591 // Copy ASM files and convert them into HTML
498 GenerateAsmFiles($CFG_TemplateDir, $CFG_SourceDir, $SourceAsmFiles, $CFG_DestinationDir); 592 Error(GenerateAsmFiles($CFG["TemplateDir"], $CFG["SourceDir"], $SourceAsmFiles, $CFG["DestinationDir"]));
499   593  
-   594 // Finish
-   595 print "O.K.\n";
-   596 return
500   597  
501 // Zpracování readme autora + verze 598 // Zpracování readme autora + verze
502 // Dodělat kontroly vstupní CFG parametrů (existence souborů a adresářů) -  
503 // Osetreni chyb - die(1) zpusobi chybu (v shellu a da se tak poznat, ze to nedopadlo) -  
504   -  
505 // Zpracování templejtů do samostatného podprogramu (vyřešit indent...) 599 // Zpracování templejtů do samostatného podprogramu (vyřešit indent...)
506 // tím se vyřeší i en/cs verze Asm souboru 600 // tím se vyřeší i en/cs verze Asm souboru
507   -  
508 // Generovat log do souboru místo printu (zvážit) oddělit chyby a varování 601 // Generovat log do souboru místo printu (zvážit) oddělit chyby a varování
509 // Vyčistit cílový adresář 602 // Vyčistit cílový adresář
510 // Process all FORTH files 603 // Process all FORTH files
511 // Problém s rekurzí (potenciální nekonečno) 604 // Problém s rekurzí (potenciální nekonečno)
512 // Chtělo by to do stránek vkládat info o verzi a (c) 605 // Chtělo by to do stránek vkládat info o verzi a (c)
513 ?> 606 ?>