Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse draft documents #35

Merged
merged 5 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/pubid/ieee/identifier.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
require 'date'
require "yaml"

UPDATE_CODES = YAML.load_file(File.join(File.dirname(__FILE__), "../../../update_codes.yaml"))

module Pubid::Ieee
class Identifier
Expand All @@ -9,6 +12,13 @@ def initialize(**opts)
opts.each { |key, value| send("#{key}=", value.is_a?(Enumerable) && value || value.to_s) }
end

def self.update_old_code(code)
UPDATE_CODES.each do |from, to|
code = code.gsub(from.match?(/^\/.*\/$/) ? Regexp.new(from[1..-2]) : from, to)
end
code
end

def self.merge_parameters(params)
return params unless params.is_a?(Array)

Expand All @@ -26,14 +36,14 @@ def self.merge_parameters(params)
end

def self.parse(code)
new(**merge_parameters(Parser.new.parse(code)).to_h)
new(**merge_parameters(Parser.new.parse(update_old_code(code))).to_h)

rescue Parslet::ParseFailed => failure
raise Pubid::Ieee::Errors::ParseError, "#{failure.message}\ncause: #{failure.parse_failure_cause.ascii_tree}"
end

def to_s
"#{publisher}#{copublisher} #{type}#{number}#{part}#{subpart}#{year}#{edition}#{alternative}"
"#{publisher}#{copublisher} #{type}#{number}#{part}#{subpart}#{year}#{draft}#{edition}#{alternative}"
end

def copublisher
Expand Down Expand Up @@ -89,5 +99,14 @@ def edition
result += "-#{@edition[:day]}" if @edition[:day]
result
end

def draft
return "" unless @draft

result = "/D#{@draft[:version]}"
result += ".#{@draft[:revision]}" if @draft[:revision]
result += ", #{@draft[:month]} #{@draft[:year]}" if @draft[:month]
result
end
end
end
18 changes: 16 additions & 2 deletions lib/pubid/ieee/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Parser < Parslet::Parser
end

rule(:subpart) do
(str(".") | str("-")) >> match('\d').repeat(1)
(str(".") | str("-")) >> match('[\da-z]').repeat(1)
end

rule(:type) do
Expand All @@ -45,6 +45,15 @@ class Parser < Parslet::Parser

end

rule(:draft) do
# /D14, April 2020
# /D7 November, 2019
str(" ").maybe >> str("/D") >> match('[\dA-Za-z]').repeat(1).as(:version) >>
((str(".") | str("r")) >> digits.as(:revision)).maybe >>
((str(", ") | str(" ")) >> match("[A-Za-z]").repeat(1).as(:month) >>
(str(" ") | str(", ")) >> match('\d').repeat(4, 4).as(:year)).maybe
end

rule(:identifier) do
organization.as(:publisher) >> ((str("/ ") | str("/")) >> organization.as(:copublisher)).repeat >>
str(" ") >> (type.as(:type) >> str(" ")).maybe >> (
Expand All @@ -64,14 +73,19 @@ class Parser < Parslet::Parser
# C37.0781-1972
(part >> year) |
# C57.19.101
(part >> subpart) |
(part >> subpart.as(:subpart)) |
# IEEE P11073-10101
# IEEE P11073-10420/D4D5
# trick to avoid being partially parsed by year
(str("-") >> match('[\dA-Z]').repeat(5).as(:part)) |
# 581.1978
year |
# IEC 62525-Edition 1.0 - 2007
edition.as(:edition) |
# 61691-6
part
).maybe >>
draft.as(:draft).maybe >>
edition.as(:edition).maybe >>
# dual-PubIDs
(str(" ") >>
Expand Down
Loading