hpricot 0.8.2-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/CHANGELOG +88 -0
  2. data/COPYING +18 -0
  3. data/README +275 -0
  4. data/Rakefile +272 -0
  5. data/ext/fast_xs/FastXsService.java +1030 -0
  6. data/ext/fast_xs/extconf.rb +4 -0
  7. data/ext/fast_xs/fast_xs.c +201 -0
  8. data/ext/hpricot_scan/HpricotCss.java +831 -0
  9. data/ext/hpricot_scan/HpricotScanService.java +2086 -0
  10. data/ext/hpricot_scan/extconf.rb +6 -0
  11. data/ext/hpricot_scan/hpricot_common.rl +76 -0
  12. data/ext/hpricot_scan/hpricot_css.c +3503 -0
  13. data/ext/hpricot_scan/hpricot_css.java.rl +155 -0
  14. data/ext/hpricot_scan/hpricot_css.rl +115 -0
  15. data/ext/hpricot_scan/hpricot_scan.c +6927 -0
  16. data/ext/hpricot_scan/hpricot_scan.h +79 -0
  17. data/ext/hpricot_scan/hpricot_scan.java.rl +1152 -0
  18. data/ext/hpricot_scan/hpricot_scan.rl +788 -0
  19. data/extras/mingw-rbconfig.rb +176 -0
  20. data/lib/fast_xs.jar +0 -0
  21. data/lib/hpricot.rb +26 -0
  22. data/lib/hpricot/blankslate.rb +63 -0
  23. data/lib/hpricot/builder.rb +216 -0
  24. data/lib/hpricot/elements.rb +510 -0
  25. data/lib/hpricot/htmlinfo.rb +691 -0
  26. data/lib/hpricot/inspect.rb +103 -0
  27. data/lib/hpricot/modules.rb +40 -0
  28. data/lib/hpricot/parse.rb +38 -0
  29. data/lib/hpricot/tag.rb +219 -0
  30. data/lib/hpricot/tags.rb +164 -0
  31. data/lib/hpricot/traverse.rb +839 -0
  32. data/lib/hpricot/xchar.rb +94 -0
  33. data/lib/hpricot_scan.jar +0 -0
  34. data/test/files/basic.xhtml +17 -0
  35. data/test/files/boingboing.html +2266 -0
  36. data/test/files/cy0.html +3653 -0
  37. data/test/files/immob.html +400 -0
  38. data/test/files/pace_application.html +1320 -0
  39. data/test/files/tenderlove.html +16 -0
  40. data/test/files/uswebgen.html +220 -0
  41. data/test/files/utf8.html +1054 -0
  42. data/test/files/week9.html +1723 -0
  43. data/test/files/why.xml +19 -0
  44. data/test/load_files.rb +7 -0
  45. data/test/nokogiri-bench.rb +64 -0
  46. data/test/test_alter.rb +96 -0
  47. data/test/test_builder.rb +37 -0
  48. data/test/test_parser.rb +428 -0
  49. data/test/test_paths.rb +25 -0
  50. data/test/test_preserved.rb +88 -0
  51. data/test/test_xml.rb +28 -0
  52. metadata +112 -0
@@ -0,0 +1,88 @@
1
+ = 0.8.2
2
+ === 5 November, 2009
3
+ * Bring JRuby support up to speed, including Java-based hpricot_css support
4
+ * Change JRuby fast_xs to have same escaping behavior as C fast_xs
5
+
6
+ = 0.8.1
7
+ === 3 April, 2009
8
+ * fix for issue #2, downcasing of html attributes inside the parser.
9
+ * solve issue #3 with bogus etags being preserved in `to_s` rather than just `to_original_html`.
10
+ * fix error when attempting to reparent cleared node. (issue #5)
11
+ * Hpricot::Attributes proxy object for using `ele.attributes[k] = v` directly.
12
+ however, it is preferred to use the jquery-like `elements.attr(k, v)`.
13
+
14
+ = 0.8
15
+ === 31st March, 2009
16
+ * Saving memory and speed by using RStruct-based elements in the C extension.
17
+ * Bug in tag parsing, causing runaway <script> and <style> tags in HTML.
18
+ * Problem compiling under Ruby 1.9, due to our_rb_hash_lookup function meant for Ruby 1.8.
19
+ * CData was missing inner_text method.
20
+
21
+ = 0.7
22
+ === 17th March, 2009
23
+ * Rewritten parser routine, much lighter on memory, quite a bit faster.
24
+ * Friendlier with Ruby 1.9.
25
+ * Fixes to nth-child and text() selectors.
26
+
27
+ = 0.6
28
+ === 15th June, 2007
29
+ * Hpricot for JRuby -- nice work Ola Bini!
30
+ * Inline Markaby for Hpricot documents.
31
+ * XML tags and attributes are no longer downcased like HTML is.
32
+ * new syntax for grabbing everything between two elements using a Range in the search method: (doc/("font".."font/br")) or in nodes_at like so: (doc/"font").nodes_at("*".."br"). Only works with either a pair of siblings or a set of a parent and a sibling.
33
+ * Ignore self-closing endings on tags (such as form) which are containers. Treat them like open parent tags. Reported by Jonathan Nichols on the hpricot list.
34
+ * Escaping of attributes, yanked from Jim Weirich and Sam Ruby's work in Builder.
35
+ * Element#raw_attributes gives unescaped data. Element#attributes gives escaped.
36
+ * Added: Elements#attr, Elements#remove_attr, Elements#remove_class.
37
+ * Added: Traverse#preceding, Traverse#following, Traverse#previous, Traverse#next.
38
+
39
+ = 0.5
40
+ === 31rd January, 2007
41
+
42
+ * support for a[text()="Click Me!"] and h3[text()*="space"] and the like.
43
+ * Hpricot.buffer_size accessor for increasing Hpricot's buffer if you're encountering huge ASP.NET viewstate attribs.
44
+ * some support for colons in tag names (not full namespace support yet.)
45
+ * Element.to_original_html will attempt to preserve the original HTML while merging your changes.
46
+ * Element.to_plain_text converts an element's contents to a simple text format.
47
+ * Element.inner_text removes all tags and returns text nodes concatenated into a single string.
48
+ * no @raw_string variable kept for comments, text, and cdata -- as it's redundant.
49
+ * xpath-style indices (//p/a[1]) but keep in mind that they aren't zero-based.
50
+ * node_position is the index among all sibling nodes, while position is the position among children of identical type.
51
+ * comment() and text() search criteria, like: //p/text(), which selects all text inside paragraph tags.
52
+ * every element has css_path and xpath methods which return respective absolute paths.
53
+ * more flexibility all around: in parsing attributes, tags, comments and cdata.
54
+
55
+ = 0.4
56
+ === 11th August, 2006
57
+
58
+ * The :fixup_tags option will try to sort out the hierarchy so elements end up with the right parents.
59
+ * Elements such as *script* and *style* (identified as having CDATA contents) receive a single text node as their children now. Previously, Hpricot was parsing out tags found in scripts.
60
+ * Better scanning of partially quoted attributes (found by Brent Beardsly on http://uswebgen.com/)
61
+ * Better scanning of unquoted attributes -- thanks to Aaron Patterson for the test cases!
62
+ * Some tags were being output in the empty tag style, although browsers hated that. FIXED!
63
+ * Added Elements#at for finding single elements.
64
+ * Added Elem::Trav#[] and Elem::Trav#[]= for reading and writing attributes.
65
+
66
+ = 0.3
67
+ === 7th July, 2006
68
+
69
+ * Fixed negative string size error on empty tokens. (news.bbc.co.uk)
70
+ * Allow the parser to accept just text nodes. (such as: <tt>Hpricot.parse('TEXT')</tt>)
71
+ * from JQuery to Hpricot::Elements: remove, empty, append, prepend, before, after, wrap, set,
72
+ html(...), to_html, to_s.
73
+ * on containers: to_html, replace_child, insert_before, insert_after, innerHTML=.
74
+ * Hpricot(...) is an alias for parse.
75
+ * open up all properties to setters, let people do as they may.
76
+ * use to_html for the full html of a node or set of elements.
77
+ * doctypes were messed.
78
+
79
+ = 0.2
80
+ === 4th July, 2006
81
+
82
+ * Rewrote the HTree parser to be simpler, more adequate for the common man. Will add encoding back in later.
83
+
84
+ = 0.1
85
+ === 3rd July, 2006
86
+
87
+ * For whatever reason, wrote this HTML parser in C.
88
+ I guess Ragel is addictive and I want to improve HTree.
data/COPYING ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2006 why the lucky stiff
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,275 @@
1
+ = Hpricot, Read Any HTML
2
+
3
+ Hpricot is a fast, flexible HTML parser written in C. It's designed to be very
4
+ accommodating (like Tanaka Akira's HTree) and to have a very helpful library
5
+ (like some JavaScript libs -- JQuery, Prototype -- give you.) The XPath and CSS
6
+ parser, in fact, is based on John Resig's JQuery.
7
+
8
+ Also, Hpricot can be handy for reading broken XML files, since many of the same
9
+ techniques can be used. If a quote is missing, Hpricot tries to figure it out.
10
+ If tags overlap, Hpricot works on sorting them out. You know, that sort of
11
+ thing.
12
+
13
+ *Please read this entire document* before making assumptions about how this
14
+ software works.
15
+
16
+ == An Overview
17
+
18
+ Let's clear up what Hpricot is.
19
+
20
+ # Hpricot is *a standalone library*. It requires no other libraries. Just Ruby!
21
+ # While priding itself on speed, Hpricot *works hard to sort out bad HTML* and
22
+ pays a small penalty in order to get that right. So that's slightly more important
23
+ to me than speed.
24
+ # *If you can see it in Firefox, then Hpricot should parse it.* That's
25
+ how it should be! Let me know the minute it's otherwise.
26
+ # Primarily, Hpricot is used for reading HTML and tries to sort out troubled
27
+ HTML by having some idea of what good HTML is. Some people still like to use
28
+ Hpricot for XML reading, but *remember to use the Hpricot::XML() method* for that!
29
+
30
+ == The Hpricot Kingdom
31
+
32
+ First, here are all the links you need to know:
33
+
34
+ * http://wiki.github.com/hpricot/hpricot is the Hpricot wiki and
35
+ http://github.com/hpricot/hpricot/issues is the bug tracker.
36
+ Go there for news and recipes and patches. It's the center of activity.
37
+ * http://github.com/hpricot/hpricot is the main Git
38
+ repository for Hpricot. You can get the latest code there.
39
+ * See COPYING for the terms of this software. (Spoiler: it's absolutely free.)
40
+
41
+ If you have any trouble, don't hesitate to contact the author. As always, I'm
42
+ not going to say "Use at your own risk" because I don't want this library to be
43
+ risky. If you trip on something, I'll share the liability by repairing things
44
+ as quickly as I can. Your responsibility is to report the inadequacies.
45
+
46
+ == Installing Hpricot
47
+
48
+ You may get the latest stable version from Rubyforge. Win32 binaries,
49
+ Java binaries (for JRuby), and source gems are available.
50
+
51
+ $ gem install hpricot
52
+
53
+ == An Hpricot Showcase
54
+
55
+ We're going to run through a big pile of examples to get you jump-started.
56
+ Many of these examples are also found at
57
+ http://wiki.github.com/hpricot/hpricot/hpricot-basics, in case you
58
+ want to add some of your own.
59
+
60
+ === Loading Hpricot Itself
61
+
62
+ You have probably got the gem, right? To load Hpricot:
63
+
64
+ require 'rubygems'
65
+ require 'hpricot'
66
+
67
+ If you've installed the plain source distribution, go ahead and just:
68
+
69
+ require 'hpricot'
70
+
71
+ === Load an HTML Page
72
+
73
+ The <tt>Hpricot()</tt> method takes a string or any IO object and loads the
74
+ contents into a document object.
75
+
76
+ doc = Hpricot("<p>A simple <b>test</b> string.</p>")
77
+
78
+ To load from a file, just get the stream open:
79
+
80
+ doc = open("index.html") { |f| Hpricot(f) }
81
+
82
+ To load from a web URL, use <tt>open-uri</tt>, which comes with Ruby:
83
+
84
+ require 'open-uri'
85
+ doc = open("http://qwantz.com/") { |f| Hpricot(f) }
86
+
87
+ Hpricot uses an internal buffer to parse the file, so the IO will stream
88
+ properly and large documents won't be loaded into memory all at once. However,
89
+ the parsed document object will be present in memory, in its entirety.
90
+
91
+ === Search for Elements
92
+
93
+ Use <tt>Doc.search</tt>:
94
+
95
+ doc.search("//p[@class='posted']")
96
+ #=> #<Hpricot:Elements[{p ...}, {p ...}]>
97
+
98
+ <tt>Doc.search</tt> can take an XPath or CSS expression. In the above example,
99
+ all paragraph <tt><p></tt> elements are grabbed which have a <tt>class</tt>
100
+ attribute of <tt>"posted"</tt>.
101
+
102
+ A shortcut is to use the divisor:
103
+
104
+ (doc/"p.posted")
105
+ #=> #<Hpricot:Elements[{p ...}, {p ...}]>
106
+
107
+ === Finding Just One Element
108
+
109
+ If you're looking for a single element, the <tt>at</tt> method will return the
110
+ first element matched by the expression. In this case, you'll get back the
111
+ element itself rather than the <tt>Hpricot::Elements</tt> array.
112
+
113
+ doc.at("body")['onload']
114
+
115
+ The above code will find the body tag and give you back the <tt>onload</tt>
116
+ attribute. This is the most common reason to use the element directly: when
117
+ reading and writing HTML attributes.
118
+
119
+ === Fetching the Contents of an Element
120
+
121
+ Just as with browser scripting, the <tt>inner_html</tt> property can be used to
122
+ get the inner contents of an element.
123
+
124
+ (doc/"#elementID").inner_html
125
+ #=> "..<b>contents</b>.."
126
+
127
+ If your expression matches more than one element, you'll get back the contents
128
+ of ''all the matched elements''. So you may want to use <tt>first</tt> to be
129
+ sure you get back only one.
130
+
131
+ (doc/"#elementID").first.inner_html
132
+ #=> "..<b>contents</b>.."
133
+
134
+ === Fetching the HTML for an Element
135
+
136
+ If you want the HTML for the whole element (not just the contents), use
137
+ <tt>to_html</tt>:
138
+
139
+ (doc/"#elementID").to_html
140
+ #=> "<div id='elementID'>...</div>"
141
+
142
+ === Looping
143
+
144
+ All searches return a set of <tt>Hpricot::Elements</tt>. Go ahead and loop
145
+ through them like you would an array.
146
+
147
+ (doc/"p/a/img").each do |img|
148
+ puts img.attributes['class']
149
+ end
150
+
151
+ === Continuing Searches
152
+
153
+ Searches can be continued from a collection of elements, in order to search deeper.
154
+
155
+ # find all paragraphs.
156
+ elements = doc.search("/html/body//p")
157
+ # continue the search by finding any images within those paragraphs.
158
+ (elements/"img")
159
+ #=> #<Hpricot::Elements[{img ...}, {img ...}]>
160
+
161
+ Searches can also be continued by searching within container elements.
162
+
163
+ # find all images within paragraphs.
164
+ doc.search("/html/body//p").each do |para|
165
+ puts "== Found a paragraph =="
166
+ pp para
167
+
168
+ imgs = para.search("img")
169
+ if imgs.any?
170
+ puts "== Found #{imgs.length} images inside =="
171
+ end
172
+ end
173
+
174
+ Of course, the most succinct ways to do the above are using CSS or XPath.
175
+
176
+ # the xpath version
177
+ (doc/"/html/body//p//img")
178
+ # the css version
179
+ (doc/"html > body > p img")
180
+ # ..or symbols work, too!
181
+ (doc/:html/:body/:p/:img)
182
+
183
+ === Looping Edits
184
+
185
+ You may certainly edit objects from within your search loops. Then, when you
186
+ spit out the HTML, the altered elements will show.
187
+
188
+ (doc/"span.entryPermalink").each do |span|
189
+ span.attributes['class'] = 'newLinks'
190
+ end
191
+ puts doc
192
+
193
+ This changes all <tt>span.entryPermalink</tt> elements to
194
+ <tt>span.newLinks</tt>. Keep in mind that there are often more convenient ways
195
+ of doing this. Such as the <tt>set</tt> method:
196
+
197
+ (doc/"span.entryPermalink").set(:class => 'newLinks')
198
+
199
+ === Figuring Out Paths
200
+
201
+ Every element can tell you its unique path (either XPath or CSS) to get to the
202
+ element from the root tag.
203
+
204
+ The <tt>css_path</tt> method:
205
+
206
+ doc.at("div > div:nth(1)").css_path
207
+ #=> "div > div:nth(1)"
208
+ doc.at("#header").css_path
209
+ #=> "#header"
210
+
211
+ Or, the <tt>xpath</tt> method:
212
+
213
+ doc.at("div > div:nth(1)").xpath
214
+ #=> "/div/div:eq(1)"
215
+ doc.at("#header").xpath
216
+ #=> "//div[@id='header']"
217
+
218
+ == Hpricot Fixups
219
+
220
+ When loading HTML documents, you have a few settings that can make Hpricot more
221
+ or less intense about how it gets involved.
222
+
223
+ == :fixup_tags
224
+
225
+ Really, there are so many ways to clean up HTML and your intentions may be to
226
+ keep the HTML as-is. So Hpricot's default behavior is to keep things flexible.
227
+ Making sure to open and close all the tags, but ignore any validation problems.
228
+
229
+ As of Hpricot 0.4, there's a new <tt>:fixup_tags</tt> option which will attempt
230
+ to shift the document's tags to meet XHTML 1.0 Strict.
231
+
232
+ doc = open("index.html") { |f| Hpricot f, :fixup_tags => true }
233
+
234
+ This doesn't quite meet the XHTML 1.0 Strict standard, it just tries to follow
235
+ the rules a bit better. Like: say Hpricot finds a paragraph in a link, it's
236
+ going to move the paragraph below the link. Or up and out of other elements
237
+ where paragraphs don't belong.
238
+
239
+ If an unknown element is found, it is ignored. Again, <tt>:fixup_tags</tt>.
240
+
241
+ == :xhtml_strict
242
+
243
+ So, let's go beyond just trying to fix the hierarchy. The
244
+ <tt>:xhtml_strict</tt> option really tries to force the document to be an XHTML
245
+ 1.0 Strict document. Even at the cost of removing elements that get in the way.
246
+
247
+ doc = open("index.html") { |f| Hpricot f, :xhtml_strict => true }
248
+
249
+ What measures does <tt>:xhtml_strict</tt> take?
250
+
251
+ 1. Shift elements into their proper containers just like :fixup_tags.
252
+ 2. Remove unknown elements.
253
+ 3. Remove unknown attributes.
254
+ 4. Remove illegal content.
255
+ 5. Alter the doctype to XHTML 1.0 Strict.
256
+
257
+ == Hpricot.XML()
258
+
259
+ The last option is the <tt>:xml</tt> option, which makes some slight variations
260
+ on the standard mode. The main difference is that :xml mode won't try to output
261
+ tags which are friendlier for browsers. For example, if an opening and closing
262
+ <tt>br</tt> tag is found, XML mode won't try to turn that into an empty element.
263
+
264
+ XML mode also doesn't downcase the tags and attributes for you. So pay attention
265
+ to case, friends.
266
+
267
+ The primary way to use Hpricot's XML mode is to call the Hpricot.XML method:
268
+
269
+ doc = open("http://redhanded.hobix.com/index.xml") do |f|
270
+ Hpricot.XML(f)
271
+ end
272
+
273
+ *Also, :fixup_tags is canceled out by the :xml option.* This is because
274
+ :fixup_tags makes assumptions based how HTML is structured. Specifically, how
275
+ tags are defined in the XHTML 1.0 DTD.
@@ -0,0 +1,272 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'rake/gempackagetask'
4
+ require 'rake/rdoctask'
5
+ require 'rake/testtask'
6
+ require 'fileutils'
7
+ include FileUtils
8
+
9
+ RbConfig = Config unless defined?(RbConfig)
10
+
11
+ NAME = "hpricot"
12
+ REV = (`#{ENV['GIT'] || "git"} rev-list HEAD`.split.length + 1).to_s
13
+ VERS = ENV['VERSION'] || "0.8" + (REV ? ".#{REV}" : "")
14
+ PKG = "#{NAME}-#{VERS}"
15
+ BIN = "*.{bundle,jar,so,o,obj,pdb,lib,def,exp,class}"
16
+ CLEAN.include ["ext/hpricot_scan/#{BIN}", "ext/fast_xs/#{BIN}", "lib/**/#{BIN}",
17
+ 'ext/fast_xs/Makefile', 'ext/hpricot_scan/Makefile',
18
+ '**/.*.sw?', '*.gem', '.config', 'pkg']
19
+ RDOC_OPTS = ['--quiet', '--title', 'The Hpricot Reference', '--main', 'README', '--inline-source']
20
+ PKG_FILES = %w(CHANGELOG COPYING README Rakefile) +
21
+ Dir.glob("{bin,doc,test,lib,extras}/**/*") +
22
+ Dir.glob("ext/**/*.{h,java,c,rb,rl}") +
23
+ %w[ext/hpricot_scan/hpricot_scan.c ext/hpricot_scan/hpricot_css.c ext/hpricot_scan/HpricotScanService.java] # needed because they are generated later
24
+ RAGEL_C_CODE_GENERATION_STYLES = {
25
+ "table_driven" => 'T0',
26
+ "faster_table_driven" => 'T1',
27
+ "flat_table_driven" => 'F0',
28
+ "faster_flat_table_driven" => 'F1',
29
+ "goto_driven" => 'G0',
30
+ "faster_goto_driven" => 'G1',
31
+ "really_fast goto_driven" => 'G2'
32
+ # "n_way_split_really_fast_goto_driven" => 'P<N>'
33
+ }
34
+ DEFAULT_RAGEL_C_CODE_GENERATION = "really_fast goto_driven"
35
+ SPEC =
36
+ Gem::Specification.new do |s|
37
+ s.name = NAME
38
+ s.version = VERS
39
+ s.platform = Gem::Platform::RUBY
40
+ s.has_rdoc = true
41
+ s.rdoc_options += RDOC_OPTS
42
+ s.extra_rdoc_files = ["README", "CHANGELOG", "COPYING"]
43
+ s.summary = "a swift, liberal HTML parser with a fantastic library"
44
+ s.description = s.summary
45
+ s.author = "why the lucky stiff"
46
+ s.email = 'why@ruby-lang.org'
47
+ s.homepage = 'http://code.whytheluckystiff.net/hpricot/'
48
+ s.rubyforge_project = 'hobix'
49
+ s.files = PKG_FILES
50
+ s.require_paths = ["lib"]
51
+ s.extensions = FileList["ext/**/extconf.rb"].to_a
52
+ s.bindir = "bin"
53
+ end
54
+
55
+ Win32Spec = SPEC.dup
56
+ Win32Spec.platform = 'x86-mswin32'
57
+ Win32Spec.files = PKG_FILES + ["lib/hpricot_scan.so", "lib/fast_xs.so"]
58
+ Win32Spec.extensions = []
59
+
60
+ WIN32_PKG_DIR = "#{PKG}-mswin32"
61
+
62
+ desc "Does a full compile, test run"
63
+ if defined?(JRUBY_VERSION)
64
+ task :default => [:compile_java, :test]
65
+ else
66
+ task :default => [:compile, :test]
67
+ end
68
+
69
+ desc "Packages up Hpricot."
70
+ task :package => [:clean, :ragel]
71
+
72
+ desc "Releases packages for all Hpricot packages and platforms."
73
+ task :release => [:package, :package_win32, :package_jruby]
74
+
75
+
76
+ desc "Run all the tests"
77
+ Rake::TestTask.new do |t|
78
+ t.libs << "test"
79
+ t.test_files = FileList['test/test_*.rb']
80
+ t.verbose = true
81
+ end
82
+
83
+ Rake::RDocTask.new do |rdoc|
84
+ rdoc.rdoc_dir = 'doc/rdoc'
85
+ rdoc.options += RDOC_OPTS
86
+ rdoc.main = "README"
87
+ rdoc.rdoc_files.add ['README', 'CHANGELOG', 'COPYING', 'lib/**/*.rb']
88
+ end
89
+
90
+ Rake::GemPackageTask.new(SPEC) do |p|
91
+ p.need_tar = true
92
+ p.gem_spec = SPEC
93
+ end
94
+
95
+ ['hpricot_scan', 'fast_xs'].each do |extension|
96
+ ext = "ext/#{extension}"
97
+ ext_so = "#{ext}/#{extension}.#{Config::CONFIG['DLEXT']}"
98
+ ext_files = FileList[
99
+ "#{ext}/*.c",
100
+ "#{ext}/*.h",
101
+ "#{ext}/*.rl",
102
+ "#{ext}/extconf.rb",
103
+ "#{ext}/Makefile",
104
+ "lib"
105
+ ]
106
+
107
+ desc "Builds just the #{extension} extension"
108
+ task extension.to_sym => [:ragel, "#{ext}/Makefile", ext_so ]
109
+
110
+ file "#{ext}/Makefile" => ["#{ext}/extconf.rb"] do
111
+ Dir.chdir(ext) do ruby "extconf.rb" end
112
+ end
113
+
114
+ file ext_so => ext_files do
115
+ Dir.chdir(ext) do
116
+ sh(RUBY_PLATFORM =~ /mswin/ ? 'nmake' : 'make')
117
+ end
118
+ cp ext_so, "lib"
119
+ end
120
+
121
+ desc "Cross-compile the #{extension} extension for win32"
122
+ file "#{extension}_win32" => [WIN32_PKG_DIR] do
123
+ cp "extras/mingw-rbconfig.rb", "#{WIN32_PKG_DIR}/ext/#{extension}/rbconfig.rb"
124
+ sh "cd #{WIN32_PKG_DIR}/ext/#{extension}/ && ruby -I. extconf.rb && make"
125
+ mv "#{WIN32_PKG_DIR}/ext/#{extension}/#{extension}.so", "#{WIN32_PKG_DIR}/lib"
126
+ end
127
+ end
128
+
129
+ task "lib" do
130
+ directory "lib"
131
+ end
132
+
133
+ desc "Compiles the Ruby extension"
134
+ task :compile => [:hpricot_scan, :fast_xs] do
135
+ if Dir.glob(File.join("lib","hpricot_scan.*")).length == 0
136
+ STDERR.puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
137
+ STDERR.puts "Gem actually failed to build. Your system is"
138
+ STDERR.puts "NOT configured properly to build hpricot."
139
+ STDERR.puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
140
+ exit(1)
141
+ end
142
+ end
143
+
144
+ desc "Determines the Ragel version and displays it on the console along with the location of the Ragel binary."
145
+ task :ragel_version do
146
+ @ragel_v = `ragel -v`[/(version )(\S*)/,2].to_f
147
+ puts "Using ragel version: #{@ragel_v}, location: #{`which ragel`}"
148
+ @ragel_v
149
+ end
150
+
151
+ desc "Generates the C scanner code with Ragel."
152
+ task :ragel => [:ragel_version] do
153
+ if @ragel_v >= 6.1
154
+ @ragel_c_code_generation_style = RAGEL_C_CODE_GENERATION_STYLES[DEFAULT_RAGEL_C_CODE_GENERATION]
155
+ Dir.chdir("ext/hpricot_scan") do
156
+ sh %{ragel hpricot_scan.rl -#{@ragel_c_code_generation_style} -o hpricot_scan.c}
157
+ sh %{ragel hpricot_css.rl -#{@ragel_c_code_generation_style} -o hpricot_css.c}
158
+ end
159
+ else
160
+ STDERR.puts "Ragel 6.1 or greater is required."
161
+ exit(1)
162
+ end
163
+ end
164
+
165
+ # Java only supports the table-driven code
166
+ # generation style at this point.
167
+ desc "Generates the Java scanner code using the Ragel table-driven code generation style."
168
+ task :ragel_java => [:ragel_version] do
169
+ if @ragel_v >= 6.1
170
+ puts "compiling with ragel version #{@ragel_v}"
171
+ Dir.chdir("ext/hpricot_scan") do
172
+ sh %{ragel -J -o HpricotCss.java hpricot_css.java.rl}
173
+ sh %{ragel -J -o HpricotScanService.java hpricot_scan.java.rl}
174
+ end
175
+ else
176
+ STDERR.puts "Ragel 6.1 or greater is required."
177
+ exit(1)
178
+ end
179
+ end
180
+
181
+ ### Win32 Packages ###
182
+
183
+ desc "Package up the Win32 distribution."
184
+ file WIN32_PKG_DIR => [:package] do
185
+ sh "tar zxf pkg/#{PKG}.tgz"
186
+ mv PKG, WIN32_PKG_DIR
187
+ end
188
+
189
+ desc "Build the binary RubyGems package for win32"
190
+ task :package_win32 => ["fast_xs_win32", "hpricot_scan_win32"] do
191
+ Dir.chdir("#{WIN32_PKG_DIR}") do
192
+ Gem::Builder.new(Win32Spec).build
193
+ verbose(true) {
194
+ mv Dir["*.gem"].first, "../pkg/"
195
+ }
196
+ end
197
+ end
198
+
199
+ CLEAN.include WIN32_PKG_DIR
200
+
201
+ ### JRuby Packages ###
202
+
203
+ def java_classpath_arg # myriad of ways to discover JRuby classpath
204
+ begin
205
+ cpath = Java::java.lang.System.getProperty('java.class.path').split(File::PATH_SEPARATOR)
206
+ cpath += Java::java.lang.System.getProperty('sun.boot.class.path').split(File::PATH_SEPARATOR)
207
+ jruby_cpath = cpath.compact.join(File::PATH_SEPARATOR)
208
+ rescue => e
209
+ end
210
+ unless jruby_cpath
211
+ jruby_cpath = ENV['JRUBY_PARENT_CLASSPATH'] || ENV['JRUBY_HOME'] &&
212
+ FileList["#{ENV['JRUBY_HOME']}/lib/*.jar"].join(File::PATH_SEPARATOR)
213
+ end
214
+ jruby_cpath ? "-cp \"#{jruby_cpath}\"" : ""
215
+ end
216
+
217
+ def compile_java(filenames, jarname)
218
+ sh %{javac -source 1.5 -target 1.5 #{java_classpath_arg} #{filenames.join(" ")}}
219
+ sh %{jar cf #{jarname} *.class}
220
+ end
221
+
222
+ task :hpricot_scan_java => [:ragel_java] do
223
+ Dir.chdir "ext/hpricot_scan" do
224
+ compile_java(["HpricotScanService.java", "HpricotCss.java"], "hpricot_scan.jar")
225
+ end
226
+ end
227
+
228
+ task :fast_xs_java do
229
+ Dir.chdir "ext/fast_xs" do
230
+ compile_java(["FastXsService.java"], "fast_xs.jar")
231
+ end
232
+ end
233
+
234
+ desc "Compiles the JRuby extensions"
235
+ task :compile_java => [:hpricot_scan_java, :fast_xs_java] do
236
+ %w(hpricot_scan fast_xs).each {|ext| mv "ext/#{ext}/#{ext}.jar", "lib"}
237
+ end
238
+
239
+ JRubySpec = SPEC.dup
240
+ JRubySpec.platform = 'java'
241
+ JRubySpec.files = PKG_FILES + ["lib/hpricot_scan.jar", "lib/fast_xs.jar"]
242
+ JRubySpec.extensions = []
243
+
244
+ JRUBY_PKG_DIR = "#{PKG}-java"
245
+
246
+ desc "Package up the JRuby distribution."
247
+ file JRUBY_PKG_DIR => [:ragel_java, :package] do
248
+ sh "tar zxf pkg/#{PKG}.tgz"
249
+ mv PKG, JRUBY_PKG_DIR
250
+ end
251
+
252
+ desc "Build the RubyGems package for JRuby"
253
+ task :package_jruby => JRUBY_PKG_DIR do
254
+ Dir.chdir("#{JRUBY_PKG_DIR}") do
255
+ Rake::Task[:compile_java].invoke
256
+ Gem::Builder.new(JRubySpec).build
257
+ verbose(true) {
258
+ mv Dir["*.gem"].first, "../pkg/#{JRUBY_PKG_DIR}.gem"
259
+ }
260
+ end
261
+ end
262
+
263
+ CLEAN.include JRUBY_PKG_DIR
264
+
265
+ task :install do
266
+ sh %{rake package}
267
+ sh %{sudo gem install pkg/#{NAME}-#{VERS}}
268
+ end
269
+
270
+ task :uninstall => [:clean] do
271
+ sh %{sudo gem uninstall #{NAME}}
272
+ end