Class: MarkUS

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

Defined Under Namespace

Classes: MarkUSString

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

}}}



105
106
107
108
109
110
111
# File 'lib/markus.rb', line 105

def method_missing(name,*args, &blk) #{{{ # :nodoc:
  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.



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

def __markus_file
  @__markus_file
end

.__markus_includesObject

Returns the value of attribute __markus_includes.



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

def __markus_includes
  @__markus_includes
end

.__markus_indentObject

Returns the value of attribute __markus_indent.



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

def __markus_indent
  @__markus_indent
end

.__markus_reloadObject

Returns the value of attribute __markus_reload.



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

def __markus_reload
  @__markus_reload
end

.__markus_reload_timestampObject

Returns the value of attribute __markus_reload_timestamp.



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

def __markus_reload_timestamp
  @__markus_reload_timestamp
end

.__markus_templatesObject

Returns the value of attribute __markus_templates.



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

def __markus_templates
  @__markus_templates
end

Instance Attribute Details

#__markus_indent(lvl = 0) ⇒ Object

}}}



101
102
103
# File 'lib/markus.rb', line 101

def __markus_indent
  @__markus_indent
end

#__markus_reloadObject

Returns the value of attribute __markus_reload.



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

def __markus_reload
  @__markus_reload
end

Class Method Details

.__markus_do_reloadObject

}}}



264
265
266
267
268
269
270
271
272
# File 'lib/markus.rb', line 264

def self:: __markus_do_reload  #{{{ # :nodoc:
  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

}}}



278
279
280
281
282
283
284
# File 'lib/markus.rb', line 278

def self::indent #{{{
  if self.__markus_indent.nil?
    @@__markus_indent = true
  else
    self.__markus_indent = true
  end
end

.inherited(subclass) ⇒ Object

}}}



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/markus.rb', line 235

def self::inherited(subclass) #{{{ # :nodoc:
  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           = self.class_variable_defined?(:@@__markus_indent) ? @@__markus_indent : false
    self.__markus_includes         = []
  end
end

.reloadObject

}}}



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

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

.template(name, &p) ⇒ Object

}}}



274
275
276
277
# File 'lib/markus.rb', line 274

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

.templates(some) ⇒ Object

}}}



286
287
288
289
# File 'lib/markus.rb', line 286

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

}}}



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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/markus.rb', line 152

def __markus_json(tname,*args,&blk) #{{{ # :nodoc:
  attrs = content = nil
  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 = '[]' 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 = '{}' if attrs == "{  }"
      when String
        content = a.class == ::MarkUS::MarkUSString ? a : "\"#{a.gsub(/"/,'\\\"')}\""
      when Integer, Float
        content = a
      else
        content = "\"#{a.to_s}\""
    end
  end

  @__markus_level += 1
  mpsic = @__markus_parent

  if mpsic == :a && !tname.nil?
    @__markus_buffer << __markus_indent + "{"
  end

  if [content, attrs, blk].compact.length > 1
    @__markus_parent = nil

    @__markus_buffer << __markus_indent + "\"#{tname}\": {"
    __markus_json "attributes", ::MarkUS::MarkUSString.new(attrs) if attrs
    __markus_json "value", ::MarkUS::MarkUSString.new(content) if content
    __markus_json "content", &blk if blk
    @__markus_buffer.last.chomp!(',')
    @__markus_buffer << __markus_indent + "},"
  else
    if blk
      if mpsic == :a && !tname.nil?
        @__markus_parent = nil
        __markus_json tname, *args, &blk
      else
        @__markus_parent = type = blk.parameters.length == 1 && blk.parameters[0][1] == :array ? :a : :h
        @__markus_buffer << __markus_indent + "#{tname.nil? ? '' : "\"#{tname}\": "}#{type == :a ? '[' : '{'}"

        c1 = @__markus_buffer.length
        res = blk.call
        c2 = @__markus_buffer.length
        if c1 == c2
          @__markus_buffer.last << "#{type == :a ? ']' : '}'},"
        else
          @__markus_buffer << res + ',' if type == :a && res.is_a?(String)
          @__markus_buffer.last.chomp!(',')
          @__markus_buffer << __markus_indent + "#{type == :a ? ']' : '}'},"
        end
      end
    else
      @__markus_buffer << __markus_indent + "\"#{tname}\": #{attrs || content},"
    end
  end

  if mpsic == :a && !tname.nil?
    @__markus_buffer.last.chomp!(',')
    @__markus_buffer << __markus_indent + "},"
  end

  @__markus_level -= 1
  @__markus_parent = mpsic
end

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

}}}



112
113
114
115
116
117
118
119
# File 'lib/markus.rb', line 112

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

#__markus_xml(tname, *args) ⇒ Object

}}}



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/markus.rb', line 120

def __markus_xml(tname,*args) #{{{ # :nodoc:
  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(" ")
        attrs = '' if attrs == ' '
      when String
        content = EscapeUtils.escape_html(a).tr("\v\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u0011\u0012\u0014\u0015\u0016\u0017\u0018\u0019\u0020\u0021\u0022\u0023\u0024\u0025\u0026\u0027\u0028\u0029\u0030\u0031",'')
      when Integer
        content = a
    end
  end
  @__markus_level += 1
  if block_given?
    @__markus_buffer << __markus_indent + "<#{tname}#{attrs}>"
    @__markus_buffer << __markus_indent(1) + "#{content}" unless content.nil?
    res = yield
    @__markus_buffer << res if String === res
    @__markus_buffer << __markus_indent + "</#{tname}>"
  else
    if @__markus_mode == :xml && content.nil?
      @__markus_buffer << __markus_indent + "<#{tname}#{attrs}/>"
    else
      @__markus_buffer << __markus_indent + "<#{tname}#{attrs}>#{content}</#{tname}>"
    end
  end
  @__markus_level -= 1
end

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

}}}



76
77
78
# File 'lib/markus.rb', line 76

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

#html_!(name, params = {}) ⇒ Object

}}}



48
49
50
# File 'lib/markus.rb', line 48

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

#json_!(name, params = {}) ⇒ Object

}}}



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

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

#markus_!(name, type, params = {}) ⇒ Object

}}}



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

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

  params.each do |k,v|
    self.instance_variable_set(("@" + k.to_s).to_sym, v)
  end if params.is_a? Hash

  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.chomp!(',') if @__markus_mode == :json
  self.class.__markus_indent ? @__markus_buffer.join("\n") : @__markus_buffer.join
end

#template_!(name, *args) ⇒ Object

}}}



97
98
99
# File 'lib/markus.rb', line 97

def template_!(name,*args) #{{{
  instance_exec *args, &self.class.__markus_templates[name]
end

#value_!(val) ⇒ Object

}}}



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

def value_!(val) #{{{
  case val
    when String
      content = "\"#{val.gsub(/"/,'\\\"')}\""
    when Integer, Float
      content = val
    else
      content = "null"
  end
  @__markus_level += 1
  if self.class.__markus_indent
    @__markus_buffer << "#{"  " * @__markus_level}#{content},"
  else
    @__markus_buffer << "#{content},"
  end
  @__markus_level -= 1
  nil
end

#xml_!(name, params = {}) ⇒ Object

}}}



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

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