Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolves #59 track header attributes in source document #60

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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` attr 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
Loading