-
Notifications
You must be signed in to change notification settings - Fork 0
/
uMain.pas
81 lines (72 loc) · 1.64 KB
/
uMain.pas
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
unit uMain;
{$IFDEF FPC}
{$mode objfpc}
{$H+}
//{$modeswitch typehelpers}
{$ENDIF}
interface
uses
{$IFDEF DARWIN}
{$IFDEF UseCThreads}
cthreads,
{$ENDIF}
{$ENDIF}
{$IFDEF DARWIN}
cwstring,
{$ENDIF}
sysutils,
Classes,
uUtil,
uOptions,
uHTMLResEmb;
function ProcessFile: Integer;
implementation
function ProcessFile: Integer;
var
ProcessingMessage: string;
DocPath: string;
sl: TStringList;
msStdIn: TMemoryStream;
HTMLProcessor: THTMLProcessor;
begin
ProcessingMessage := '';
DocPath := ExtractFilePath(Opts.InputFile);
sl := TStringList.Create;
HTMLProcessor := nil;
msStdIn := nil;
try
try
if (Opts.StdIn) then
begin
msStdin := StdInToMemoryStream;
msStdIn.Position := 0; // !!
sl.LoadFromStream(msStdIn);
end
else
sl.LoadFromFile(Opts.InputFile);
HTMLProcessor := THTMLProcessor.Create(DocPath, Opts.EmbedCSS, Opts.EmbedJavaScript, Opts.EmbedImages);
if not(HTMLProcessor.ProcessHTML(sl, ProcessingMessage)) then
Result := ERR_PROCESS
else
begin
if (Opts.StdOut) then
Write(sl.Text)
else
sl.SaveToFile(Opts.OutputFile);
Result := SUCCESS;
end;
if (Result = ERR_PROCESS) then
WriteLn(StdErr, 'Processing failed.');
if (ProcessingMessage <> '') then
WriteLn(StdErr, '(Warning) messages from HTML processing:', NL, ProcessingMessage);
except
Result := ERR_PROCESS;
WriteLn(StdErr, 'Processing error: ', NL, Exception(ExceptObject).Message);
end;
finally
msStdIn.Free;
HTMLProcessor.Free;
sl.Free;
end;
end;
end.