Class: Teepee::Commander
- Inherits:
-
Object
- Object
- Teepee::Commander
- Defined in:
- lib/teepee/commander.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
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
- #degrees2radians(degrees) ⇒ Object
- #del(expressions) ⇒ Object
- #dollar ⇒ 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
-
#initialize(params) ⇒ Commander
constructor
A new instance of Commander.
- #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
- #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
- #radians2degrees(radians) ⇒ Object
- #right_brace ⇒ Object
- #right_bracket ⇒ Object
- #right_strip(expressions) ⇒ Object
- #round(number, precision = nil, *_) ⇒ Object
- #set_operator(expressions) ⇒ 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
- #unless_operator(expressions) ⇒ Object
- #user(user) ⇒ Object
- #valid_email_address?(email_address) ⇒ Boolean
- #valid_uri?(uri) ⇒ Boolean
- #when_operator(expressions) ⇒ Object
Constructor Details
#initialize(params) ⇒ Commander
Returns a new instance of Commander.
42 43 44 45 |
# 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. @variables = Hash.new end |
Instance Attribute Details
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
40 41 42 |
# File 'lib/teepee/commander.rb', line 40 def variables @variables end |
Instance Method Details
#%(numbers) ⇒ Object
202 203 204 |
# File 'lib/teepee/commander.rb', line 202 def % numbers to_numbers(numbers).inject { |base, percent| base*percent/100.0 } end |
#*(numbers) ⇒ Object
185 186 187 |
# File 'lib/teepee/commander.rb', line 185 def * numbers ensure_numeric to_numbers(numbers).inject 1, :* end |
#**(numbers) ⇒ Object
198 199 200 |
# File 'lib/teepee/commander.rb', line 198 def ** numbers ensure_numeric to_numbers(numbers).reduce :** end |
#+(numbers) ⇒ Object
172 173 174 |
# File 'lib/teepee/commander.rb', line 172 def + numbers ensure_numeric to_numbers(numbers).inject 0, :+ end |
#-(numbers) ⇒ Object
176 177 178 179 180 181 182 183 |
# File 'lib/teepee/commander.rb', line 176 def - numbers numbers = to_numbers numbers if numbers.length == 1 ensure_numeric -numbers.first else ensure_numeric numbers.reduce :- end end |
#/(numbers) ⇒ Object
189 190 191 192 193 194 195 196 |
# File 'lib/teepee/commander.rb', line 189 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
218 219 220 |
# File 'lib/teepee/commander.rb', line 218 def acos angle ensure_numeric Math.acos angle.to_number end |
#acosh(angle) ⇒ Object
222 223 224 |
# File 'lib/teepee/commander.rb', line 222 def acosh angle ensure_numeric Math.acosh angle.to_number end |
#add_percentage(numbers) ⇒ Object
206 207 208 |
# File 'lib/teepee/commander.rb', line 206 def add_percentage numbers to_numbers(numbers).inject {|base, percent| base * (1+percent/100.0) } end |
#asin(angle) ⇒ Object
226 227 228 |
# File 'lib/teepee/commander.rb', line 226 def asin angle ensure_numeric Math.asin angle.to_number end |
#asinh(angle) ⇒ Object
230 231 232 |
# File 'lib/teepee/commander.rb', line 230 def asinh angle ensure_numeric Math.asinh angle.to_number end |
#atan(angle) ⇒ Object
234 235 236 |
# File 'lib/teepee/commander.rb', line 234 def atan angle ensure_numeric Math.atan angle.to_number end |
#atanh(angle) ⇒ Object
238 239 240 |
# File 'lib/teepee/commander.rb', line 238 def atanh angle ensure_numeric Math.atanh angle.to_number end |
#b(expressions) ⇒ Object
242 243 244 |
# File 'lib/teepee/commander.rb', line 242 def b expressions html_tag :b, expressions end |
#backquote ⇒ Object
246 247 248 |
# File 'lib/teepee/commander.rb', line 246 def backquote "`" end |
#backslash ⇒ Object
250 251 252 |
# File 'lib/teepee/commander.rb', line 250 def backslash "\\" end |
#big(expressions) ⇒ Object
254 255 256 |
# File 'lib/teepee/commander.rb', line 254 def big expressions html_tag :big, expressions end |
#bookmarks_folder_id(id) ⇒ Object
258 259 260 |
# File 'lib/teepee/commander.rb', line 258 def bookmarks_folder_id id id_command_handler id, :Folder, "folder", "folders", "folders/bookmarks_inline", "bookmarks" end |
#boolean_and(booleans) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/teepee/commander.rb', line 262 def boolean_and booleans if booleans.empty? true_constant elsif false_constant? booleans.first false_constant elsif true_constant? booleans.first 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
274 275 276 |
# File 'lib/teepee/commander.rb', line 274 def boolean_nand booleans boolean_not boolean_and booleans end |
#boolean_nor(booleans) ⇒ Object
278 279 280 |
# File 'lib/teepee/commander.rb', line 278 def boolean_nor booleans boolean_not boolean_or booleans end |
#boolean_not(boolean) ⇒ Object
282 283 284 285 286 287 288 289 290 |
# File 'lib/teepee/commander.rb', line 282 def boolean_not boolean 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
292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/teepee/commander.rb', line 292 def boolean_or booleans if booleans.empty? false_constant elsif true_constant? booleans.first true_constant elsif false_constant? booleans.first or booleans.first.kind_of? WhitespaceToken boolean_or booleans[1..-1] else command_error "Not a boolean value" end end |
#boolean_xnor(booleans) ⇒ Object
304 305 306 |
# File 'lib/teepee/commander.rb', line 304 def boolean_xnor booleans boolean_not boolean_xor booleans end |
#boolean_xor(booleans) ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/teepee/commander.rb', line 308 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
337 338 339 |
# File 'lib/teepee/commander.rb', line 337 def br html_tag :br, nil end |
#case_operator(expressions) ⇒ Object
341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'lib/teepee/commander.rb', line 341 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, test_value] form elsif not rest.empty? cond_helper value, rest end end cond_helper value, rest end end |
#ceiling(number) ⇒ Object
356 357 358 |
# File 'lib/teepee/commander.rb', line 356 def ceiling number ensure_numeric number.to_number.ceil end |
#command_error(message) ⇒ Object
53 54 55 |
# File 'lib/teepee/commander.rb', line 53 def command_error %{<span style="color: red">[#{}]</span>} end |
#command_not_yet_implemented(command) ⇒ Object
57 58 59 |
# File 'lib/teepee/commander.rb', line 57 def command_not_yet_implemented command command_error "The command #{command} is not yet implemented." end |
#comment(expressions) ⇒ Object
360 361 362 |
# File 'lib/teepee/commander.rb', line 360 def comment expressions nil end |
#cond_operator(expressions) ⇒ Object
364 365 366 367 368 369 370 371 |
# File 'lib/teepee/commander.rb', line 364 def cond_operator expressions conditional, _, form, *rest = strip expressions if true_constant? conditional form elsif not rest.empty? cond_operator rest end end |
#cos(angle) ⇒ Object
373 374 375 |
# File 'lib/teepee/commander.rb', line 373 def cos angle ensure_numeric Math.cos angle.to_number end |
#cosh(angle) ⇒ Object
377 378 379 |
# File 'lib/teepee/commander.rb', line 377 def cosh angle ensure_numeric Math.cosh angle.to_number end |
#degrees2radians(degrees) ⇒ Object
381 382 383 |
# File 'lib/teepee/commander.rb', line 381 def degrees2radians degrees ensure_numeric degrees.to_number * Math::PI / 180.0 end |
#del(expressions) ⇒ Object
385 386 387 |
# File 'lib/teepee/commander.rb', line 385 def del expressions html_tag :del, expressions end |
#dollar ⇒ Object
389 390 391 |
# File 'lib/teepee/commander.rb', line 389 def dollar "$" end |
#e ⇒ Object
393 394 395 |
# File 'lib/teepee/commander.rb', line 393 def e Math::E end |
#ensure_boolean(boolean) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/teepee/commander.rb', line 75 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
65 66 67 68 69 70 71 72 73 |
# File 'lib/teepee/commander.rb', line 65 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
407 408 409 |
# File 'lib/teepee/commander.rb', line 407 def enumerate expressions html_tag :ol, expressions end |
#enumerate_lowercase(expressions) ⇒ Object
419 420 421 |
# File 'lib/teepee/commander.rb', line 419 def enumerate_lowercase expressions html_tag :ol, expressions, {type: "a"} end |
#enumerate_numeric(expressions) ⇒ Object
411 412 413 |
# File 'lib/teepee/commander.rb', line 411 def enumerate_numeric expressions html_tag :ol, expressions, {type: "1"} end |
#enumerate_roman_lowercase(expressions) ⇒ Object
427 428 429 |
# File 'lib/teepee/commander.rb', line 427 def enumerate_roman_lowercase expressions html_tag :ol, expressions, {type: "i"} end |
#enumerate_roman_uppercase(expressions) ⇒ Object
423 424 425 |
# File 'lib/teepee/commander.rb', line 423 def enumerate_roman_uppercase expressions html_tag :ol, expressions, {type: "I"} end |
#enumerate_uppercase(expressions) ⇒ Object
415 416 417 |
# File 'lib/teepee/commander.rb', line 415 def enumerate_uppercase expressions html_tag :ol, expressions, {type: "A"} end |
#equal(expressions) ⇒ Object
397 398 399 400 401 402 403 404 405 |
# File 'lib/teepee/commander.rb', line 397 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
431 432 433 |
# File 'lib/teepee/commander.rb', line 431 def erf number ensure_numeric Math.erf number.to_number end |
#erfc(number) ⇒ Object
435 436 437 |
# File 'lib/teepee/commander.rb', line 435 def erfc number ensure_numeric Math.erfc number.to_number end |
#false_constant ⇒ Object
439 440 441 |
# File 'lib/teepee/commander.rb', line 439 def false_constant "false" end |
#false_constant?(expression) ⇒ Boolean
150 151 152 |
# File 'lib/teepee/commander.rb', line 150 def false_constant? expression expression.to_s == "false" end |
#floor(number) ⇒ Object
443 444 445 |
# File 'lib/teepee/commander.rb', line 443 def floor number ensure_numeric number.to_number.floor end |
#folder_id(id) ⇒ Object
447 448 449 |
# File 'lib/teepee/commander.rb', line 447 def folder_id id id_command_handler id, :Folder end |
#forum_id(id) ⇒ Object
451 452 453 |
# File 'lib/teepee/commander.rb', line 451 def forum_id id id_command_handler id, :Forum end |
#gamma(number) ⇒ Object
455 456 457 |
# File 'lib/teepee/commander.rb', line 455 def gamma number ensure_numeric Math.gamma number.to_number end |
#get_operator(variable) ⇒ Object
459 460 461 |
# File 'lib/teepee/commander.rb', line 459 def get_operator variable @variables[variable.to_s].to_html end |
#greater_than(numbers) ⇒ Object
463 464 465 466 467 468 469 470 471 |
# File 'lib/teepee/commander.rb', line 463 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
473 474 475 476 477 478 479 480 481 |
# File 'lib/teepee/commander.rb', line 473 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
483 484 485 |
# File 'lib/teepee/commander.rb', line 483 def h1 expressions html_tag :h1, expressions end |
#h2(expressions) ⇒ Object
487 488 489 |
# File 'lib/teepee/commander.rb', line 487 def h2 expressions html_tag :h2, expressions end |
#h3(expressions) ⇒ Object
491 492 493 |
# File 'lib/teepee/commander.rb', line 491 def h3 expressions html_tag :h3, expressions end |
#h4(expressions) ⇒ Object
495 496 497 |
# File 'lib/teepee/commander.rb', line 495 def h4 expressions html_tag :h4, expressions end |
#h5(expressions) ⇒ Object
499 500 501 |
# File 'lib/teepee/commander.rb', line 499 def h5 expressions html_tag :h5, expressions end |
#h6(expressions) ⇒ Object
503 504 505 |
# File 'lib/teepee/commander.rb', line 503 def h6 expressions html_tag :h6, expressions end |
#html_tag(tag, expressions, attribs = nil) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/teepee/commander.rb', line 83 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
564 565 566 |
# File 'lib/teepee/commander.rb', line 564 def hypot numbers ensure_numeric Math.sqrt to_numbers(numbers).map {|n| n**2} end |
#i ⇒ Object
507 508 509 |
# File 'lib/teepee/commander.rb', line 507 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
109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/teepee/commander.rb', line 109 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
511 512 513 514 515 516 517 518 519 |
# File 'lib/teepee/commander.rb', line 511 def if_operator expressions expressions = strip expressions conditional, _, true_clause, _, false_clause = expressions if true_constant? conditional true_clause.to_html elsif false_clause false_clause.to_html end end |
#image(expressions) ⇒ Object
521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
# File 'lib/teepee/commander.rb', line 521 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 |
#it(expressions) ⇒ Object
536 537 538 |
# File 'lib/teepee/commander.rb', line 536 def it expressions html_tag :i, expressions end |
#item(expressions) ⇒ Object
540 541 542 |
# File 'lib/teepee/commander.rb', line 540 def item expressions html_tag :li, expressions end |
#itemize(expressions) ⇒ Object
544 545 546 |
# File 'lib/teepee/commander.rb', line 544 def itemize expressions html_tag :ul, expressions end |
#itemize_circle(expressions) ⇒ Object
552 553 554 |
# File 'lib/teepee/commander.rb', line 552 def itemize_circle expressions html_tag :ul, expressions, {style: "list-style-type:circle"} end |
#itemize_disc(expressions) ⇒ Object
548 549 550 |
# File 'lib/teepee/commander.rb', line 548 def itemize_disc expressions html_tag :ul, expressions, {style: "list-style-type:disc"} end |
#itemize_none(expressions) ⇒ Object
560 561 562 |
# File 'lib/teepee/commander.rb', line 560 def itemize_none expressions html_tag :ul, expressions, {style: "list-style-type:none"} end |
#itemize_square(expressions) ⇒ Object
556 557 558 |
# File 'lib/teepee/commander.rb', line 556 def itemize_square expressions html_tag :ul, expressions, {style: "list-style-type:square"} end |
#keyword_id(id) ⇒ Object
568 569 570 |
# File 'lib/teepee/commander.rb', line 568 def keyword_id id id_command_handler id, :Keyword end |
#ld(number) ⇒ Object
572 573 574 |
# File 'lib/teepee/commander.rb', line 572 def ld number ensure_numeric Math.log2 number.to_number end |
#ldexp(fraction, exponent) ⇒ Object
576 577 578 |
# File 'lib/teepee/commander.rb', line 576 def ldexp fraction, exponent ensure_numeric Math.ldexp fraction.to_number, exponent.to_number end |
#left_brace ⇒ Object
580 581 582 |
# File 'lib/teepee/commander.rb', line 580 def left_brace "{" end |
#left_bracket ⇒ Object
584 585 586 |
# File 'lib/teepee/commander.rb', line 584 def left_bracket "[" end |
#left_strip(expressions) ⇒ Object
124 125 126 127 128 129 |
# File 'lib/teepee/commander.rb', line 124 def left_strip expressions while expressions.first.kind_of? WhitespaceToken expressions.shift end expressions end |
#less_than(numbers) ⇒ Object
588 589 590 591 592 593 594 595 596 |
# File 'lib/teepee/commander.rb', line 588 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
598 599 600 601 602 603 604 605 606 |
# File 'lib/teepee/commander.rb', line 598 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
608 609 610 |
# File 'lib/teepee/commander.rb', line 608 def lgamma number ensure_numeric Math::lgamma(number.to_number).first end |
#link(expressions) ⇒ Object
612 613 614 615 616 617 618 619 620 621 622 |
# File 'lib/teepee/commander.rb', line 612 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
624 625 626 |
# File 'lib/teepee/commander.rb', line 624 def link_id id id_command_handler id, :Link end |
#ln(number) ⇒ Object
628 629 630 |
# File 'lib/teepee/commander.rb', line 628 def ln number ensure_numeric Math.log number.to_number end |
#log(base, number) ⇒ Object
632 633 634 635 636 637 638 639 |
# File 'lib/teepee/commander.rb', line 632 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
641 642 643 |
# File 'lib/teepee/commander.rb', line 641 def log10 number ensure_numeric Math.log10 number.to_number end |
#mailto(email_address) ⇒ Object
645 646 647 648 649 650 651 652 |
# File 'lib/teepee/commander.rb', line 645 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
654 655 656 |
# File 'lib/teepee/commander.rb', line 654 def mod numbers ensure_numeric to_numbers(numbers).reduce :% end |
#nbsp(count) ⇒ Object
658 659 660 661 662 663 664 |
# File 'lib/teepee/commander.rb', line 658 def nbsp count if count and count.to_number and count.to_number > 0 " " * count.to_number else " " end end |
#not_equal(numbers) ⇒ Object
666 667 668 669 670 671 672 673 674 |
# File 'lib/teepee/commander.rb', line 666 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
676 677 678 |
# File 'lib/teepee/commander.rb', line 676 def note_id id id_command_handler id, :Note end |
#number_from_word(word) ⇒ Object
158 159 160 161 162 163 164 |
# File 'lib/teepee/commander.rb', line 158 def number_from_word word begin word.to_number rescue ArgumentError, NoMethodError nil end end |
#numeric?(*numbers) ⇒ Boolean
61 62 63 |
# File 'lib/teepee/commander.rb', line 61 def numeric? *numbers numbers.all? {|number| number.kind_of? Numeric} end |
#percent_total(numbers) ⇒ Object
214 215 216 |
# File 'lib/teepee/commander.rb', line 214 def percent_total numbers to_numbers(numbers).inject {|total, part| Float(part)/Float(total)*100.0 } end |
#pi ⇒ Object
680 681 682 |
# File 'lib/teepee/commander.rb', line 680 def pi Math::PI end |
#pipe ⇒ Object
684 685 686 |
# File 'lib/teepee/commander.rb', line 684 def pipe "|" end |
#pipe?(expression) ⇒ Boolean
154 155 156 |
# File 'lib/teepee/commander.rb', line 154 def pipe? expression expression.is_a? PipeToken end |
#prog1_operator(expressions) ⇒ Object
688 689 690 |
# File 'lib/teepee/commander.rb', line 688 def prog1_operator expressions expressions.map(&:to_html).first end |
#progn_operator(expressions) ⇒ Object
692 693 694 |
# File 'lib/teepee/commander.rb', line 692 def progn_operator expressions expressions.map(&:to_html).last end |
#radians2degrees(radians) ⇒ Object
696 697 698 |
# File 'lib/teepee/commander.rb', line 696 def radians2degrees radians ensure_numeric(radians.to_number * 180.0 / Math::PI) end |
#right_brace ⇒ Object
700 701 702 |
# File 'lib/teepee/commander.rb', line 700 def right_brace "}" end |
#right_bracket ⇒ Object
704 705 706 |
# File 'lib/teepee/commander.rb', line 704 def right_bracket "]" end |
#right_strip(expressions) ⇒ Object
131 132 133 134 135 136 |
# File 'lib/teepee/commander.rb', line 131 def right_strip expressions while expressions.last.kind_of? WhitespaceToken expressions.pop end expressions end |
#round(number, precision = nil, *_) ⇒ Object
708 709 710 711 712 713 714 |
# File 'lib/teepee/commander.rb', line 708 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 |
#set_operator(expressions) ⇒ Object
716 717 718 719 720 |
# File 'lib/teepee/commander.rb', line 716 def set_operator expressions variable, _, value = expressions @variables[variable.to_s] = value value.to_html end |
#sin(angle) ⇒ Object
722 723 724 |
# File 'lib/teepee/commander.rb', line 722 def sin angle ensure_numeric Math.sin angle.to_number end |
#sinh(angle) ⇒ Object
726 727 728 |
# File 'lib/teepee/commander.rb', line 726 def sinh angle ensure_numeric Math.sinh angle.to_number end |
#small(expressions) ⇒ Object
730 731 732 |
# File 'lib/teepee/commander.rb', line 730 def small expressions html_tag :small, expressions end |
#space ⇒ Object
742 743 744 |
# File 'lib/teepee/commander.rb', line 742 def space " " end |
#span_operator(expressions) ⇒ Object
746 747 748 |
# File 'lib/teepee/commander.rb', line 746 def span_operator expressions html_tag :span, expressions end |
#sqrt(number) ⇒ Object
738 739 740 |
# File 'lib/teepee/commander.rb', line 738 def sqrt number ensure_numeric Math.sqrt number.to_number end |
#squiggle ⇒ Object
734 735 736 |
# File 'lib/teepee/commander.rb', line 734 def squiggle "~" end |
#strip(expressions) ⇒ Object
138 139 140 |
# File 'lib/teepee/commander.rb', line 138 def strip expressions left_strip right_strip expressions end |
#sub(expressions) ⇒ Object
750 751 752 |
# File 'lib/teepee/commander.rb', line 750 def sub expressions html_tag :sub, expressions end |
#subtract_percentage(numbers) ⇒ Object
210 211 212 |
# File 'lib/teepee/commander.rb', line 210 def subtract_percentage numbers to_numbers(numbers).inject {|base, percent| base * (1-percent/100.0) } end |
#sup(expressions) ⇒ Object
754 755 756 |
# File 'lib/teepee/commander.rb', line 754 def sup expressions html_tag :sup, expressions end |
#table(expressions) ⇒ Object
758 759 760 |
# File 'lib/teepee/commander.rb', line 758 def table expressions html_tag :table, expressions end |
#table_data(expressions) ⇒ Object
762 763 764 |
# File 'lib/teepee/commander.rb', line 762 def table_data expressions html_tag :td, expressions end |
#table_header(expressions) ⇒ Object
766 767 768 |
# File 'lib/teepee/commander.rb', line 766 def table_header expressions html_tag :th, expressions end |
#table_row(expressions) ⇒ Object
770 771 772 |
# File 'lib/teepee/commander.rb', line 770 def table_row expressions html_tag :tr, expressions end |
#tag_id(id) ⇒ Object
774 775 776 |
# File 'lib/teepee/commander.rb', line 774 def tag_id id id_command_handler id, :Tag end |
#tan(angle) ⇒ Object
778 779 780 |
# File 'lib/teepee/commander.rb', line 778 def tan angle ensure_numeric Math.tan angle.to_number end |
#tanh(angle) ⇒ Object
782 783 784 |
# File 'lib/teepee/commander.rb', line 782 def tanh angle ensure_numeric Math.tanh angle.to_number end |
#tb_href(target, string) ⇒ Object
105 106 107 |
# File 'lib/teepee/commander.rb', line 105 def tb_href target, string %{<a href="#{TB_COM}/#{target}">#{string}</a>} end |
#to_numbers(words) ⇒ Object
166 167 168 |
# File 'lib/teepee/commander.rb', line 166 def to_numbers words words.map {|word| number_from_word word}.reject &:nil? end |
#true_constant ⇒ Object
786 787 788 |
# File 'lib/teepee/commander.rb', line 786 def true_constant "true" end |
#true_constant?(expression) ⇒ Boolean
146 147 148 |
# File 'lib/teepee/commander.rb', line 146 def true_constant? expression expression.to_s == "true" end |
#tt(expressions) ⇒ Object
790 791 792 |
# File 'lib/teepee/commander.rb', line 790 def tt expressions html_tag :tt, expressions end |
#u(expressions) ⇒ Object
794 795 796 |
# File 'lib/teepee/commander.rb', line 794 def u expressions html_tag :u, expressions end |
#unless_operator(expressions) ⇒ Object
798 799 800 801 802 803 804 805 806 807 808 809 |
# File 'lib/teepee/commander.rb', line 798 def unless_operator expressions expressions = strip expressions conditional = expressions.first expressions = strip expressions.rest if false_constant? conditional if expressions.length <= 1 expressions.first else span_operator expressions end end end |
#user(user) ⇒ Object
811 812 813 814 815 816 817 |
# File 'lib/teepee/commander.rb', line 811 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
142 143 144 |
# File 'lib/teepee/commander.rb', line 142 def valid_email_address? email_address email_address =~ /\A[[:graph:]]+@[\w.]+\z/ end |
#valid_uri?(uri) ⇒ Boolean
47 48 49 50 51 |
# File 'lib/teepee/commander.rb', line 47 def valid_uri? uri (!! (u = URI.parse(uri))) and not u.scheme.nil? rescue URI::InvalidURIError false end |
#when_operator(expressions) ⇒ Object
819 820 821 822 823 824 825 826 827 828 829 830 |
# File 'lib/teepee/commander.rb', line 819 def when_operator expressions expressions = strip expressions conditional = expressions.first expressions = strip expressions.rest if true_constant? conditional if expressions.length <= 1 expressions.first else span_operator expressions end end end |