Class: Html

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

Overview

Copyright © 2013-2015 SUSE LLC

This program is free software; you can redistribute it and/or modify it under the terms of version 3 of the GNU General Public License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, contact SUSE LLC.

To contact SUSE about this file by physical or electronic mail, you may find current contact information at www.suse.com

Class Method Summary collapse

Class Method Details

.diff_to_object(diff) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/html.rb', line 57

def self.diff_to_object(diff)
  lines = diff.lines[2..-1]
  diff_object = {
    file: diff[/--- a(.*)/, 1],
    additions: lines.select { |l| l.start_with?("+") }.length,
    deletions: lines.select { |l| l.start_with?("-") }.length
  }

  original_line_number = 0
  new_line_number = 0
  diff_object[:lines] = lines.map do |line|
    line = ERB::Util.html_escape(line.chomp).
      gsub("\\", "\").
      gsub("\t", " "*8)
    case line
    when /^@.*/
      entry = {
        type: "header",
        content: line
      }
      original_line_number = line[/-(\d+)/, 1].to_i
      new_line_number = line[/\+(\d+)/, 1].to_i
    when /^ .*/, ""
      entry = {
        type: "common",
        new_line_number: new_line_number,
        original_line_number: original_line_number,
        content: line[1..-1]
      }
      new_line_number += 1
      original_line_number += 1
    when /^\+.*/
      entry = {
        type: "addition",
        new_line_number: new_line_number,
        content: line[1..-1]
      }
      new_line_number += 1
    when /^\-.*/
      entry = {
        type: "deletion",
        original_line_number: original_line_number,
        content: line[1..-1]
      }
      original_line_number += 1
    end

    entry
  end

  diff_object
end

.generate(description) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/html.rb', line 19

def self.generate(description)
  template = Haml::Engine.new(
    File.read(File.join(Machinery::ROOT, "html", "index.html.haml"))
  )
  target = description.store.description_path(description.name)

  diffs_dir = description.scope_file_store("analyze/config_file_diffs").path
  if description.config_files && diffs_dir
    # Enrich description with the config file diffs
    description.config_files.files.each do |file|
      path = File.join(diffs_dir, file.name + ".diff")
      file.diff = diff_to_object(File.read(path)) if File.exists?(path)
    end
  end

  FileUtils.cp_r(File.join(Machinery::ROOT, "html", "assets"), target)
  File.write(File.join(target, "index.html"), template.render(binding))

  # Generate JSON and escape the 's and "s in order to not break the JSON
  # string in javascript.
  json = description.to_hash.to_json.gsub("'", "\\\\'").gsub("\"", "\\\\\"")
  File.write(File.join(target, "assets/description.js"),<<-EOT
    function getDescription() {
      return JSON.parse('#{json}'
      )
    }
    EOT
  )
  target
end

.scope_help(scope) ⇒ Object

Template helpers



52
53
54
55
# File 'lib/html.rb', line 52

def self.scope_help(scope)
  text = File.read(File.join(Machinery::ROOT, "plugins", "docs", "#{scope}.md"))
  Kramdown::Document.new(text).to_html
end