Class: HldrProcessor

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location, options) ⇒ HldrProcessor



9
10
11
12
13
14
15
16
17
# File 'lib/hldr/hldr_processor.rb', line 9

def initialize(location, options)
    begin
        @doc = Nokogiri::HTML(open(location))
    rescue
        puts "Could not open input file '#{location}'"
        exit(1)
    end
        @options = options
end

Class Method Details

.generate_cacheObject

creates a cache location



77
78
79
# File 'lib/hldr/hldr_processor.rb', line 77

def HldrProcessor.generate_cache
    Dir::mkdir ".hldr" if !Dir::exist? ".hldr"
end

.generate_configObject

creates a new config file



82
83
84
# File 'lib/hldr/hldr_processor.rb', line 82

def HldrProcessor.generate_config
    File::open ".hldrconfig", "w" if !File::exist? ".hldrconfig"
end

Instance Method Details

#add_scaffoldingObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/hldr/hldr_processor.rb', line 31

def add_scaffolding

    hldr_config = YAML.load_file('.hldrconfig')
    hldr_config["scaffolding"].each do |resource|

        # if ends in .css or is a hash with value of css, add style
        if resource[-4..-1] == ".css"
            
            css_res = Nokogiri::XML::Node.new "style", @doc
            css_res.content = open(resource).read
            css_res[:type] = "text/css"

            head = @doc.at_css("html") 
            head << css_res
            
        end

    end 

end

#inline_assetsObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hldr/hldr_processor.rb', line 52

def inline_assets

    inliners = {
        :link   => StyleInliner, 
        :script => ScriptInliner
    }

    # inhibit embedding images if flag set
    if @options[:no_embed]
        inliners[:img] = ImageInliner
    end

    inliners.each do |tag, inliner|

        @doc.css(tag.to_s).each do |element| 
            inliner::embed(element) 
        end

    end

    

end

#to_sObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hldr/hldr_processor.rb', line 19

def to_s

    ## add config file scaffolding
    add_scaffolding

    ## process inlining
    inline_assets
    
    return @doc.to_html

end