Module: Deplate::CommonGround
- Included in:
- Base
- Defined in:
- lib/deplate/common.rb
Overview
require ‘forwardable’
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
- #container ⇒ Object
-
#deplate ⇒ Object
pointer to the master deplater.
- #doc_slot(default = :body, overwrite = false) ⇒ Object
- #doc_type(default = :body, overwrite = false) ⇒ Object
-
#elt ⇒ Object
the element’s parsed value.
-
#epilogue ⇒ Object
Returns the value of attribute epilogue.
-
#indentation ⇒ Object
Returns the value of attribute indentation.
-
#indentation_level ⇒ Object
Returns the value of attribute indentation_level.
-
#keep_whitespace ⇒ Object
Returns the value of attribute keep_whitespace.
-
#level ⇒ Object
Returns the value of attribute level.
-
#level_as_list ⇒ Object
Returns the value of attribute level_as_list.
-
#level_as_string ⇒ Object
Returns the value of attribute level_as_string.
-
#match ⇒ Object
Returns the value of attribute match.
-
#prologue ⇒ Object
Returns the value of attribute prologue.
-
#prototype ⇒ Object
readonly
Returns the value of attribute prototype.
-
#source ⇒ Object
Returns the value of attribute source.
-
#styles ⇒ Object
Returns the value of attribute styles.
-
#text ⇒ Object
Returns the value of attribute text.
Class Method Summary collapse
Instance Method Summary collapse
- #can_be_labelled ⇒ Object
- #element_or_particle(obj) ⇒ Object
- #filter_template(template, vars = nil, args = {}) ⇒ Object
- #format_element(agent, *args) ⇒ Object
- #get_id ⇒ Object
- #heading_level ⇒ Object
-
#initialize(deplate, args = {}) ⇒ Object
attr_accessor :expected.
- #labels_sans_id ⇒ Object
- #log(text, condition = nil) ⇒ Object
- #match_expected(expected = nil, invoker = self) ⇒ Object
- #output(*body) ⇒ Object
- #output_at(type, slot, *body) ⇒ Object
- #output_file_name(args = {}) ⇒ Object
- #output_location(args = {}) ⇒ Object
- #output_preferably_at(type, slot, *body) ⇒ Object
- #plain_caption? ⇒ Boolean
- #styles_as_string(sep = ' ') ⇒ Object
- #tagged_as?(*tag) ⇒ Boolean
- #top_container ⇒ Object
- #update_args(opts = {}) ⇒ Object
- #update_id(opts = {}) ⇒ Object
- #update_styles(styles = nil) ⇒ Object (also: #push_styles)
- #warn_unpexpected(expected, got) ⇒ Object
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
20 21 22 |
# File 'lib/deplate/common.rb', line 20 def args @args end |
#container ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/deplate/common.rb', line 73 def container if @container return @container.container || @container else return nil end end |
#deplate ⇒ Object
pointer to the master deplater
16 17 18 |
# File 'lib/deplate/common.rb', line 16 def deplate @deplate end |
#doc_slot(default = :body, overwrite = false) ⇒ Object
195 196 197 198 199 200 201 202 203 204 |
# File 'lib/deplate/common.rb', line 195 def doc_slot(default=:body, overwrite=false) if (overwrite and default) or !@doc_slot if @args['slot'] @doc_slot = @args['slot'] else @doc_slot = default end end return @doc_slot end |
#doc_type(default = :body, overwrite = false) ⇒ Object
206 207 208 209 210 211 |
# File 'lib/deplate/common.rb', line 206 def doc_type(default=:body, overwrite=false) if (overwrite and default) or !@doc_type @doc_type = @args['type'] || default end return @doc_type end |
#elt ⇒ Object
the element’s parsed value
32 33 34 |
# File 'lib/deplate/common.rb', line 32 def elt @elt end |
#epilogue ⇒ Object
Returns the value of attribute epilogue.
36 37 38 |
# File 'lib/deplate/common.rb', line 36 def epilogue @epilogue end |
#indentation ⇒ Object
Returns the value of attribute indentation.
18 19 20 |
# File 'lib/deplate/common.rb', line 18 def indentation @indentation end |
#indentation_level ⇒ Object
Returns the value of attribute indentation_level.
19 20 21 |
# File 'lib/deplate/common.rb', line 19 def indentation_level @indentation_level end |
#keep_whitespace ⇒ Object
Returns the value of attribute keep_whitespace.
24 25 26 |
# File 'lib/deplate/common.rb', line 24 def keep_whitespace @keep_whitespace end |
#level ⇒ Object
Returns the value of attribute level.
28 29 30 |
# File 'lib/deplate/common.rb', line 28 def level @level end |
#level_as_list ⇒ Object
Returns the value of attribute level_as_list.
27 28 29 |
# File 'lib/deplate/common.rb', line 27 def level_as_list @level_as_list end |
#level_as_string ⇒ Object
Returns the value of attribute level_as_string.
26 27 28 |
# File 'lib/deplate/common.rb', line 26 def level_as_string @level_as_string end |
#match ⇒ Object
Returns the value of attribute match.
25 26 27 |
# File 'lib/deplate/common.rb', line 25 def match @match end |
#prologue ⇒ Object
Returns the value of attribute prologue.
35 36 37 |
# File 'lib/deplate/common.rb', line 35 def prologue @prologue end |
#prototype ⇒ Object (readonly)
Returns the value of attribute prototype.
29 30 31 |
# File 'lib/deplate/common.rb', line 29 def prototype @prototype end |
#source ⇒ Object
Returns the value of attribute source.
34 35 36 |
# File 'lib/deplate/common.rb', line 34 def source @source end |
#styles ⇒ Object
Returns the value of attribute styles.
21 22 23 |
# File 'lib/deplate/common.rb', line 21 def styles @styles end |
#text ⇒ Object
Returns the value of attribute text.
33 34 35 |
# File 'lib/deplate/common.rb', line 33 def text @text end |
Class Method Details
.get_explicit_id(args = @args) ⇒ Object
137 138 139 |
# File 'lib/deplate/common.rb', line 137 def get_explicit_id(args=@args) args[:id] || args['id'] end |
.post_process_text(text, args = @args) ⇒ Object
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/deplate/common.rb', line 297 def post_process_text(text, args=@args) sub = args['sub'] || args['s'] if sub sep = sub[0..0] rx, rp = sub[1..-1].split(Regexp.new(Regexp.quote(sep))) text.gsub!(Regexp.new(rx), rp) end tr = args['tr'] if tr sep = tr[0..0] rx, rp = tr[1..-1].split(Regexp.new(Regexp.quote(sep))) text.tr!(rx, rp) end if args['upcase'] text.upcase! end if args['downcase'] text.downcase! end if args['capitalize'] text.capitalize! end text end |
Instance Method Details
#can_be_labelled ⇒ Object
155 156 157 |
# File 'lib/deplate/common.rb', line 155 def can_be_labelled @can_be_labelled && doc_type == :body end |
#element_or_particle(obj) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/deplate/common.rb', line 182 def element_or_particle(obj) if obj.kind_of?(Deplate::Element::Clip) obj = obj.elt.first end for c in [Deplate::Element, Deplate::Particle] if obj == c or obj === c or obj.kind_of?(c) return c end end log(['Neither block element nor inline text particle', self.class, obj], :error) return nil end |
#filter_template(template, vars = nil, args = {}) ⇒ Object
323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/deplate/common.rb', line 323 def filter_template(template, vars=nil, args={}) d = args[:deplate] ||@deplate v = vars || d.variables s = args[:source] || @source c = args[:container] || @container || self t = Deplate::Template.new(:template => template, :source => s, :container => c) Deplate::Define.let_variables(d, v) do t = t.fill_in(d, :source => s) end t end |
#format_element(agent, *args) ⇒ Object
234 235 236 |
# File 'lib/deplate/common.rb', line 234 def format_element(agent, *args) return @deplate.formatter.format_element(agent, *args) end |
#get_id ⇒ Object
142 143 144 145 |
# File 'lib/deplate/common.rb', line 142 def get_id # get_explicit_id || @label[0] get_explicit_id end |
#heading_level ⇒ Object
86 87 88 |
# File 'lib/deplate/common.rb', line 86 def heading_level @heading && @heading.level_as_string end |
#initialize(deplate, args = {}) ⇒ Object
attr_accessor :expected
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/deplate/common.rb', line 39 def initialize(deplate, args={}) @deplate = deplate # @args = {:class => self.class, :self => self, :deplate => deplate} @args = args[:args] || {:self => self, :deplate => deplate} if (cn = self.class.name) cn = cn.split(/::/) if cn[0] == 'Deplate' acc = [] cn[-2..-1].each do |cnpart| acc << cnpart globalargs = @deplate.variables["$#{acc.join}"] if globalargs @args.merge!(globalargs) {|k,o,n| o} end end end end self.level_as_string = args[:level_as_string] || deplate.get_current_heading @heading = deplate.current_heading_element @level_as_list = args[:level_as_string] || deplate.current_heading.dup @elt = args[:elt] || nil @text = args[:text] || nil @styles = args[:styles] || [] @source = args[:source] || nil @expected = args[:expected] || nil @container = args[:container] || nil @prologue = args[:prologue] || [] @epilogue = args[:epilogue] || [] @can_be_labelled = args[:can_be_labelled] || true @keep_whitespace = args[:keep_whitespace] || false @doc_slot = args[:doc_slot] || nil @doc_type = args[:doc_type] || nil end |
#labels_sans_id ⇒ Object
283 284 285 286 287 288 |
# File 'lib/deplate/common.rb', line 283 def labels_sans_id id = get_id lbl = @label.dup lbl.delete(id) lbl end |
#log(text, condition = nil) ⇒ Object
159 160 161 |
# File 'lib/deplate/common.rb', line 159 def log(text, condition=nil) @deplate.log(text, condition, @source) end |
#match_expected(expected = nil, invoker = self) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/deplate/common.rb', line 163 def match_expected(expected=nil, invoker=self) if kind_of?(Deplate::Regions::Inlatex) return elsif expected if defined?(invoker.prototype) cc = element_or_particle(invoker.prototype || self) cl = invoker.prototype.class else cc = expected cl = self.class end unless kind_of?(expected) or expected != cc invoker.log(['Expected something of a different class', expected, cl, invoker.class], :error) end # else # invoker.log('Neither element nor particle', :anyway) end end |
#output(*body) ⇒ Object
213 214 215 |
# File 'lib/deplate/common.rb', line 213 def output(*body) @deplate.formatter.output(self, *body) end |
#output_at(type, slot, *body) ⇒ Object
221 222 223 |
# File 'lib/deplate/common.rb', line 221 def output_at(type, slot, *body) @deplate.formatter.output_at(type, slot, *body) end |
#output_file_name(args = {}) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/deplate/common.rb', line 242 def output_file_name(args={}) obj = args[:object] || self label = args[:label] relative = args[:relative] basename = args[:basename] level_as_string = args[:level_as_string] if level_as_string rv = @deplate.file_name_by_level(level_as_string) elsif label rv = @deplate.get_filename_for_label(self, label) || '' else if obj.kind_of?(Deplate::BaseParticle) or obj.kind_of?(Deplate::BaseParticle) obj = obj.container end if obj.kind_of?(Deplate::BaseElement) th = obj.top_heading rv = th.destination end end if rv if basename return File.basename(rv) elsif relative == '' return File.basename(rv) elsif relative out = relative.output_file_name if out == rv return '' else dir = File.dirname(out) return @deplate.relative_path(rv, dir) end else return rv end else log(['Internal error in #output_file_name', obj.class.name], :error) raise Exception end end |
#output_location(args = {}) ⇒ Object
290 291 292 293 294 295 |
# File 'lib/deplate/common.rb', line 290 def output_location(args={}) location = [output_file_name(args)] id = get_id location << id if id location.join('#') end |
#output_preferably_at(type, slot, *body) ⇒ Object
217 218 219 |
# File 'lib/deplate/common.rb', line 217 def output_preferably_at(type, slot, *body) @deplate.formatter.output_at(doc_type(type), doc_slot(slot), *body) end |
#plain_caption? ⇒ Boolean
238 239 240 |
# File 'lib/deplate/common.rb', line 238 def plain_caption? @deplate.variables['headings'] == 'plain' or @args['plain'] end |
#styles_as_string(sep = ' ') ⇒ Object
147 148 149 150 151 152 153 |
# File 'lib/deplate/common.rb', line 147 def styles_as_string(sep=' ') if @styles.empty? nil else @styles.join(sep) end end |
#tagged_as?(*tag) ⇒ Boolean
128 129 130 131 132 133 134 135 |
# File 'lib/deplate/common.rb', line 128 def tagged_as?(*tag) = @args['tag'] if = Deplate::Core.split_list(, ',', nil, @source) return .any? {|t| tag.include?(t)} end false end |
#top_container ⇒ Object
81 82 83 84 |
# File 'lib/deplate/common.rb', line 81 def top_container c = container return c && c.top_heading end |
#update_args(opts = {}) ⇒ Object
90 91 92 93 |
# File 'lib/deplate/common.rb', line 90 def update_args(opts={}) update_styles update_id(opts) end |
#update_id(opts = {}) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/deplate/common.rb', line 116 def update_id(opts={}) my_id = @args['id'] if my_id and my_id != @args[:id] aid = @args[:id] @label << aid if aid @args[:id] = my_id if block_given? yield(my_id) end end end |
#update_styles(styles = nil) ⇒ Object Also known as: push_styles
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/deplate/common.rb', line 95 def update_styles(styles=nil) styles ||= @args['style'] || [] case styles when String styles = Deplate::Core.split_list(styles, ',', ';', @source) end if @deplate.variables['styledTags'] = @args['tag'] if = Deplate::Core.split_list(, ',', nil, @source) .map! {|t| "TAG#{t}"} styles += end end if styles and !styles.empty? @styles += styles @styles.uniq! end end |