我正在尝试使用整洁的lib来清理存储在数据库中的html代码。 我已经尝试了TidyPas和diTidy来实现这一目标,但无论我使用什么,我都会遇到访问冲突 - 它会破坏parseString。 任何人都可以帮忙或有建议吗?
这是我的功能(使用diTidy),但TidyPas获得相同的结果:
procedure tdmSME.TidyHTML(aField : tField);
var
aString : String;
nString : AnsiString;
ResultCode:LongInt;
TidyHandle: TidyDoc;
Target: TidyBuffer;
ErrorBuffer: TidyBuffer;
begin
aString := rxStrUtils.replaceStr(aField.AsString, #10, ' ');
aString := rxStrUtils.replaceStr(aString, #13, '');
nString := AnsiString(aString);
TidyHandle := tidyCreate;
tidyOptSetBool(TidyHandle, TidyMark, 0);
tidyOptSetInt(TidyHandle, TidyDocTypeMode, 1);
tidyOptSetBool(TidyHandle, TidyHTMLOut, 1);
tidyOptSetBool(TidyHandle, TidyMakeClean,1);
tidyOptSetBool(TidyHandle, TidyDropFontTags, 1);
ResultCode := tidySetErrorBuffer(TidyHandle, @ErrorBuffer);
try
try
if ResultCode >= 0 then
ResultCode := tidyParseString(TidyHandle, pAnsiChar(nString));----Access violation
if ResultCode >= 0 then
ResultCode := tidyCleanAndRepair(TidyHandle);
if ResultCode >= 0 then
ResultCode := tidyRunDiagnostics(TidyHandle);
if ResultCode >= 1 then
tidyOptSetBool(TidyHandle, TidyForceOutput, 1);
TidyBufInit(@Target);
if ResultCode >= 0 then
ResultCode := tidySaveBuffer(TidyHandle, @Target);
if ResultCode >= 0 then
begin
// if ResultCode > 0 then // Warnings?
// begin
// ShowMessage(PAnsiChar(ErrorBuffer.bp));
// end;
aField.AsString := string(Target.bp);
end
else
ShowMessage('A severe error (' + intToStr(ResultCode) + ') occurred: ');
except
on E:Exception do
ShowMessage('Some Error occurred on ' + aString + ':' + E.Message);
end;
finally
tidyBufFree(@Target);
tidyBufFree(@ErrorBuffer);
tidyRelease(TidyHandle);
end;
end;