Class: Coradoc::Element::TextElement

Inherits:
Base
  • Object
show all
Defined in:
lib/coradoc/element/text_element.rb

Direct Known Subclasses

Highlight

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#children_accessors, children_accessors, declare_children, #simplify_block_content, visit, #visit

Constructor Details

#initialize(content, options = {}) ⇒ TextElement

Returns a new instance of TextElement.



8
9
10
11
12
13
14
15
16
# File 'lib/coradoc/element/text_element.rb', line 8

def initialize(content, options = {})
  @content = content # .to_s
  @id = options.fetch(:id, nil)
  @line_break = options.fetch(:line_break, "")
  @html_cleanup = options.fetch(:html_cleanup, false)
  if @html_cleanup
    @content = treat_text_to_adoc(@content)
  end
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



4
5
6
# File 'lib/coradoc/element/text_element.rb', line 4

def content
  @content
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/coradoc/element/text_element.rb', line 4

def id
  @id
end

#line_breakObject

Returns the value of attribute line_break.



4
5
6
# File 'lib/coradoc/element/text_element.rb', line 4

def line_break
  @line_break
end

Class Method Details

.escape_keychars(string) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/coradoc/element/text_element.rb', line 63

def self.escape_keychars(string)
  subs = { "*" => '\*', "_" => '\_' }
  string
    .gsub(/((?<=\s)[\*_]+)|[\*_]+(?=\s)/) do |n|
    n.chars.map do |char|
      subs[char]
    end.join
  end
end

Instance Method Details



45
46
47
# File 'lib/coradoc/element/text_element.rb', line 45

def escape_links(text)
  text.gsub(/<<([^ ][^>]*)>>/, "\\<<\\1>>")
end

#inspectObject



18
19
20
21
22
23
24
25
# File 'lib/coradoc/element/text_element.rb', line 18

def inspect
  str = "TextElement"
  str += "(#{@id})" if @id
  str += ": "
  str += @content.inspect
  str += " + #{@line_break.inspect}" unless line_break.empty?
  str
end

#preserve_keychars_within_backticks(text) ⇒ Object



57
58
59
60
61
# File 'lib/coradoc/element/text_element.rb', line 57

def preserve_keychars_within_backticks(text)
  text.gsub(/`.*?`/) do |match|
    match.gsub('\_', "_").gsub('\*', "*")
  end
end

#preserve_nbsp(text) ⇒ Object



41
42
43
# File 'lib/coradoc/element/text_element.rb', line 41

def preserve_nbsp(text)
  text.gsub(/\u00A0/, "&nbsp;")
end

#remove_border_newlines(text) ⇒ Object



49
50
51
# File 'lib/coradoc/element/text_element.rb', line 49

def remove_border_newlines(text)
  text.gsub(/\A\n+/, "").gsub(/\n+\z/, "")
end

#remove_inner_newlines(text) ⇒ Object



53
54
55
# File 'lib/coradoc/element/text_element.rb', line 53

def remove_inner_newlines(text)
  text.tr("\n\t", " ").squeeze(" ")
end

#to_adocObject



27
28
29
# File 'lib/coradoc/element/text_element.rb', line 27

def to_adoc
  Coradoc::Generator.gen_adoc(@content) + @line_break
end

#treat_text_to_adoc(text) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/coradoc/element/text_element.rb', line 31

def treat_text_to_adoc(text)
  text = preserve_nbsp(text)
  text = remove_border_newlines(text)
  text = remove_inner_newlines(text)
  text = self.class.escape_keychars(text)

  text = preserve_keychars_within_backticks(text)
  escape_links(text)
end