Module: SemiStatic::Convertable

Includes:
ERB::Util
Included in:
Index, Layout, Page, Post, Snippet
Defined in:
lib/semi-static/convertable.rb

Overview

Support conversion of the source file into another format.

Instance Method Summary collapse

Instance Method Details

#content(options = {}) ⇒ Object

Format the source file.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/semi-static/convertable.rb', line 17

def content(options={})
    return @content unless @content.nil?
    
    options = { :page => self }.merge(options)
    for name, value in options
        eval "#{name.to_s} = options[name]"
    end

    site.stats.record(self.class.name.split('::').last, source_path) do
        case self.source_ext
        when '.md', '.markdown'
            @content = Maruku.new(self.source_content).to_html
        when '.haml'
            Haml::Engine.new(self.source_content, :filename => source_path).render(binding)
        when '.erb'
            erb = ERB.new self.source_content, nil, '-'
            erb.filename = source_path
            erb.result(binding)
        when '.html'
            self.source_content
        else
            raise ArgumentError, "Unsupported format: #{self.source_path}"
        end
    end
end

#layoutObject

:nodoc:



49
50
51
52
53
54
55
# File 'lib/semi-static/convertable.rb', line 49

def layout #:nodoc:
    if layout_name.nil?
        return nil
    else
        return site.layouts[layout_name.to_sym]
    end
end

#layout_nameObject

:nodoc:



43
44
45
46
47
# File 'lib/semi-static/convertable.rb', line 43

def layout_name #:nodoc:
    unless .nil? || !.include?(:layout)
        [:layout].to_sym
    end
end

#loadObject

:nodoc:



7
8
9
10
11
12
13
# File 'lib/semi-static/convertable.rb', line 7

def load #:nodoc:
    @content = nil
    if layout
        layout.load
    end
    super
end

#object_ref(object) ⇒ Object

This method is adapted from Haml::Buffer#parse_object_ref – it’s used to make it easier for Haml and ERB layouts to generate the same output so I can use the same test output for both.



89
90
91
92
93
94
# File 'lib/semi-static/convertable.rb', line 89

def object_ref(object) #:nodoc:
    return '' if object.nil?
    name = underscore(object.class)
    id = "#{name}_#{object.id || 'new'}"
    return "class='#{name}' id='#{id}'"
end

#render(options = {}) ⇒ Object

Wrap the formatted source file in the Layout (if any).



70
71
72
73
74
75
76
77
# File 'lib/semi-static/convertable.rb', line 70

def render(options={})
    content = self.content(options)
    if layout
        options = { :page => self }.merge options
        content = layout.render(options.merge( :content => content ))
    end
    return content
end

#snippet(name) ⇒ Object

Helper method used to insert a Snippet into the formatted output.



81
82
83
84
# File 'lib/semi-static/convertable.rb', line 81

def snippet(name)
    name = name.to_s
    site.snippets[name].render :page => self
end

#source_mtimeObject

Add the layout used (if any) to the list of files used to determine the objects modification time.



60
61
62
63
64
65
66
# File 'lib/semi-static/convertable.rb', line 60

def source_mtime
    mtime = super
    if layout && layout.source_mtime > mtime
        mtime = layout.source_mtime
    end
    return mtime
end

#underscore(camel_cased_word) ⇒ Object

Changes a word from camel case to underscores. Based on the method of the same name in Rails’ Inflector, but copied here so it’ll run properly without Rails.



99
100
101
102
103
104
105
# File 'lib/semi-static/convertable.rb', line 99

def underscore(camel_cased_word) #:nodoc:
    camel_cased_word.to_s.gsub(/::/, '_').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end