Class: DocXify::Element::Paragraph
- Defined in:
- lib/docxify/element/paragraph.rb
Instance Attribute Summary collapse
-
#align ⇒ Object
Returns the value of attribute align.
-
#background ⇒ Object
Returns the value of attribute background.
-
#color ⇒ Object
Returns the value of attribute color.
-
#font ⇒ Object
Returns the value of attribute font.
-
#hanging_cm ⇒ Object
Returns the value of attribute hanging_cm.
-
#inline_styling ⇒ Object
Returns the value of attribute inline_styling.
-
#size ⇒ Object
Returns the value of attribute size.
-
#tab_stops_cm ⇒ Object
Returns the value of attribute tab_stops_cm.
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
-
#initialize(text, options = {}) ⇒ Paragraph
constructor
A new instance of Paragraph.
- #ref_for_href(href) ⇒ Object
- #to_s(_container = nil) ⇒ Object
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, = {}) super() @document = [:document] @text = text @font = [:font] || @document&.font || "Times New Roman" @size = [:size] || @document&.size || 14 @color = [:color] || @document&.color || "#000000" @highlight = [:highlight] || false @background = [:background] if [:background] @background ||= @document&.background if @document&.background @align = [:align] || :left @after = [:after] @inline_styling = .key?(:inline_styling) ? [:inline_styling] : true @tab_stops_cm = [:tab_stops_cm] || [] @hanging_cm = [:hanging_cm] || 0 end |
Instance Attribute Details
#align ⇒ Object
Returns the value of attribute align.
4 5 6 |
# File 'lib/docxify/element/paragraph.rb', line 4 def align @align end |
#background ⇒ Object
Returns the value of attribute background.
4 5 6 |
# File 'lib/docxify/element/paragraph.rb', line 4 def background @background end |
#color ⇒ Object
Returns the value of attribute color.
4 5 6 |
# File 'lib/docxify/element/paragraph.rb', line 4 def color @color end |
#font ⇒ Object
Returns the value of attribute font.
4 5 6 |
# File 'lib/docxify/element/paragraph.rb', line 4 def font @font end |
#hanging_cm ⇒ Object
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_styling ⇒ Object
Returns the value of attribute inline_styling.
4 5 6 |
# File 'lib/docxify/element/paragraph.rb', line 4 def inline_styling @inline_styling end |
#size ⇒ Object
Returns the value of attribute size.
4 5 6 |
# File 'lib/docxify/element/paragraph.rb', line 4 def size @size end |
#tab_stops_cm ⇒ Object
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 |
#text ⇒ Object
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("<", "<").gsub(">", ">")] 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 |