fasthtmlparser.pas 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. unit FastHTMLParser;
  86. interface
  87. uses
  88. {$IFDEF KOL_MCK}
  89. KOL;
  90. {$else}
  91. SysUtils;
  92. {$ENDIF}
  93. {$IFDEF DEBUGLN_ON}
  94. // dummy, default debugging
  95. procedure debugproc(s: string);
  96. // for custom debugging, assign this in your units
  97. var debugln: procedure(s: string) = debugproc;
  98. {$ENDIF}
  99. type
  100. // when tag content found in HTML, including names and values
  101. // case insensitive analysis available via NoCaseTag
  102. TOnFoundTag = procedure(NoCaseTag, ActualTag: string) of object;
  103. // when text found in the HTML
  104. TOnFoundText = procedure(Text: string) of object;
  105. // Lars's modified html parser, case insensitive or case sensitive
  106. { THTMLParser }
  107. THTMLParser = class(TObject)
  108. private
  109. FDone: Boolean;
  110. public
  111. OnFoundTag: TOnFoundTag;
  112. OnFoundText: TOnFoundText;
  113. Raw: Pchar;
  114. FCurrent : PChar;
  115. constructor Create(sRaw: string);overload;
  116. constructor Create(pRaw: PChar);overload;
  117. procedure Exec;
  118. procedure NilOnFoundTag(NoCaseTag, ActualTag: string);
  119. procedure NilOnFoundText(Text: string);
  120. Public
  121. Function CurrentPos : Integer;
  122. property Done: Boolean read FDone write FDone;
  123. end;
  124. implementation
  125. // default debugging, do nothing, let user do his own by assigning DebugLn var
  126. procedure debugproc(s: string);
  127. begin
  128. end;
  129. function CopyBuffer(StartIndex: PChar; Length: Integer): string;
  130. begin
  131. SetLength(Result, Length);
  132. StrLCopy(@Result[1], StartIndex, Length);
  133. end;
  134. { ************************ THTMLParser ************************************** }
  135. constructor THTMLParser.Create(pRaw: Pchar);
  136. begin
  137. if pRaw = '' then exit;
  138. if pRaw = nil then exit;
  139. Raw:= pRaw;
  140. end;
  141. constructor THTMLParser.Create(sRaw: string);
  142. begin
  143. if sRaw = '' then exit;
  144. Raw:= Pchar(sRaw);
  145. end;
  146. { default dummy "do nothing" events if events are unassigned }
  147. procedure THTMLParser.NilOnFoundTag(NoCaseTag, ActualTag: string);
  148. begin
  149. end;
  150. procedure THTMLParser.NilOnFoundText(Text: string);
  151. begin
  152. end;
  153. function THTMLParser.CurrentPos: Integer;
  154. begin
  155. if Assigned(Raw) and Assigned(FCurrent) then
  156. Result:=FCurrent-Raw
  157. else
  158. Result:=0;
  159. end;
  160. procedure THTMLParser.Exec;
  161. var
  162. L: Integer;
  163. TL: Integer;
  164. I: Integer;
  165. TagStart,
  166. TextStart,
  167. P: PChar; // Pointer to current char.
  168. C: Char;
  169. begin
  170. {$IFDEF DEBUGLN_ON}debugln('FastHtmlParser Exec Begin');{$ENDIF}
  171. { set nil events once rather than checking for nil each time tag is found }
  172. if not assigned(OnFoundText) then
  173. OnFoundText:= NilOnFoundText;
  174. if not assigned(OnFoundTag) then
  175. OnFoundTag:= NilOnFoundTag;
  176. TL:= StrLen(Raw);
  177. I:= 0;
  178. P:= Raw;
  179. Done:= False;
  180. if P <> nil then
  181. begin
  182. TagStart:= nil;
  183. repeat
  184. TextStart:= P;
  185. { Get next tag position }
  186. while Not (P^ in [ '<', #0 ]) do
  187. begin
  188. Inc(P); Inc(I);
  189. if I >= TL then
  190. begin
  191. Done:= True;
  192. Break;
  193. end;
  194. end;
  195. if Done then Break;
  196. { Is there any text before ? }
  197. if (TextStart <> nil) and (P > TextStart) then
  198. begin
  199. L:= P - TextStart;
  200. { Yes, copy to buffer }
  201. FCurrent:=P;
  202. OnFoundText( CopyBuffer(TextStart, L) );
  203. end else
  204. begin
  205. TextStart:= nil;
  206. end;
  207. { No }
  208. TagStart:= P;
  209. while Not (P^ in [ '>', #0]) do
  210. begin
  211. // Find string in tag
  212. if (P^ = '"') or (P^ = '''') then
  213. begin
  214. C:= P^;
  215. Inc(P); Inc(I); // Skip current char " or '
  216. // Skip until string end
  217. while Not (P^ in [C, #0]) do
  218. begin
  219. Inc(P);Inc(I);
  220. end;
  221. end;
  222. Inc(P);Inc(I);
  223. if I >= TL then
  224. begin
  225. Done:= True;
  226. Break;
  227. end;
  228. end;
  229. if Done then Break;
  230. { Copy this tag to buffer }
  231. L:= P - TagStart + 1;
  232. FCurrent:=P;
  233. OnFoundTag( uppercase(CopyBuffer(TagStart, L )), CopyBuffer(TagStart, L ) ); //L505: added uppercase
  234. Inc(P); Inc(I);
  235. if I >= TL then Break;
  236. until (Done);
  237. end;
  238. {$IFDEF DEBUGLN_ON}debugln('FastHtmlParser Exec End');{$ENDIF}
  239. end;
  240. end.