Class: Teepee::Commander
- Inherits:
-
Object
- Object
- Teepee::Commander
- Defined in:
- lib/teepee/commander.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#parser ⇒ Object
Returns the value of attribute parser.
Instance Method Summary collapse
- #%(numbers) ⇒ Object
- #*(numbers) ⇒ Object
- #**(numbers) ⇒ Object
-
#+(numbers) ⇒ Object
—————————————————————————-.
- #-(numbers) ⇒ Object
- #/(numbers) ⇒ Object
- #acos(angle) ⇒ Object
- #acosh(angle) ⇒ Object
- #add_percentage(numbers) ⇒ Object
- #asin(angle) ⇒ Object
- #asinh(angle) ⇒ Object
- #atan(angle) ⇒ Object
- #atanh(angle) ⇒ Object
- #b(expressions) ⇒ Object
- #backquote ⇒ Object
- #backslash ⇒ Object
- #big(expressions) ⇒ Object
- #bookmarks_folder_id(id) ⇒ Object
- #boolean_and(booleans) ⇒ Object
- #boolean_nand(booleans) ⇒ Object
- #boolean_nor(booleans) ⇒ Object
- #boolean_not(boolean) ⇒ Object
- #boolean_or(booleans) ⇒ Object
- #boolean_xnor(booleans) ⇒ Object
- #boolean_xor(booleans) ⇒ Object
- #br ⇒ Object
- #case_operator(expressions) ⇒ Object
- #ceiling(number) ⇒ Object
- #command_error(message) ⇒ Object
- #command_not_yet_implemented(command) ⇒ Object
- #comment(expressions) ⇒ Object
- #cond_operator(expressions) ⇒ Object
- #cos(angle) ⇒ Object
- #cosh(angle) ⇒ Object
- #decrement(variable) ⇒ Object
- #define(expressions) ⇒ Object
- #degrees2radians(degrees) ⇒ Object
- #del(expressions) ⇒ Object
- #dollar ⇒ Object
- #dotimes(expressions) ⇒ Object
- #e ⇒ Object
- #ensure_boolean(boolean) ⇒ Object
- #ensure_numeric(number) ⇒ Object
- #enumerate(expressions) ⇒ Object
- #enumerate_lowercase(expressions) ⇒ Object
- #enumerate_numeric(expressions) ⇒ Object
- #enumerate_roman_lowercase(expressions) ⇒ Object
- #enumerate_roman_uppercase(expressions) ⇒ Object
- #enumerate_uppercase(expressions) ⇒ Object
- #equal(expressions) ⇒ Object
- #erf(number) ⇒ Object
- #erfc(number) ⇒ Object
- #false_constant ⇒ Object
- #false_constant?(expression) ⇒ Boolean
- #floor(number) ⇒ Object
- #folder_id(id) ⇒ Object
- #forum_id(id) ⇒ Object
- #gamma(number) ⇒ Object
- #get_operator(variable) ⇒ Object
- #greater_than(numbers) ⇒ Object
- #greater_than_or_equal(numbers) ⇒ Object
- #h1(expressions) ⇒ Object
- #h2(expressions) ⇒ Object
- #h3(expressions) ⇒ Object
- #h4(expressions) ⇒ Object
- #h5(expressions) ⇒ Object
- #h6(expressions) ⇒ Object
- #html_tag(tag, expressions, attribs = nil) ⇒ Object
- #hypot(numbers) ⇒ Object
- #i ⇒ Object
- #id_command_handler(id, klass, singular = klass.to_s.camelcase_to_snakecase, plural = singular.pluralize, partial = "#{plural}/inline", view = "") ⇒ Object
- #if_operator(expressions) ⇒ Object
- #image(expressions) ⇒ Object
- #increment(variable) ⇒ Object
-
#initialize(params) ⇒ Commander
constructor
A new instance of Commander.
- #is_defined?(variables) ⇒ Boolean
- #it(expressions) ⇒ Object
- #item(expressions) ⇒ Object
- #itemize(expressions) ⇒ Object
- #itemize_circle(expressions) ⇒ Object
- #itemize_disc(expressions) ⇒ Object
- #itemize_none(expressions) ⇒ Object
- #itemize_square(expressions) ⇒ Object
- #keyword_id(id) ⇒ Object
- #ld(number) ⇒ Object
- #ldexp(fraction, exponent) ⇒ Object
- #left_brace ⇒ Object
- #left_bracket ⇒ Object
- #left_strip(expressions) ⇒ Object
- #less_than(numbers) ⇒ Object
- #less_than_or_equal(numbers) ⇒ Object
- #lgamma(number) ⇒ Object
- #link(expressions) ⇒ Object
- #link_id(id) ⇒ Object
- #ln(number) ⇒ Object
- #log(base, number) ⇒ Object
- #log10(number) ⇒ Object
- #mailto(email_address) ⇒ Object
- #mod(numbers) ⇒ Object
- #nbsp(count) ⇒ Object
- #non_numeric_error(nan) ⇒ Object
- #not_equal(numbers) ⇒ Object
- #note_id(id) ⇒ Object
- #number_from_word(word) ⇒ Object
- #numeric?(*numbers) ⇒ Boolean
- #percent_total(numbers) ⇒ Object
- #pi ⇒ Object
- #pipe ⇒ Object
- #pipe?(expression) ⇒ Boolean
- #prog1_operator(expressions) ⇒ Object
- #progn_operator(expressions) ⇒ Object
- #prognil(expressions) ⇒ Object
- #radians2degrees(radians) ⇒ Object
- #right_brace ⇒ Object
- #right_bracket ⇒ Object
- #right_strip(expressions) ⇒ Object
- #round(number, precision = nil, *_) ⇒ Object
- #sin(angle) ⇒ Object
- #sinh(angle) ⇒ Object
- #small(expressions) ⇒ Object
- #space ⇒ Object
- #span_operator(expressions) ⇒ Object
- #sqrt(number) ⇒ Object
- #squiggle ⇒ Object
- #strip(expressions) ⇒ Object
- #sub(expressions) ⇒ Object
- #subtract_percentage(numbers) ⇒ Object
- #sup(expressions) ⇒ Object
- #table(expressions) ⇒ Object
- #table_data(expressions) ⇒ Object
- #table_header(expressions) ⇒ Object
- #table_row(expressions) ⇒ Object
- #tag_id(id) ⇒ Object
- #tan(angle) ⇒ Object
- #tanh(angle) ⇒ Object
- #tb_href(target, string) ⇒ Object
- #to_numbers(words) ⇒ Object
- #true_constant ⇒ Object
- #true_constant?(expression) ⇒ Boolean
- #tt(expressions) ⇒ Object
- #u(expressions) ⇒ Object
- #undefine(expressions) ⇒ Object
- #unless_operator(expressions) ⇒ Object
- #user(user) ⇒ Object
- #valid_email_address?(email_address) ⇒ Boolean
- #valid_uri?(uri) ⇒ Boolean
- #variable_not_defined_error(variable) ⇒ Object
- #when_operator(expressions) ⇒ Object
Constructor Details
#initialize(params) ⇒ Commander
Returns a new instance of Commander.
42 43 44 |
# File 'lib/teepee/commander.rb', line 42 def initialize params # We don't use the params in the base class, but might in derived classes. end |
Instance Attribute Details
#parser ⇒ Object
Returns the value of attribute parser.
40 41 42 |
# File 'lib/teepee/commander.rb', line 40 def parser @parser end |
Instance Method Details
#%(numbers) ⇒ Object
209 210 211 |
# File 'lib/teepee/commander.rb', line 209 def % numbers to_numbers(numbers).inject { |base, percent| base*percent/100.0 } end |
#*(numbers) ⇒ Object
192 193 194 |
# File 'lib/teepee/commander.rb', line 192 def * numbers ensure_numeric to_numbers(numbers).inject 1, :* end |
#**(numbers) ⇒ Object
205 206 207 |
# File 'lib/teepee/commander.rb', line 205 def ** numbers ensure_numeric to_numbers(numbers).reduce :** end |
#+(numbers) ⇒ Object
179 180 181 |
# File 'lib/teepee/commander.rb', line 179 def + numbers ensure_numeric to_numbers(numbers).inject 0, :+ end |
#-(numbers) ⇒ Object
183 184 185 186 187 188 189 190 |
# File 'lib/teepee/commander.rb', line 183 def - numbers numbers = to_numbers numbers if numbers.length == 1 ensure_numeric -numbers.first else ensure_numeric numbers.reduce :- end end |
#/(numbers) ⇒ Object
196 197 198 199 200 201 202 203 |
# File 'lib/teepee/commander.rb', line 196 def / numbers numbers = to_numbers numbers if numbers.length == 1 ensure_numeric 1 / numbers.first else ensure_numeric numbers.reduce :/ end end |
#acos(angle) ⇒ Object
225 226 227 |
# File 'lib/teepee/commander.rb', line 225 def acos angle ensure_numeric Math.acos angle.to_number end |
#acosh(angle) ⇒ Object
229 230 231 |
# File 'lib/teepee/commander.rb', line 229 def acosh angle ensure_numeric Math.acosh angle.to_number end |
#add_percentage(numbers) ⇒ Object
213 214 215 |
# File 'lib/teepee/commander.rb', line 213 def add_percentage numbers to_numbers(numbers).inject {|base, percent| base * (1+percent/100.0) } end |
#asin(angle) ⇒ Object
233 234 235 |
# File 'lib/teepee/commander.rb', line 233 def asin angle ensure_numeric Math.asin angle.to_number end |
#asinh(angle) ⇒ Object
237 238 239 |
# File 'lib/teepee/commander.rb', line 237 def asinh angle ensure_numeric Math.asinh angle.to_number end |
#atan(angle) ⇒ Object
241 242 243 |
# File 'lib/teepee/commander.rb', line 241 def atan angle ensure_numeric Math.atan angle.to_number end |
#atanh(angle) ⇒ Object
245 246 247 |
# File 'lib/teepee/commander.rb', line 245 def atanh angle ensure_numeric Math.atanh angle.to_number end |
#b(expressions) ⇒ Object
249 250 251 |
# File 'lib/teepee/commander.rb', line 249 def b expressions html_tag :b, expressions end |
#backquote ⇒ Object
253 254 255 |
# File 'lib/teepee/commander.rb', line 253 def backquote "`" end |
#backslash ⇒ Object
257 258 259 |
# File 'lib/teepee/commander.rb', line 257 def backslash "\\" end |
#big(expressions) ⇒ Object
261 262 263 |
# File 'lib/teepee/commander.rb', line 261 def big expressions html_tag :big, expressions end |
#bookmarks_folder_id(id) ⇒ Object
265 266 267 |
# File 'lib/teepee/commander.rb', line 265 def bookmarks_folder_id id id_command_handler id, :Folder, "folder", "folders", "folders/bookmarks_inline", "bookmarks" end |
#boolean_and(booleans) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/teepee/commander.rb', line 269 def boolean_and booleans if booleans.empty? return true_constant end b = booleans.first.to_html if false_constant? b false_constant elsif true_constant? b or booleans.first.kind_of? WhitespaceToken boolean_and booleans[1..-1] else command_error "Not a boolean value #{booleans.first}" end end |
#boolean_nand(booleans) ⇒ Object
283 284 285 |
# File 'lib/teepee/commander.rb', line 283 def boolean_nand booleans boolean_not boolean_and booleans end |
#boolean_nor(booleans) ⇒ Object
287 288 289 |
# File 'lib/teepee/commander.rb', line 287 def boolean_nor booleans boolean_not boolean_or booleans end |
#boolean_not(boolean) ⇒ Object
291 292 293 294 295 296 297 298 299 300 |
# File 'lib/teepee/commander.rb', line 291 def boolean_not boolean boolean = boolean.to_html if true_constant? boolean false_constant elsif false_constant? boolean true_constant else command_error "Not a boolean value" end end |
#boolean_or(booleans) ⇒ Object
302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/teepee/commander.rb', line 302 def boolean_or booleans if booleans.empty? return false_constant end b = booleans.first.to_html if true_constant? b true_constant elsif false_constant? b or booleans.first.kind_of? WhitespaceToken boolean_or booleans[1..-1] else command_error "Not a boolean value" end end |
#boolean_xnor(booleans) ⇒ Object
316 317 318 |
# File 'lib/teepee/commander.rb', line 316 def boolean_xnor booleans boolean_not boolean_xor booleans end |
#boolean_xor(booleans) ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/teepee/commander.rb', line 320 def boolean_xor booleans # There are two schools of thought as to what a multi-variable XOR is. # 1. Chained XORs, giving a parity check. # 2. 'Exclusively' one true for ALL inputs. # I'm going with the second: one and only one true, the rest false. # It seems therefore that the zero-argument version should be false then. if booleans.empty? false_constant else any_trues = false booleans.each do |boolean| if true_constant? boolean if any_trues return false_constant else any_trues = true end elsif false_constant? boolean # do nothing elsif boolean.kind_of? WhitespaceToken # do nothing else return command_error "Not a boolean value" end end return any_trues.to_s end end |
#br ⇒ Object
349 350 351 |
# File 'lib/teepee/commander.rb', line 349 def br html_tag :br, nil end |
#case_operator(expressions) ⇒ Object
353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/teepee/commander.rb', line 353 def case_operator expressions value, _, *rest = strip expressions if value and not rest.empty? def cond_helper value, expressions test_value, _, form, *rest = strip expressions if equal [value.to_html, test_value.to_html] form elsif not rest.empty? cond_helper value, rest end end cond_helper value, rest end end |
#ceiling(number) ⇒ Object
368 369 370 |
# File 'lib/teepee/commander.rb', line 368 def ceiling number ensure_numeric number.to_number.ceil end |
#command_error(message) ⇒ Object
52 53 54 |
# File 'lib/teepee/commander.rb', line 52 def command_error %{<span style="color: red">[#{message}]</span>} end |
#command_not_yet_implemented(command) ⇒ Object
56 57 58 |
# File 'lib/teepee/commander.rb', line 56 def command_not_yet_implemented command command_error "The command #{command} is not yet implemented." end |
#comment(expressions) ⇒ Object
372 373 374 |
# File 'lib/teepee/commander.rb', line 372 def comment expressions nil end |
#cond_operator(expressions) ⇒ Object
376 377 378 379 380 381 382 383 |
# File 'lib/teepee/commander.rb', line 376 def cond_operator expressions conditional, _, form, *rest = strip expressions if true_constant? conditional.to_html form elsif not rest.empty? cond_operator rest end end |
#cos(angle) ⇒ Object
385 386 387 |
# File 'lib/teepee/commander.rb', line 385 def cos angle ensure_numeric Math.cos angle.to_number end |
#cosh(angle) ⇒ Object
389 390 391 |
# File 'lib/teepee/commander.rb', line 389 def cosh angle ensure_numeric Math.cosh angle.to_number end |
#decrement(variable) ⇒ Object
393 394 395 396 397 398 |
# File 'lib/teepee/commander.rb', line 393 def decrement variable return variable_not_defined_error variable if not is_defined? variable old_value = get_operator(variable).to_number return non_numeric_error old_value if not numeric? old_value @parser.variables[variable.to_html] = old_value - 1 end |
#define(expressions) ⇒ Object
400 401 402 403 404 405 406 |
# File 'lib/teepee/commander.rb', line 400 def define expressions variable, _, value = expressions k = variable.to_html v = value.to_html @parser.variables[k] = v get_operator k end |
#degrees2radians(degrees) ⇒ Object
418 419 420 |
# File 'lib/teepee/commander.rb', line 418 def degrees2radians degrees ensure_numeric degrees.to_number * Math::PI / 180.0 end |
#del(expressions) ⇒ Object
422 423 424 |
# File 'lib/teepee/commander.rb', line 422 def del expressions html_tag :del, expressions end |
#dollar ⇒ Object
426 427 428 |
# File 'lib/teepee/commander.rb', line 426 def dollar "$" end |
#dotimes(expressions) ⇒ Object
430 431 432 433 434 |
# File 'lib/teepee/commander.rb', line 430 def dotimes expressions n = expressions.first.to_number return "" if n.nil? or n < 1 span_operator expressions[1..-1] * n end |
#e ⇒ Object
436 437 438 |
# File 'lib/teepee/commander.rb', line 436 def e Math::E end |
#ensure_boolean(boolean) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/teepee/commander.rb', line 82 def ensure_boolean boolean if boolean.to_s == "true" or boolean.to_s == "false" boolean else command_error "Non-boolean value." end end |
#ensure_numeric(number) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/teepee/commander.rb', line 72 def ensure_numeric number if number.kind_of? Complex command_error "Complex numbers are not yet supported." elsif not number.kind_of? Numeric command_error "Non-numeric result." else number end end |
#enumerate(expressions) ⇒ Object
450 451 452 |
# File 'lib/teepee/commander.rb', line 450 def enumerate expressions html_tag :ol, expressions end |
#enumerate_lowercase(expressions) ⇒ Object
462 463 464 |
# File 'lib/teepee/commander.rb', line 462 def enumerate_lowercase expressions html_tag :ol, expressions, {type: "a"} end |
#enumerate_numeric(expressions) ⇒ Object
454 455 456 |
# File 'lib/teepee/commander.rb', line 454 def enumerate_numeric expressions html_tag :ol, expressions, {type: "1"} end |
#enumerate_roman_lowercase(expressions) ⇒ Object
470 471 472 |
# File 'lib/teepee/commander.rb', line 470 def enumerate_roman_lowercase expressions html_tag :ol, expressions, {type: "i"} end |
#enumerate_roman_uppercase(expressions) ⇒ Object
466 467 468 |
# File 'lib/teepee/commander.rb', line 466 def enumerate_roman_uppercase expressions html_tag :ol, expressions, {type: "I"} end |
#enumerate_uppercase(expressions) ⇒ Object
458 459 460 |
# File 'lib/teepee/commander.rb', line 458 def enumerate_uppercase expressions html_tag :ol, expressions, {type: "A"} end |
#equal(expressions) ⇒ Object
440 441 442 443 444 445 446 447 448 |
# File 'lib/teepee/commander.rb', line 440 def equal expressions if expressions.empty? true_constant elsif expressions.length == 1 true_constant else expressions[0].to_s == expressions[1].to_s and equal expressions.rest end end |
#erf(number) ⇒ Object
474 475 476 |
# File 'lib/teepee/commander.rb', line 474 def erf number ensure_numeric Math.erf number.to_number end |
#erfc(number) ⇒ Object
478 479 480 |
# File 'lib/teepee/commander.rb', line 478 def erfc number ensure_numeric Math.erfc number.to_number end |
#false_constant ⇒ Object
482 483 484 |
# File 'lib/teepee/commander.rb', line 482 def false_constant "false" end |
#false_constant?(expression) ⇒ Boolean
157 158 159 |
# File 'lib/teepee/commander.rb', line 157 def false_constant? expression expression.to_s == "false" end |
#floor(number) ⇒ Object
486 487 488 |
# File 'lib/teepee/commander.rb', line 486 def floor number ensure_numeric number.to_number.floor end |
#folder_id(id) ⇒ Object
490 491 492 |
# File 'lib/teepee/commander.rb', line 490 def folder_id id id_command_handler id, :Folder end |
#forum_id(id) ⇒ Object
494 495 496 |
# File 'lib/teepee/commander.rb', line 494 def forum_id id id_command_handler id, :Forum end |
#gamma(number) ⇒ Object
498 499 500 |
# File 'lib/teepee/commander.rb', line 498 def gamma number ensure_numeric Math.gamma number.to_number end |
#get_operator(variable) ⇒ Object
502 503 504 |
# File 'lib/teepee/commander.rb', line 502 def get_operator variable @parser.variables[variable.to_html].to_html end |
#greater_than(numbers) ⇒ Object
506 507 508 509 510 511 512 513 514 |
# File 'lib/teepee/commander.rb', line 506 def greater_than numbers if numbers.empty? true_constant elsif numbers.length == 1 true_constant else numbers[0].to_number > numbers[1].to_number and greater_than numbers.rest end end |
#greater_than_or_equal(numbers) ⇒ Object
516 517 518 519 520 521 522 523 524 |
# File 'lib/teepee/commander.rb', line 516 def greater_than_or_equal numbers if numbers.empty? true_constant elsif numbers.length == 1 true_constant else numbers[0].to_number >= numbers[1].to_number and greater_than_or_equal numbers.rest end end |
#h1(expressions) ⇒ Object
526 527 528 |
# File 'lib/teepee/commander.rb', line 526 def h1 expressions html_tag :h1, expressions end |
#h2(expressions) ⇒ Object
530 531 532 |
# File 'lib/teepee/commander.rb', line 530 def h2 expressions html_tag :h2, expressions end |
#h3(expressions) ⇒ Object
534 535 536 |
# File 'lib/teepee/commander.rb', line 534 def h3 expressions html_tag :h3, expressions end |
#h4(expressions) ⇒ Object
538 539 540 |
# File 'lib/teepee/commander.rb', line 538 def h4 expressions html_tag :h4, expressions end |
#h5(expressions) ⇒ Object
542 543 544 |
# File 'lib/teepee/commander.rb', line 542 def h5 expressions html_tag :h5, expressions end |
#h6(expressions) ⇒ Object
546 547 548 |
# File 'lib/teepee/commander.rb', line 546 def h6 expressions html_tag :h6, expressions end |
#html_tag(tag, expressions, attribs = nil) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/teepee/commander.rb', line 90 def html_tag tag, expressions, attribs=nil opening_tag = if attribs attribs_string = attribs.map {|k,v| %{#{k}="#{v}"}}.join " " if expressions.nil? "<#{tag} #{attribs_string}/>" else "<#{tag} #{attribs_string}>" end else if expressions.nil? "<#{tag}/>" else "<#{tag}>" end end if expressions.nil? opening_tag else opening_tag + expressions.map(&:to_html).join.strip + "</#{tag}>" end end |
#hypot(numbers) ⇒ Object
614 615 616 |
# File 'lib/teepee/commander.rb', line 614 def hypot numbers ensure_numeric Math.sqrt to_numbers(numbers).map {|n| n**2} end |
#i ⇒ Object
550 551 552 |
# File 'lib/teepee/commander.rb', line 550 def i command_error "Complex numbers are not yet supported." end |
#id_command_handler(id, klass, singular = klass.to_s.camelcase_to_snakecase, plural = singular.pluralize, partial = "#{plural}/inline", view = "") ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/teepee/commander.rb', line 116 def id_command_handler(id, klass, singular = klass.to_s.camelcase_to_snakecase, plural = singular.pluralize, partial = "#{plural}/inline", view="") if not id command_error "#{singular}_id: error: no #{singular} ID specified" elsif not id.to_s =~ /\A[0-9]+\z/ command_error "#{singular}_id: error: invalid #{singular} ID specified" else tb_href "#{plural}/#{id.to_s}/#{view}", "#{klass.to_s} ##{id.to_s}" end end |
#if_operator(expressions) ⇒ Object
554 555 556 557 558 559 560 561 562 |
# File 'lib/teepee/commander.rb', line 554 def if_operator expressions expressions = strip expressions conditional, _, true_clause, _, false_clause = expressions if true_constant? conditional.to_html true_clause.to_html elsif false_clause false_clause.to_html end end |
#image(expressions) ⇒ Object
564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
# File 'lib/teepee/commander.rb', line 564 def image expressions expressions = strip expressions uri, *alt_text = expressions uri = ERB::Util.html_escape uri.to_s if not valid_uri? uri command_error "Not a valid URI for the image." else if alt_text.empty? html_tag :img, nil, {src: uri} else html_tag :img, nil, {src: uri, alt: alt_text.map(&:to_s).join.strip} end end end |
#increment(variable) ⇒ Object
579 580 581 582 583 584 |
# File 'lib/teepee/commander.rb', line 579 def increment variable return variable_not_defined_error variable if not is_defined? variable old_value = get_operator(variable).to_number return non_numeric_error old_value if not numeric? old_value @parser.variables[variable.to_html] = old_value + 1 end |
#is_defined?(variables) ⇒ Boolean
408 409 410 411 412 413 414 415 416 |
# File 'lib/teepee/commander.rb', line 408 def is_defined? variables if not variables.is_a? Array return is_defined? [variables] elsif Set.new(variables.map(&:to_html)).subset? Set.new(@parser.variables.keys) true_constant else false_constant end end |
#it(expressions) ⇒ Object
586 587 588 |
# File 'lib/teepee/commander.rb', line 586 def it expressions html_tag :i, expressions end |
#item(expressions) ⇒ Object
590 591 592 |
# File 'lib/teepee/commander.rb', line 590 def item expressions html_tag :li, expressions end |
#itemize(expressions) ⇒ Object
594 595 596 |
# File 'lib/teepee/commander.rb', line 594 def itemize expressions html_tag :ul, expressions end |
#itemize_circle(expressions) ⇒ Object
602 603 604 |
# File 'lib/teepee/commander.rb', line 602 def itemize_circle expressions html_tag :ul, expressions, {style: "list-style-type:circle"} end |
#itemize_disc(expressions) ⇒ Object
598 599 600 |
# File 'lib/teepee/commander.rb', line 598 def itemize_disc expressions html_tag :ul, expressions, {style: "list-style-type:disc"} end |
#itemize_none(expressions) ⇒ Object
610 611 612 |
# File 'lib/teepee/commander.rb', line 610 def itemize_none expressions html_tag :ul, expressions, {style: "list-style-type:none"} end |
#itemize_square(expressions) ⇒ Object
606 607 608 |
# File 'lib/teepee/commander.rb', line 606 def itemize_square expressions html_tag :ul, expressions, {style: "list-style-type:square"} end |
#keyword_id(id) ⇒ Object
618 619 620 |
# File 'lib/teepee/commander.rb', line 618 def keyword_id id id_command_handler id, :Keyword end |
#ld(number) ⇒ Object
622 623 624 |
# File 'lib/teepee/commander.rb', line 622 def ld number ensure_numeric Math.log2 number.to_number end |
#ldexp(fraction, exponent) ⇒ Object
626 627 628 |
# File 'lib/teepee/commander.rb', line 626 def ldexp fraction, exponent ensure_numeric Math.ldexp fraction.to_number, exponent.to_number end |
#left_brace ⇒ Object
630 631 632 |
# File 'lib/teepee/commander.rb', line 630 def left_brace "{" end |
#left_bracket ⇒ Object
634 635 636 |
# File 'lib/teepee/commander.rb', line 634 def left_bracket "[" end |
#left_strip(expressions) ⇒ Object
131 132 133 134 135 136 |
# File 'lib/teepee/commander.rb', line 131 def left_strip expressions while expressions.first.kind_of? WhitespaceToken expressions.shift end expressions end |
#less_than(numbers) ⇒ Object
638 639 640 641 642 643 644 645 646 |
# File 'lib/teepee/commander.rb', line 638 def less_than numbers if numbers.empty? true_constant elsif numbers.length == 1 true_constant else numbers[0].to_number < numbers[1].to_number and less_than numbers.rest end end |
#less_than_or_equal(numbers) ⇒ Object
648 649 650 651 652 653 654 655 656 |
# File 'lib/teepee/commander.rb', line 648 def less_than_or_equal numbers if numbers.empty? true_constant elsif numbers.length == 1 true_constant else numbers[0].to_number <= numbers[1].to_number and less_than_or_equal numbers.rest end end |
#lgamma(number) ⇒ Object
658 659 660 |
# File 'lib/teepee/commander.rb', line 658 def lgamma number ensure_numeric Math::lgamma(number.to_number).first end |
#link(expressions) ⇒ Object
662 663 664 665 666 667 668 669 670 671 672 |
# File 'lib/teepee/commander.rb', line 662 def link expressions expressions = strip expressions uri, *desc = expressions uri = ERB::Util.html_escape uri.to_s if not valid_uri? uri command_error "Not a valid URI." else desc = [uri] if desc.empty? html_tag :a, desc, {href: uri} end end |
#link_id(id) ⇒ Object
674 675 676 |
# File 'lib/teepee/commander.rb', line 674 def link_id id id_command_handler id, :Link end |
#ln(number) ⇒ Object
678 679 680 |
# File 'lib/teepee/commander.rb', line 678 def ln number ensure_numeric Math.log number.to_number end |
#log(base, number) ⇒ Object
682 683 684 685 686 687 688 689 |
# File 'lib/teepee/commander.rb', line 682 def log base, number if number.nil? number, base = base, number ensure_numeric Math.log10 number.to_number # default to log base 10 else ensure_numeric Math.log number.to_number, base.to_number end end |
#log10(number) ⇒ Object
691 692 693 |
# File 'lib/teepee/commander.rb', line 691 def log10 number ensure_numeric Math.log10 number.to_number end |
#mailto(email_address) ⇒ Object
695 696 697 698 699 700 701 702 |
# File 'lib/teepee/commander.rb', line 695 def mailto email_address email_address = email_address.to_s if valid_email_address? email_address html_tag :a, [email_address], {href: "mailto:#{email_address}"} else command_error "I'm not sure that's a valid email address." end end |
#mod(numbers) ⇒ Object
704 705 706 |
# File 'lib/teepee/commander.rb', line 704 def mod numbers ensure_numeric to_numbers(numbers).reduce :% end |
#nbsp(count) ⇒ Object
708 709 710 711 712 713 714 |
# File 'lib/teepee/commander.rb', line 708 def nbsp count if count and count.to_number and count.to_number > 0 " " * count.to_number else " " end end |
#non_numeric_error(nan) ⇒ Object
68 69 70 |
# File 'lib/teepee/commander.rb', line 68 def non_numeric_error nan command_error "The value \"#{nan}\" is not a number." end |
#not_equal(numbers) ⇒ Object
716 717 718 719 720 721 722 723 724 |
# File 'lib/teepee/commander.rb', line 716 def not_equal numbers if numbers.empty? true_constant elsif numbers.length == 1 true_constant else numbers[0].to_number != numbers[1].to_number and equal numbers.rest end end |
#note_id(id) ⇒ Object
726 727 728 |
# File 'lib/teepee/commander.rb', line 726 def note_id id id_command_handler id, :Note end |
#number_from_word(word) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/teepee/commander.rb', line 165 def number_from_word word begin word.to_number rescue ArgumentError, NoMethodError nil end end |
#numeric?(*numbers) ⇒ Boolean
64 65 66 |
# File 'lib/teepee/commander.rb', line 64 def numeric? *numbers numbers.all? {|number| number.kind_of? Numeric} end |
#percent_total(numbers) ⇒ Object
221 222 223 |
# File 'lib/teepee/commander.rb', line 221 def percent_total numbers to_numbers(numbers).inject {|total, part| Float(part)/Float(total)*100.0 } end |
#pi ⇒ Object
730 731 732 |
# File 'lib/teepee/commander.rb', line 730 def pi Math::PI end |
#pipe ⇒ Object
734 735 736 |
# File 'lib/teepee/commander.rb', line 734 def pipe "|" end |
#pipe?(expression) ⇒ Boolean
161 162 163 |
# File 'lib/teepee/commander.rb', line 161 def pipe? expression expression.is_a? PipeToken end |
#prog1_operator(expressions) ⇒ Object
738 739 740 |
# File 'lib/teepee/commander.rb', line 738 def prog1_operator expressions expressions.map(&:to_html).first end |
#progn_operator(expressions) ⇒ Object
742 743 744 |
# File 'lib/teepee/commander.rb', line 742 def progn_operator expressions expressions.map(&:to_html).last end |
#prognil(expressions) ⇒ Object
746 747 748 749 |
# File 'lib/teepee/commander.rb', line 746 def prognil expressions expressions.map(&:to_html) "" end |
#radians2degrees(radians) ⇒ Object
751 752 753 |
# File 'lib/teepee/commander.rb', line 751 def radians2degrees radians ensure_numeric(radians.to_number * 180.0 / Math::PI) end |
#right_brace ⇒ Object
755 756 757 |
# File 'lib/teepee/commander.rb', line 755 def right_brace "}" end |
#right_bracket ⇒ Object
759 760 761 |
# File 'lib/teepee/commander.rb', line 759 def right_bracket "]" end |
#right_strip(expressions) ⇒ Object
138 139 140 141 142 143 |
# File 'lib/teepee/commander.rb', line 138 def right_strip expressions while expressions.last.kind_of? WhitespaceToken expressions.pop end expressions end |
#round(number, precision = nil, *_) ⇒ Object
763 764 765 766 767 768 769 |
# File 'lib/teepee/commander.rb', line 763 def round number, precision = nil, *_ if precision.nil? or precision.to_number.nil? ensure_numeric number.to_number.round else ensure_numeric number.to_number.round precision.to_number end end |
#sin(angle) ⇒ Object
771 772 773 |
# File 'lib/teepee/commander.rb', line 771 def sin angle ensure_numeric Math.sin angle.to_number end |
#sinh(angle) ⇒ Object
775 776 777 |
# File 'lib/teepee/commander.rb', line 775 def sinh angle ensure_numeric Math.sinh angle.to_number end |
#small(expressions) ⇒ Object
779 780 781 |
# File 'lib/teepee/commander.rb', line 779 def small expressions html_tag :small, expressions end |
#space ⇒ Object
791 792 793 |
# File 'lib/teepee/commander.rb', line 791 def space " " end |
#span_operator(expressions) ⇒ Object
795 796 797 |
# File 'lib/teepee/commander.rb', line 795 def span_operator expressions html_tag :span, expressions end |
#sqrt(number) ⇒ Object
787 788 789 |
# File 'lib/teepee/commander.rb', line 787 def sqrt number ensure_numeric Math.sqrt number.to_number end |
#squiggle ⇒ Object
783 784 785 |
# File 'lib/teepee/commander.rb', line 783 def squiggle "~" end |
#strip(expressions) ⇒ Object
145 146 147 |
# File 'lib/teepee/commander.rb', line 145 def strip expressions left_strip right_strip expressions end |
#sub(expressions) ⇒ Object
799 800 801 |
# File 'lib/teepee/commander.rb', line 799 def sub expressions html_tag :sub, expressions end |
#subtract_percentage(numbers) ⇒ Object
217 218 219 |
# File 'lib/teepee/commander.rb', line 217 def subtract_percentage numbers to_numbers(numbers).inject {|base, percent| base * (1-percent/100.0) } end |
#sup(expressions) ⇒ Object
803 804 805 |
# File 'lib/teepee/commander.rb', line 803 def sup expressions html_tag :sup, expressions end |
#table(expressions) ⇒ Object
807 808 809 |
# File 'lib/teepee/commander.rb', line 807 def table expressions html_tag :table, expressions end |
#table_data(expressions) ⇒ Object
811 812 813 |
# File 'lib/teepee/commander.rb', line 811 def table_data expressions html_tag :td, expressions end |
#table_header(expressions) ⇒ Object
815 816 817 |
# File 'lib/teepee/commander.rb', line 815 def table_header expressions html_tag :th, expressions end |
#table_row(expressions) ⇒ Object
819 820 821 |
# File 'lib/teepee/commander.rb', line 819 def table_row expressions html_tag :tr, expressions end |
#tag_id(id) ⇒ Object
823 824 825 |
# File 'lib/teepee/commander.rb', line 823 def tag_id id id_command_handler id, :Tag end |
#tan(angle) ⇒ Object
827 828 829 |
# File 'lib/teepee/commander.rb', line 827 def tan angle ensure_numeric Math.tan angle.to_number end |
#tanh(angle) ⇒ Object
831 832 833 |
# File 'lib/teepee/commander.rb', line 831 def tanh angle ensure_numeric Math.tanh angle.to_number end |
#tb_href(target, string) ⇒ Object
112 113 114 |
# File 'lib/teepee/commander.rb', line 112 def tb_href target, string %{<a href="#{TB_COM}/#{target}">#{string}</a>} end |
#to_numbers(words) ⇒ Object
173 174 175 |
# File 'lib/teepee/commander.rb', line 173 def to_numbers words words.map {|word| number_from_word word}.reject &:nil? end |
#true_constant ⇒ Object
835 836 837 |
# File 'lib/teepee/commander.rb', line 835 def true_constant "true" end |
#true_constant?(expression) ⇒ Boolean
153 154 155 |
# File 'lib/teepee/commander.rb', line 153 def true_constant? expression expression.to_s == "true" end |
#tt(expressions) ⇒ Object
839 840 841 |
# File 'lib/teepee/commander.rb', line 839 def tt expressions html_tag :tt, expressions end |
#u(expressions) ⇒ Object
843 844 845 |
# File 'lib/teepee/commander.rb', line 843 def u expressions html_tag :u, expressions end |
#undefine(expressions) ⇒ Object
847 848 849 850 851 852 |
# File 'lib/teepee/commander.rb', line 847 def undefine expressions expressions.each do |expression| @parser.variables.delete expression.to_html end "" end |
#unless_operator(expressions) ⇒ Object
854 855 856 857 858 859 860 861 862 863 864 865 |
# File 'lib/teepee/commander.rb', line 854 def unless_operator expressions expressions = strip expressions conditional = expressions.first expressions = strip expressions.rest if false_constant? conditional.to_html if expressions.length <= 1 expressions.first else span_operator expressions end end end |
#user(user) ⇒ Object
867 868 869 870 871 872 873 |
# File 'lib/teepee/commander.rb', line 867 def user user if not user command_error "user: error: no user specified" else tb_href "users/#{user}", user.to_s end end |
#valid_email_address?(email_address) ⇒ Boolean
149 150 151 |
# File 'lib/teepee/commander.rb', line 149 def valid_email_address? email_address email_address =~ /\A[[:graph:]]+@[\w.]+\z/ end |
#valid_uri?(uri) ⇒ Boolean
46 47 48 49 50 |
# File 'lib/teepee/commander.rb', line 46 def valid_uri? uri (!! (u = URI.parse(uri))) and not u.scheme.nil? rescue URI::InvalidURIError false end |
#variable_not_defined_error(variable) ⇒ Object
60 61 62 |
# File 'lib/teepee/commander.rb', line 60 def variable_not_defined_error variable command_error "The variable \"#{variable}\" is not defined." end |
#when_operator(expressions) ⇒ Object
875 876 877 878 879 880 881 882 883 884 885 886 |
# File 'lib/teepee/commander.rb', line 875 def when_operator expressions expressions = strip expressions conditional = expressions.first expressions = strip expressions.rest if true_constant? conditional.to_html if expressions.length <= 1 expressions.first else span_operator expressions end end end |