-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhtmlpp.pp
More file actions
193 lines (169 loc) · 5.05 KB
/
htmlpp.pp
File metadata and controls
193 lines (169 loc) · 5.05 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
unit htmlpp;
{$mode ObjFPC}{$H+}
interface
uses
Types, Classes, SysUtils;
Type
{ THTMLPostProcessor }
TProcessorLogEvent = procedure(sender : TObject; const Msg : string) of object;
THTMLPostProcessor = class(TComponent)
private
FBackup: Boolean;
FBaseDir: String;
FBaseDirNewIssueURL: String;
FDirs: TStringDynArray;
FNewIssueURL: String;
FOnLog: TProcessorLogEvent;
FRecurse: Boolean;
FTimeStamp: Boolean;
procedure GetFilesInDir(aDir: string; aFiles: TStrings);
protected
procedure DoLog(Const Msg : String); overload;
procedure DoLog(Const Fmt : String; const Args : Array of const); overload;
public
constructor create(aOwner : TComponent); override;
procedure processfile(const aBaseDir, aFileName : String);
procedure execute; virtual;
property TimeStamp : Boolean Read FTimeStamp Write FTimeStamp;
property Backup : Boolean Read FBackup Write FBackup;
property Recurse : Boolean Read FRecurse Write FRecurse;
property Dirs : TStringDynArray read FDirs Write FDirs;
property NewIssueURL : String Read FNewIssueURL Write FNewIssueURL;
property BaseDir : String Read FBaseDirNewIssueURL Write FBaseDir;
property OnLog : TProcessorLogEvent Read FOnLog Write FOnLog;
end;
implementation
procedure THTMLPostProcessor.DoLog(const Msg: String);
begin
if assigned(FOnLog) then
FOnLog(Self,Msg);
end;
procedure THTMLPostProcessor.DoLog(const Fmt: String; const Args: array of const);
begin
if assigned(FOnLog) then
DoLog(Format(Fmt,Args));
end;
constructor THTMLPostProcessor.create(aOwner: TComponent);
begin
inherited create(aOwner);
end;
procedure THTMLPostProcessor.GetFilesInDir(aDir: string; aFiles : TStrings);
var
lInfo : TSearchRec;
lCount : Integer;
begin
lCount:=0;
if FindFirst(aDir+'*.html',0,lInfo)=0 then
try
repeat
aFiles.add(aDir+lInfo.Name);
inc(lcount);
until FindNext(lInfo)<>0;
finally
FindClose(lInfo);
end;
DoLog('Found %d files in directory "%s"',[lCount,aDir]);
if Recurse then
if FindFirst(aDir+AllFilesMask,faDirectory,lInfo)=0 then
try
repeat
if ((lInfo.Attr and faDirectory)<>0)
and (lInfo.Name<>'.')
and (lInfo.Name<>'..') then
GetFilesInDir(IncludeTrailingPathDelimiter(aDir+lInfo.Name),aFiles);
until FindNext(lInfo)<>0;
finally
FindClose(lInfo);
end;
end;
procedure THTMLPostProcessor.processfile(const aBaseDir, aFileName: String);
var
lFile : TStrings;
lLast : integer;
procedure addLine(const aLine : string);
begin
lFile.Insert(lLast,aLine);
inc(lLast);
end;
var
lLink,lReportFile,lFooterLine : String;
lFooter,lFooterPos : Integer;
begin
lFile:=TStringList.Create;
try
lFile.LoadFromFile(aFileName);
if Backup then
lFile.SaveToFile(aFileName+'.bak');
lLast:=lFile.Count-1;
While (lLast>=0) and (Pos('</html>',lFile[lLast])=0) do
Dec(lLast);
While (lLast>=0) and (Pos('</body>',lFile[lLast])=0) do
Dec(lLast);
lFooter:=lLast;
While (lFooter>=0) and (Pos('</footer>',lFile[lFooter])=0) do
Dec(lFooter);
if lFooter<>0 then
begin
// Make sure the closing tag is on a line by it's own, so we can insert correctly.
lFooterLine:=lFile[lFooter];
lFooterPos:=Pos('</footer>',lFooterLine);
if lFooterPos>1 then
begin
lFile[lFooter]:=Copy(lFooterLine,1,lFooterPos-1);
Delete(lFooterLine,1,lFooterPos-1);
inc(lFooter);
lFile.Insert(lFooter,lFooterLine);
end;
lLast:=lFooter;
end;
if lLast<0 then
Raise Exception.Create('End of html not found while treating '+aFileName);
if lFooter=0 then
begin
AddLine('<footer>');
AddLine('<hr>');
end;
if FTimeStamp then
AddLine('<span class="timestamp">Page generated on '+FormatDateTime('yyyy-mm-dd',Date)+'.</span> ');
lReportFile:=ExtractRelativePath(aBaseDir,aFileName);
lLink:=NewIssueURL+'?issue%5Btitle%5D=issue%20in%20page%20'+lReportFile;
lLink:=Format('<a class="reportissue-link" href="%s">Report a problem on this page</a>',[lLink]);
AddLine(lLink);
if lFooter=0 then
AddLine('</footer>');
lFile.SaveToFile(aFileName);
finally
lFile.Free;
end;
end;
procedure THTMLPostProcessor.execute;
var
lFiles : TStrings;
lDir, lFile : string;
begin
if NewIssueURL='' then
NewIssueURL:='https://gitlab.com/freepascal.org/fpc/documentation/-/issues/new';
if FBaseDir<>'' then
FBaseDir:=IncludeTrailingPathDelimiter(FBaseDir);
lFiles:=TStringList.Create;
try
for lDir in FDirs do
begin
lFiles.Clear;
GetFilesInDir(IncludeTrailingPathDelimiter(lDir),lFiles);
For lFile in lFiles do
begin
DoLog('Processing file %s',[lFile]);
try
processfile(FBaseDir,lFile);
except
on E : Exception do
DoLog('Exception %s processing file "%s": %s',[E.ClassName,lFile,E.Message]);
end;
end;
end;
finally
lFiles.Free;
end;
end;
end.