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 series range #33

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions lib/pubid/itu/identifier/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
module Pubid::Itu
module Identifier
class Base < Pubid::Core::Identifier::Base
attr_accessor :series, :sector, :date, :amendment, :subseries, :second_number, :annex
attr_accessor :series, :sector, :date, :amendment, :subseries,
:second_number, :annex, :range

extend Forwardable

Expand All @@ -15,7 +16,7 @@ def self.type
# @param edition [String] document's edition version, e.g. "3.0", "1.0"
def initialize(publisher: "ITU", series: nil, sector: nil, part: nil,
date: nil, amendment: nil, subseries: nil, number: nil,
second_number: nil, annex: nil, **opts)
second_number: nil, annex: nil, range: nil, **opts)

super(**opts.merge(publisher: publisher, number: number))
@series = series
Expand All @@ -26,6 +27,7 @@ def initialize(publisher: "ITU", series: nil, sector: nil, part: nil,
@subseries = subseries
@second_number = second_number
@annex = annex
@range = range
end

def to_s(**opts)
Expand Down
9 changes: 7 additions & 2 deletions lib/pubid/itu/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ class Parser < Pubid::Core::Parser
str("OB") | str("Operational Bulletin") |
# Recommendation series
array_to_str(Identifier.config.series["T"].keys.sort_by(&:length).reverse)
).as(:series) >>
).capture(:series).as(:series) >>
# "No. " for Operational Bulletin
(dot | str(" No. ")).maybe
end

rule(:series_range) do
(dash >> dynamic { |s, c| str(c.captures[:series]) } >> dot >> full_number).as(:range)
end

rule(:sector_series_number) do
(
# ITU-R
Expand All @@ -47,7 +51,8 @@ class Parser < Pubid::Core::Parser
# ITU-T
(str("T").as(:sector) >> type.maybe >> (space | dash) >>
t_sector_series.maybe >> full_number >>
(str("/") >> t_sector_series.maybe >> full_number).as(:second_number).maybe) |
(str("/") >> t_sector_series.maybe >> full_number).as(:second_number).maybe >>
series_range.maybe) |
# ITU-D
(str("D") >> space >> full_number)
)
Expand Down
6 changes: 5 additions & 1 deletion lib/pubid/itu/renderer/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def render_type_series(params)

def render_identifier(params)
"%{publisher}-%{sector} #{render_type_series(params)}%{number}%{subseries}"\
"%{part}%{second_number}%{annex}%{amendment}%{corrigendum}%{supplement}%{date}" % params
"%{part}%{second_number}%{range}%{annex}%{amendment}%{corrigendum}%{supplement}%{date}" % params
end

def render_number(number, _opts, params)
Expand Down Expand Up @@ -72,5 +72,9 @@ def render_annex(annex, _opts, _params)
def render_corrigendum(corrigendum, opts, params)
"#{render_date(corrigendum[:date], opts, params)} Cor. #{corrigendum[:number]}"
end

def render_range(range, _opts, params)
"-#{params[:series]}.#{range[:number]}"
end
end
end
6 changes: 6 additions & 0 deletions spec/pubid_itu/identifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ module Pubid::Itu
it { expect(subject.base).to be_a(Identifier::Annex) }
end

context "ITU-T Q.400-Q.490" do
let(:pubid) { "ITU-T Q.400-Q.490" }

it_behaves_like "converts pubid to pubid"
end

describe "parse identifiers from examples files" do
context "parses IEC identifiers from itu-r.txt" do
let(:examples_file) { "itu-r.txt" }
Expand Down