Skip to content

Commit

Permalink
Merge pull request #9 from Alwinator/feature/8_hide-if-empty-list-sample
Browse files Browse the repository at this point in the history
Fix #8
  • Loading branch information
Alwinator authored Apr 28, 2022
2 parents 3c4475b + 5dc07b4 commit 0653c54
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ jobs:
- name: Build enhanced-rendering as PDF
run: asciidoctor-pdf -a allow-uri-read -r asciidoctor-lists samples/enhanced-rendering.adoc

- name: Build hide-if-empty-list-sample as PDF
run: asciidoctor-pdf -a allow-uri-read -r asciidoctor-lists samples/hide-if-empty-list-sample.adoc

- name: Archive production artifacts
uses: actions/upload-artifact@v2
with:
Expand Down
5 changes: 5 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ Allows rendering links in the caption

Sample: link:samples/enhanced-rendering.adoc[]

=== hide_empty_section
Removes the section when no elements are found

Sample: link:samples/hide-if-empty-list-sample.adoc

== Docker
[source,bash]
----
Expand Down
40 changes: 24 additions & 16 deletions lib/asciidoctor-lists/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ class ListMacro < ::Asciidoctor::Extensions::BlockMacroProcessor
use_dsl
named :"list-of"
name_positional_attributes 'enhanced_rendering'
name_positional_attributes 'hide_empty_section'

def process(parent, target, attrs)
uuid = SecureRandom.uuid
ListMacroAttributes[uuid] = {
element: target,
enhanced_rendering: attrs['enhanced_rendering']
enhanced_rendering: attrs['enhanced_rendering'],
hide_empty_section: attrs['hide_empty_section']
}
create_paragraph parent, uuid, {}
end
Expand All @@ -36,28 +38,34 @@ def process(document)

params = ListMacroAttributes[block.lines[0]]
enhanced_rendering = params[:enhanced_rendering]
hide_empty_section = params[:hide_empty_section]

document.find_by(context: params[:element].to_sym).each do |element|
elements = document.find_by(context: params[:element].to_sym)
if elements.length > 0
elements.each do |element|

if element.caption or element.title
unless element.id
element.id = SecureRandom.uuid
end
if element.caption or element.title
unless element.id
element.id = SecureRandom.uuid
end

if enhanced_rendering
if element.caption
references_asciidoc << %(xref:#{element.id}[#{element.caption}]#{element.instance_variable_get(:@title)} +)
else element.caption
references_asciidoc << %(xref:#{element.id}[#{element.instance_variable_get(:@title)}] +)
if enhanced_rendering
if element.caption
references_asciidoc << %(xref:#{element.id}[#{element.caption}]#{element.instance_variable_get(:@title)} +)
else element.caption
references_asciidoc << %(xref:#{element.id}[#{element.instance_variable_get(:@title)}] +)
end
else
if element.caption
references_asciidoc << %(xref:#{element.id}[#{element.caption}]#{element.title} +)
else element.caption
references_asciidoc << %(xref:#{element.id}[#{element.title}] +)
end
else
if element.caption
references_asciidoc << %(xref:#{element.id}[#{element.caption}]#{element.title} +)
else element.caption
references_asciidoc << %(xref:#{element.id}[#{element.title}] +)
end
end
end
elsif hide_empty_section
block.parent.parent.blocks.delete block.parent
end

block_index = block.parent.blocks.index do |b|
Expand Down
14 changes: 14 additions & 0 deletions samples/hide-if-empty-list-sample.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
== Test the List Macro for empty lists
:listing-caption: Code

.Another wikipedia SVG image
image::https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/SVG_Logo.svg/400px-SVG_Logo.svg.png[SVG,100,100]

=== List of figures
list-of::image[hide_empty_section=true]

=== List of tables
list-of::table[hide_empty_section=true]

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

0 comments on commit 0653c54

Please sign in to comment.