-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadpconverter.pp
179 lines (146 loc) · 4.58 KB
/
adpconverter.pp
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
unit adpconverter;
{$mode objfpc}{$H+}
interface
uses
fpjson, jsonparser, jsonscanner, Classes, SysUtils, adpdata, adputils, adpparser;
type
{ TPageConverter }
TPageConverter = Class
private
function adp_parse(adp: Unicodestring; aRecognizeProperties: Boolean; amode:TParseMode): Unicodestring;
function generate_page(fn: ansistring; aMode:TParseMode): Unicodestring;
procedure loadparam;
Public
master_template:ansistring;
slave_html:Unicodestring;
fproperties:Tproperties;
fmessages:Tmessages;
default_master:string ; // ='default-master.adp';
Options : TOptions;
Datasources : TDatasources;
Constructor Create(anoption:TOptions; aDatasources : TDatasources);
destructor Destroy; override;
procedure Execute(const aExt, LangName, LangEncoding: AnsiString);
end;
implementation
{ TPageConverter }
constructor TPageConverter.Create(anoption:TOptions; aDatasources : TDatasources);
begin
options:=anoption;
Datasources:=aDatasources;
default_master:=anOption.default_master;
FProperties:=TProperties.Create;
fmessages:=TMessages.Create(anOption);
end;
destructor TPageConverter.Destroy;
begin
FreeAndNil(FProperties);
FreeAndNil(FMessages);
inherited Destroy;
end;
procedure TPageConverter.loadparam;
var
s,key,value:ansistring;
p:byte;
begin
for s in options.defines do
begin
p:=pos('=',s);
key:=copy(s,1,p-1);
value:=copy(s,p+1,255);
fproperties.Write(UTF8Decode(key),UTF8Decode(value));
end;
end;
function TPageConverter.adp_parse(adp:Unicodestring; aRecognizeProperties: Boolean; amode:TParseMode):Unicodestring;
Var
Parser : TADPParser;
begin
Parser:=TADPParser.Create(Options,FMessages,FProperties,Datasources);
try
Parser.recognize_properties:=aRecognizeProperties;
Parser.Mode:=amode;
Parser.master_template:=self.master_template;
Parser.slave_html:=Slave_html;
Result:=Parser.Parse(adp);
master_template := Parser.master_template;
finally
Parser.Free;
end;
end;
function TPageConverter.generate_page(fn:ansistring ; amode:TParseMode):Unicodestring;
var
adpfile:file;
adpansi:ansistring;
adpwide:Unicodestring;
begin
repeat
master_template:='';
if not fileexists(fn) then // there are paths with ../.. in the templates and we
// don't change dirs.
fn:=extractfilename(fn); // retry without dir info.
assign(adpfile,fn);
reset(adpfile,1);
setlength(adpansi,filesize(adpfile));
blockread(adpfile,adpansi[1],filesize(adpfile));
adpwide:=UTF8Decode(adpansi);
close(adpfile);
{ adpwide:=FProperties.ReplaceInString(adpwide);}
{Do two passes. The first pass if/else tags are expanded...}
if length(adpwide)>0 then
slave_html:=adp_parse(adpwide,False,amode)
else
slave_html:='';
{... so the second pass can apply properties.}
if length(adpwide)>0 then
slave_html:=adp_parse(slave_html,True,amode)
else
slave_html:='';
{ fix /down/old/i386-beos.adp site-master.adp file not found by wangyouworld }
if ((master_template <> '') and not FileExists(master_template)) then master_template := options.masterpath+master_template;
// Writeln('Master template now: ',master_template);
fn:=master_template;
until master_template='';
generate_page:=slave_html;
end;
procedure TPageConverter.Execute(const aExt, LangName, LangEncoding: AnsiString);
var
htmlfile:text;
page:Unicodestring;
begin
loadparam;
options.locale:=langname;
options.output_encoding:=LangEncoding;
// Writeln('Input file : ',options.InputFile);
fProperties.Write('x',UTF8Decode(aext));
fProperties.Write('output_encoding',UTF8Decode(Langencoding));
if LangName='' then
options.catalogfile := 'catalog.en.adp'
else
options.catalogfile := 'catalog.'+langName+'.adp';
{Process catalog first.}
if FileExists(options.catalogfile) then
begin
generate_page(options.catalogfile,process_catalog);
end
else
begin
{ Fix catalogfile notfound by wangyouworld }
options.catalogfile := Options.masterpath+options.catalogfile;
generate_page(options.catalogfile,process_catalog);
end;
{Process page.}
page:=generate_page(options.inputfile,process_page);
if options.outputfile='' then
write(page)
else
begin
if (options.locale<>'') then
options.outputfile:=options.outputfile+'.'+options.locale;
assign(htmlfile,options.outputfile);
rewrite(htmlfile);
{Possible Unicodestring bug? Manually convert to ansistring.}
write(htmlfile,ansistring(page));
close(htmlfile);
end;
end;
end.