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
# 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, "")
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



50
51
52
53
54
55
56
57
58
# File 'lib/coradoc/element/text_element.rb', line 50

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



32
33
34
# File 'lib/coradoc/element/text_element.rb', line 32

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

#preserve_keychars_within_backticks(text) ⇒ Object



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

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

#preserve_nbsp(text) ⇒ Object



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

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

#remove_border_newlines(text) ⇒ Object



36
37
38
# File 'lib/coradoc/element/text_element.rb', line 36

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

#remove_inner_newlines(text) ⇒ Object



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

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

#to_adocObject



14
15
16
# File 'lib/coradoc/element/text_element.rb', line 14

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

#treat_text_to_adoc(text) ⇒ Object



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

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