program DITidy_Extract_Links; {$APPTYPE CONSOLE} {$I DI.inc} uses {$IFDEF FastMM}FastMM4, {$ENDIF}SysUtils, DITidy; { Calls itself recursively to iterate all children of a given node. Each node is checked for links, which are written out to the console. } procedure WriteLinks(const ANode: TidyNode); var a: TidyAttr; c: TidyNode; begin c := tidyGetChild(ANode); while Assigned(c) do begin case tidyNodeGetType(ANode) of TidyNode_Start, TidyNode_StartEnd: case tidyNodeGetId(ANode) of TidyTag_A: begin a := tidyAttrGetByID(ANode, TidyAttr_HREF); if Assigned(a) then WriteLn(tidyAttrValue(a)); end; end; end; { Now for the children ... } WriteLinks(c); c := tidyGetNext(c); end; end; label lblDone; var Doc: TidyDoc; ErrorBuffer: TidyBuffer; FileName: string = ''; RC: Integer; begin if ParamCount <> 1 then begin WriteLn('Usage: ', ExtractFileName(ParamStr(0)), ' <File>'); goto lblDone; end; FileName := ParamStr(1); Doc := tidyCreate; RC := tidySetErrorBuffer(Doc, @ErrorBuffer); if RC >= 0 then begin { Parse the file. } RC := tidyParseFile(Doc, PAnsiChar(AnsiString(FileName))); { Show potential warnings and errors. } WriteLn(PAnsiChar(ErrorBuffer.bp)); { If document loaded OK, write out its links. } if RC >= 0 then begin WriteLn; WriteLn('Links in ', FileName, ':'); WriteLn; WriteLinks(tidyGetRoot(Doc)); end; end; tidyBufFree(@ErrorBuffer); tidyRelease(Doc); lblDone: WriteLn; WriteLn('Done - Press ENTER to exit'); ReadLn; end. |