From 9e95dd9d2eb87f56799a3465bc045d69c6dc8e4e Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Fri, 22 Mar 2019 14:03:14 -0600 Subject: resolves #2134 add parse_attributes helper to base extension Processor class (PR #3189) --- CHANGELOG.adoc | 1 + lib/asciidoctor/extensions.rb | 15 +++++++++++++++ test/extensions_test.rb | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 9b8b7eb1..99038dd0 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -93,6 +93,7 @@ Enhancements / Compliance:: * substitute replacements in author values used in document header (#2441) * require space after semi-colon that separates multiple authors (#2441) * catalog inline anchors at start of callout list items (#2818) (*@owenh000*) + * add parse_attributes helper method to base extension Processor class (#2134) Improvements:: diff --git a/lib/asciidoctor/extensions.rb b/lib/asciidoctor/extensions.rb index 5e45e08a..1cc16601 100644 --- a/lib/asciidoctor/extensions.rb +++ b/lib/asciidoctor/extensions.rb @@ -215,6 +215,21 @@ module Extensions parent end + # Public: Parses the attrlist String into a Hash of attributes + # + # block - the current AbstractBlock or the parent AbstractBlock if there is no current block (used for applying subs) + # attrlist - the list of attributes as a String + # opts - an optional Hash of options to control processing: + # :positional_attributes - an Array of attribute names to map positional arguments to (optional, default: false) + # :sub_attributes - enables attribute substitution on the attrlist argument (optional, default: false) + # + # Returns a Hash of parsed attributes + def parse_attributes block, attrlist, opts = {} + return {} if attrlist ? attrlist.empty? : true + attrlist = block.sub_attributes attrlist if opts[:sub_attributes] && (attrlist.include? ATTR_REF_HEAD) + (AttributeList.new attrlist).parse (opts[:positional_attributes] || []) + end + # TODO fill out remaining methods [ [:create_paragraph, :create_block, :paragraph], diff --git a/test/extensions_test.rb b/test/extensions_test.rb index 1f794acd..a30c1490 100644 --- a/test/extensions_test.rb +++ b/test/extensions_test.rb @@ -1474,6 +1474,38 @@ context 'Extensions' do end end + test 'can use parse_attributes to parse attrlist' do + begin + parsed_attrs = nil + Asciidoctor::Extensions.register do + block do + named :attrs + on_context :open + process do |parent, reader, attrs| + parsed_attrs = parse_attributes parent, reader.read_line, positional_attributes: ['a', 'b'] + parsed_attrs.update parse_attributes parent, 'foo={foo}', sub_attributes: true + nil + end + end + end + input = <<~'EOS' + :foo: bar + + [attrs] + -- + a,b,c,key=val + -- + EOS + doc = document_from_string input + assert_equal 'a', parsed_attrs['a'] + assert_equal 'b', parsed_attrs['b'] + assert_equal 'val', parsed_attrs['key'] + assert_equal 'bar', parsed_attrs['foo'] + ensure + Asciidoctor::Extensions.unregister_all + end + end + test 'create_section should set up all section properties' do begin sect = nil -- cgit v1.2.3