summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2017-06-18 16:37:51 -0600
committerDan Allen <dan.j.allen@gmail.com>2017-06-18 16:41:59 -0600
commit83e82ee68c3270249c195cc18499ff34c8cd7df9 (patch)
tree94e980d09fcb4cf6ccb19d8e16dbfbe07a4f004e
parentee9c5478f7ee669a7251472a62451ba6cff3cf01 (diff)
change Parser.parse_style_attribute to only return parsed style
- only return parsed style (not original style) - update API docs for method - optimize method return; consolidate code
-rw-r--r--lib/asciidoctor/parser.rb51
-rw-r--r--test/parser_test.rb24
2 files changed, 25 insertions, 50 deletions
diff --git a/lib/asciidoctor/parser.rb b/lib/asciidoctor/parser.rb
index a606b679..733c8869 100644
--- a/lib/asciidoctor/parser.rb
+++ b/lib/asciidoctor/parser.rb
@@ -465,10 +465,7 @@ class Parser
source_location = reader.cursor if sourcemap
this_line = reader.read_line
delimited_block = block_context = cloaked_context = terminator = nil
- # QUESTION put this inside call to rekey attributes?
- if attributes[1]
- style, _ = parse_style_attribute(attributes, reader)
- end
+ style = attributes[1] ? (parse_style_attribute attributes, reader) : nil
if (delimited_blk_match = is_delimited_block? this_line, true)
delimited_block = true
@@ -1575,8 +1572,8 @@ class Parser
sect_reftext = attributes['reftext']
end
- # parse style, id, and role from first positional attribute if present
- style, _ = parse_style_attribute attributes, reader unless attributes[1].nil_or_empty?
+ # parse style, id, and role attributes from first positional attribute if present
+ style = attributes[1] ? (parse_style_attribute attributes, reader) : nil
if style
if style == 'abstract' && document.doctype == 'book'
sect_name, sect_level = 'chapter', 1
@@ -2522,34 +2519,27 @@ class Parser
#
# Parse the first positional attribute to extract the style, role and id
# parts, assign the values to their cooresponding attribute keys and return
- # both the original style attribute and the parsed value from the first
- # positional attribute.
+ # the parsed style from the first positional attribute.
#
# attributes - The Hash of attributes to process and update
#
# Examples
#
# puts attributes
- # => {1 => "abstract#intro.lead%fragment", "style" => "preamble"}
+ # => { 1 => "abstract#intro.lead%fragment", "style" => "preamble" }
#
# parse_style_attribute(attributes)
- # => ["abstract", "preamble"]
+ # => "abstract"
#
# puts attributes
- # => {1 => "abstract#intro.lead", "style" => "abstract", "id" => "intro",
- # "role" => "lead", "options" => ["fragment"], "fragment-option" => ''}
+ # => { 1 => "abstract#intro.lead%fragment", "style" => "abstract", "id" => "intro",
+ # "role" => "lead", "options" => "fragment", "fragment-option" => '' }
#
- # Returns a two-element Array of the parsed style from the
- # first positional attribute and the original style that was
- # replaced
+ # Returns the String style parsed from the first positional attribute
def self.parse_style_attribute(attributes, reader = nil)
- original_style = attributes['style']
- raw_style = attributes[1]
- # NOTE spaces are not allowed in shorthand, so if we find one, this ain't shorthand
- if raw_style && !raw_style.include?(' ') && Compliance.shorthand_property_syntax
- type = :style
- collector = []
- parsed = {}
+ # NOTE spaces are not allowed in shorthand, so if we detect one, this ain't no shorthand
+ if (raw_style = attributes[1]) && !raw_style.include?(' ') && Compliance.shorthand_property_syntax
+ type, collector, parsed = :style, [], {}
# QUESTION should this be a private method? (though, it's never called if shorthand isn't used)
save_current = lambda {
if collector.empty?
@@ -2590,36 +2580,29 @@ class Parser
# small optimization if no shorthand is found
if type == :style
- parsed_style = attributes['style'] = raw_style
+ attributes['style'] = raw_style
else
save_current.call
- if parsed.key? :style
- parsed_style = attributes['style'] = parsed[:style]
- else
- parsed_style = nil
- end
+ parsed_style = attributes['style'] = parsed[:style] if parsed.key? :style
attributes['id'] = parsed[:id] if parsed.key? :id
attributes['role'] = parsed[:role] * ' ' if parsed.key? :role
if parsed.key? :option
- (options = parsed[:option]).each do |option|
- attributes[%(#{option}-option)] = ''
- end
+ (options = parsed[:option]).each {|option| attributes[%(#{option}-option)] = '' }
if (existing_opts = attributes['options'])
attributes['options'] = (options + existing_opts.split(',')) * ','
else
attributes['options'] = options * ','
end
end
- end
- [parsed_style, original_style]
+ parsed_style
+ end
else
attributes['style'] = raw_style
- [raw_style, original_style]
end
end
diff --git a/test/parser_test.rb b/test/parser_test.rb
index 70ee9b0c..db7fcce7 100644
--- a/test/parser_test.rb
+++ b/test/parser_test.rb
@@ -281,9 +281,8 @@ context "Parser" do
test 'parse style attribute with id and role' do
attributes = {1 => 'style#id.role'}
- style, original_style = Asciidoctor::Parser.parse_style_attribute(attributes)
+ style = Asciidoctor::Parser.parse_style_attribute(attributes)
assert_equal 'style', style
- assert_nil original_style
assert_equal 'style', attributes['style']
assert_equal 'id', attributes['id']
assert_equal 'role', attributes['role']
@@ -292,9 +291,8 @@ context "Parser" do
test 'parse style attribute with style, role, id and option' do
attributes = {1 => 'style.role#id%fragment'}
- style, original_style = Asciidoctor::Parser.parse_style_attribute(attributes)
+ style = Asciidoctor::Parser.parse_style_attribute(attributes)
assert_equal 'style', style
- assert_nil original_style
assert_equal 'style', attributes['style']
assert_equal 'id', attributes['id']
assert_equal 'role', attributes['role']
@@ -305,9 +303,8 @@ context "Parser" do
test 'parse style attribute with style, id and multiple roles' do
attributes = {1 => 'style#id.role1.role2'}
- style, original_style = Asciidoctor::Parser.parse_style_attribute(attributes)
+ style = Asciidoctor::Parser.parse_style_attribute(attributes)
assert_equal 'style', style
- assert_nil original_style
assert_equal 'style', attributes['style']
assert_equal 'id', attributes['id']
assert_equal 'role1 role2', attributes['role']
@@ -316,9 +313,8 @@ context "Parser" do
test 'parse style attribute with style, multiple roles and id' do
attributes = {1 => 'style.role1.role2#id'}
- style, original_style = Asciidoctor::Parser.parse_style_attribute(attributes)
+ style = Asciidoctor::Parser.parse_style_attribute(attributes)
assert_equal 'style', style
- assert_nil original_style
assert_equal 'style', attributes['style']
assert_equal 'id', attributes['id']
assert_equal 'role1 role2', attributes['role']
@@ -327,18 +323,16 @@ context "Parser" do
test 'parse style attribute with positional and original style' do
attributes = {1 => 'new_style', 'style' => 'original_style'}
- style, original_style = Asciidoctor::Parser.parse_style_attribute(attributes)
+ style = Asciidoctor::Parser.parse_style_attribute(attributes)
assert_equal 'new_style', style
- assert_equal 'original_style', original_style
assert_equal 'new_style', attributes['style']
assert_equal 'new_style', attributes[1]
end
test 'parse style attribute with id and role only' do
attributes = {1 => '#id.role'}
- style, original_style = Asciidoctor::Parser.parse_style_attribute(attributes)
+ style = Asciidoctor::Parser.parse_style_attribute(attributes)
assert_nil style
- assert_nil original_style
assert_equal 'id', attributes['id']
assert_equal 'role', attributes['role']
assert_equal '#id.role', attributes[1]
@@ -346,9 +340,8 @@ context "Parser" do
test 'parse empty style attribute' do
attributes = {1 => nil}
- style, original_style = Asciidoctor::Parser.parse_style_attribute(attributes)
+ style = Asciidoctor::Parser.parse_style_attribute(attributes)
assert_nil style
- assert_nil original_style
assert_nil attributes['id']
assert_nil attributes['role']
assert_nil attributes[1]
@@ -356,9 +349,8 @@ context "Parser" do
test 'parse style attribute with option should preserve existing options' do
attributes = {1 => '%header', 'options' => 'footer', 'footer-option' => ''}
- style, original_style = Asciidoctor::Parser.parse_style_attribute(attributes)
+ style = Asciidoctor::Parser.parse_style_attribute(attributes)
assert_nil style
- assert_nil original_style
assert_equal 'header,footer', attributes['options']
assert_equal '', attributes['header-option']
assert_equal '', attributes['footer-option']