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.
- #inlay? ⇒ Boolean
- #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.
21 22 23 |
# File 'lib/deplate/common.rb', line 21 def args @args end |
#container ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/deplate/common.rb', line 74 def container if @container return @container.container || @container else return nil end end |
#deplate ⇒ Object
pointer to the master deplater
17 18 19 |
# File 'lib/deplate/common.rb', line 17 def deplate @deplate end |
#doc_slot(default = :body, overwrite = false) ⇒ Object
200 201 202 203 204 205 206 207 208 209 |
# File 'lib/deplate/common.rb', line 200 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
211 212 213 214 215 216 |
# File 'lib/deplate/common.rb', line 211 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
33 34 35 |
# File 'lib/deplate/common.rb', line 33 def elt @elt end |
#epilogue ⇒ Object
Returns the value of attribute epilogue.
37 38 39 |
# File 'lib/deplate/common.rb', line 37 def epilogue @epilogue end |
#indentation ⇒ Object
Returns the value of attribute indentation.
19 20 21 |
# File 'lib/deplate/common.rb', line 19 def indentation @indentation end |
#indentation_level ⇒ Object
Returns the value of attribute indentation_level.
20 21 22 |
# File 'lib/deplate/common.rb', line 20 def indentation_level @indentation_level end |
#keep_whitespace ⇒ Object
Returns the value of attribute keep_whitespace.
25 26 27 |
# File 'lib/deplate/common.rb', line 25 def keep_whitespace @keep_whitespace end |
#level ⇒ Object
Returns the value of attribute level.
29 30 31 |
# File 'lib/deplate/common.rb', line 29 def level @level end |
#level_as_list ⇒ Object
Returns the value of attribute level_as_list.
28 29 30 |
# File 'lib/deplate/common.rb', line 28 def level_as_list @level_as_list end |
#level_as_string ⇒ Object
Returns the value of attribute level_as_string.
27 28 29 |
# File 'lib/deplate/common.rb', line 27 def level_as_string @level_as_string end |
#match ⇒ Object
Returns the value of attribute match.
26 27 28 |
# File 'lib/deplate/common.rb', line 26 def match @match end |
#prologue ⇒ Object
Returns the value of attribute prologue.
36 37 38 |
# File 'lib/deplate/common.rb', line 36 def prologue @prologue end |
#prototype ⇒ Object (readonly)
Returns the value of attribute prototype.
30 31 32 |
# File 'lib/deplate/common.rb', line 30 def prototype @prototype end |
#source ⇒ Object
Returns the value of attribute source.
35 36 37 |
# File 'lib/deplate/common.rb', line 35 def source @source end |
#styles ⇒ Object
Returns the value of attribute styles.
22 23 24 |
# File 'lib/deplate/common.rb', line 22 def styles @styles end |
#text ⇒ Object
Returns the value of attribute text.
34 35 36 |
# File 'lib/deplate/common.rb', line 34 def text @text end |
Class Method Details
.get_explicit_id(args = @args) ⇒ Object
142 143 144 |
# File 'lib/deplate/common.rb', line 142 def get_explicit_id(args=@args) args[:id] || args['id'] end |
.post_process_text(text, args = @args) ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/deplate/common.rb', line 302 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
160 161 162 |
# File 'lib/deplate/common.rb', line 160 def can_be_labelled @can_be_labelled && doc_type == :body end |
#element_or_particle(obj) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/deplate/common.rb', line 187 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
328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/deplate/common.rb', line 328 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
239 240 241 |
# File 'lib/deplate/common.rb', line 239 def format_element(agent, *args) return @deplate.formatter.format_element(agent, *args) end |
#get_id ⇒ Object
147 148 149 150 |
# File 'lib/deplate/common.rb', line 147 def get_id # get_explicit_id || @label[0] get_explicit_id end |
#heading_level ⇒ Object
87 88 89 |
# File 'lib/deplate/common.rb', line 87 def heading_level @heading && @heading.level_as_string end |
#initialize(deplate, args = {}) ⇒ Object
attr_accessor :expected
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 72 |
# File 'lib/deplate/common.rb', line 40 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 |
#inlay? ⇒ Boolean
138 139 140 |
# File 'lib/deplate/common.rb', line 138 def inlay? args['inlay'] end |
#labels_sans_id ⇒ Object
288 289 290 291 292 293 |
# File 'lib/deplate/common.rb', line 288 def labels_sans_id id = get_id lbl = @label.dup lbl.delete(id) lbl end |
#log(text, condition = nil) ⇒ Object
164 165 166 |
# File 'lib/deplate/common.rb', line 164 def log(text, condition=nil) @deplate.log(text, condition, @source) end |
#match_expected(expected = nil, invoker = self) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/deplate/common.rb', line 168 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
218 219 220 |
# File 'lib/deplate/common.rb', line 218 def output(*body) @deplate.formatter.output(self, *body) end |
#output_at(type, slot, *body) ⇒ Object
226 227 228 |
# File 'lib/deplate/common.rb', line 226 def output_at(type, slot, *body) @deplate.formatter.output_at(type, slot, *body) end |
#output_file_name(args = {}) ⇒ Object
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 282 283 284 285 286 |
# File 'lib/deplate/common.rb', line 247 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
295 296 297 298 299 300 |
# File 'lib/deplate/common.rb', line 295 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
222 223 224 |
# File 'lib/deplate/common.rb', line 222 def output_preferably_at(type, slot, *body) @deplate.formatter.output_at(doc_type(type), doc_slot(slot), *body) end |
#plain_caption? ⇒ Boolean
243 244 245 |
# File 'lib/deplate/common.rb', line 243 def @deplate.variables['headings'] == 'plain' or @args['plain'] end |
#styles_as_string(sep = ' ') ⇒ Object
152 153 154 155 156 157 158 |
# File 'lib/deplate/common.rb', line 152 def styles_as_string(sep=' ') if @styles.empty? nil else @styles.join(sep) end end |
#tagged_as?(*tag) ⇒ Boolean
129 130 131 132 133 134 135 136 |
# File 'lib/deplate/common.rb', line 129 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
82 83 84 85 |
# File 'lib/deplate/common.rb', line 82 def top_container c = container return c && c.top_heading end |
#update_args(opts = {}) ⇒ Object
91 92 93 94 |
# File 'lib/deplate/common.rb', line 91 def update_args(opts={}) update_styles update_id(opts) end |
#update_id(opts = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/deplate/common.rb', line 117 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
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/deplate/common.rb', line 96 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 |