Class: Asciidoctor::ListsExtended::HtmlRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor-lists-extended/html_renderer.rb

Overview

Replaces list-of

UUID placeholder paragraphs with AsciiDoc xref content

for HTML5 and other non-PDF backends.

Preserves full backwards-compatibility with the original asciidoctor-lists gem:

- enhanced_rendering parameter: separates caption and title
- hide_empty_section parameter: removes parent section when no entries found

Instance Method Summary collapse

Instance Method Details

#render(document) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/asciidoctor-lists-extended/html_renderer.rb', line 12

def render(document)
  uuid_blocks = document.find_by do |b|
    b.content_model == :simple &&
      b.lines.size == 1 &&
      ListMacroAttributes.key?(b.lines[0])
  end

  uuid_blocks.each do |block|
    params             = ListMacroAttributes[block.lines[0]]
    enhanced_rendering = params[:enhanced_rendering]
    hide_empty_section = params[:hide_empty_section]

    scope    = scope_node_for(block, document)
    scope    = document if params[:scope_global]
    elements = scope.find_by(
      traverse_documents: true,
      context: params[:element].to_sym
    )

    titled_elements = elements.select { |e| (e.caption || e.title) && !e.has_role?('exclude-from-listof') }

    if titled_elements.empty? && hide_empty_section
      block.parent.parent.blocks.delete(block.parent)
      next
    end

    strip_period = params[:strip_period]

    references_asciidoc = titled_elements.map do |element|
      caption_text = element.caption&.rstrip
      caption_text = caption_text.sub(/\.\z/, '') if strip_period && caption_text
      if enhanced_rendering
        if caption_text
          %(xref:#{element.id}[#{caption_text}] #{element.instance_variable_get(:@title)} +)
        else
          %(xref:#{element.id}[#{element.instance_variable_get(:@title)}] +)
        end
      else
        if caption_text
          %(xref:#{element.id}[#{caption_text}] #{element.title} +)
        else
          %(xref:#{element.id}[#{element.title}] +)
        end
      end
    end

    block_index     = block.parent.blocks.index { |b| b == block }
    parsed_blocks   = parse_asciidoc(block.parent, references_asciidoc)
    parsed_blocks.reverse_each { |b| block.parent.blocks.insert block_index, b }
    block.parent.blocks.delete_at block_index + parsed_blocks.size
  end
end