Class: Html2Docx::DocumentObjects::Paragraph
- Inherits:
-
Object
- Object
- Html2Docx::DocumentObjects::Paragraph
- Defined in:
- lib/Html2Docx/document_objects/paragraph.rb
Instance Method Summary collapse
- #add_bookmark_end_tag(name) ⇒ Object
- #add_bookmark_start_tag(name) ⇒ Object
- #add_font_color(text_style, color) ⇒ Object
- #add_image(image) ⇒ Object
- #add_italic_text(text_style) ⇒ Object
- #add_link_class(text_style) ⇒ Object
- #add_paragraph(paragraph_object) ⇒ Object
- #add_paragraph_alignment(value) ⇒ Object
- #add_paragraph_background_color(value) ⇒ Object
- #add_paragraph_child(children) ⇒ Object
- #add_paragraph_class(value) ⇒ Object
- #add_paragraph_indent(value) ⇒ Object
- #add_paragraph_line_height(value) ⇒ Object
- #add_paragraph_style(paragraph_object) ⇒ Object
- #add_paragraph_text(value) ⇒ Object
- #add_stroke_text(text_style) ⇒ Object
- #add_strong_text(text_style) ⇒ Object
- #add_underline_text(text_style) ⇒ Object
- #create_hyperlink_tag(destination) ⇒ Object
- #create_paragraph(paragraph_object) ⇒ Object
- #create_text_field ⇒ Object
- #create_text_style ⇒ Object
-
#initialize(document, relation, tmp_path) ⇒ Paragraph
constructor
A new instance of Paragraph.
- #render ⇒ Object
Constructor Details
#initialize(document, relation, tmp_path) ⇒ Paragraph
Returns a new instance of Paragraph.
4 5 6 7 8 9 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 4 def initialize(document, relation, tmp_path) @document = document @relation = relation @tmp_path = tmp_path @paragraph = nil end |
Instance Method Details
#add_bookmark_end_tag(name) ⇒ Object
31 32 33 34 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 31 def add_bookmark_end_tag(name) bookmark_end_tag = @relation.create_internal_link_end_tag(name, @document) @paragraph.add_child(bookmark_end_tag) end |
#add_bookmark_start_tag(name) ⇒ Object
26 27 28 29 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 26 def add_bookmark_start_tag(name) bookmark_start_tag = @relation.create_internal_link_start_tag(name, @document) @paragraph.add_child(bookmark_start_tag) end |
#add_font_color(text_style, color) ⇒ Object
204 205 206 207 208 209 210 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 204 def add_font_color(text_style, color) color_text = Nokogiri::XML::Node.new('w:color', @document) color_text['w:val'] = Helpers::DocumentHelper.convert_hex_color(color) text_style.add_child(color_text) text_style end |
#add_image(image) ⇒ Object
228 229 230 231 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 228 def add_image(image) image_object_helper = DocumentObjects::Image.new(@document, @relation, @tmp_path) image_object_helper.add_image(image) end |
#add_italic_text(text_style) ⇒ Object
197 198 199 200 201 202 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 197 def add_italic_text(text_style) italic_text = Nokogiri::XML::Node.new('w:i', @document) text_style.add_child(italic_text) text_style end |
#add_link_class(text_style) ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 182 def add_link_class(text_style) r_style_tag = Nokogiri::XML::Node.new('w:rStyle', @document) r_style_tag['w:val'] = 'Hyperlink' text_style.add_child(r_style_tag) text_style end |
#add_paragraph(paragraph_object) ⇒ Object
11 12 13 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 11 def add_paragraph(paragraph_object) create_paragraph(paragraph_object) end |
#add_paragraph_alignment(value) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 89 def add_paragraph_alignment(value) align_tag = Nokogiri::XML::Node.new('w:jc', @document) value = value.downcase value = 'both' if value == 'justify' align_tag['w:val'] = value align_tag end |
#add_paragraph_background_color(value) ⇒ Object
98 99 100 101 102 103 104 105 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 98 def add_paragraph_background_color(value) background_tag = Nokogiri::XML::Node.new('w:shd', @document) background_tag['w:val'] = 'clear' background_tag['w:color'] = 'auto' background_tag['w:fill'] = Helpers::DocumentHelper.convert_hex_color(value) background_tag end |
#add_paragraph_child(children) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 114 def add_paragraph_child(children) children.each do |child| text_field = create_text_field text_style = create_text_style case child.name when 'strong' text_field.add_child add_strong_text(text_style) when 'i' text_field.add_child add_italic_text(text_style) when 'font' color = child.attr('color') text_field.add_child add_font_color(text_style, color) unless color.nil? when 'u' text_field.add_child add_underline_text(text_style) when 's' text_field.add_child add_stroke_text(text_style) when 'a' href = child.attr('href') hyperlink_tag = create_hyperlink_tag(href) text_field.add_child(add_link_class(text_style)) hyperlink_tag.add_child(text_field) text_field.add_child add_paragraph_text(child.text) hyperlink_tag.add_child text_field @paragraph.add_child hyperlink_tag next when 'img' text_field.add_child add_image(child) @paragraph.add_child(text_field) next end paragraph_id = child.attr('id') add_bookmark_start_tag(paragraph_id) if paragraph_id text_field.add_child add_paragraph_text(child.text) add_bookmark_end_tag(paragraph_id) if paragraph_id @paragraph.add_child text_field end end |
#add_paragraph_class(value) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 82 def add_paragraph_class(value) class_tag = Nokogiri::XML::Node.new('w:pStyle', @document) class_tag['w:val'] = value class_tag end |
#add_paragraph_indent(value) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 75 def add_paragraph_indent(value) indent_tag = Nokogiri::XML::Node.new('w:ind', @document) indent_tag['w:firstLine'] = Helpers::DocumentHelper.px_to_indent(value) indent_tag end |
#add_paragraph_line_height(value) ⇒ Object
107 108 109 110 111 112 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 107 def add_paragraph_line_height(value) line_height_tag = Nokogiri::XML::Node.new('w:spacing', @document) line_height_tag['w:line'] = Helpers::DocumentHelper.line_height(value) line_height_tag end |
#add_paragraph_style(paragraph_object) ⇒ Object
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 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 36 def add_paragraph_style(paragraph_object) paragraph_style = Nokogiri::XML::Node.new('w:pPr', @document) paragraph_styles = [] style_attribute = paragraph_object.attr('style') style_attributes = style_attribute.split(';') if style_attribute paragraph_class = paragraph_object.attr('class') paragraph_class = paragraph_class.split(' ')&.first if paragraph_class if style_attributes style_attributes.each do |style| style = style.strip attribute, value = style.scan(/(.+):\s?(.+);?/).flatten case attribute when 'text-indent' paragraph_styles.push add_paragraph_indent(value) when 'text-align' paragraph_styles.push add_paragraph_alignment(value) when 'background-color' paragraph_styles.push add_paragraph_background_color(value) when 'line-height' paragraph_styles.push add_paragraph_line_height(value) end end end unless paragraph_class.nil? paragraph_styles.push add_paragraph_class(paragraph_class) end paragraph_styles.each do |style| paragraph_style.add_child(style) end @paragraph.add_child(paragraph_style) end |
#add_paragraph_text(value) ⇒ Object
174 175 176 177 178 179 180 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 174 def add_paragraph_text(value) plain_text = Nokogiri::XML::Node.new('w:t', @document) plain_text['xml:space'] = 'preserve' plain_text.content = value plain_text end |
#add_stroke_text(text_style) ⇒ Object
220 221 222 223 224 225 226 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 220 def add_stroke_text(text_style) stroke_text = Nokogiri::XML::Node.new('w:dstrike ', @document) stroke_text['w:val'] = true text_style.add_child(stroke_text) text_style end |
#add_strong_text(text_style) ⇒ Object
190 191 192 193 194 195 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 190 def add_strong_text(text_style) strong_text = Nokogiri::XML::Node.new('w:b', @document) text_style.add_child(strong_text) text_style end |
#add_underline_text(text_style) ⇒ Object
212 213 214 215 216 217 218 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 212 def add_underline_text(text_style) underline_text = Nokogiri::XML::Node.new('w:u', @document) underline_text['w:val'] = 'single' text_style.add_child(underline_text) text_style end |
#create_hyperlink_tag(destination) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 162 def create_hyperlink_tag(destination) hyperlink_tag = Nokogiri::XML::Node.new('w:hyperlink', @document) if destination.start_with?('#') hyperlink_tag['w:anchor'] = destination.delete('#') else hyperlink_tag['r:id'] = @relation.create_external_link_id(destination) end hyperlink_tag end |
#create_paragraph(paragraph_object) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 15 def create_paragraph(paragraph_object) @paragraph = Nokogiri::XML::Node.new('w:p', @document) paragraph_id = paragraph_object.attr('id') add_paragraph_style paragraph_object add_bookmark_start_tag(paragraph_id) if paragraph_id add_paragraph_child paragraph_object.children add_bookmark_end_tag(paragraph_id) if paragraph_id end |
#create_text_field ⇒ Object
154 155 156 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 154 def create_text_field Nokogiri::XML::Node.new('w:r', @document) end |
#create_text_style ⇒ Object
158 159 160 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 158 def create_text_style Nokogiri::XML::Node.new('w:rPr', @document) end |
#render ⇒ Object
233 234 235 |
# File 'lib/Html2Docx/document_objects/paragraph.rb', line 233 def render @paragraph end |