Class: Docxout

Inherits:
Object
  • Object
show all
Defined in:
lib/newsman/docx_output.rb

Overview

This class represents a report output in DOCX (MS Word) format. It builds a minimal, valid OOXML package by hand (no external docx-builder gem), so every run of text is explicitly styled with Times New Roman, 12pt.

Constant Summary collapse

CONTENT_TYPES =

Content types part, required by every OOXML package.

<<~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/numbering.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml"/></Types>
XML
RELS =

Package-level relationships, pointing at the main document part.

<<~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
DOCUMENT_RELS =

Document-level relationships, pointing at the numbering part used by lists.

<<~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/numbering" Target="numbering.xml"/></Relationships>
XML
NUMBERING =

Numbering definitions: numId 1 is a bulleted list, numId 2 is a decimal (numbered) list.

<<~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:numFmt w:val="bullet"/><w:lvlText w:val="&#8226;"/><w:pPr><w:ind w:left="720" w:hanging="360"/></w:pPr></w:lvl></w:abstractNum><w:abstractNum w:abstractNumId="1"><w:lvl w:ilvl="0"><w:numFmt w:val="decimal"/><w:lvlText w:val="%1."/><w:pPr><w:ind w:left="720" w:hanging="360"/></w:pPr></w:lvl></w:abstractNum><w:num w:numId="1"><w:abstractNumId w:val="0"/></w:num><w:num w:numId="2"><w:abstractNumId w:val="1"/></w:num></w:numbering>
XML
HEADING =

Matches markdown headers, e.g. "## Title" (levels 1-6).

/^\s{0,3}(\#{1,6})\s+(.+?)\s*$/
BULLET =

Matches markdown bullet list items, e.g. "- item" or "* item".

/^\s{0,3}[-*]\s+(.+?)\s*$/
NUMBERED =

Matches markdown numbered list items, e.g. "1. item".

/^\s{0,3}\d+\.\s+(.+?)\s*$/
FONT =
'Times New Roman'
SIZE =

Word expresses font size in half-points, so 12pt is 24.

'24'

Instance Method Summary collapse

Constructor Details

#initialize(root = '.') ⇒ Docxout

Returns a new instance of Docxout.



65
66
67
# File 'lib/newsman/docx_output.rb', line 65

def initialize(root = '.')
  @root = root
end

Instance Method Details

#filename(reporter, model) ⇒ Object



77
78
79
80
81
# File 'lib/newsman/docx_output.rb', line 77

def filename(reporter, model)
  date = Time.new.strftime('%d.%m.%Y')
  model = model.gsub('.', '-')
  "#{date}.#{reporter}.#{model}.docx"
end


69
70
71
72
73
74
75
# File 'lib/newsman/docx_output.rb', line 69

def print(report, reporter, model)
  puts "Create a docx file in a directory #{@root}"
  path = File.join(@root, filename(reporter, model))
  write_package(path, report)
  puts "Report was successfully printed to a #{path}"
  File.basename(path)
end