Class: DocXify::Element::Paragraph

Inherits:
Base
  • Object
show all
Defined in:
lib/docxify/element/paragraph.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, options = {}) ⇒ Paragraph

Returns a new instance of Paragraph.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/docxify/element/paragraph.rb', line 6

def initialize(text, options = {})
  super()
  @document = options[:document]
  @text = text
  @font = options[:font] || @document&.font || "Times New Roman"
  @size = options[:size] || @document&.size || 14
  @color = options[:color] || @document&.color || "#000000"
  @highlight = options[:highlight] || false
  @background = options[:background] if options[:background]
  @background ||= @document&.background if @document&.background
  @align = options[:align] || :left
  @after = options[:after]
  @inline_styling = options.key?(:inline_styling) ? options[:inline_styling] : true
  @tab_stops_cm = options[:tab_stops_cm] || []
  @hanging_cm = options[:hanging_cm] || 0
end

Instance Attribute Details

#alignObject

Returns the value of attribute align.



4
5
6
# File 'lib/docxify/element/paragraph.rb', line 4

def align
  @align
end

#backgroundObject

Returns the value of attribute background.



4
5
6
# File 'lib/docxify/element/paragraph.rb', line 4

def background
  @background
end

#colorObject

Returns the value of attribute color.



4
5
6
# File 'lib/docxify/element/paragraph.rb', line 4

def color
  @color
end

#fontObject

Returns the value of attribute font.



4
5
6
# File 'lib/docxify/element/paragraph.rb', line 4

def font
  @font
end

#hanging_cmObject

Returns the value of attribute hanging_cm.



4
5
6
# File 'lib/docxify/element/paragraph.rb', line 4

def hanging_cm
  @hanging_cm
end

#inline_stylingObject

Returns the value of attribute inline_styling.



4
5
6
# File 'lib/docxify/element/paragraph.rb', line 4

def inline_styling
  @inline_styling
end

#sizeObject

Returns the value of attribute size.



4
5
6
# File 'lib/docxify/element/paragraph.rb', line 4

def size
  @size
end

#tab_stops_cmObject

Returns the value of attribute tab_stops_cm.



4
5
6
# File 'lib/docxify/element/paragraph.rb', line 4

def tab_stops_cm
  @tab_stops_cm
end

#textObject

Returns the value of attribute text.



4
5
6
# File 'lib/docxify/element/paragraph.rb', line 4

def text
  @text
end

Instance Method Details

#ref_for_href(href) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/docxify/element/paragraph.rb', line 83

def ref_for_href(href)
  relation = nil

  @document.relationships.select { |r| r.is_a?(DocXify::Element::WebAddress) }.each do |r|
    if r.target == href
      relation = r
      break
    end
  end

  if relation.nil?
    relation = DocXify::Element::WebAddress.new(href)
    @document.relationships << relation
  end

  relation.reference
end

#to_s(_container = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/docxify/element/paragraph.rb', line 23

def to_s(_container = nil)
  nodes = if @inline_styling
    parse_simple_html(@text)
  else
    [@text.gsub("<", "&lt;").gsub(">", "&gt;")]
  end

  xml = "<w:p>"

  xml << "<w:pPr>"
  xml << "<w:jc w:val=\"#{@align}\"/>" if @align != :left
  xml << "<w:spacing w:after=\"#{DocXify.pt2spacing @after}\"/>" if @after

  if tab_stops_cm.any?
    xml << "<w:tabs>"
    tab_stops_cm.each do |stop|
      xml << "<w:tab w:pos=\"#{DocXify.cm2dxa(stop)}\" w:val=\"left\"/>"
    end
    xml << "</w:tabs>"
  end

  if @hanging_cm&.positive?
    xml << "<w:ind w:hanging=\"#{DocXify.cm2dxa(@hanging_cm)}\" w:left=\"#{DocXify.cm2dxa(@hanging_cm)}\"/>"
  end

  xml << "</w:pPr>"

  nodes.each do |node|
    if node.is_a?(Hash) && node[:tag] == "a"
      xml << "<w:hyperlink r:id=\"#{ref_for_href(node[:attributes][:href])}\">"
    end

    xml << "<w:r>"
    xml << <<~XML
      <w:rPr>
        <w:rFonts w:ascii="#{@font}" w:cs="#{@font}" w:hAnsi="#{@font}"/>
        <w:color w:val="#{@color.gsub("#", "")}"/>
        <w:sz w:val="#{DocXify.pt2halfpt(@size)}"/>
        <w:szCs w:val="#{DocXify.pt2halfpt(@size)}"/>
        #{"<w:highlight w:val=\"yellow\"/>" if @highlight || (node.is_a?(Hash) && node[:tag].match?("mark"))}
        #{"<w:b/><w:bCs/>" if node.is_a?(Hash) && node[:tag].match?("b")}
        #{"<w:i/><w:iCs/>" if node.is_a?(Hash) && node[:tag].match?("i")}
        #{"<w:u w:val=\"single\"/>" if node.is_a?(Hash) && (node[:tag].match?("u") || node[:tag] == "a")}
        #{"<w:rStyle w:val=\"Hyperlink\"/>" if node.is_a?(Hash) && node[:tag] == "a"}
      </w:rPr>
    XML

    content = node.is_a?(Hash) ? node[:content] : node
    content = content.gsub("{CHECKBOX_EMPTY}", "").gsub("{CHECKBOX_CHECKED}", "")
    xml << "<w:t xml:space=\"preserve\">#{content}</w:t>"
    xml << "</w:r>"

    if node.is_a?(Hash) && node[:tag] == "a"
      xml << "</w:hyperlink>"
    end
  end

  xml << "</w:p>"
end