Class: Htmlout

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

Overview

This class represents a report output in HTML format.

Constant Summary collapse

TEMPLATE =
<<~HTML
  <head>
    <title><%= title %></title>
  </head>
  <body>
    <h1><%= title %></h1>
    <p><a href="<%= docx %>">Download as Word (.docx)</a></p>
    <%= body %>
  </body>
HTML

Instance Method Summary collapse

Constructor Details

#initialize(root = '.') ⇒ Htmlout

Returns a new instance of Htmlout.



41
42
43
# File 'lib/newsman/html_output.rb', line 41

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

Instance Method Details

#filename(reporter, model) ⇒ Object



64
65
66
67
68
# File 'lib/newsman/html_output.rb', line 64

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

rubocop:disable Metrics/AbcSize



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/newsman/html_output.rb', line 46

def print(report, reporter, model)
  title = title(reporter)
  body = to_html(report)
  docx = Docxout.new(@root).print(report, reporter, model)
  puts "Create a html file in a directory #{@root}"
  file = File.new(File.join(@root, filename(reporter, model)), 'w')
  puts "File #{file.path} was successfully created"
  file.puts Nokogiri::HTML(ERB.new(TEMPLATE).result(binding), &:noblanks).to_xhtml(indent: 2)
  puts "Report was successfully printed to a #{file.path}"
  file.close
end

#title(reporter) ⇒ Object

rubocop:enable Metrics/AbcSize



59
60
61
62
# File 'lib/newsman/html_output.rb', line 59

def title(reporter)
  date = Time.new.strftime('%d.%m.%Y')
  "#{reporter} #{date}"
end

#to_html(report) ⇒ Object



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

def to_html(report)
  Redcarpet::Markdown.new(
    Redcarpet::Render::HTML.new(no_links: true, hard_wrap: true, prettify: true),
    autolink: true,
    tables: true
  ).render(report)
end