Skip to content

Commit

Permalink
resolves #59 track header attributes in source document and assign to…
Browse files Browse the repository at this point in the history
… source_header_attributes property on Document instance
  • Loading branch information
mojavelinux committed Nov 23, 2024
1 parent ffc8bc6 commit 024ea89
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
This document provides a curated view of the changes to Asciidoctor Reducer in each release.
For a detailed view of what has changed, refer to the {url-repo}/commits/main[commit history] on GitHub.

== Unreleased

=== Added

* Track header attributes in source document and assign to `source_header_attributes` property (reader) on Document instance (#59)

== 1.0.6 (2024-02-12) - @mojavelinux

=== Fixed
Expand Down
2 changes: 2 additions & 0 deletions lib/asciidoctor/reducer/extensions.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'asciidoctor' unless defined? Asciidoctor.load
require_relative 'header_attribute_tracker'
require_relative 'preprocessor'
require_relative 'tree_processor'

Expand All @@ -10,6 +11,7 @@ module Extensions

def group
proc do
document.extend HeaderAttributeTracker
next if document.options[:reduced] # group invoked again if includes are found and sourcemap option is true
preprocessor Preprocessor
tree_processor TreeProcessor
Expand Down
16 changes: 16 additions & 0 deletions lib/asciidoctor/reducer/header_attribute_tracker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Asciidoctor::Reducer
module HeaderAttributeTracker
def self.extended instance
instance.singleton_class.send :attr_reader, :source_header_attributes
end

def finalize_header(*) # rubocop:disable Style/MethodDefParentheses
@source_header_attributes = @attributes_modified.each_with_object({}) do |name, accum|
accum[name] = @attributes[name]
end
super
end
end
end
1 change: 1 addition & 0 deletions spec/extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
(expect group).to be_a Proc
doc = Asciidoctor.load []
reg = (Asciidoctor::Extensions.create described_class.key, &group).activate doc
(expect doc.singleton_class.ancestors.map(&:name)).to include 'Asciidoctor::Reducer::HeaderAttributeTracker'
(expect reg.preprocessors).to have_size 1
(expect reg.tree_processors).to have_size 1
end
Expand Down
13 changes: 12 additions & 1 deletion spec/reducer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# frozen_string_literal: true

describe Asciidoctor::Reducer do
let :empty_hash do
{}
end

it 'should be able to require library from Ruby process' do
# NOTE asciidoctor/reducer/version will already be required by Bundler
script_file = fixture_file 'print_version.rb'
Expand Down Expand Up @@ -43,6 +47,7 @@
(expect doc.attr 'docname').to eql (input_file_basename '.adoc')
(expect doc.attr 'docfile').to eql input_file
(expect doc.attr 'docdir').to eql (File.dirname input_file)
(expect doc.source_header_attributes).to eql empty_hash
end)
end
end
Expand All @@ -61,6 +66,7 @@
(expect doc.options[:reduced]).to be_falsy
(expect doc.sourcemap).to be_falsy
(expect doc.blocks[0].source_location).to be_nil
(expect doc.source_header_attributes).to eql empty_hash
end)
end
end
Expand Down Expand Up @@ -1704,6 +1710,7 @@
(expect (doc.attr? 'sectnums')).to be true
(expect (doc.attr? 'icons', 'font')).to be true
(expect (doc.attr? 'toc')).to be true
(expect doc.source_header_attributes).to eql 'sectnums' => '', 'icons' => 'font', 'toc' => ''
end)
end
end
Expand Down Expand Up @@ -1811,13 +1818,15 @@
run_scenario do
input_source <<~'END'
= Book Title
:chaptersdir: ignored
include::{chaptersdir}/ch1.adoc[]
END

reduce_options attributes: 'chaptersdir=chapters', sourcemap: true
expected_source <<~'END'
= Book Title
:chaptersdir: ignored
== Chapter One
Expand All @@ -1828,7 +1837,9 @@
delegate.call doc
blocks = doc.find_by {|it| it.context != :document }
(expect blocks).to have_size 3
(expect (blocks.map {|it| it.lineno })).to eql [1, 3, 5]
(expect (blocks.map {|it| it.lineno })).to eql [1, 4, 6]
# the hash is empty since the attribute is hard set by the API
(expect doc.source_header_attributes).to eql empty_hash
end)
end
end
Expand Down

0 comments on commit 024ea89

Please sign in to comment.