microformats2 1.0.2 → 2.0.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/Guardfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +117 -26
- data/Rakefile +65 -9
- data/lib/microformats2.rb +30 -115
- data/lib/microformats2/collection.rb +71 -0
- data/lib/microformats2/format.rb +101 -0
- data/lib/microformats2/format_parser.rb +59 -0
- data/lib/microformats2/implied_property/foundation.rb +58 -0
- data/lib/microformats2/implied_property/name.rb +34 -0
- data/lib/microformats2/implied_property/photo.rb +24 -0
- data/lib/microformats2/implied_property/url.rb +20 -0
- data/lib/microformats2/property.rb +10 -0
- data/lib/microformats2/property/date_time.rb +31 -0
- data/lib/microformats2/property/embedded.rb +9 -0
- data/lib/microformats2/property/foundation.rb +80 -0
- data/lib/microformats2/property/text.rb +16 -0
- data/lib/microformats2/property/url.rb +18 -0
- data/lib/microformats2/property_parser.rb +54 -0
- data/lib/microformats2/version.rb +3 -0
- data/microformats2.gemspec +30 -0
- data/spec/lib/microformats2/collection_spec.rb +181 -0
- data/spec/lib/microformats2/implied_property/name_spec.rb +31 -0
- data/spec/lib/microformats2/implied_property/photo_spec.rb +31 -0
- data/spec/lib/microformats2/implied_property/url_spec.rb +31 -0
- data/spec/lib/microformats2_spec.rb +30 -0
- data/spec/spec_helper.rb +10 -0
- data/spec/support/cases/microformat2-node.jit.su/h-adr/h-adr-0.html +3 -0
- data/spec/support/cases/microformat2-node.jit.su/h-adr/h-adr-0.js +10 -0
- data/spec/support/cases/microformat2-node.jit.su/h-adr/h-adr-1.html +10 -0
- data/spec/support/cases/microformat2-node.jit.su/h-adr/h-adr-1.js +16 -0
- data/spec/support/cases/microformat2-node.jit.su/h-adr/h-adr-2.html +12 -0
- data/spec/support/cases/microformat2-node.jit.su/h-adr/h-adr-2.js +16 -0
- data/spec/support/cases/microformat2-node.jit.su/h-adr/h-adr-3.html +6 -0
- data/spec/support/cases/microformat2-node.jit.su/h-adr/h-adr-3.js +12 -0
- data/spec/support/cases/microformat2-node.jit.su/h-card/h-card-0.html +3 -0
- data/spec/support/cases/microformat2-node.jit.su/h-card/h-card-0.js +10 -0
- data/spec/support/cases/microformat2-node.jit.su/h-card/h-card-1.html +3 -0
- data/spec/support/cases/microformat2-node.jit.su/h-card/h-card-1.js +11 -0
- data/spec/support/cases/microformat2-node.jit.su/h-card/h-card-2.html +4 -0
- data/spec/support/cases/microformat2-node.jit.su/h-card/h-card-2.js +12 -0
- data/spec/support/cases/microformat2-node.jit.su/h-card/h-card-3.html +11 -0
- data/spec/support/cases/microformat2-node.jit.su/h-card/h-card-3.js +15 -0
- data/spec/support/cases/microformat2-node.jit.su/h-card/h-card-4.html +6 -0
- data/spec/support/cases/microformat2-node.jit.su/h-card/h-card-4.js +26 -0
- data/spec/support/cases/microformat2-node.jit.su/h-card/h-card-5.html +6 -0
- data/spec/support/cases/microformat2-node.jit.su/h-card/h-card-5.js +30 -0
- data/spec/support/cases/microformat2-node.jit.su/h-card/h-card-6.html +6 -0
- data/spec/support/cases/microformat2-node.jit.su/h-card/h-card-6.js +29 -0
- data/spec/support/cases/microformat2-node.jit.su/h-card/h-card-7.html +16 -0
- data/spec/support/cases/microformat2-node.jit.su/h-card/h-card-7.js +15 -0
- data/spec/support/cases/microformat2-node.jit.su/h-entry/h-entry-0.html +4 -0
- data/spec/support/cases/microformat2-node.jit.su/h-entry/h-entry-0.js +10 -0
- data/spec/support/cases/microformat2-node.jit.su/h-entry/h-entry-1.html +3 -0
- data/spec/support/cases/microformat2-node.jit.su/h-entry/h-entry-1.js +11 -0
- data/spec/support/cases/microformat2-node.jit.su/h-entry/h-entry-2.html +21 -0
- data/spec/support/cases/microformat2-node.jit.su/h-entry/h-entry-2.js +28 -0
- data/spec/support/cases/microformat2-node.jit.su/h-entry/h-entry-3.html +22 -0
- data/spec/support/cases/microformat2-node.jit.su/h-entry/h-entry-3.js +21 -0
- data/spec/support/cases/microformat2-node.jit.su/h-event/h-event-0.html +3 -0
- data/spec/support/cases/microformat2-node.jit.su/h-event/h-event-0.js +10 -0
- data/spec/support/cases/microformat2-node.jit.su/h-event/h-event-1.html +3 -0
- data/spec/support/cases/microformat2-node.jit.su/h-event/h-event-1.js +11 -0
- data/spec/support/cases/microformat2-node.jit.su/h-event/h-event-2.html +14 -0
- data/spec/support/cases/microformat2-node.jit.su/h-event/h-event-2.js +57 -0
- data/spec/support/cases/microformat2-node.jit.su/h-event/h-event-3.html +15 -0
- data/spec/support/cases/microformat2-node.jit.su/h-event/h-event-3.js +35 -0
- data/spec/support/cases/microformat2-node.jit.su/h-event/h-event-4.html +9 -0
- data/spec/support/cases/microformat2-node.jit.su/h-event/h-event-4.js +12 -0
- data/spec/support/cases/microformat2-node.jit.su/h-event/h-event-5.html +45 -0
- data/spec/support/cases/microformat2-node.jit.su/h-event/h-event-5.js +22 -0
- data/spec/support/cases/microformat2-node.jit.su/h-event/h-event-6.html +51 -0
- data/spec/support/cases/microformat2-node.jit.su/h-event/h-event-6.js +21 -0
- data/spec/support/cases/microformat2-node.jit.su/h-event/h-event-7.html +22 -0
- data/spec/support/cases/microformat2-node.jit.su/h-event/h-event-7.js +20 -0
- data/spec/support/cases/microformat2-node.jit.su/h-geo/h-geo-0.html +5 -0
- data/spec/support/cases/microformat2-node.jit.su/h-geo/h-geo-0.js +10 -0
- data/spec/support/cases/microformat2-node.jit.su/h-geo/h-geo-1.html +7 -0
- data/spec/support/cases/microformat2-node.jit.su/h-geo/h-geo-1.js +12 -0
- data/spec/support/cases/microformat2-node.jit.su/h-geo/h-geo-2.html +10 -0
- data/spec/support/cases/microformat2-node.jit.su/h-geo/h-geo-2.js +13 -0
- data/spec/support/cases/microformat2-node.jit.su/h-geo/h-geo-3.html +12 -0
- data/spec/support/cases/microformat2-node.jit.su/h-geo/h-geo-3.js +12 -0
- data/spec/support/cases/microformat2-node.jit.su/h-geo/h-geo-4.html +12 -0
- data/spec/support/cases/microformat2-node.jit.su/h-geo/h-geo-4.js +12 -0
- data/spec/support/cases/microformat2-node.jit.su/h-geo/h-geo-5.html +6 -0
- data/spec/support/cases/microformat2-node.jit.su/h-geo/h-geo-5.js +12 -0
- data/spec/support/cases/microformat2-node.jit.su/h-news/h-news-0.html +26 -0
- data/spec/support/cases/microformat2-node.jit.su/h-news/h-news-0.js +37 -0
- data/spec/support/cases/microformat2-node.jit.su/h-news/h-news-1.html +37 -0
- data/spec/support/cases/microformat2-node.jit.su/h-news/h-news-1.js +48 -0
- data/spec/support/cases/microformat2-node.jit.su/h-org/h-org-0.html +3 -0
- data/spec/support/cases/microformat2-node.jit.su/h-org/h-org-0.js +10 -0
- data/spec/support/cases/microformat2-node.jit.su/h-org/h-org-1.html +3 -0
- data/spec/support/cases/microformat2-node.jit.su/h-org/h-org-1.js +11 -0
- data/spec/support/cases/microformat2-node.jit.su/h-org/h-org-2.html +6 -0
- data/spec/support/cases/microformat2-node.jit.su/h-org/h-org-2.js +12 -0
- data/spec/support/cases/microformat2-node.jit.su/h-product/h-product-0.html +3 -0
- data/spec/support/cases/microformat2-node.jit.su/h-product/h-product-0.js +10 -0
- data/spec/support/cases/microformat2-node.jit.su/h-product/h-product-1.html +3 -0
- data/spec/support/cases/microformat2-node.jit.su/h-product/h-product-1.js +11 -0
- data/spec/support/cases/microformat2-node.jit.su/h-product/h-product-2.html +11 -0
- data/spec/support/cases/microformat2-node.jit.su/h-product/h-product-2.js +23 -0
- data/spec/support/cases/microformat2-node.jit.su/h-product/h-product-3.html +22 -0
- data/spec/support/cases/microformat2-node.jit.su/h-product/h-product-3.js +42 -0
- data/spec/support/cases/microformat2-node.jit.su/h-recipe/h-recipe-0.html +9 -0
- data/spec/support/cases/microformat2-node.jit.su/h-recipe/h-recipe-0.js +11 -0
- data/spec/support/cases/microformat2-node.jit.su/h-recipe/h-recipe-1.html +67 -0
- data/spec/support/cases/microformat2-node.jit.su/h-recipe/h-recipe-1.js +51 -0
- data/spec/support/cases/microformat2-node.jit.su/h-resume/h-resume-0.html +3 -0
- data/spec/support/cases/microformat2-node.jit.su/h-resume/h-resume-0.js +10 -0
- data/spec/support/cases/microformat2-node.jit.su/h-resume/h-resume-1.html +20 -0
- data/spec/support/cases/microformat2-node.jit.su/h-resume/h-resume-1.js +39 -0
- data/spec/support/cases/microformat2-node.jit.su/h-resume/h-resume-2.html +17 -0
- data/spec/support/cases/microformat2-node.jit.su/h-resume/h-resume-2.js +54 -0
- data/spec/support/cases/microformat2-node.jit.su/h-resume/h-resume-3.html +15 -0
- data/spec/support/cases/microformat2-node.jit.su/h-resume/h-resume-3.js +54 -0
- data/spec/support/cases/microformat2-node.jit.su/h-resume/h-resume-4.html +15 -0
- data/spec/support/cases/microformat2-node.jit.su/h-resume/h-resume-4.js +12 -0
- data/spec/support/cases/microformat2-node.jit.su/h-resume/h-resume-5.html +13 -0
- data/spec/support/cases/microformat2-node.jit.su/h-resume/h-resume-5.js +27 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review-aggregate/h-review-aggregate-0.html +10 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review-aggregate/h-review-aggregate-0.js +19 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review-aggregate/h-review-aggregate-1.html +20 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review-aggregate/h-review-aggregate-1.js +26 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review-aggregate/h-review-aggregate-2.html +15 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review-aggregate/h-review-aggregate-2.js +23 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review/h-review-0.html +3 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review/h-review-0.js +10 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review/h-review-1.html +3 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review/h-review-1.js +11 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review/h-review-2.html +3 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review/h-review-2.js +11 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review/h-review-3.html +6 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review/h-review-3.js +19 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review/h-review-4.html +8 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review/h-review-4.js +20 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review/h-review-5.html +24 -0
- data/spec/support/cases/microformat2-node.jit.su/h-review/h-review-5.js +37 -0
- data/spec/support/cases/microformat2-node.jit.su/includes/includes-0.html +18 -0
- data/spec/support/cases/microformat2-node.jit.su/includes/includes-0.js +43 -0
- data/spec/support/cases/microformat2-node.jit.su/includes/includes-1.html +26 -0
- data/spec/support/cases/microformat2-node.jit.su/includes/includes-1.js +54 -0
- data/spec/support/cases/microformat2-node.jit.su/includes/includes-2.html +13 -0
- data/spec/support/cases/microformat2-node.jit.su/includes/includes-2.js +20 -0
- data/spec/support/cases/microformat2-node.jit.su/includes/includes-3.html +20 -0
- data/spec/support/cases/microformat2-node.jit.su/includes/includes-3.js +40 -0
- data/spec/support/cases/microformat2-node.jit.su/includes/includes-4.html +23 -0
- data/spec/support/cases/microformat2-node.jit.su/includes/includes-4.js +42 -0
- data/spec/support/cases/microformat2-node.jit.su/rel/rel-0.html +3 -0
- data/spec/support/cases/microformat2-node.jit.su/rel/rel-0.js +10 -0
- data/spec/support/cases/microformat2-node.jit.su/rel/rel-1.html +3 -0
- data/spec/support/cases/microformat2-node.jit.su/rel/rel-1.js +10 -0
- data/spec/support/cases/microformat2-node.jit.su/rel/rel-2.html +12 -0
- data/spec/support/cases/microformat2-node.jit.su/rel/rel-2.js +19 -0
- data/spec/support/cases/microformat2-node.jit.su/rel/rel-3.html +21 -0
- data/spec/support/cases/microformat2-node.jit.su/rel/rel-3.js +26 -0
- data/spec/support/cases/microformat2-node.jit.su/rel/rel-4.html +15 -0
- data/spec/support/cases/microformat2-node.jit.su/rel/rel-4.js +42 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-0.html +2 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-0.js +9 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-1.html +2 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-1.js +10 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-2.html +5 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-2.js +11 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-3.html +17 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-3.js +20 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-4.html +16 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-4.js +24 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-5.html +7 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-5.js +11 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-6.html +9 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-6.js +18 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-7.html +9 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-7.js +18 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-8.html +9 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-8.js +17 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-9.html +9 -0
- data/spec/support/cases/microformats.org/microformats-2/microformats-2-9.js +17 -0
- data/spec/support/lib/microformats2/implied_property/name-fail.html +60 -0
- data/spec/support/lib/microformats2/implied_property/name-pass.html +30 -0
- data/spec/support/lib/microformats2/implied_property/photo-fail.html +60 -0
- data/spec/support/lib/microformats2/implied_property/photo-pass.html +30 -0
- data/spec/support/lib/microformats2/implied_property/url-fail.html +18 -0
- data/spec/support/lib/microformats2/implied_property/url-pass.html +12 -0
- data/spec/support/lib/microformats2/nested-format-with-property.html +12 -0
- data/spec/support/lib/microformats2/nested-format-with-property.js +15 -0
- data/spec/support/lib/microformats2/nested-format-without-property.html +13 -0
- data/spec/support/lib/microformats2/nested-property.html +12 -0
- data/spec/support/lib/microformats2/nested-property.js +9 -0
- data/spec/support/lib/microformats2/simple.html +19 -0
- data/spec/support/lib/microformats2/simple.js +10 -0
- metadata +535 -85
- data/.autotest +0 -23
- data/.gemtest +0 -0
- data/History.txt +0 -19
- data/Manifest.txt +0 -10
- data/bin/microformats2 +0 -3
- data/test/IndieWebCamp.html +0 -351
- data/test/simple.html +0 -30
- data/test/test_microformats2.rb +0 -185
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 G5
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,43 +1,134 @@
|
|
1
|
-
#
|
1
|
+
# Microformats2
|
2
2
|
|
3
|
-
|
3
|
+
[![Build Status](https://travis-ci.org/G5/microformats2.png?branch=master)](https://travis-ci.org/G5/microformats2)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/G5/microformats2.png)](https://codeclimate.com/github/G5/microformats2)
|
4
5
|
|
5
|
-
|
6
|
+
A Ruby gem to parse HTML containing one or more [microformats2](http://microformats.org/wiki/microformats-2)
|
7
|
+
and return a collection of dynamically defined Ruby objects.
|
6
8
|
|
7
|
-
|
9
|
+
A work in progress.
|
8
10
|
|
9
|
-
|
11
|
+
Implemented:
|
10
12
|
|
11
|
-
*
|
12
|
-
*
|
13
|
-
*
|
14
|
-
*
|
15
|
-
*
|
13
|
+
* [parsing depth first, doc order](parse_a_document_for_microformats)
|
14
|
+
* [parsing a p- property](http://microformats.org/wiki/microformats2-parsing#parsing_a_p-_property)
|
15
|
+
* [parsing a u- property](http://microformats.org/wiki/microformats2-parsing#parsing_a_u-_property)
|
16
|
+
* [parsing a dt- property](http://microformats.org/wiki/microformats2-parsing#parsing_a_dt-_property)
|
17
|
+
* [parsing a e- property](http://microformats.org/wiki/microformats2-parsing#parsing_a_e-_property)
|
18
|
+
* [parsing implied properties](http://microformats.org/wiki/microformats-2-parsing#parsing_for_implied_properties)
|
19
|
+
* nested properties
|
20
|
+
* nested microformat with associated property
|
16
21
|
|
17
|
-
|
22
|
+
Not Implemented:
|
18
23
|
|
19
|
-
|
24
|
+
* [normalize u-* property values](http://microformats.org/wiki/microformats2-parsing-faq#normalizing_u-.2A_property_values)
|
25
|
+
* nested microformat without associated property
|
26
|
+
* [rel](http://microformats.org/wiki/rel)
|
27
|
+
* [value-class-pattern](http://microformats.org/wiki/value-class-pattern)
|
28
|
+
* [include-pattern](http://microformats.org/wiki/include-pattern)
|
29
|
+
* recognition of [vendor extensions](http://microformats.org/wiki/microformats2#VENDOR_EXTENSIONS)
|
30
|
+
* backwards compatable support for microformats v1
|
20
31
|
|
21
|
-
## REQUIREMENTS
|
22
32
|
|
23
|
-
|
24
|
-
* Nokogiri
|
33
|
+
## Current Version
|
25
34
|
|
26
|
-
|
35
|
+
2.0.0.pre1
|
27
36
|
|
28
|
-
sudo gem install microformats2
|
29
37
|
|
30
|
-
##
|
38
|
+
## Requirements
|
31
39
|
|
32
|
-
|
40
|
+
* "nokogiri", "~> 1.5.6"
|
41
|
+
* "json", "~> 1.7.6"
|
42
|
+
* "activesupport", "~> 3.2.12"
|
33
43
|
|
34
|
-
rake newb
|
35
44
|
|
36
|
-
|
37
|
-
run the tests/specs, and generate the RDoc.
|
45
|
+
## Installation
|
38
46
|
|
39
|
-
|
47
|
+
Add this line to your application's Gemfile:
|
40
48
|
|
41
|
-
|
42
|
-
|
43
|
-
|
49
|
+
gem 'microformats2'
|
50
|
+
|
51
|
+
And then execute:
|
52
|
+
|
53
|
+
$ bundle
|
54
|
+
|
55
|
+
Or install it yourself as:
|
56
|
+
|
57
|
+
$ gem install microformats2
|
58
|
+
|
59
|
+
|
60
|
+
## Usage
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
require "microformats2"
|
64
|
+
|
65
|
+
source = '<div class="h-card"><p class="p-name">Jessica Lynn Suttles</p></div>'
|
66
|
+
collection = Microformats.parse(source)
|
67
|
+
collection.cards.first.names.first.to_s #=> "Jessica Lynn Suttles"
|
68
|
+
collection.card.name.to_s #=> "Jessica Lynn Suttles"
|
69
|
+
```
|
70
|
+
|
71
|
+
* `source` can be a URL, filepath, or HTML
|
72
|
+
|
73
|
+
## Authors
|
74
|
+
|
75
|
+
* Jessica Lynn Suttles / [@jlsuttles](https://github.com/jlsuttles)
|
76
|
+
|
77
|
+
|
78
|
+
## Contributions
|
79
|
+
|
80
|
+
1. Fork it
|
81
|
+
2. Get it running (see Installation above)
|
82
|
+
3. Create your feature branch (`git checkout -b my-new-feature`)
|
83
|
+
4. Write your code and **specs**
|
84
|
+
5. Commit your changes (`git commit -am 'Add some feature'`)
|
85
|
+
6. Push to the branch (`git push origin my-new-feature`)
|
86
|
+
7. Create new Pull Request
|
87
|
+
|
88
|
+
If you find bugs, have feature requests or questions, please
|
89
|
+
[file an issue](https://github.com/G5/microformats2/issues).
|
90
|
+
|
91
|
+
|
92
|
+
## Specs
|
93
|
+
|
94
|
+
To update spec cases that are scraped from other sites.
|
95
|
+
**Warning:** This could break specs.
|
96
|
+
```bash
|
97
|
+
rake specs:update
|
98
|
+
```
|
99
|
+
|
100
|
+
To run specs
|
101
|
+
```bash
|
102
|
+
rake spec
|
103
|
+
```
|
104
|
+
|
105
|
+
To keep specs running
|
106
|
+
```bash
|
107
|
+
guard
|
108
|
+
```
|
109
|
+
|
110
|
+
|
111
|
+
## License
|
112
|
+
|
113
|
+
Copyright (c) 2013 G5
|
114
|
+
|
115
|
+
MIT License
|
116
|
+
|
117
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
118
|
+
a copy of this software and associated documentation files (the
|
119
|
+
"Software"), to deal in the Software without restriction, including
|
120
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
121
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
122
|
+
permit persons to whom the Software is furnished to do so, subject to
|
123
|
+
the following conditions:
|
124
|
+
|
125
|
+
The above copyright notice and this permission notice shall be
|
126
|
+
included in all copies or substantial portions of the Software.
|
127
|
+
|
128
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
129
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
130
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
131
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
132
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
133
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
134
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
@@ -1,13 +1,69 @@
|
|
1
|
-
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rspec/core/rake_task"
|
3
|
+
require "nokogiri"
|
4
|
+
require "open-uri"
|
5
|
+
require "pp"
|
2
6
|
|
3
|
-
|
4
|
-
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
8
|
+
task :default => [:spec]
|
5
9
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
10
|
+
namespace :specs do
|
11
|
+
task :update do
|
12
|
+
sources = [
|
13
|
+
{ dir: "microformats.org",
|
14
|
+
urls: ["http://microformats.org/wiki/microformats-2"],
|
15
|
+
html_selector: ".source-html4strict",
|
16
|
+
json_selector: ".source-javascript",
|
17
|
+
html_method: "inner_text"
|
18
|
+
},
|
19
|
+
{ dir: "microformat2-node.jit.su",
|
20
|
+
urls: [
|
21
|
+
"http://microformat2-node.jit.su/h-adr.html",
|
22
|
+
"http://microformat2-node.jit.su/h-card.html",
|
23
|
+
"http://microformat2-node.jit.su/h-entry.html",
|
24
|
+
"http://microformat2-node.jit.su/h-event.html",
|
25
|
+
"http://microformat2-node.jit.su/h-geo.html",
|
26
|
+
"http://microformat2-node.jit.su/h-news.html",
|
27
|
+
"http://microformat2-node.jit.su/h-org.html",
|
28
|
+
"http://microformat2-node.jit.su/h-product.html",
|
29
|
+
"http://microformat2-node.jit.su/h-recipe.html",
|
30
|
+
"http://microformat2-node.jit.su/h-resume.html",
|
31
|
+
"http://microformat2-node.jit.su/h-review-aggregate.html",
|
32
|
+
"http://microformat2-node.jit.su/h-review.html",
|
33
|
+
"http://microformat2-node.jit.su/rel.html",
|
34
|
+
"http://microformat2-node.jit.su/includes.html",
|
35
|
+
],
|
36
|
+
html_selector: ".e-x-microformat",
|
37
|
+
json_selector: ".language-json",
|
38
|
+
html_method: "inner_html"
|
39
|
+
}
|
40
|
+
]
|
41
|
+
|
42
|
+
sources.each do |source|
|
43
|
+
source[:urls].each do |url|
|
44
|
+
document = Nokogiri::HTML(open(url).read)
|
45
|
+
html = document.css(source[:html_selector]).map { |e| e.send(source[:html_method]) }
|
46
|
+
json = document.css(source[:json_selector]).map { |e| e.inner_text }
|
47
|
+
|
48
|
+
name = url.split("/").last.gsub(/[.]\w+/, "")
|
49
|
+
path = File.join "spec/support/cases", source[:dir], name
|
50
|
+
|
51
|
+
FileUtils.mkdir_p(path)
|
11
52
|
|
53
|
+
([html.length, json.length].min).times do |index|
|
12
54
|
|
13
|
-
#
|
55
|
+
File.open(File.join(path, "#{name}-#{index}.html"), "w") do |f|
|
56
|
+
f.write "<!-- #{url} -->\n"
|
57
|
+
f.write html[index]
|
58
|
+
end
|
59
|
+
|
60
|
+
File.open(File.join(path, "#{name}-#{index}.js"), "w") do |f|
|
61
|
+
f.write "// #{url}\n"
|
62
|
+
f.write json[index]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/microformats2.rb
CHANGED
@@ -1,121 +1,36 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require "nokogiri"
|
2
|
+
require "open-uri"
|
3
|
+
require "json"
|
4
|
+
require "active_support/inflector"
|
5
|
+
|
6
|
+
require "microformats2/version"
|
7
|
+
require "microformats2/format_parser"
|
8
|
+
require "microformats2/property_parser"
|
9
|
+
require "microformats2/collection"
|
10
|
+
require "microformats2/format"
|
11
|
+
require "microformats2/property/foundation"
|
12
|
+
require "microformats2/property/text"
|
13
|
+
require "microformats2/property/url"
|
14
|
+
require "microformats2/property/date_time"
|
15
|
+
require "microformats2/property/embedded"
|
16
|
+
require "microformats2/property"
|
17
|
+
require "microformats2/implied_property/foundation"
|
18
|
+
require "microformats2/implied_property/name"
|
19
|
+
require "microformats2/implied_property/photo"
|
20
|
+
require "microformats2/implied_property/url"
|
4
21
|
|
5
22
|
module Microformats2
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
raise LoadError, "argument must be a String or File" unless [String, File].include?(html.class)
|
12
|
-
|
13
|
-
html = html.read if IO === html
|
14
|
-
|
15
|
-
doc = Nokogiri::HTML(html)
|
16
|
-
microformats = Hash.new{|hash, key| hash[key] = Array.new}
|
17
|
-
doc.css("*[class*=h-]").each do |microformat|
|
18
|
-
microformat.attribute("class").to_s.split.each do |mf_class|
|
19
|
-
if mf_class =~ /^h-/
|
20
|
-
constant_name = mf_class.gsub("-","_").gsub(/^([a-z])/){$1.upcase}.gsub(/_(.)/) { $1.upcase }
|
21
|
-
|
22
|
-
if Object.const_defined?(constant_name)
|
23
|
-
klass = Object.const_get(constant_name)
|
24
|
-
else
|
25
|
-
klass = Class.new
|
26
|
-
Object.const_set constant_name, klass
|
27
|
-
end
|
28
|
-
|
29
|
-
obj = klass.new
|
30
|
-
|
31
|
-
add_properties(microformat, obj)
|
32
|
-
|
33
|
-
microformats[constant_name.downcase.to_sym] << obj
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
microformats
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.add_method(obj, method_name)
|
42
|
-
unless obj.respond_to?(method_name)
|
43
|
-
obj.class.class_eval { attr_accessor method_name }
|
23
|
+
class << self
|
24
|
+
def parse(html)
|
25
|
+
html = read_html(html)
|
26
|
+
document = Nokogiri::HTML(html)
|
27
|
+
Collection.new(document).parse
|
44
28
|
end
|
45
29
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
if cur = obj.send(method_name)
|
51
|
-
if cur.kind_of? Array
|
52
|
-
cur << value
|
53
|
-
else
|
54
|
-
obj.send("#{method_name}=", [cur, value])
|
55
|
-
end
|
56
|
-
else
|
57
|
-
obj.send("#{method_name}=", value)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
class Stripper
|
62
|
-
def transform(property)
|
63
|
-
property.text.gsub(/\n+/, " ").gsub(/\s+/, " ").strip
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
class URL
|
68
|
-
def transform(property)
|
69
|
-
(property.attribute("href") || property.text).to_s
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
class Date
|
74
|
-
def transform(property)
|
75
|
-
value = (property.attribute("title") || property.text).to_s
|
76
|
-
|
77
|
-
if value[0..0] =~ /[a-zA-Z]/
|
78
|
-
value
|
79
|
-
else
|
80
|
-
DateTime.parse(value)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
class TimeThingy
|
86
|
-
def transform(property)
|
87
|
-
Time.parse((property.attribute("title") || property.text).to_s)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
FormatClass = {
|
92
|
-
"p" => Stripper.new,
|
93
|
-
"n" => Stripper.new,
|
94
|
-
"e" => Stripper.new,
|
95
|
-
"i" => Stripper.new,
|
96
|
-
"u" => URL.new,
|
97
|
-
"d" => Date.new,
|
98
|
-
"t" => TimeThingy.new
|
99
|
-
}
|
100
|
-
|
101
|
-
def self.add_properties(mf, obj)
|
102
|
-
FormatClass.each do |letter, trans|
|
103
|
-
mf.css("*[class*=#{letter}-]").each do |property|
|
104
|
-
property.attribute("class").to_s.split.each do |css_class|
|
105
|
-
if css_class[0..1] == "#{letter}-"
|
106
|
-
css_class = css_class[2..-1].gsub("-","_")
|
107
|
-
method_name = css_class.gsub("-","_")
|
108
|
-
value = trans.transform(property)
|
109
|
-
|
110
|
-
if method_name == "class"
|
111
|
-
method_name = "klass"
|
112
|
-
end
|
113
|
-
|
114
|
-
add_method(obj, method_name)
|
115
|
-
populate_method(obj, method_name, value)
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
30
|
+
def read_html(html)
|
31
|
+
open(html).read
|
32
|
+
rescue Errno::ENOENT, Errno::ENAMETOOLONG => e
|
33
|
+
html
|
119
34
|
end
|
120
|
-
end
|
35
|
+
end # class << self
|
121
36
|
end
|