diff options
| -rw-r--r-- | lib/asciidoctor/parser.rb | 12 | ||||
| -rw-r--r-- | lib/asciidoctor/reader.rb | 13 | ||||
| -rw-r--r-- | test/fixtures/chapter-a.adoc | 3 | ||||
| -rw-r--r-- | test/fixtures/master.adoc | 5 | ||||
| -rw-r--r-- | test/reader_test.rb | 23 | ||||
| -rw-r--r-- | test/sections_test.rb | 25 |
6 files changed, 80 insertions, 1 deletions
diff --git a/lib/asciidoctor/parser.rb b/lib/asciidoctor/parser.rb index d8345ae6..39afedda 100644 --- a/lib/asciidoctor/parser.rb +++ b/lib/asciidoctor/parser.rb @@ -2072,6 +2072,7 @@ class Parser # # returns a 2-element array containing the attribute name and value def self.store_attribute(name, value, doc = nil, attrs = nil) + # TODO move processing of attribute value to utility method if name.end_with?('!') # a nil value signals the attribute should be deleted (undefined) value = nil @@ -2085,10 +2086,19 @@ class Parser name = sanitize_attribute_name(name) accessible = true if doc + # support relative leveloffset values + if name == 'leveloffset' && value + case value[0..0] + when '+' + value = ((doc.attr 'leveloffset', 0).to_i + (value[1..-1] || 0).to_i).to_s + when '-' + value = ((doc.attr 'leveloffset', 0).to_i - (value[1..-1] || 0).to_i).to_s + end + end accessible = value.nil? ? doc.delete_attribute(name) : doc.set_attribute(name, value) end - unless !accessible || attrs.nil? + if accessible && attrs Document::AttributeEntry.new(name, value).save_to(attrs) end diff --git a/lib/asciidoctor/reader.rb b/lib/asciidoctor/reader.rb index 84d251f1..0ae2a653 100644 --- a/lib/asciidoctor/reader.rb +++ b/lib/asciidoctor/reader.rb @@ -1031,6 +1031,19 @@ class PreprocessorReader < Reader # effectively fill the buffer @lines = prepare_lines data, :normalize => true, :condense => false, :indent => attributes['indent'] + + # FIXME we eventually want to handle leveloffset without affecting the lines + if attributes.has_key? 'leveloffset' + @lines.unshift '' + @lines.unshift %(:leveloffset: #{attributes['leveloffset']}) + @lines.push '' + if (old_leveloffset = @document.attr 'leveloffset') + @lines.push %(:leveloffset: #{old_leveloffset}) + else + @lines.push ':leveloffset!:' + end + end + # FIXME kind of a hack #Document::AttributeEntry.new('infile', @file).save_to_next_block @document #Document::AttributeEntry.new('indir', @dir).save_to_next_block @document diff --git a/test/fixtures/chapter-a.adoc b/test/fixtures/chapter-a.adoc new file mode 100644 index 00000000..a66a1bee --- /dev/null +++ b/test/fixtures/chapter-a.adoc @@ -0,0 +1,3 @@ += Chapter A + +content diff --git a/test/fixtures/master.adoc b/test/fixtures/master.adoc new file mode 100644 index 00000000..5793b145 --- /dev/null +++ b/test/fixtures/master.adoc @@ -0,0 +1,5 @@ += Master Document + +preamble + +include::chapter-a.adoc[leveloffset=+1] diff --git a/test/reader_test.rb b/test/reader_test.rb index 145f0a7b..77ba006b 100644 --- a/test/reader_test.rb +++ b/test/reader_test.rb @@ -774,6 +774,29 @@ include::fixtures/include-file.asciidoc[] source = lines * ::Asciidoctor::EOL assert_match(/included content/, source) end + + test 'leveloffset attribute entries should be added to content if leveloffset attribute is specified' do + input = <<-EOS +include::fixtures/master.adoc[] + EOS + + expected = <<-EOS.chomp.split(::Asciidoctor::EOL) += Master Document + +preamble + +:leveloffset: +1 + += Chapter A + +content + +:leveloffset!: + EOS + + document = Asciidoctor.load input, :safe => :safe, :base_dir => DIRNAME, :parse => false + assert_equal expected, document.reader.read_lines + end test 'attributes are substituted in target of include directive' do input = <<-EOS diff --git a/test/sections_test.rb b/test/sections_test.rb index 6f4adb7e..7df0bba9 100644 --- a/test/sections_test.rb +++ b/test/sections_test.rb @@ -667,6 +667,31 @@ Standalone preamble. assert_xpath '//*[@class = "sect1"]/h2[text() = "Standalone Document"]', output, 1 assert_xpath '//*[@class = "sect1"]/h2[text() = "Level 1 Section"]', output, 1 end + + test 'should add relative offset value to current leveloffset' do + input = <<-EOS += Master Document +Doc Writer + +Master preamble. + +:leveloffset: 1 + += Chapter 1 + +content + +:leveloffset: +1 + += Standalone Section + +content + EOS + + output = render_string input + assert_xpath '//*[@class = "sect1"]/h2[text() = "Chapter 1"]', output, 1 + assert_xpath '//*[@class = "sect2"]/h3[text() = "Standalone Section"]', output, 1 + end end context 'Section Numbering' do |
