Module: Coradoc::CoreModel::Builder::TextBuilder Private

Included in:
Coradoc::CoreModel::Builder
Defined in:
lib/coradoc/core_model/builder/text_builder.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.

Text building module for Builder

Contains methods for building text and inline elements from AST structures.

Instance Method Summary collapse

Instance Method Details

#build_nested_inlines(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.

Build nested inline elements



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/coradoc/core_model/builder/text_builder.rb', line 74

def build_nested_inlines(ast)
  nested = []

  ast.each_value do |value|
    next unless value.is_a?(Array)

    value.each do |item|
      nested << build_inline(item) if item.is_a?(Hash) && has_inline_structure?(item)
    end
  end

  nested
end

#build_paragraph_content(lines_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.

Build paragraph content from lines



13
14
15
16
17
# File 'lib/coradoc/core_model/builder/text_builder.rb', line 13

def build_paragraph_content(lines_ast)
  return [] unless lines_ast

  Array(lines_ast).map { |line| build_text_element(line) }.compact
end

#build_text(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.

Build text from AST



34
35
36
# File 'lib/coradoc/core_model/builder/text_builder.rb', line 34

def build_text(ast)
  build_text_element(ast)
end

#build_text_element(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.

Build text element



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/coradoc/core_model/builder/text_builder.rb', line 20

def build_text_element(ast)
  return { type: :text, content: ast } if ast.is_a?(String)

  text_ast = ast[:text] ? ast : { text: ast }

  {
    type: :text,
    content: extract_text_content(text_ast),
    line_break: text_ast[:line_break],
    id: text_ast[:id]
  }
end

#extract_inline_content(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.

Extract inline content



64
65
66
67
68
69
70
71
# File 'lib/coradoc/core_model/builder/text_builder.rb', line 64

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_text_content(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 text content from various formats



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/coradoc/core_model/builder/text_builder.rb', line 39

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