Module: Coradoc::AsciiDoc::Builder::Detection

Included in:
Coradoc::AsciiDoc::Builder
Defined in:
lib/coradoc/asciidoc/builder/detection.rb

Instance Method Summary collapse

Instance Method Details

#annotation_typesObject



167
168
169
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 167

def annotation_types
  %w[note warning caution important tip reviewer sidebar]
end

#detect_block_type(ast) ⇒ Object



30
31
32
33
34
35
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 30

def detect_block_type(ast)
  return :annotation if extract_annotation_type(ast)
  return :list if ast[:marker] && list_markers.include?(ast[:marker].to_s)

  :generic
end

#detect_constrained(ast, format_type) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 66

def detect_constrained(ast, format_type)
  if format_type.end_with?('_constrained')
    return true
  elsif format_type.end_with?('_unconstrained')
    return false
  end

  key = "#{format_type}_constrained".to_sym
  return true if ast.key?(key)

  unconstrained_key = "#{format_type}_unconstrained".to_sym
  return false if ast.key?(unconstrained_key)

  true
end

#detect_element_type(ast) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 7

def detect_element_type(ast)
  return :header if ast.key?(:header) || has_header_structure?(ast)
  return :section if ast.key?(:section) || has_section_structure?(ast)
  return :block if ast.key?(:block) || has_block_structure?(ast)
  return :list if ast.key?(:list) || ast.key?(:unordered) ||
                  ast.key?(:ordered) || ast.key?(:definition_list)
  return :paragraph if ast.key?(:paragraph)
  return :text if ast.key?(:text)
  return :attribute if ast.key?(:key) && ast.key?(:value)
  return :document_attributes if ast.key?(:document_attributes)
  return :inline if has_inline_structure?(ast)
  return :line_break if ast.key?(:line_break) && ast.keys.length == 1
  return :comment_line if ast.key?(:comment_line)
  return :comment_block if ast.key?(:comment_block)
  return :include if ast.key?(:include)
  return :table if ast.key?(:table)
  return :unparsed if ast.key?(:unparsed)
  return :tag if ast.key?(:tag)
  return :bibliography_entry if ast.key?(:bibliography_entry)

  nil
end

#detect_inline_format(ast) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 53

def detect_inline_format(ast)
  ast.each_key do |key|
    key_str = key.to_s
    return key_str if key_str.end_with?('_constrained', '_unconstrained')
  end

  inline_format_types.each do |format|
    return format.to_s if ast.key?(format)
  end

  'text'
end

#detect_marker_level(ast) ⇒ Object



132
133
134
135
136
137
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 132

def detect_marker_level(ast)
  marker = ast[:marker]&.to_s
  return marker.length if marker&.match?(/^[*.]+$/)

  1
end

#detect_marker_type(ast) ⇒ Object



122
123
124
125
126
127
128
129
130
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 122

def detect_marker_type(ast)
  marker = ast[:marker]&.to_s
  return 'unordered' if marker&.start_with?('*')
  return 'unordered' if marker&.start_with?('-')
  return 'ordered' if marker&.match?(/^\d+\./) || marker&.start_with?('.')
  return 'definition' if marker&.end_with?('::')

  'unordered'
end

#extract_annotation_label(ast) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 107

def extract_annotation_label(ast)
  attr_list = ast[:attribute_list]
  return nil unless attr_list.is_a?(Hash)

  if attr_list[:named]
    named = Array(attr_list[:named])
    reviewer_attr = named.find do |n|
      n.is_a?(Hash) && n[:key] == 'reviewer'
    end
    return reviewer_attr[:value] if reviewer_attr
  end

  nil
end

#extract_annotation_type(ast) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 92

def extract_annotation_type(ast)
  attr_list = ast[:attribute_list]
  if attr_list.is_a?(Hash) && attr_list[:positional]
    positional = Array(attr_list[:positional])
    annotation = positional.find do |p|
      annotation_types.include?(p.to_s.downcase)
    end
    return annotation.to_s.downcase if annotation
  end

  return ast[:admonition_type]&.to_s&.downcase if ast[:admonition_type]

  nil
end

#extract_inline_content(ast, format_type) ⇒ Object



158
159
160
161
162
163
164
165
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 158

def extract_inline_content(ast, format_type)
  content_key = format_type.to_sym
  content = ast[content_key] ||
            ast["#{format_type}_constrained".to_sym] ||
            ast["#{format_type}_unconstrained".to_sym]

  extract_text_content(content)
end

#extract_level(ast) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 82

def extract_level(ast)
  if ast[:level]
    level_str = ast[:level].to_s
    return level_str.length - 1 if level_str.start_with?('=')
    return level_str.to_i if level_str.match?(/^\d+$/)
  end

  1
end

#extract_text_content(content) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 139

def extract_text_content(content)
  case content
  when String
    content
  when Array
    content.map { |c| extract_text_content(c) }.join
  when Hash
    if content[:text]
      extract_text_content(content[:text])
    elsif content[:paragraph_text]
      extract_text_content(content[:paragraph_text])
    else
      content.values.map { |v| extract_text_content(v) }.join
    end
  else
    content.to_s
  end
end

#has_block_structure?(ast) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 45

def has_block_structure?(ast)
  ast.key?(:delimiter) || ast.key?(:lines)
end

#has_header_structure?(ast) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 37

def has_header_structure?(ast)
  ast.key?(:title) && (ast.key?(:author) || ast.key?(:revision))
end

#has_inline_structure?(ast) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 49

def has_inline_structure?(ast)
  inline_format_types.any? { |type| ast.key?(type) }
end

#has_section_structure?(ast) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 41

def has_section_structure?(ast)
  ast.key?(:title) && ast.key?(:level)
end

#inline_format_typesObject



175
176
177
178
179
180
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 175

def inline_format_types
  %i[bold italic monospace superscript subscript highlight span link
     cross_reference bold_constrained bold_unconstrained
     italic_constrained italic_unconstrained monospace_constrained
     monospace_unconstrained]
end

#list_markersObject



171
172
173
# File 'lib/coradoc/asciidoc/builder/detection.rb', line 171

def list_markers
  %w[* - . ::]
end