Sha256: 5a81fac6167b429f5da97a041b3cedea10f6712551d93c98272fb43c280f296c

Contents?: true

Size: 1.88 KB

Versions: 5

Compression:

Stored size: 1.88 KB

Contents

require 'cgi'
require 'how_is/pulse'

module HowIs
  class HtmlReport < BaseReport
    def format
      :html
    end

    def title(_text)
      @title = _text
      @r += "<h1>#{_text}</h1>"
    end

    def header(_text)
      @r += "<h2>#{_text}</h2>"
    end

    def link(_text, url)
      %Q[<a href="#{url}">#{_text}</a>]
    end

    def monthly_summary
      pulse.html_summary
    end

    def horizontal_bar_graph(data)
      biggest = data.map { |x| x[1] }.max
      get_percentage = ->(number_of_issues) { number_of_issues * 100 / biggest }

      longest_label_length = data.map(&:first).map(&:length).max
      label_width = "#{longest_label_length}ch"

      @r += '<table class="horizontal-bar-graph">'
      data.each do |row|
        percentage = get_percentage.(row[1])

        if row[2]
          label_text = link(row[0], row[2])
        else
          label_text = row[1]
        end

        @r += <<-EOF
  <tr>
    <td style="width: #{label_width}">#{label_text}</td>
    <td><span class="fill" style="width: #{percentage}%">#{row[1]}</span></td>
  </tr>
        EOF
      end
      @r += "</table>"
    end

    def text(_text)
      @r += "<p>#{_text}</p>"
    end

    def export(&block)
      @r = ''
      instance_exec(&block)
    end

    def export!(file, &block)
      report = export(&block)

      File.open(file, 'w') do |f|
        f.puts <<-EOF
<!DOCTYPE html>
<html>
<head>
  <title>#{@title}</title>
  <style>
  body { font: sans-serif; }
  main {
    max-width: 600px;
    max-width: 72ch;
    margin: auto;
  }

  .horizontal-bar-graph {
    position: relative;
    width: 100%;
  }
  .horizontal-bar-graph .fill {
    display: inline-block;
    background: #CCC;
  }
  </style>
</head>
<body>
  <main>
  #{report}
  </main>
</body>
</html>
        EOF
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
how_is-8.0.0 lib/how_is/report/html.rb
how_is-7.0.0 lib/how_is/report/html.rb
how_is-6.0.0 lib/how_is/report/html.rb
how_is-5.0.0 lib/how_is/report/html.rb
how_is-4.0.0 lib/how_is/report/html.rb