📄 utemplateprocessor.pas
字号:
unit UTemplateProcessor;
{模板处理}
interface
uses
LibXMLParser,UPatternMatcher,classes,UElementFactory;
type
TTemplateProcessor=class
function Process(Match:Tmatch):string;
end;
function ProcessContents(Match:TMatch;Parser:TXMLParser):string;
var
TemplateProcessor:TTemplateProcessor;
implementation
uses
UVariables,SysUtils,UUtils;
function ProcessContents(Match:TMatch;Parser:TXMLParser):string;
var
name:string;
begin
result:='';
if parser.CurPartType=ptEmptyTag then exit;
name:=Parser.CurName;
while parser.Scan do begin
if (parser.CurPartType=ptendTag) and (parser.CurName=name) then
break
else
case parser.CurPartType of
ptContent:result:=result+GetElementContents(parser);
ptEmptyTag,
ptStartTag,
ptEndTag:begin
if parser.CurStart[-1] in CWhitespace then result:=result+' ';
result:=result+ElementFactory.get(parser.CurName).Process(Match,Parser);
end;
end
end;
end;
function TTEmplateProcessor.Process(match:TMatch):string;
var
Parser:TXMLParser;
//Added program begin
i:integer;
//Added program end
begin
result:='';
if match._template='' then exit;
Parser:=TXMLParser.Create;
Parser.LoadFromBuffer(PChar(match._template));
Parser.StartScan;
while parser.Scan do begin
case parser.CurPartType of
ptContent:result:=result+GetElementContents(parser);
ptEmptyTag,
ptStartTag,
ptEndTag:begin
if (parser.CurStart<>Parser.DocBuffer) and (parser.CurStart[-1] in CWhitespace) then result:=result+' ';
result:=result+ElementFactory.get(parser.CurName).Process(Match,Parser);
end;
end
end;
Parser.Free;
result:=ConvertWs(result,true);
//中文字符处理完后的去空格处理
i:=1;
while i<length(result)-1 do
begin
if ord(result[i]) in [$81..$FF] then //GB 码
if result[i+2]=' ' then
begin
delete(result,i+2,1);
dec(i);
end
else
inc(i);
inc(i);
end;
result:=Trim(result);
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -