Top Level Namespace

Instance Method Summary collapse

Instance Method Details

#nlt(viewname, sub = {}, opt = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/nolate.rb', line 123

def nlt(viewname, sub={}, opt={})
    viewname = "#{viewname}.nlt" if viewname.is_a?(Symbol)
    unless nlt_templates[viewname]
        filename = "views/#{viewname}"
        raise "NOLATE error: no template at #{filename}" \
            unless File.exists?(filename)
        nlt_templates[viewname] = nlt_compile(File.read(filename))
    end
    nlt_eval(nlt_templates[viewname], sub, opt, "views/#{viewname}")
end

#nlt_compile(template) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/nolate.rb', line 77

def nlt_compile(template)
    s = "__=[]; "
    nlt_parse(template).each do |action, param, newlines|
        if action == :evalo or action == :eval
            if param[(-1..-1)] != "\n" and param.index("#")
                lines = param.split("\n")
                if lines[-1] =~ /^ *#/
                    lines[-1] = ""
                    param = lines.join("\n")
                end
            end
        end
        case action
            when :evalo then s << "@__ = (#{param}); __<< @__.to_s; @__; "
            when :eval  then s << "#{param}; "
            when :sub   then s << "__<< __sub[#{param.to_sym.inspect}]; @__; "
            when :verb  then s << "__<<#{param}; @__; "
        end
        s << "\n"*newlines
    end
    s << "\n__.join"
end

#nlt_empty_binding(__sub_) ⇒ Object

NOLATE, A NO LAme TEmplate system

Please read the README file for more information

Copyright © 2011, Salvatore Sanfilippo <antirez at gmail dot com>

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

*  Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

*  Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.



33
34
35
36
# File 'lib/nolate.rb', line 33

def nlt_empty_binding(__sub_)
    __sub = __sub_
    return binding()
end

#nlt_eval(code, sub = {}, opt = {}, file = "evaluated_string") ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/nolate.rb', line 100

def nlt_eval(code, sub = {}, opt = {}, file="evaluated_string")
    # Make sure that nested calls will not substitute the layout
    saved = @nolate_no_layout
    @nolate_no_layout = true
    content = eval(code, nlt_empty_binding(sub), file, 1)
    @nolate_no_layout = saved

    # And... make sure that the layout will not trigger an infinite recursion
    # substituting itself forever.
    if $nolate_layout and !@nolate_no_layout and !(opt[:layout] == false)
        saved = $nolate_layout
        if !opt[:layout]
            use = $nolate_layout
        else
            use = opt[:layout]
        end
        $nolate_layout = nil
        content = nlt(use,{:content => content})
        $nolate_layout = saved
    end
    content
end

#nlt_flush_templatesObject



42
43
44
# File 'lib/nolate.rb', line 42

def nlt_flush_templates
    $nolate_templates = {}
end

#nlt_parse(str) ⇒ Object



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

def nlt_parse(str)
    i = -1  # I wish I had map.with_index in 1.8 :(
    prev_was_eval = false # Previous token was an :eval?
    str.split(/<%(.*?)%>/m).map do |s|
        i, first_char = i + 1, s[0..0]
        newlines = s.count("\n")
        if i % 2 == 0
            j = 0
            if prev_was_eval and s != "\n" and s != "\r\n"
                j += 1 if s[j..j] == "\r"
                j += 1 if s[j..j] == "\n"
            end
            prev_was_eval = false
            [:verb, s[j..-1].inspect, newlines]
        elsif first_char == "="
            prev_was_eval = false
            [:evalo, s[1..-1], newlines]
        elsif first_char == "#"
            prev_was_eval = false
            [:sub,   s[1..-1].to_sym, newlines]
        else
            prev_was_eval = true
            [:eval,  s, 0]
        end
    end
end

#nlt_set_layout(layout) ⇒ Object



46
47
48
# File 'lib/nolate.rb', line 46

def nlt_set_layout(layout)
    $nolate_layout = layout
end

#nlt_templatesObject



38
39
40
# File 'lib/nolate.rb', line 38

def nlt_templates
    $nolate_templates ||= {}
end

#nolate(str, sub = {}, opt = {}) ⇒ Object



134
135
136
# File 'lib/nolate.rb', line 134

def nolate(str, sub={}, opt={})
    nlt_eval(nlt_compile(str), sub, opt)
end