summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2017-03-18 16:32:49 -0600
committerGitHub <noreply@github.com>2017-03-18 16:32:49 -0600
commitf583a310baf9bab618678f23c61bf11717ff194a (patch)
treeac508f94f1143e9a22c4d805c43ea7aab4973b18
parenta4ff466ede1f0c4d0ef955b9ecd2c06ec5e52b28 (diff)
use delete instead of tr to remove spaces; optimize options parsing (PR #2088)
-rw-r--r--lib/asciidoctor/attribute_list.rb7
-rw-r--r--lib/asciidoctor/parser.rb17
-rw-r--r--lib/asciidoctor/substitutors.rb3
-rw-r--r--test/parser_test.rb4
4 files changed, 24 insertions, 7 deletions
diff --git a/lib/asciidoctor/attribute_list.rb b/lib/asciidoctor/attribute_list.rb
index 6fdff1b7..c5a68cf1 100644
--- a/lib/asciidoctor/attribute_list.rb
+++ b/lib/asciidoctor/attribute_list.rb
@@ -163,7 +163,12 @@ class AttributeList
case name
when 'options', 'opts'
name = 'options'
- value.tr(' ', '').split(',').each {|opt| @attributes[%(#{opt}-option)] = '' }
+ if value.include? ','
+ value = value.delete ' ' if value.include? ' '
+ value.split(',').each {|opt| @attributes[%(#{opt}-option)] = '' unless opt.empty? }
+ else
+ @attributes[%(#{value = value.strip}-option)] = ''
+ end
@attributes[name] = value
when 'title'
@attributes[name] = value
diff --git a/lib/asciidoctor/parser.rb b/lib/asciidoctor/parser.rb
index 8bd886fb..d8133c14 100644
--- a/lib/asciidoctor/parser.rb
+++ b/lib/asciidoctor/parser.rb
@@ -799,14 +799,25 @@ class Parser
when :listing, :fenced_code, :source
if block_context == :fenced_code
attributes['style'] = 'source'
- language, linenums = this_line[3..-1].tr(' ', '').split(',', 2)
+ if (ll = this_line.length) == 3
+ language = nil
+ elsif (comma_idx = (language = this_line[3, ll - 3]).index ',')
+ if comma_idx > 0
+ language = language[0, comma_idx].strip
+ attributes['linenums'] = '' if comma_idx < ll - 4
+ else
+ language = nil
+ attributes['linenums'] = '' if ll > 4
+ end
+ else
+ language = language.lstrip
+ end
if language.nil_or_empty?
if (default_language = document.attributes['source-language'])
attributes['language'] = default_language
end
else
attributes['language'] = language
- attributes['linenums'] = '' unless linenums.nil_or_empty?
end
if !attributes.key?('indent') && document.attributes.key?('source-indent')
attributes['indent'] = document.attributes['source-indent']
@@ -2345,7 +2356,7 @@ class Parser
# returns a Hash of attributes that specify how to format
# and layout the cells in the table.
def self.parse_colspecs records
- records = records.tr ' ', '' if records.include? ' '
+ records = records.delete ' ' if records.include? ' '
# check for deprecated syntax: single number, equal column spread
if records == records.to_i.to_s
return ::Array.new(records.to_i) { { 'width' => 1 } }
diff --git a/lib/asciidoctor/substitutors.rb b/lib/asciidoctor/substitutors.rb
index a4c14448..f153da17 100644
--- a/lib/asciidoctor/substitutors.rb
+++ b/lib/asciidoctor/substitutors.rb
@@ -1309,7 +1309,8 @@ module Substitutors
return [] if subs.nil_or_empty?
candidates = nil
modifiers_present = SubModifierSniffRx.match? subs
- subs.tr(' ', '').split(',').each do |key|
+ subs = subs.delete ' ' if subs.include? ' '
+ subs.split(',').each do |key|
modifier_operation = nil
if modifiers_present
if (first = key.chr) == '+'
diff --git a/test/parser_test.rb b/test/parser_test.rb
index b277aa5c..6caab147 100644
--- a/test/parser_test.rb
+++ b/test/parser_test.rb
@@ -172,7 +172,7 @@ context "Parser" do
test "collect options attribute" do
attributes = {}
line = "quote, options='opt1,opt2 , opt3'"
- expected = {1 => 'quote', 'options' => 'opt1,opt2 , opt3', 'opt1-option' => '', 'opt2-option' => '', 'opt3-option' => ''}
+ expected = {1 => 'quote', 'options' => 'opt1,opt2,opt3', 'opt1-option' => '', 'opt2-option' => '', 'opt3-option' => ''}
Asciidoctor::AttributeList.new(line).parse_into(attributes)
assert_equal expected, attributes
end
@@ -180,7 +180,7 @@ context "Parser" do
test "collect opts attribute as options" do
attributes = {}
line = "quote, opts='opt1,opt2 , opt3'"
- expected = {1 => 'quote', 'options' => 'opt1,opt2 , opt3', 'opt1-option' => '', 'opt2-option' => '', 'opt3-option' => ''}
+ expected = {1 => 'quote', 'options' => 'opt1,opt2,opt3', 'opt1-option' => '', 'opt2-option' => '', 'opt3-option' => ''}
Asciidoctor::AttributeList.new(line).parse_into(attributes)
assert_equal expected, attributes
end