Class: LERB

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

Defined Under Namespace

Classes: BuildError, Error, ExecutionError

Constant Summary collapse

DEFAULT_KEY =

loaders

"inline.txt.erb"
TAG_FORMATS =

format

%w|xml html|
TRIM_MODE =

constructor

"<>-"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, lineno, key, content) ⇒ LERB

Returns a new instance of LERB.



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/lerb.rb', line 131

def initialize filename, lineno, key, content
  segments = key.split("/").last.split(".")
  name, format = segments[0..1]

  if format.gsub(/[^a-z0-9]/, "") != format
    raise BuildError, "key '#{key}' has an invalid format '#{format}'"
  end

  super content, trim_mode: TRIM_MODE
  self.filename, self.lineno, @key, @name, @format = filename, lineno, key, name, format
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



129
130
131
# File 'lib/lerb.rb', line 129

def format
  @format
end

#keyObject (readonly)

Returns the value of attribute key.



129
130
131
# File 'lib/lerb.rb', line 129

def key
  @key
end

#nameObject (readonly)

Returns the value of attribute name.



129
130
131
# File 'lib/lerb.rb', line 129

def name
  @name
end

Class Method Details

._load(erbs, filename) ⇒ Object



48
49
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
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
# File 'lib/lerb.rb', line 48

def self._load erbs, filename
  is_erb = filename.end_with? ".erb"
  is_ignoring_ruby = !is_erb
  is_accepting_views = false
  
  puts
  puts "LERB filename: #{filename}".on_red
  
  puts "LERB is_erb: #{is_erb}".on_red
  puts "LERB is_ignoring_ruby: #{is_ignoring_ruby}".on_red
  puts "LERB is_accepting_views: #{is_accepting_views}".on_red

  current_lineno = 0
  current_content = ""
  current_key = is_erb ? filename.split("/").last : DEFAULT_KEY

  if current_key
    puts "LERB declare: #{current_key} | because not erb".green
  end

  File.readlines(filename).each.with_index do |line, lineno|
    is_line_end = line == "__END__\n"

    # stop ignoring ruby lines if line is __END__
    # move to next line if ignoring ruby lines

    if is_ignoring_ruby
      puts "LERB ignore: #{lineno}: #{line[0..-2]}".light_black
      if is_line_end
        is_ignoring_ruby = false
        is_accepting_views = true
        current_lineno = lineno + 1
        puts "LERB declare: #{current_key} | current".green if current_key
      end
      next
    end

    if is_accepting_views && line[0..6] == "# view "
      _load_into erbs, filename, current_lineno, current_key, current_content
      current_key = line[7..-1].strip
      current_lineno = lineno + 1
      current_content = ""
      puts "LERB declare: #{current_key} | #{lineno}: #{line[0..-2]}".green
    else
      current_content += line
      puts "LERB keeping: #{lineno}: #{line[0..-2]}".bold.white
    end

    if is_line_end
      puts "LERB warning: #{lineno}: #{line[0..-2]} found! No longer accepting views".light_yellow
      is_accepting_views = false
    end
  end

  _load_into erbs, filename, current_lineno, current_key, current_content

  erbs
end

._load_into(erbs, filename, lineno, key, content) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/lerb.rb', line 107

def self._load_into erbs, filename, lineno, key, content
  return unless key
  return unless key.end_with? "erb"
  return if content.strip.empty?

  content += "\n" if content[-1] != "\n"

  erbs.push new filename, lineno, key, content
end

.load(path_radical) ⇒ Object



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

def self.load path_radical
  erbs = []

  "#{path_radical}.rb".tap do |filename|
    _load erbs, filename
  end

  Dir.glob("#{path_radical}.*.erb").each do |filename|
    _load erbs, filename
  end

  Dir.glob("#{path_radical}/*.*.erb").each do |filename|
    _load erbs, filename
  end

  #

  puts "#{erbs.size} erbs".on_red
  erbs.each do |h|
    puts "key: #{h.key}".on_green
  end

  erbs
end

.puts(string = nil) ⇒ Object

output



15
16
17
# File 'lib/lerb.rb', line 15

def self.puts string=nil
  super if $LERB_VERBOSE
end

Instance Method Details

#result(the_binding, receiver = :unset) ⇒ Object

result



145
146
147
148
149
150
151
# File 'lib/lerb.rb', line 145

def result the_binding, receiver=:unset
  super the_binding
rescue NameError => e
  raise unless e.receiver == receiver
  message = "ERB template for a #{e.receiver.class} instance could not find method '#{e.name}'"
  raise ExecutionError, message, [e.backtrace[0]]
end

#tags?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/lerb.rb', line 121

def tags?
  TAG_FORMATS.include? format
end