Module: Hlsv::OoxmlBuilder
- Defined in:
- lib/hlsv/html2word.rb
Overview
OOXML Builder
Class Method Summary collapse
-
.build_ppr(opts = {}) ⇒ Object
Builds the <w:pPr> with the common options.
- .content_types ⇒ Object
-
.cover_page(title:, date:, downloaded_at:) ⇒ Object
Cover page.
-
.document(blocks) ⇒ Object
Document body.
- .empty_rels ⇒ Object
-
.footer_xml(doc_title) ⇒ Object
── Word footer ──────────────────────────────────────────────────────────── Two zones separated by a center/right tab stop: left : document title right : PAGE / NUMPAGES (native Word fields).
-
.heading(text, opts = {}) ⇒ Object
Paragraph with a named Word style.
-
.list_item(text, opts = {}) ⇒ Object
Bulleted list item.
- .numbering ⇒ Object
- .page_break ⇒ Object
-
.paragraph(text, opts = {}) ⇒ Object
Generic paragraph.
- .rels ⇒ Object
- .settings ⇒ Object
-
.status_paragraph(text, opts = {}) ⇒ Object
Colored paragraph for statuses.
- .styles ⇒ Object
-
.toc_field ⇒ Object
TOC on its own page.
-
.word_rels ⇒ Object
Document relationships: styles, numbering, settings, header, footer.
- .x(str) ⇒ Object
Class Method Details
.build_ppr(opts = {}) ⇒ Object
Builds the <w:pPr> with the common options
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/hlsv/html2word.rb', line 195 def self.build_ppr(opts = {}) style = opts[:style] align = opts[:align] space_before = opts[:space_before] || 0 space_after = opts[:space_after] || 80 keep_lines = opts[:keep_lines] || false keep_next = opts[:keep_next] || false inner = '' inner += "<w:pStyle w:val=\"#{style}\"/>" if style inner += '<w:keepNext/>' if keep_next inner += '<w:keepLines/>' if keep_lines inner += "<w:spacing w:before=\"#{space_before}\" w:after=\"#{space_after}\"/>" inner += "<w:jc w:val=\"#{align}\"/>" if align "<w:pPr>#{inner}</w:pPr>" end |
.content_types ⇒ Object
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
# File 'lib/hlsv/html2word.rb', line 484 def self.content_types <<~XML <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"> <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/> <Default Extension="xml" ContentType="application/xml"/> <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/> <Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/> <Override PartName="/word/numbering.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml"/> <Override PartName="/word/settings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"/> <Override PartName="/word/footer1.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml"/> </Types> XML end |
.cover_page(title:, date:, downloaded_at:) ⇒ Object
Cover page
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/hlsv/html2word.rb', line 319 def self.cover_page(title:, date:, downloaded_at:) title_para = paragraph(title, sz: 44, bold: true, color: '1F3864', align: 'right', space_before: 2880, space_after: 120) hr = <<~XML <w:p> <w:pPr> <w:jc w:val="right"/> <w:pBdr><w:bottom w:val="single" w:sz="12" w:space="1" w:color="2E75B6"/></w:pBdr> <w:spacing w:before="0" w:after="120"/> </w:pPr> </w:p> XML date_para = paragraph(date, sz: 20, color: '444444', align: 'right', space_before: 0, space_after: 0) downloaded_para = paragraph(downloaded_at, sz: 20, color: '444444', align: 'right', space_before: 0, space_after: 0) title_para + hr + date_para + downloaded_para + page_break end |
.document(blocks) ⇒ Object
Document body
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/hlsv/html2word.rb', line 361 def self.document(blocks) paras = blocks.map do |b| m = b. case b.type when :cover cover_page(title: b.content[:title], date: b.content[:date], downloaded_at: b.content[:downloaded_at].to_s) when :toc toc_field when :h2 heading(b.content, style: 'Heading2', space_before: 280, space_after: 120, keep_lines: m[:keep_lines], keep_next: m[:keep_next]) when :h3 heading(b.content, style: 'Heading3', space_before: 200, space_after: 80, keep_lines: m[:keep_lines], keep_next: m[:keep_next]) when :paragraph paragraph(b.content, sz: 20, color: '000000', keep_lines: m[:keep_lines], keep_next: m[:keep_next]) when :status status_paragraph(b.content, color: m[:color] || '000000', keep_lines: m[:keep_lines], keep_next: m[:keep_next]) when :list color = m[:color] || '000000' label = m[:label] kl = m[:keep_lines] || false kn = m[:keep_next] || false out = '' out += status_paragraph(label, color: color, keep_lines: kl, keep_next: true) if label out += b.content.each_with_index.map do |item, idx| item_kn = (idx == b.content.size - 1) ? kn : true list_item(item, color: color, keep_lines: kl, keep_next: item_kn) end.join out end end.join("\n") <<~XML <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <w:document xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" mc:Ignorable="w14"> <w:body> #{paras} <w:sectPr> <w:footerReference w:type="default" r:id="rId11"/> <w:pgSz w:w="12240" w:h="15840"/> <w:pgMar w:top="1080" w:right="1080" w:bottom="1080" w:left="1080" w:header="720" w:footer="720" w:gutter="0"/> </w:sectPr> </w:body> </w:document> XML end |
.empty_rels ⇒ Object
536 537 538 539 540 541 542 |
# File 'lib/hlsv/html2word.rb', line 536 def self.empty_rels <<~XML <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> </Relationships> XML end |
.footer_xml(doc_title) ⇒ Object
── Word footer ──────────────────────────────────────────────────────────── Two zones separated by a center/right tab stop:
left : document title
right : PAGE / NUMPAGES (native Word fields)
The PAGE and NUMPAGES fields are inserted via <w:fldChar> / <w:instrText>
- same mechanism as the TOC, with no external dependency.
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/hlsv/html2word.rb', line 273 def self.(doc_title) rpr_base = "<w:rFonts w:ascii=\"#{FONT}\" w:hAnsi=\"#{FONT}\" w:cs=\"#{FONT}\"/>" \ "<w:sz w:val=\"18\"/><w:szCs w:val=\"18\"/><w:color w:val=\"666666\"/>" # PAGE field page_field = <<~XML <w:r><w:rPr>#{rpr_base}</w:rPr><w:fldChar w:fldCharType="begin"/></w:r> <w:r><w:rPr>#{rpr_base}</w:rPr><w:instrText xml:space="preserve"> PAGE </w:instrText></w:r> <w:r><w:rPr>#{rpr_base}</w:rPr><w:fldChar w:fldCharType="separate"/></w:r> <w:r><w:rPr>#{rpr_base}</w:rPr><w:t>1</w:t></w:r> <w:r><w:rPr>#{rpr_base}</w:rPr><w:fldChar w:fldCharType="end"/></w:r> XML # NUMPAGES field numpages_field = <<~XML <w:r><w:rPr>#{rpr_base}</w:rPr><w:fldChar w:fldCharType="begin"/></w:r> <w:r><w:rPr>#{rpr_base}</w:rPr><w:instrText xml:space="preserve"> NUMPAGES </w:instrText></w:r> <w:r><w:rPr>#{rpr_base}</w:rPr><w:fldChar w:fldCharType="separate"/></w:r> <w:r><w:rPr>#{rpr_base}</w:rPr><w:t>1</w:t></w:r> <w:r><w:rPr>#{rpr_base}</w:rPr><w:fldChar w:fldCharType="end"/></w:r> XML <<~XML <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <w:ftr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> <w:p> <w:pPr> <w:spacing w:before="80" w:after="0"/> <w:pBdr> <w:top w:val="single" w:sz="6" w:space="1" w:color="CCCCCC"/> </w:pBdr> <w:tabs> <w:tab w:val="right" w:pos="9360"/> </w:tabs> </w:pPr> <w:r><w:rPr>#{rpr_base}</w:rPr><w:t xml:space="preserve">#{x(doc_title)}</w:t></w:r> <w:r><w:rPr>#{rpr_base}</w:rPr><w:tab/></w:r> #{page_field.strip} <w:r><w:rPr>#{rpr_base}</w:rPr><w:t xml:space="preserve"> / </w:t></w:r> #{numpages_field.strip} </w:p> </w:ftr> XML end |
.heading(text, opts = {}) ⇒ Object
Paragraph with a named Word style
228 229 230 231 |
# File 'lib/hlsv/html2word.rb', line 228 def self.heading(text, opts = {}) ppr = build_ppr(opts) "<w:p>#{ppr}<w:r><w:t xml:space=\"preserve\">#{x(text)}</w:t></w:r></w:p>" end |
.list_item(text, opts = {}) ⇒ Object
Bulleted list item
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/hlsv/html2word.rb', line 243 def self.list_item(text, opts = {}) color = opts[:color] || '000000' sz = opts[:sz] || 20 keep_lines = opts[:keep_lines] || false keep_next = opts[:keep_next] || false kl = keep_lines ? '<w:keepLines/>' : '' kn = keep_next ? '<w:keepNext/>' : '' rpr = "<w:rFonts w:ascii=\"#{FONT}\" w:hAnsi=\"#{FONT}\" w:cs=\"#{FONT}\"/>" rpr += "<w:sz w:val=\"#{sz}\"/><w:szCs w:val=\"#{sz}\"/><w:color w:val=\"#{color}\"/>" <<~XML <w:p> <w:pPr> #{kl}#{kn} <w:numPr><w:ilvl w:val="0"/><w:numId w:val="1"/></w:numPr> <w:spacing w:before="0" w:after="60"/> </w:pPr> <w:r><w:rPr>#{rpr}</w:rPr><w:t xml:space="preserve">#{x(text)}</w:t></w:r> </w:p> XML end |
.numbering ⇒ Object
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
# File 'lib/hlsv/html2word.rb', line 467 def self.numbering <<~XML <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <w:numbering xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> <w:abstractNum w:abstractNumId="0"> <w:lvl w:ilvl="0"> <w:start w:val="1"/><w:numFmt w:val="bullet"/> <w:lvlText w:val="•"/><w:lvlJc w:val="left"/> <w:pPr><w:ind w:left="720" w:hanging="360"/></w:pPr> <w:rPr><w:rFonts w:ascii="#{FONT}" w:hAnsi="#{FONT}" w:cs="#{FONT}"/></w:rPr> </w:lvl> </w:abstractNum> <w:num w:numId="1"><w:abstractNumId w:val="0"/></w:num> </w:numbering> XML end |
.page_break ⇒ Object
190 191 192 |
# File 'lib/hlsv/html2word.rb', line 190 def self.page_break "<w:p><w:r><w:br w:type=\"page\"/></w:r></w:p>" end |
.paragraph(text, opts = {}) ⇒ Object
Generic paragraph
213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/hlsv/html2word.rb', line 213 def self.paragraph(text, opts = {}) sz = opts[:sz] || 20 bold = opts[:bold] || false color = opts[:color] || '000000' ppr = build_ppr(opts) rpr = "<w:rFonts w:ascii=\"#{FONT}\" w:hAnsi=\"#{FONT}\" w:cs=\"#{FONT}\"/>" rpr += "<w:sz w:val=\"#{sz}\"/><w:szCs w:val=\"#{sz}\"/>" rpr += '<w:b/><w:bCs/>' if bold rpr += "<w:color w:val=\"#{color}\"/>" "<w:p>#{ppr}<w:r><w:rPr>#{rpr}</w:rPr><w:t xml:space=\"preserve\">#{x(text)}</w:t></w:r></w:p>" end |
.rels ⇒ Object
504 505 506 507 508 509 510 511 512 513 |
# File 'lib/hlsv/html2word.rb', line 504 def self.rels <<~XML <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/> </Relationships> XML end |
.settings ⇒ Object
544 545 546 547 548 549 550 551 552 |
# File 'lib/hlsv/html2word.rb', line 544 def self.settings <<~XML <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> <w:updateFields w:val="true"/> <w:defaultTabStop w:val="720"/> </w:settings> XML end |
.status_paragraph(text, opts = {}) ⇒ Object
Colored paragraph for statuses
234 235 236 237 238 239 240 |
# File 'lib/hlsv/html2word.rb', line 234 def self.status_paragraph(text, opts = {}) color = opts[:color] || '000000' ppr = build_ppr(opts.merge(space_before: 0, space_after: 80)) rpr = "<w:rFonts w:ascii=\"#{FONT}\" w:hAnsi=\"#{FONT}\" w:cs=\"#{FONT}\"/>" rpr += "<w:color w:val=\"#{color}\"/><w:sz w:val=\"20\"/><w:szCs w:val=\"20\"/>" "<w:p>#{ppr}<w:r><w:rPr>#{rpr}</w:rPr><w:t xml:space=\"preserve\">#{x(text)}</w:t></w:r></w:p>" end |
.styles ⇒ Object
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/hlsv/html2word.rb', line 428 def self.styles <<~XML <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> <w:style w:type="paragraph" w:default="1" w:styleId="Normal"> <w:name w:val="Normal"/> <w:rPr> <w:rFonts w:ascii="#{FONT}" w:hAnsi="#{FONT}" w:cs="#{FONT}"/> <w:sz w:val="20"/><w:szCs w:val="20"/><w:color w:val="000000"/> </w:rPr> </w:style> <w:style w:type="paragraph" w:styleId="Heading1"> <w:name w:val="heading 1"/><w:basedOn w:val="Normal"/><w:next w:val="Normal"/> <w:pPr><w:outlineLvl w:val="0"/></w:pPr> <w:rPr> <w:rFonts w:ascii="#{FONT}" w:hAnsi="#{FONT}" w:cs="#{FONT}"/> <w:b/><w:bCs/><w:sz w:val="40"/><w:szCs w:val="40"/><w:color w:val="1F3864"/> </w:rPr> </w:style> <w:style w:type="paragraph" w:styleId="Heading2"> <w:name w:val="heading 2"/><w:basedOn w:val="Normal"/><w:next w:val="Normal"/> <w:pPr><w:outlineLvl w:val="1"/></w:pPr> <w:rPr> <w:rFonts w:ascii="#{FONT}" w:hAnsi="#{FONT}" w:cs="#{FONT}"/> <w:b/><w:bCs/><w:sz w:val="30"/><w:szCs w:val="30"/><w:color w:val="2E75B6"/> </w:rPr> </w:style> <w:style w:type="paragraph" w:styleId="Heading3"> <w:name w:val="heading 3"/><w:basedOn w:val="Normal"/><w:next w:val="Normal"/> <w:pPr><w:outlineLvl w:val="2"/></w:pPr> <w:rPr> <w:rFonts w:ascii="#{FONT}" w:hAnsi="#{FONT}" w:cs="#{FONT}"/> <w:b/><w:bCs/><w:sz w:val="24"/><w:szCs w:val="24"/><w:color w:val="404040"/> </w:rPr> </w:style> </w:styles> XML end |
.toc_field ⇒ Object
TOC on its own page
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/hlsv/html2word.rb', line 341 def self.toc_field toc_title = heading('Table of Contents', style: 'Heading1', space_before: 0, space_after: 160) toc_content = <<~'XML' <w:p> <w:pPr><w:spacing w:before="0" w:after="80"/></w:pPr> <w:r><w:fldChar w:fldCharType="begin"/></w:r> <w:r><w:instrText xml:space="preserve"> TOC \o "1-3" \h \z \u </w:instrText></w:r> <w:r><w:fldChar w:fldCharType="separate"/></w:r> <w:r> <w:rPr><w:color w:val="888888"/></w:rPr> <w:t>Right-click and select "Update Field" to generate the table of contents.</w:t> </w:r> <w:r><w:fldChar w:fldCharType="end"/></w:r> </w:p> XML toc_title + toc_content + page_break end |
.word_rels ⇒ Object
Document relationships: styles, numbering, settings, header, footer
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
# File 'lib/hlsv/html2word.rb', line 516 def self.word_rels <<~XML <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/> <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" Target="numbering.xml"/> <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/> <Relationship Id="rId11" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footer" Target="footer1.xml"/> </Relationships> XML end |
.x(str) ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/hlsv/html2word.rb', line 182 def self.x(str) str.to_s .gsub('&', '&') .gsub('<', '<') .gsub('>', '>') .gsub('"', '"') end |