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_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



168
169
170
# File 'lib/coradoc/core_model/builder/detection.rb', line 168

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
# File 'lib/coradoc/core_model/builder/detection.rb', line 38

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

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



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

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



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/coradoc/core_model/builder/detection.rb', line 126

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



118
119
120
121
122
123
# File 'lib/coradoc/core_model/builder/detection.rb', line 118

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



107
108
109
110
111
112
113
114
115
# File 'lib/coradoc/core_model/builder/detection.rb', line 107

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



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

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



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

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



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

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)


57
58
59
# File 'lib/coradoc/core_model/builder/detection.rb', line 57

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)


47
48
49
# File 'lib/coradoc/core_model/builder/detection.rb', line 47

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)


62
63
64
# File 'lib/coradoc/core_model/builder/detection.rb', line 62

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)


52
53
54
# File 'lib/coradoc/core_model/builder/detection.rb', line 52

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



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

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



173
174
175
# File 'lib/coradoc/core_model/builder/detection.rb', line 173

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