Module: Coradoc::CoreModel::Builder::Detection Private

Included in:
Coradoc::CoreModel::Builder
Defined in:
lib/coradoc/core_model/builder/detection.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Detection module for Builder

Contains methods for detecting element types and extracting information from AST structures.

Instance Method Summary collapse

Instance Method Details

#annotation_delimitersObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

List of annotation delimiters (first character)



176
177
178
# File 'lib/coradoc/core_model/builder/detection.rb', line 176

def annotation_delimiters
  %w[* / =]
end

#annotation_typesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

List of annotation types



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

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

#detect_block_type(ast) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Detect block type for specialized block creation



38
39
40
41
42
43
44
45
46
47
# File 'lib/coradoc/core_model/builder/detection.rb', line 38

def detect_block_type(ast)
  return :annotation if extract_annotation_type(ast)

  delimiter = ast[:delimiter]&.to_s
  return :annotation if delimiter && annotation_delimiters.include?(delimiter[0])

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

  :generic
end

#detect_constrained(ast, format_type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Detect if inline formatting is constrained



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/coradoc/core_model/builder/detection.rb', line 143

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Detect the type of element from AST structure



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/coradoc/core_model/builder/detection.rb', line 14

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Detect inline format type



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/coradoc/core_model/builder/detection.rb', line 129

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Detect marker level



121
122
123
124
125
126
# File 'lib/coradoc/core_model/builder/detection.rb', line 121

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

  1
end

#detect_marker_type(ast) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Detect marker type for lists



110
111
112
113
114
115
116
117
118
# File 'lib/coradoc/core_model/builder/detection.rb', line 110

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

  'asterisk'
end

#extract_annotation_label(ast) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extract annotation label



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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extract annotation type from AST



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/coradoc/core_model/builder/detection.rb', line 70

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]

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

  nil
end

#extract_level(ast) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extract level from section AST



160
161
162
163
164
165
166
167
168
# File 'lib/coradoc/core_model/builder/detection.rb', line 160

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

#has_block_structure?(ast) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if AST has block structure

Returns:

  • (Boolean)


60
61
62
# File 'lib/coradoc/core_model/builder/detection.rb', line 60

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

#has_header_structure?(ast) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if AST has header structure

Returns:

  • (Boolean)


50
51
52
# File 'lib/coradoc/core_model/builder/detection.rb', line 50

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

#has_inline_structure?(ast) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if AST has inline structure

Returns:

  • (Boolean)


65
66
67
# File 'lib/coradoc/core_model/builder/detection.rb', line 65

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

#has_section_structure?(ast) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Check if AST has section structure

Returns:

  • (Boolean)


55
56
57
# File 'lib/coradoc/core_model/builder/detection.rb', line 55

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

#inline_format_typesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

List of inline format types



186
187
188
189
190
191
# File 'lib/coradoc/core_model/builder/detection.rb', line 186

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

List of list markers



181
182
183
# File 'lib/coradoc/core_model/builder/detection.rb', line 181

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