Class: AsciidoctorLists::Asciidoctor::ListTreeprocessor

Inherits:
Asciidoctor::Extensions::Treeprocessor
  • Object
show all
Defined in:
lib/asciidoctor-lists/extensions.rb

Overview

Searches for the elements and replaced the UUIDs with the lists Inspired by github.com/asciidoctor/asciidoctor-bibtex/blob/master/lib/asciidoctor-bibtex/extensions.rb#L162

Instance Method Summary collapse

Instance Method Details

#parse_asciidoc(parent, content, attributes = {}) ⇒ Object

This is an adapted version of Asciidoctor::Extension::parse_content, where resultant blocks are returned as a list instead of attached to the parent.



85
86
87
88
89
90
91
92
93
# File 'lib/asciidoctor-lists/extensions.rb', line 85

def parse_asciidoc(parent, content, attributes = {})
  result = []
  reader = ::Asciidoctor::Reader.new content
  while reader.has_more_lines?
    block = ::Asciidoctor::Parser.next_block reader, parent, attributes
    result << block if block
  end
  result
end

#process(document) ⇒ Object



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/asciidoctor-lists/extensions.rb', line 30

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) \
       && (ListMacroAttributes.keys.include?(b.lines[0]))
  end
  tof_blocks.each do |block|
    references_asciidoc = []

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

    elements = document.find_by(traverse_documents: true, 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
            document.catalog[:refs][element.id] = element
          end

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

    block_index = block.parent.blocks.index do |b|
      b == block
    end
    reference_blocks = parse_asciidoc block.parent, references_asciidoc
    reference_blocks.reverse.each do |b|
      block.parent.blocks.insert block_index, b
    end
    block.parent.blocks.delete_at block_index + reference_blocks.size
  end
end