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>
    <%= body %>
  </body>
HTML

Instance Method Summary collapse

Constructor Details

#initialize(root = '.') ⇒ Htmlout

Returns a new instance of Htmlout.



39
40
41
# File 'lib/newsman/html_output.rb', line 39

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

Instance Method Details

#filename(reporter, model) ⇒ Object



61
62
63
64
65
# File 'lib/newsman/html_output.rb', line 61

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

rubocop:disable Metrics/AbcSize



44
45
46
47
48
49
50
51
52
53
# File 'lib/newsman/html_output.rb', line 44

def print(report, reporter, model)
  title = title(reporter)
  body = to_html(report)
  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



56
57
58
59
# File 'lib/newsman/html_output.rb', line 56

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

#to_html(report) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/newsman/html_output.rb', line 67

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