fasthtmlparser.pas 8.6 KB

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