Skip to content

Commit

Permalink
Value Class Pattern with tz in a seperate field
Browse files Browse the repository at this point in the history
fixes #73
  • Loading branch information
Ben Roberts committed Jun 25, 2017
1 parent 7fd715f commit c793c90
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 33 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -34,9 +34,9 @@ Not Implemented:

## Current Version

4.0.6
4.0.7

![Version 4.0.6](https://img.shields.io/badge/VERSION-4.0.6-green.svg)
![Version 4.0.7](https://img.shields.io/badge/VERSION-4.0.7-green.svg)


## Requirements
Expand Down
82 changes: 52 additions & 30 deletions lib/microformats/time_property_parser.rb
Expand Up @@ -6,6 +6,7 @@ def parse(element, base: nil, element_type: , format_class_array: [], backcompat
@duration_value = nil
@date_value = nil
@time_value = nil
@tz_value = nil

@property_type = element_type

Expand All @@ -14,7 +15,7 @@ def parse(element, base: nil, element_type: , format_class_array: [], backcompat

parse_value_class_pattern(element)

if @duration_value.nil? and @time_value.nil? and @date_value.nil?
if @duration_value.nil? and @time_value.nil? and @date_value.nil? and @tz_value.nil?

value = nil
if ['time', 'ins', 'del'].include? element.name and not element.attribute('datetime').nil?
Expand All @@ -31,16 +32,17 @@ def parse(element, base: nil, element_type: , format_class_array: [], backcompat

end

if not @duration_value.nil?
if not @duration_value.nil?
@duration_value
elsif not @time_value.nil? and not @date_value.nil?
@date_value + ' ' + @time_value
elsif not @time_value.nil?
@time_value
elsif not @date_value.nil?
@date_value
else
nil
result = nil
result = result.to_s + @date_value unless @date_value.nil?
unless @time_value.nil?
result = result.to_s + ' ' unless result.nil?
result = result.to_s + @time_value
end
result = result.to_s + @tz_value unless @tz_value.nil?
result
end
end

Expand All @@ -50,7 +52,7 @@ def parse_value_class_pattern(element)

def parse_element(element)

if @duration_value.nil? or (@time_value.nil? and @date_value.nil?)
if @duration_value.nil? or (@time_value.nil? and @date_value.nil? and @tz_value.nil?)
if value_title_classes(element).length >= 1
value = element.attribute('title').value.strip
elsif value_classes(element).length >= 1
Expand Down Expand Up @@ -84,31 +86,39 @@ def parse_dt(data, normalize: false)
#TODO this still allows a lot of non correct values such as 39th day of the month, etc
begin
case data.strip
when /^(\d{4}-[01]\d-[0-3]\d)[tT ]([0-2]\d:[0-5]\d(:[0-5]\d)?([zZ]|[-+][01]?\d:?[0-5]\d)?)$/
@date_value = $1 if @date_value.nil?
@time_value = $2.gsub(/z/, 'Z') if @time_value.nil?
when /^(\d{4}-[01]\d-[0-3]\d)[tT ]([0-2]\d:[0-5]\d(:[0-5]\d)? ?[-+]\d\d)$/
when /^P\d*W$/
@duration_value = data if @duration_value.nil?

when /^P(\d+Y)?(\d+M)?(\d+D)?(T(\d+H)?(\d+M)?(\d+S)?)?$/
@duration_value = data if @duration_value.nil?

when /^(\d{4}-[01]\d-[0-3]\d)[tT ]([0-2]\d:[0-5]\d(:[0-5]\d)?)?([zZ]|[-+][01]?\d:?[0-5]\d)?$/
@date_value = $1 if @date_value.nil?
@time_value = $2 if @time_value.nil?
when /^(\d{4}-[01]\d-[0-3]\d)[tT ]([0-2]\d:[0-5]\d:[0-5]\d ?[-+]\d\d):?(\d\d)$/
@tz_value = $4.gsub(/z/, 'Z') if @tz_value.nil?

when /^(\d{4}-[01]\d-[0-3]\d)[tT ]([0-2]\d:[0-5]\d(:[0-5]\d)?)( ?[-+]\d\d:?(\d\d)?)$/
@date_value = $1 if @date_value.nil?
@time_value = $2 if @time_value.nil?
if normalize
@time_value = $2 + $3 if @time_value.nil?
@tz_value = $4.gsub(/z/, 'Z').gsub(/:/,'') if @tz_value.nil?
else
@time_value = $2 + ':' + $3 if @time_value.nil?
@tz_value = $4.gsub(/z/, 'Z') if @tz_value.nil?
end
when /^(\d{4}-[01]\d-[0-3]\d)[tT ]([0-2]\d:[0-5]\d)( ?[-+]\d\d:?\d\d)$/

when /^(\d{4}-[0-3]\d\d)[tT ]([0-2]\d:[0-5]\d(:[0-5]\d)?)?([zZ]|[-+][01]?\d:?[0-5]\d)?$/
@date_value = $1 if @date_value.nil?
@time_value = $2 if @time_value.nil?
@tz_value = $4.gsub(/z/, 'Z') if @tz_value.nil?

when /^(\d{4}-[0-3]\d\d)[tT ]([0-2]\d:[0-5]\d(:[0-5]\d)?)( ?[-+]\d\d:?(\d\d)?)$/
@date_value = $1 if @date_value.nil?
@time_value = $2 if @time_value.nil?
if normalize
@time_value = $2 + $3.gsub(/:/,'') if @time_value.nil?
@tz_value = $4.gsub(/z/, 'Z').gsub(/:/,'') if @tz_value.nil?
else
@time_value = $2 + $3 if @time_value.nil?
@tz_value = $4.gsub(/z/, 'Z') if @tz_value.nil?
end
when /^P\d*W$/
@duration_value = data if @duration_value.nil?

when /^P(\d+Y)?(\d+M)?(\d+D)?(T(\d+H)?(\d+M)?(\d+S)?)?$/
@duration_value = data if @duration_value.nil?

when /^(\d{4})-([01]?\d)-([0-3]?\d)$/
@date_value = DateTime.new($1.to_i, $2.to_i, $3.to_i).strftime('%F') if @date_value.nil?
Expand All @@ -119,22 +129,34 @@ def parse_dt(data, normalize: false)
when /^(\d{4})-([01]?\d)$/
@date_value = data if @date_value.nil?

when /^[0-2]\d:[0-5]\d(:[0-5]\d)?([zZ]|[-+][01]\d:?\d\d)?$/
when /^([zZ]|[-+][01]?\d:?[0-5]\d)$/
if normalize
@time_value = data.gsub(/z/, 'Z').gsub(/([+-]\d\d):(\d\d)/, '\1\2') if @time_value.nil?
@tz_value = $1.gsub(/z/, 'Z').gsub(/:/,'') if @tz_value.nil?
else
@time_value = data.gsub(/z/, 'Z') if @time_value.nil?
@tz_value = $1.gsub(/z/, 'Z') if @tz_value.nil?
end

when /^([0-2]\d:[0-5]\d(:[0-5]\d)?)([zZ]|[-+][01]\d:?\d\d)?$/
@time_value = $1 if @time_value.nil?
if normalize
@tz_value = $3.gsub(/z/, 'Z').gsub(/:/,'') if @tz_value.nil?
else
@tz_value = $3.gsub(/z/, 'Z') if @tz_value.nil?
end

when /^[0-2]\d:[0-5]\d[zZ]?$/
@time_value = Time.parse(data).strftime('%H:%M') if @time_value.nil?
when /^([0-2]\d:[0-5]\d:[0-5]\d[-+][01]\d:?[0-5]\d)$/
@tz_value = 'Z'

when /^([0-2]\d:[0-5]\d:[0-5]\d)([-+][01]\d:?[0-5]\d)$/
Time.parse(data).strftime('%T') #to make sure this time doesn't throw an error
@time_value = $1 if @time_value.nil?
@tz_value = $2 if @tz_value.nil?

when /^([0-2][0-0]:[0-5]\d[-+][01]\d:?[0-5]\d)$/
when /^([0-2][0-0]:[0-5]\d)([-+][01]\d:?[0-5]\d)$/
Time.parse(data).strftime('%H:%M') #to make sure this time doesn't throw an error
@time_value = $1 if @time_value.nil?
@tz_value = $2 if @tz_value.nil?

when /^([01]?\d):?([0-5]\d)?p\.?m\.?$/i
@time_value = ($1.to_i + 12).to_s + ':' + $2.to_s.rjust(2,'0') if @time_value.nil?
Expand Down
2 changes: 1 addition & 1 deletion lib/microformats/version.rb
@@ -1,3 +1,3 @@
module Microformats
VERSION = "4.0.6"
VERSION = "4.0.7"
end
13 changes: 13 additions & 0 deletions spec/support/lib/edge_cases/vcp-dates.html
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<base href="http://example.org/">
<body>
<div class="h-event">
<span class="e-summary">HomebrewWebsiteClub Berlin</span> will be next on
<span class="dt-start">
<span class="value">2017-05-31</span>, from
<span class="value">19:00</span> (UTC<span class="value">+02:00</span>)
</span> to <span class="dt-end">21:00</span>.
</div>
</body>
</html>
30 changes: 30 additions & 0 deletions spec/support/lib/edge_cases/vcp-dates.js
@@ -0,0 +1,30 @@
{
"items": [
{
"type": [
"h-event"
],
"properties": {
"summary": [
{
"value": "HomebrewWebsiteClub Berlin",
"html": "HomebrewWebsiteClub Berlin"
}
],
"start": [
"2017-05-31 19:00+0200"
],
"end": [
"2017-05-31 21:00"
],
"name": [
"HomebrewWebsiteClub Berlin will be next on \n \n 2017-05-31, from\n 19:00 (UTC+02:00)\n to 21:00."
]
}
}
],
"rels": {
},
"rel-urls": {
}
}

0 comments on commit c793c90

Please sign in to comment.