Class: Coradoc::Element::TextElement
- Inherits:
-
Base
- Object
- Base
- Coradoc::Element::TextElement
show all
- Defined in:
- lib/coradoc/element/text_element.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
access_children, #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 @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
#content ⇒ Object
Returns the value of attribute content.
4
5
6
|
# File 'lib/coradoc/element/text_element.rb', line 4
def content
@content
end
|
#id ⇒ Object
Returns the value of attribute id.
4
5
6
|
# File 'lib/coradoc/element/text_element.rb', line 4
def id
@id
end
|
#line_break ⇒ Object
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
66
67
68
69
70
71
72
73
74
|
# File 'lib/coradoc/element/text_element.rb', line 66
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
#escape_links(text) ⇒ Object
48
49
50
|
# File 'lib/coradoc/element/text_element.rb', line 48
def escape_links(text)
text.gsub(/<<([^ ][^>]*)>>/, "\\<<\\1>>")
end
|
#inspect ⇒ Object
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.to_s.empty?
str
end
|
#preserve_keychars_within_backticks(text) ⇒ Object
60
61
62
63
64
|
# File 'lib/coradoc/element/text_element.rb', line 60
def preserve_keychars_within_backticks(text)
text.gsub(/`.*?`/) do |match|
match.gsub('\_', "_").gsub('\*', "*")
end
end
|
#preserve_nbsp(text) ⇒ Object
44
45
46
|
# File 'lib/coradoc/element/text_element.rb', line 44
def preserve_nbsp(text)
text.gsub(/\u00A0/, " ")
end
|
#remove_border_newlines(text) ⇒ Object
52
53
54
|
# File 'lib/coradoc/element/text_element.rb', line 52
def remove_border_newlines(text)
text.gsub(/\A\n+/, "").gsub(/\n+\z/, "")
end
|
#remove_inner_newlines(text) ⇒ Object
56
57
58
|
# File 'lib/coradoc/element/text_element.rb', line 56
def remove_inner_newlines(text)
text.tr("\n\t", " ").squeeze(" ")
end
|
#to_adoc ⇒ Object
27
28
29
30
31
32
|
# File 'lib/coradoc/element/text_element.rb', line 27
def to_adoc
str = ""
str += "[[#{@id}]] " if @id
str += Coradoc::Generator.gen_adoc(@content) + @line_break.to_s
str
end
|
#treat_text_to_adoc(text) ⇒ Object
34
35
36
37
38
39
40
41
42
|
# File 'lib/coradoc/element/text_element.rb', line 34
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
|