Class: MarkUS

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

Overview

Markup UnderScore —- - -

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object

}}}



75
76
77
78
79
80
81
# File 'lib/markus.rb', line 75

def method_missing(name,*args, &blk) #{{{
  if name.to_s =~ /(.*)(__)$/ || name.to_s =~ /(.*)(_)$/
    __markus_method_missing $1, *args, &blk
  else
    super
  end  
end

Class Attribute Details

.__markus_fileObject

Returns the value of attribute __markus_file.



24
25
26
# File 'lib/markus.rb', line 24

def __markus_file
  @__markus_file
end

.__markus_includesObject

Returns the value of attribute __markus_includes.



27
28
29
# File 'lib/markus.rb', line 27

def __markus_includes
  @__markus_includes
end

.__markus_indentObject

Returns the value of attribute __markus_indent.



26
27
28
# File 'lib/markus.rb', line 26

def __markus_indent
  @__markus_indent
end

.__markus_reloadObject

Returns the value of attribute __markus_reload.



22
23
24
# File 'lib/markus.rb', line 22

def __markus_reload
  @__markus_reload
end

.__markus_reload_timestampObject

Returns the value of attribute __markus_reload_timestamp.



23
24
25
# File 'lib/markus.rb', line 23

def __markus_reload_timestamp
  @__markus_reload_timestamp
end

.__markus_templatesObject

Returns the value of attribute __markus_templates.



25
26
27
# File 'lib/markus.rb', line 25

def __markus_templates
  @__markus_templates
end

Instance Attribute Details

#__markus_indentObject

Returns the value of attribute __markus_indent.



30
31
32
# File 'lib/markus.rb', line 30

def __markus_indent
  @__markus_indent
end

#__markus_reloadObject

Returns the value of attribute __markus_reload.



30
31
32
# File 'lib/markus.rb', line 30

def __markus_reload
  @__markus_reload
end

Class Method Details

.__markus_do_reloadObject

}}}



247
248
249
250
251
252
253
254
255
# File 'lib/markus.rb', line 247

def self:: __markus_do_reload  #{{{
  if self.__markus_reload && self.__markus_reload_timestamp
    if File.stat(self.__markus_file).mtime > self.__markus_reload_timestamp
      load self.__markus_file
      return true
    end
  end
  false
end

.indentObject

}}}



261
262
263
# File 'lib/markus.rb', line 261

def self::indent #{{{
  self.__markus_indent = true
end

.inherited(subclass) ⇒ Object

}}}



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/markus.rb', line 218

def self::inherited(subclass) #{{{
  subclass.instance_eval do |i|
    # This array contains strings like "/path/to/a.rb:3:in `instance_eval'".
    strings_ary = caller

    # We look for the last string containing "<top (required)>".
    # only works in > 1.9
    val = "<top (required)>"
    until require_index = strings_ary.index {|x| x.include?(val) }
      val = "<main>"
    end
    require_string = strings_ary[require_index]
    filepath = File.expand_path(require_string[/^(.*):\d+:in/, 1])


    # We use a regex to extract the filepath from require_string. Other defaults.
    self.__markus_reload           = false
    self.__markus_reload_timestamp = nil
    self.__markus_file             = filepath
    self.__markus_templates        = {}
    self.__markus_indent           = false
    self.__markus_includes         = []
  end
end

.reloadObject

}}}



243
244
245
246
# File 'lib/markus.rb', line 243

def  self::reload #{{{
  self.__markus_reload = true
  self.__markus_reload_timestamp = File.stat(self.__markus_file).mtime
end

.template(name, &p) ⇒ Object

}}}



257
258
259
260
# File 'lib/markus.rb', line 257

def self::template(name,&p) #{{{
  self.__markus_templates ||= {}
  self.__markus_templates[name] = p
end

.templates(some) ⇒ Object

}}}



265
266
267
268
# File 'lib/markus.rb', line 265

def self::templates(some)
  self.__markus_includes << some
  self.__markus_templates.merge! some.__markus_templates
end

Instance Method Details

#__markus_json(tname, *args, &blk) ⇒ Object

}}}



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/markus.rb', line 141

def __markus_json(tname,*args,&blk) #{{{
  attrs = nil
  content = "null"
  args.each do |a|
    case a
      when Array
        attrs = "[ " + a.collect { |value|
          case value
            when Integer, Float
              value.nil? ? nil : value.to_s
            else
              value.nil? ? nil : "\"#{value.to_s.gsub(/"/,'\\\"')}\""
          end
        }.compact.join(", ").strip + " ]"
        attrs = nil if attrs == "[  ]"
      when Hash
        attrs = "{ " + a.collect { |key,value|
          case value
            when Integer, Float
              value.nil? ? nil : "\"#{key}\": #{value}"
            else
              value.nil? ? nil : "\"#{key}\": \"#{value.to_s.gsub(/"/,'\\\"')}\""
          end
        }.compact.join(", ").strip + " }"
        attrs = nil if attrs == "{  }"
      when String
        content = "\"#{a.gsub(/"/,'\\\"')}\""
      when Integer, Float
        content = a
      else
        content = "\"#{a.to_s}\""
    end  
  end
  if blk
    @__markus_level += 1
    mpsic = @__markus_parent
    if mpsic == :a
      @__markus_parent = nil
      if self.class.__markus_indent
        @__markus_buffer << "#{"  " * @__markus_level}{"
      else  
        @__markus_buffer << "{"
      end
      __markus_json tname, *args, &blk
      @__markus_buffer.last.chop!
      if self.class.__markus_indent
        @__markus_buffer << "#{"  " * @__markus_level}},"
      else  
        @__markus_buffer << "},"
      end
    else 
      @__markus_parent = type = blk.parameters.length == 1 && blk.parameters[0][1] == :array ? :a : :h
      if self.class.__markus_indent
        @__markus_buffer << "#{"  " * @__markus_level}#{tname.nil? ? '' : "\"#{tname}\": "}#{type == :a ? '[' : '{'}"
      else  
        @__markus_buffer << "#{tname.nil? ? '' : "\"#{tname}\": "}#{type == :a ? '[' : '{'}"
      end
      res = blk.call
      @__markus_buffer << res + ',' if type == :a && res.is_a?(String)
      @__markus_buffer.last.chop!
      if self.class.__markus_indent
        @__markus_buffer << "#{"  " * @__markus_level}#{type == :a ? ']' : '}'},"
      else
        @__markus_buffer << "#{type == :a ? ']' : '}'},"
      end  
    end
    @__markus_level -= 1
    @__markus_parent = mpsic
  else
    if self.class.__markus_indent
      @__markus_buffer << "#{"  " * (@__markus_level+1)}\"#{tname}\": #{attrs || content}," 
    else
      @__markus_buffer << "\"#{tname}\": #{attrs || content}," 
    end
  end  
end

#__markus_method_missing(name, *args, &blk) ⇒ Object

}}}



82
83
84
85
86
87
88
# File 'lib/markus.rb', line 82

def __markus_method_missing(name,*args, &blk) #{{{
  if @__markus_mode == :json
    __markus_json name, *args, &blk
  else
    __markus_xml name, *args, &blk
  end
end

#__markus_xml(tname, *args) ⇒ Object

}}}



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/markus.rb', line 89

def __markus_xml(tname,*args) #{{{
  attrs = ""
  content = nil
  args.each do |a|
    case a
      when Hash
        attrs << " " + a.collect { |key,value|
          value.nil? ? nil : "#{key}=\"#{value.to_s.gsub(/"/,"&#34;")}\""
        }.compact.join(" ")
      when String,Integer
        content = a
    end  
  end
  attrs = '' if attrs == ' '
  if block_given?
    @__markus_level += 1
    if self.class.__markus_indent
      @__markus_buffer << "#{"  " * @__markus_level}<#{tname}#{attrs}>"
    else  
      @__markus_buffer << "<#{tname}#{attrs}>"
    end  
    unless content.nil?
      if self.class.__markus_indent
        @__markus_buffer << "#{"  " * (@__markus_level+1)}#{content}"
      else
        @__markus_buffer << "#{content}"
      end
    end
    res = yield
    @__markus_buffer << res if String === res
    if self.class.__markus_indent
      @__markus_buffer << "#{"  " * @__markus_level}</#{tname}>"
    else
      @__markus_buffer << "</#{tname}>"
    end  
    @__markus_level -= 1
  else
    if @__markus_mode == :xml && content.nil?
      if self.class.__markus_indent
        @__markus_buffer << "#{"  " * (@__markus_level+1)}<#{tname}#{attrs}/>"
      else
        @__markus_buffer << "<#{tname}#{attrs}/>"
      end
    else
      if self.class.__markus_indent
        @__markus_buffer << "#{"  " * (@__markus_level+1)}<#{tname}#{attrs}>#{content}</#{tname}>" 
      else
        @__markus_buffer << "<#{tname}#{attrs}>#{content}</#{tname}>" 
      end
    end  
  end  
end

#element_!(name = nil, *args, &blk) ⇒ Object

}}}



68
69
70
# File 'lib/markus.rb', line 68

def element_!(name=nil, *args, &blk) #{{{
  __markus_method_missing name, *args, &blk
end

#html_!(name) ⇒ Object

}}}



44
45
46
# File 'lib/markus.rb', line 44

def html_!(name) #{{{
  markus_! name, :html
end

#json_!(name) ⇒ Object

}}}



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

def json_!(name) #{{{
  markus_! name, :json
end

#markus_!(name, type) ⇒ Object

}}}



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/markus.rb', line 48

def markus_!(name,type) #{{{
  @__markus = []
  @__markus_buffer = []
  @__markus_level = -1
  @__markus_parent = nil

  self.class.__markus_do_reload
  self.class.__markus_includes.each do |some|
    if some.__markus_do_reload
      self.class.__markus_templates.merge! some.__markus_templates
    end  
  end
  @__markus_mode = type

  template_!(name)
  @__markus_buffer.last.chop! if @__markus_mode == :json
  self.class.__markus_indent ? @__markus_buffer.join("\n") : @__markus_buffer.join
end

#template_!(name) ⇒ Object

}}}



71
72
73
# File 'lib/markus.rb', line 71

def template_!(name) #{{{
  instance_eval &self.class.__markus_templates[name]
end

#xml_!(name) ⇒ Object

}}}



41
42
43
# File 'lib/markus.rb', line 41

def xml_!(name) #{{{
  markus_! name, :xml
end