Skip to content

Commit

Permalink
Rename based on suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
Alwinator committed Mar 17, 2022
1 parent 0638c7a commit b5e6ece
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ gem install asciidoctor-lists
...
=== List of figures
element_list::[element=image]
list-of::image[]
=== List of tables
element_list::[element=table]
list-of::table[]
=== List of code snippets
element_list::[element=listing]
list-of::listing[]
----

See link:samples/list-sample.adoc[]
Expand Down
30 changes: 14 additions & 16 deletions lib/asciidoctor-lists/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,39 @@
module AsciidoctorLists
module Asciidoctor

MacroPlaceholder = Hash.new
ListMacroAttributes = Hash.new

# Replaces element_list::[element=...] with ListOfFiguresMacroPlaceholder
class ListOfFiguresMacro < ::Asciidoctor::Extensions::BlockMacroProcessor
# Replaces list-of::element[] with UUID and saves attributes in ListMacroPlaceholder
class ListMacro < ::Asciidoctor::Extensions::BlockMacroProcessor
use_dsl
named :element_list
name_positional_attributes 'element'
named :"list-of"
name_positional_attributes 'enhanced_rendering'

def process(parent, _target, attrs)
def process(parent, target, attrs)
uuid = SecureRandom.uuid
MacroPlaceholder[uuid] = {
element: attrs['element'],
ListMacroAttributes[uuid] = {
element: target,
enhanced_rendering: attrs['enhanced_rendering']
}
create_paragraph parent, uuid, {}
end
end
# Searches for the figures and replaced ListOfFiguresMacroPlaceholder with the list of figures
# Searches for the elements and replaced the UUIDs with the lists
# Inspired by https://github.com/asciidoctor/asciidoctor-bibtex/blob/master/lib/asciidoctor-bibtex/extensions.rb#L162
class ListOfFiguresTreeprocessor < ::Asciidoctor::Extensions::Treeprocessor
class ListTreeprocessor < ::Asciidoctor::Extensions::Treeprocessor
def process(document)
tof_blocks = document.find_by do |b|
# for fast search (since most searches shall fail)
(b.content_model == :simple) && (b.lines.size == 1) \
&& (MacroPlaceholder.keys.include?(b.lines[0]))
&& (ListMacroAttributes.keys.include?(b.lines[0]))
end
tof_blocks.each do |block|
references_asciidoc = []

params = MacroPlaceholder[block.lines[0]]
element_name = ":" + params[:element]
params = ListMacroAttributes[block.lines[0]]
enhanced_rendering = params[:enhanced_rendering]

document.find_by(context: eval(element_name)).each do |element|
document.find_by(context: params[:element].to_sym).each do |element|

if element.caption or element.title
unless element.id
Expand Down Expand Up @@ -90,6 +88,6 @@ def parse_asciidoc(parent, content, attributes = {})

# Register the extensions to asciidoctor
Asciidoctor::Extensions.register do
block_macro AsciidoctorLists::Asciidoctor::ListOfFiguresMacro
tree_processor AsciidoctorLists::Asciidoctor::ListOfFiguresTreeprocessor
block_macro AsciidoctorLists::Asciidoctor::ListMacro
tree_processor AsciidoctorLists::Asciidoctor::ListTreeprocessor
end
2 changes: 1 addition & 1 deletion lib/asciidoctor-lists/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module AsciidoctorLists
VERSION = '1.0.5'
VERSION = '1.0.6'
end
2 changes: 1 addition & 1 deletion samples/enhanced-rendering.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<<<
== Table of Figures
element_list::[element=image, enhanced_rendering=true]
list-of::image[enhanced_rendering=true]

<<<
:sectnums:
Expand Down
6 changes: 3 additions & 3 deletions samples/list-sample.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ end
image::https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/SVG_Logo.svg/400px-SVG_Logo.svg.png[SVG,100,100]

=== List of figures
element_list::[element=image]
list-of::image[]

=== List of tables
element_list::[element=table]
list-of::table[]

=== List of code snippets
element_list::[element=listing]
list-of::listing[]

0 comments on commit b5e6ece

Please sign in to comment.