Module: HtmlMangler

Defined in:
lib/dinner/htmlmangler.rb

Overview

A module for editing the HTML of files being processed with dinner

Class Method Summary collapse

Class Method Details

.insert_includes(html) ⇒ Object

Find @include commands in an html document and replace them with the contents of the correct file, or else display an error in the shell and leave the @include command in place



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dinner/htmlmangler.rb', line 5

def self.insert_includes(html)
  a = 0
  html[:files].each_pair do |name,path|
    lines = IO.readlines(path)
    lines.each_with_index do |line,i|
      if line =~ /^\s*<!-- @include.*/
        if File.exists?("#{Dir.pwd}/#{parse_filename(line)}")
          lines[i] = File.read("#{Dir.pwd}/#{parse_filename(line)}")
          a = i
        else
          puts "Error in #{name}, line #{i+1}: #{parse_filename(line)} missing"
        end
      end
    end
    File.open("#{Dir.pwd}/#{ConfigManager.config[:build_folder]}/#{name}","w+") do |file|
      file.puts(lines)
    end
  end
end

.insert_placeholders(html) ⇒ Object

Auto-insert placeholder images



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dinner/htmlmangler.rb', line 52

def self.insert_placeholders(html)
  html[:files].each_pair do |name,path|
    lines = IO.readlines(path)
    lines.each_with_index do |line,i|
      while line =~ /.*<!-- @placeholder.*/
        info = parse_placeholder(line.match(/<!--[^>]*?-->/)[0])
        line = line.sub(/<!--[^>]*?-->/,"<img src=\"http://placehold.it/#{info[:res]}\">")
        lines[i] = line
      end
    end
    File.open("#{Dir.pwd}/#{ConfigManager.config[:build_folder]}/#{name}","w+") do |file|
      file.puts(lines)
    end
  end
end

.insert_variables(html) ⇒ Object

Replace variables



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dinner/htmlmangler.rb', line 39

def self.insert_variables(html)
  a = 0
  html[:files].each_pair do |name,path|
    lines = IO.readlines(path)
    lines.each_with_index do |line,i|
      if line =~ /^\s*<!-- \$.*/
        # @TODO figure out how best to pick out those variable names
      end
    end
  end
end

.parse_filename(line) ⇒ Object

Take a line of an html page file and derive the name of the referenced file from it



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dinner/htmlmangler.rb', line 26

def self.parse_filename(line)
  clean_line = line.dup
  clean_line.slice!("<!-- @include")
  clean_line.slice!("-->")
  clean_line.strip!
  if clean_line.include?(".html")
    return clean_line
  else
    return clean_line + ".html"
  end
end

.parse_placeholder(line) ⇒ Object



68
69
70
71
72
# File 'lib/dinner/htmlmangler.rb', line 68

def self.parse_placeholder(line)
  info = {}
  info[:res] = line.match(/\d*x\d*/)[0]
  info
end