fasthtmlparser.pas 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. {
  2. See the section LICENSE/TERMS below for details about the copyright.
  3. }
  4. // TODO:
  5. {
  6. - OnDone event when parser is finished
  7. - advanced parsing NAME=VALUE pairs
  8. }
  9. {
  10. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  11. FastHTMLParser unit to parse HTML
  12. (disect html into its tags and text.)
  13. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  14. TITLE : Fast HTML Parser (modified)
  15. CLASS : TjsFastHTMLParser
  16. VERSION : 0.4La
  17. AUTHOR : James Azarja
  18. http://www.jazarsoft.com/
  19. CONTRIBUTORS : L505
  20. http://z505.com
  21. YourName Here...
  22. LEGAL : Copyright (C) 2004 Jazarsoft, All Rights Reserved.
  23. Modified 2005 Lars (L505)
  24. --------------------------------------------------------------------------------
  25. - Modified for use as a pure command line unit (no dialogs) for freepascal.
  26. - Also added UPPERCASE tags so that when you check for <font> it returns all
  27. tags like <FONT> and <FoNt> and <font>
  28. Use it for what reasons:
  29. -make your own web browsers,
  30. -make your own text copies of web pages for caching purposes
  31. -Grab content from websites -without- using regular expressions
  32. -Seems to be MUCH MUCH FASTER than regular expressions, as it is after all
  33. a true parser
  34. -convert website tables into spreadsheets (parse <TD> and <TR>, turn in to
  35. CSV or similar)
  36. -convert websites into text files (parse all text, and tags <BR> <P> )
  37. -convert website tables into CSV/Database (<parse <TD> and <TR>)
  38. -find certain info from a web page.. i.e. all the bold text or hyperlinks in
  39. a page.
  40. -Parse websites remotely from a CGI app using something like Sockets or
  41. Synapse and SynWrap to first get the HTML site. This would allow you to
  42. dynamically parse info from websites and display data on your site in real
  43. time.
  44. -HTML editor.. WYSIWYG or a partial WYSIWYG editor. Ambitious, but possible.
  45. -HTML property editor. Not completely wysiwyg but ability to edit proprties
  46. of tags. Work would need to be done to parse each property in a tag.
  47. --------------------------------------------------------------------------------
  48. LICENSE/TERMS
  49. --------------------------------------------------------------------------------
  50. This code may be used and modified by anyone so long as this header and
  51. copyright information remains intact.
  52. The code is provided "AS-IS" and without WARRANTY OF ANY KIND,
  53. expressed, implied or otherwise, including and without limitation, any
  54. warranty of merchantability or fitness for a particular purpose. 
  55. In no event shall the author be liable for any special, incidental,
  56. indirect or consequential damages whatsoever (including, without
  57. limitation, damages for loss of profits, business interruption, loss
  58. of information, or any other loss), whether or not advised of the
  59. possibility of damage, and on any theory of liability, arising out of
  60. or in connection with the use or inability to use this software.  
  61. --------------------------------------------------------------------------------
  62. HISTORY:
  63. --------------------------------------------------------------------------------
  64. 0.1 - James:
  65. Initial Development
  66. mostly based on Peter Irlam works & ideas
  67. 0.2 - James:
  68. Some minor bug has fixed
  69. 0.3 - James:
  70. Some jsHTMLUtil function bug has been fixed
  71. 0.4 - James:
  72. jsHTMLUtil Tag Attributes bug has been fixed
  73. thanks to Dmitry [[email protected]]
  74. 0.4L.1a - L505:
  75. Made unit work with freepascal, added UPCASE (case insensitive)
  76. exec function
  77. 0.4L.1b - L505:
  78. Changed case insensitive version to a new class instead of
  79. the old ExecUpcase
  80. //
  81. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  82. }
  83. {$IFDEF FPC}{$MODE DELPHI}{$H+}{$ENDIF}
  84. // {$DEFINE DEBUGLN_ON}
  85. {$IFNDEF FPC_DOTTEDUNITS}
  86. unit FastHTMLParser;
  87. {$ENDIF FPC_DOTTEDUNITS}
  88. interface
  89. {$IFDEF FPC_DOTTEDUNITS}
  90. uses
  91. {$IFDEF KOL_MCK}
  92. KOL;
  93. {$else}
  94. System.SysUtils;
  95. {$ENDIF}
  96. {$ELSE FPC_DOTTEDUNITS}
  97. uses
  98. {$IFDEF KOL_MCK}
  99. KOL;
  100. {$else}
  101. SysUtils;
  102. {$ENDIF}
  103. {$ENDIF FPC_DOTTEDUNITS}
  104. {$IFDEF DEBUGLN_ON}
  105. // dummy, default debugging
  106. procedure debugproc(s: String);
  107. // for custom debugging, assign this in your units
  108. var debugln: procedure(s: String) = debugproc;
  109. {$ENDIF}
  110. type
  111. // when tag content found in HTML, including names and values
  112. // case insensitive analysis available via NoCaseTag
  113. TOnFoundTag = procedure(NoCaseTag, ActualTag: AnsiString) of object;
  114. // when text found in the HTML
  115. TOnFoundText = procedure(Text: AnsiString) of object;
  116. // Lars's modified html parser, case insensitive or case sensitive
  117. { THTMLParser }
  118. THTMLParser = class(TObject)
  119. private
  120. FDone: Boolean;
  121. public
  122. OnFoundTag: TOnFoundTag;
  123. OnFoundText: TOnFoundText;
  124. Raw: PAnsiChar;
  125. FCurrent : PAnsiChar;
  126. constructor Create(sRaw: AnsiString);overload;
  127. constructor Create(sRaw: UnicodeString);overload;
  128. constructor Create(pRaw: PAnsiChar);overload;
  129. procedure Exec;
  130. procedure NilOnFoundTag(NoCaseTag, ActualTag: AnsiString);
  131. procedure NilOnFoundText(Text: AnsiString);
  132. Public
  133. Function CurrentPos : Integer;
  134. property Done: Boolean read FDone write FDone;
  135. end;
  136. implementation
  137. // default debugging, do nothing, let user do his own by assigning DebugLn var
  138. procedure debugproc(s: String);
  139. begin
  140. end;
  141. function CopyBuffer(StartIndex: PAnsiChar; Length: Integer): AnsiString;
  142. begin
  143. SetLength(Result, Length);
  144. StrLCopy(@Result[1], StartIndex, Length);
  145. end;
  146. { ************************ THTMLParser ************************************** }
  147. constructor THTMLParser.Create(pRaw: PAnsiChar);
  148. begin
  149. if pRaw = '' then exit;
  150. if pRaw = nil then exit;
  151. Raw:= pRaw;
  152. end;
  153. constructor THTMLParser.Create(sRaw: AnsiString);
  154. begin
  155. if sRaw = '' then exit;
  156. Raw:= PAnsiChar(sRaw);
  157. end;
  158. constructor THTMLParser.Create(sRaw: UnicodeString);
  159. begin
  160. Create(UTF8Encode(sRaw));
  161. end;
  162. { default dummy "do nothing" events if events are unassigned }
  163. procedure THTMLParser.NilOnFoundTag(NoCaseTag, ActualTag: AnsiString);
  164. begin
  165. end;
  166. procedure THTMLParser.NilOnFoundText(Text: AnsiString);
  167. begin
  168. end;
  169. function THTMLParser.CurrentPos: Integer;
  170. begin
  171. if Assigned(Raw) and Assigned(FCurrent) then
  172. Result:=FCurrent-Raw
  173. else
  174. Result:=0;
  175. end;
  176. procedure THTMLParser.Exec;
  177. var
  178. L: Integer;
  179. TL: Integer;
  180. I: Integer;
  181. TagStart,
  182. TextStart,
  183. P: PAnsiChar; // Pointer to current AnsiChar.
  184. C: AnsiChar;
  185. begin
  186. {$IFDEF DEBUGLN_ON}debugln('FastHtmlParser Exec Begin');{$ENDIF}
  187. { set nil events once rather than checking for nil each time tag is found }
  188. if not assigned(OnFoundText) then
  189. OnFoundText:= NilOnFoundText;
  190. if not assigned(OnFoundTag) then
  191. OnFoundTag:= NilOnFoundTag;
  192. TL:= StrLen(Raw);
  193. I:= 0;
  194. P:= Raw;
  195. Done:= False;
  196. if P <> nil then
  197. begin
  198. TagStart:= nil;
  199. repeat
  200. TextStart:= P;
  201. { Get next tag position }
  202. while Not (P^ in [ '<', #0 ]) do
  203. begin
  204. Inc(P); Inc(I);
  205. if I >= TL then
  206. begin
  207. Done:= True;
  208. Break;
  209. end;
  210. end;
  211. if Done then Break;
  212. { Is there any text before ? }
  213. if (TextStart <> nil) and (P > TextStart) then
  214. begin
  215. L:= P - TextStart;
  216. { Yes, copy to buffer }
  217. FCurrent:=P;
  218. OnFoundText( CopyBuffer(TextStart, L) );
  219. end else
  220. begin
  221. TextStart:= nil;
  222. end;
  223. { No }
  224. TagStart:= P;
  225. while Not (P^ in [ '>', #0]) do
  226. begin
  227. // Find string in tag
  228. if (P^ = '"') or (P^ = '''') then
  229. begin
  230. C:= P^;
  231. Inc(P); Inc(I); // Skip current AnsiChar " or '
  232. // Skip until string end
  233. while Not (P^ in [C, #0]) do
  234. begin
  235. Inc(P);Inc(I);
  236. end;
  237. end;
  238. Inc(P);Inc(I);
  239. if I >= TL then
  240. begin
  241. Done:= True;
  242. Break;
  243. end;
  244. end;
  245. if Done then Break;
  246. { Copy this tag to buffer }
  247. L:= P - TagStart + 1;
  248. FCurrent:=P;
  249. OnFoundTag( uppercase(CopyBuffer(TagStart, L )), CopyBuffer(TagStart, L ) ); //L505: added uppercase
  250. Inc(P); Inc(I);
  251. if I >= TL then Break;
  252. until (Done);
  253. end;
  254. {$IFDEF DEBUGLN_ON}debugln('FastHtmlParser Exec End');{$ENDIF}
  255. end;
  256. end.