-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfp2html.pp
More file actions
556 lines (504 loc) · 12 KB
/
fp2html.pp
File metadata and controls
556 lines (504 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
{ Copyright (C) Freepascal developers team
This source is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web
at <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing
to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.
}
program fp2html;
{$mode objfpc}
uses
SysUtils, getopts;
const
InputExt = '.fp';
VarNameChars: set of char = ['a'..'z', 'A'..'Z', '0'..'9', '_'];
var
TemplateFile: string;
ModifyFile: string;
InFile : array of string;
OutputExt : string;
Verbose : boolean;
OutputDir: string;
Recursive: boolean;
{****************************************************************************
Routines
****************************************************************************}
procedure Show(const s:string);
begin
if Verbose then
Write(s);
end;
{****************************************************************************
Main Stuff
****************************************************************************}
var
Done: array[0..1023] of string;
Total: word;
Function FileDone(fn:string):boolean;
var
i: word;
begin
i:=0;
while (i<Total) and (Done[i]<>fn) do
inc(i);
if Done[i]=fn then
Result:=true
else
begin
Done[Total]:=fn;
inc(Total);
Result:=false;
end;
end;
procedure Convert(fn:string);
type
TVarList = record
name: string;
value: string;
end;
var
f: text;
g: text;
t: text;
s: string;
i: longint;
//header stuff
modify: boolean;
adds: boolean;
counter: boolean;
picdir: string;
maindir: string;
header: string;
title: string;
varlist: array of TVarList;
nfn: string;
procedure WriteHtml(s:string);
var
i : Longint;
Quoted : boolean;
S2 : String;
begin
s:=Trim(s);
Quoted:=false;
SetLength(S2,0);
If Length(s)>0 then
begin
for i:= 1 TO Length(s) do
begin
If S[I]='"' THEN
Quoted:=NOT Quoted;
IF (S[I]='_') AND Not Quoted THEN
S2:=S2+' '
else
S2:=S2+S[I];
end;
S:=S2;
end;
{
i:=pos('_',s);
while (i>0) do
begin
delete(s,i,1);
insert(' ',s,i);
i:=pos('_',s);
end;}
i:=pos('&undersc;',s);
while (i>0) do
begin
delete(s,i,length('&undersc;'));
insert('_',s,i);
i:=pos('&undersc;',s);
end;
i:=pos('NEW!',s);
while (i>0) do
begin
Delete(s,i,4);
Insert('<img src="'+picdir+'new.gif" alt="New!" width="31" height="12"/>',s,i);
i:=pos('NEW!',s);
end;
if s<>'' then
write(g,s,#10);
end;
procedure InsertFile(const fn:string);
var
t : text;
s : string;
begin
assign(t,fn);
{$I-}
reset(t);
{$I+}
if ioresult<>0 then
exit;
while not eof(t) do
begin
readln(t,s);
WriteHtml(s);
end;
close(t);
end;
procedure Replace(var s: string; const s1,s2:string);
var
i : longint;
c : char;
begin
repeat
i:=pos(s1,s);
//check if true variable is found
//as in "myvar_url" and "myvar"
if i + length(s1) >= length(s) then
c := #0
else
c := s[i+length(s1)];
if (i = 0) or (c in VarNameChars) then
break
else
s := StringReplace(s, s1, s2, []);
until false;
end;
procedure AddVariable(str: string);
var
index: integer;
begin
//create an entry
if Length(varlist) = 0 then
SetLength(varlist,1)
else
SetLength(varlist, Succ(Length(varlist)));
//get variable name
index := 1;
while str[index] <> ' ' do
inc(index);
varlist[high(varlist)].name := '$' + copy(str, 2, index - 2);
//get variable value
varlist[high(varlist)].value := copy(str, index + 1, Length(str) - index);
end;
procedure ReplaceVarList;
var
i: integer;
begin
for i := 0 to High(varlist) do
Replace(s, varlist[i].name, varlist[i].value);
end;
function create_new_filename(const s:string):string;
var extbeg,langbeg:byte;
begin
extbeg:=length(s);
while (extbeg>0) and not (s[extbeg] in ['/', '.', '\', ':']) do
dec(extbeg);
if s[extbeg]<>'.' then
extbeg:=0;
if extbeg<>0 then
begin
langbeg:=extbeg-1;
while (langbeg>0) and not (s[langbeg] in ['/', '.', '\', ':']) do
dec(langbeg);
if s[langbeg]<>'.' then
langbeg:=0;
end
else
langbeg:=0;
if extbeg=0 then
create_new_filename:=s+outputext
else if langbeg=0 then
create_new_filename:=copy(s,1,extbeg-1)+outputext
else
create_new_filename:=copy(s,1,langbeg-1)+outputext+copy(s,langbeg,extbeg-langbeg);
end;
begin
{ Reset }
Title:='';
Maindir:='';
PicDir:='';
header:='';
Modify:=false;
Adds:=false;
Counter:=False;
{Create New FileName}
nfn:=create_new_filename(fn);
{ nfn := ChangeFileExt(fn, OutputExt);}
if OutputDir <> '' then
nfn := OutputDir + ExtractFileName(nfn);
{Done?}
if FileDone(nfn) then
exit;
{Open Files}
Show('Converting '+fn);
if fn=nfn then
assign(g,ChangeFileExt(fn,'$T$'))
else
begin
Show(' -> '+nfn + #10);
assign(g,nfn);
end;
assign(f,fn);
{$I-}
reset(f);
{$I+}
if ioresult<>0 then
exit;
{Read Header}
readln(f,s);
if Copy(s,1,6)='<html>' then
readln(f,s);
if Copy(s,1,4)<>'<!--' then
begin
WriteLn('Wrong input file ', fn, '!');
Close(f);
exit;
end;
readln(f,s);
while not eof(f) and (s<>'-->') do
begin
s:=Trim(s);
if Copy(s,1,6)='#TITLE' then
title:=Copy(s,8,80)
else
if Copy(s,1,7)='#HEADER' then
header:=Copy(s,9,80)
else
if Copy(s,1,8)='#MAINDIR' then
begin
maindir:=Copy(s,10,30);
if (maindir<>'') and (maindir[length(maindir)]<>'/') then
maindir:=maindir+'/';
Show('Set main dir to ' + Maindir);
end
else
if Copy(s,1,7)='#PICDIR' then
begin
picdir:=Copy(s,9,30);
if (picdir<>'') and (picdir[length(picdir)]<>'/') then
picdir:=picdir+'/';
end
else
if Copy(s,1,5)='#ADDS' then
Adds:=true
else
if Copy(s,1,8)='#COUNTER' then
Counter:=true
else
if Copy(s,1,7)='#MODIFY' then
Modify:=true
else
//check for free assignable variable
if s[1] = '#' then
AddVariable(s);
readln(f,s);
end;
if eof(f) then
begin
Close(f);
WriteLn('Wrong input file ', fn, '!');
exit;
end;
//add system variables
AddVariable('#NOW ' + DateTimeToStr(Now));
{Fix items}
if PicDir='' then
PicDir:=MainDir+'pic/';
if Title='' then
Title:='Free Pascal - Home Page';
{Read the template}
assign(t,TemplateFile);
{$I-}
reset(t);
{$I+}
if ioresult<>0 then
begin
WriteLn('Error: template file "',TemplateFile,'" not found!');
Close(f);
Halt(0);
end;
{Open output}
{$I-}
rewrite(g);
{$I+}
if ioresult<>0 then
begin
Writeln('Can''t create ',nfn);
Close(f);
Close(t);
end;
{Parse the template and fill in the stuff}
while not eof(t) do
begin
readln(t,s);
s:=Trim(s);
{Replace}
Replace(s, '$TITLE',title);
Replace(s, '$HEADER',header);
Replace(s, '"pic/','"'+picdir);
ReplaceVarList;
{Fix Index}
if s='<!-- TEXT -->' then
begin
while not eof(f) do
begin
readln(f,s);
//replace
Replace(s, '$TITLE',title);
Replace(s, '$HEADER',header);
Replace(s, '"pic/','"'+picdir);
ReplaceVarList;
WriteHtml(s);
end;
s:='';
end
else
if s='<!-- ADDS -->' then
begin
if Adds then
InsertFile('adds.fp');
end
else
if s='<!-- COUNTER -->' then
begin
if Counter then
InsertFile('counter.fp');
end
else
if s='<!-- MODIFY -->' then
begin
if Modify and (ModifyFile<>'') then
InsertFile(ModifyFile);
end
else
begin
i:=pos('<a href="',s);
if i>0 then
begin
{ insert maindir }
if (not (Copy(S,i+9,7)='http://')) and
((length(s) < i+9) or (s[i+9] <> '/')) and
(maindir<>'') then
insert(maindir,s,i+9);
end;
WriteHtml(s);
end;
end;
{Close}
close(g);
close(f);
close(t);
if fn=nfn then
begin
erase(f);
rename(g,fn);
end;
end;
{****************************************************************************
General Stuff
****************************************************************************}
procedure helpscreen;
begin
writeln(ParamStr(0), ' [Options] <InFile(s)>');
writeln;
writeln('Options:');
writeln(' -O<extension> Specify OutputExt Mask');
writeln(' -M<file> Use <file> for modifying');
writeln(' -T<file> Use <file> as template file');
writeln(' -D<path> Output directory');
writeln(' -r Recursively process directories');
writeln(' -v Be more verbose');
writeln(' -h This help screen');
halt(1);
end;
procedure getpara;
var
ch: char;
i: integer;
procedure AddInFile(str: string);
begin
//create an entry
if Length(InFile) = 0 then
SetLength(InFile,1)
else
SetLength(InFile, Succ(Length(InFile)));
InFile[High(InFile)] := str;
end;
procedure AddRecursiveFiles(SearchDir: string);
var
Info : TSearchRec;
begin
if FindFirst (SearchDir+'*',faAnyFile and faDirectory,Info)=0 then
begin
repeat
with Info do
begin
if ((Attr and faDirectory) = faDirectory) and (Name <> '.') and (Name <> '..')then
AddRecursiveFiles(IncludeTrailingPathDelimiter(Name));
if ExtractFileExt(Name) = InputExt then
AddInFile(SearchDir + Name);
end;
until FindNext(info)<>0;
end;
FindClose(Info);
end;
begin
//reset
TemplateFile:='template.fpht';
OutputExt := '.html';
Verbose := False;
OutputDir := '';
Recursive := False;
//parse options
repeat
ch:=Getopt('O:M:T:D:rvh?');
Case ch of
'O' : if OptArg[1] <> '.' then
OutputExt := '.' + OptArg
else
OutputExt := OptArg;
'M' : ModifyFile:=OptArg;
'T' : TemplateFile:=OptArg;
'D' : OutputDir := IncludeTrailingPathDelimiter(OptArg);
'r' : Recursive := True;
'v' : Verbose:=true;
'h' : helpscreen;
'?' : helpscreen;
EndOfOptions : break;
end;
until false;
//add input files
for i:= OptInd to Paramcount do
AddInFile(ChangeFileExt(ParamStr(i),InputExt));
//add recursively files
if Recursive then
AddRecursiveFiles('');
//checks
if (OutputDir <> '') and not DirectoryExists(OutputDir) then
begin
writeln('Error: Output directory does not exist!');
halt(0);
end;
end;
var
i: word;
begin
//get all the commandline parameters
GetPara;
//prevent the following files to be overwritten
FileDone('template'+OutputExt);
FileDone('adds'+OutputExt);
FileDone('counter'+OutputExt);
if ModifyFile<>'' then
FileDone(ChangeFileExt(ModifyFile,OutputExt));
//process all infiles
if Length(InFile) = 0 then
helpscreen;
for i:=0 to High(InFile) do
if FileExists(InFile[i]) then
Convert(InFile[i])
else
writeln('Error: File ', InFile[i],' does not exist!');
{ Write OK }
Show('OK'+ #10);
end.