Class: Teepee::Commander
- Inherits:
-
Object
- Object
- Teepee::Commander
- Defined in:
- lib/teepee/commander.rb
Direct Known Subclasses
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 ⇒ 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 ⇒ Commander
Returns a new instance of Commander.
40 41 42 |
# File 'lib/teepee/commander.rb', line 40 def initialize @variables = Hash.new end |
Instance Method Details
#%(numbers) ⇒ Object
199 200 201 |
# File 'lib/teepee/commander.rb', line 199 def % numbers to_numbers(numbers).inject { |base, percent| base*percent/100.0 } end |
#*(numbers) ⇒ Object
182 183 184 |
# File 'lib/teepee/commander.rb', line 182 def * numbers ensure_numeric to_numbers(numbers).inject 1, :* end |
#**(numbers) ⇒ Object
195 196 197 |
# File 'lib/teepee/commander.rb', line 195 def ** numbers ensure_numeric to_numbers(numbers).reduce :** end |
#+(numbers) ⇒ Object
169 170 171 |
# File 'lib/teepee/commander.rb', line 169 def + numbers ensure_numeric to_numbers(numbers).inject 0, :+ end |
#-(numbers) ⇒ Object
173 174 175 176 177 178 179 180 |
# File 'lib/teepee/commander.rb', line 173 def - numbers numbers = to_numbers numbers if numbers.length == 1 ensure_numeric -numbers.first else ensure_numeric numbers.reduce :- end end |
#/(numbers) ⇒ Object
186 187 188 189 190 191 192 193 |
# File 'lib/teepee/commander.rb', line 186 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
215 216 217 |
# File 'lib/teepee/commander.rb', line 215 def acos angle ensure_numeric Math.acos angle.to_number end |
#acosh(angle) ⇒ Object
219 220 221 |
# File 'lib/teepee/commander.rb', line 219 def acosh angle ensure_numeric Math.acosh angle.to_number end |
#add_percentage(numbers) ⇒ Object
203 204 205 |
# File 'lib/teepee/commander.rb', line 203 def add_percentage numbers to_numbers(numbers).inject {|base, percent| base * (1+percent/100.0) } end |
#asin(angle) ⇒ Object
223 224 225 |
# File 'lib/teepee/commander.rb', line 223 def asin angle ensure_numeric Math.asin angle.to_number end |
#asinh(angle) ⇒ Object
227 228 229 |
# File 'lib/teepee/commander.rb', line 227 def asinh angle ensure_numeric Math.asinh angle.to_number end |
#atan(angle) ⇒ Object
231 232 233 |
# File 'lib/teepee/commander.rb', line 231 def atan angle ensure_numeric Math.atan angle.to_number end |
#atanh(angle) ⇒ Object
235 236 237 |
# File 'lib/teepee/commander.rb', line 235 def atanh angle ensure_numeric Math.atanh angle.to_number end |
#b(expressions) ⇒ Object
239 240 241 |
# File 'lib/teepee/commander.rb', line 239 def b expressions html_tag :b, expressions end |
#backquote ⇒ Object
243 244 245 |
# File 'lib/teepee/commander.rb', line 243 def backquote "`" end |
#backslash ⇒ Object
247 248 249 |
# File 'lib/teepee/commander.rb', line 247 def backslash "\\" end |
#big(expressions) ⇒ Object
251 252 253 |
# File 'lib/teepee/commander.rb', line 251 def big expressions html_tag :big, expressions end |
#bookmarks_folder_id(id) ⇒ Object
255 256 257 |
# File 'lib/teepee/commander.rb', line 255 def bookmarks_folder_id id id_command_handler id, :Folder, "folder", "folders", "folders/bookmarks_inline", "bookmarks" end |
#boolean_and(booleans) ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/teepee/commander.rb', line 259 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
271 272 273 |
# File 'lib/teepee/commander.rb', line 271 def boolean_nand booleans boolean_not boolean_and booleans end |
#boolean_nor(booleans) ⇒ Object
275 276 277 |
# File 'lib/teepee/commander.rb', line 275 def boolean_nor booleans boolean_not boolean_or booleans end |
#boolean_not(boolean) ⇒ Object
279 280 281 282 283 284 285 286 287 |
# File 'lib/teepee/commander.rb', line 279 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
289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/teepee/commander.rb', line 289 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
301 302 303 |
# File 'lib/teepee/commander.rb', line 301 def boolean_xnor booleans boolean_not boolean_xor booleans end |
#boolean_xor(booleans) ⇒ Object
305 306 307 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 |
# File 'lib/teepee/commander.rb', line 305 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
334 335 336 |
# File 'lib/teepee/commander.rb', line 334 def br html_tag :br, nil end |
#case_operator(expressions) ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/teepee/commander.rb', line 338 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
353 354 355 |
# File 'lib/teepee/commander.rb', line 353 def ceiling number ensure_numeric number.to_number.ceil end |
#command_error(message) ⇒ Object
50 51 52 |
# File 'lib/teepee/commander.rb', line 50 def command_error %{<span style="color: red">[#{message}]</span>} end |
#command_not_yet_implemented(command) ⇒ Object
54 55 56 |
# File 'lib/teepee/commander.rb', line 54 def command_not_yet_implemented command command_error "The command #{command} is not yet implemented." end |
#comment(expressions) ⇒ Object
357 358 359 |
# File 'lib/teepee/commander.rb', line 357 def comment expressions nil end |
#cond_operator(expressions) ⇒ Object
361 362 363 364 365 366 367 368 |
# File 'lib/teepee/commander.rb', line 361 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
370 371 372 |
# File 'lib/teepee/commander.rb', line 370 def cos angle ensure_numeric Math.cos angle.to_number end |
#cosh(angle) ⇒ Object
374 375 376 |
# File 'lib/teepee/commander.rb', line 374 def cosh angle ensure_numeric Math.cosh angle.to_number end |
#degrees2radians(degrees) ⇒ Object
378 379 380 |
# File 'lib/teepee/commander.rb', line 378 def degrees2radians degrees ensure_numeric degrees.to_number * Math::PI / 180.0 end |
#del(expressions) ⇒ Object
382 383 384 |
# File 'lib/teepee/commander.rb', line 382 def del expressions html_tag :del, expressions end |
#dollar ⇒ Object
386 387 388 |
# File 'lib/teepee/commander.rb', line 386 def dollar "$" end |
#e ⇒ Object
390 391 392 |
# File 'lib/teepee/commander.rb', line 390 def e Math::E end |
#ensure_boolean(boolean) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/teepee/commander.rb', line 72 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
62 63 64 65 66 67 68 69 70 |
# File 'lib/teepee/commander.rb', line 62 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
404 405 406 |
# File 'lib/teepee/commander.rb', line 404 def enumerate expressions html_tag :ol, expressions end |
#enumerate_lowercase(expressions) ⇒ Object
416 417 418 |
# File 'lib/teepee/commander.rb', line 416 def enumerate_lowercase expressions html_tag :ol, expressions, {type: "a"} end |
#enumerate_numeric(expressions) ⇒ Object
408 409 410 |
# File 'lib/teepee/commander.rb', line 408 def enumerate_numeric expressions html_tag :ol, expressions, {type: "1"} end |
#enumerate_roman_lowercase(expressions) ⇒ Object
424 425 426 |
# File 'lib/teepee/commander.rb', line 424 def enumerate_roman_lowercase expressions html_tag :ol, expressions, {type: "i"} end |
#enumerate_roman_uppercase(expressions) ⇒ Object
420 421 422 |
# File 'lib/teepee/commander.rb', line 420 def enumerate_roman_uppercase expressions html_tag :ol, expressions, {type: "I"} end |
#enumerate_uppercase(expressions) ⇒ Object
412 413 414 |
# File 'lib/teepee/commander.rb', line 412 def enumerate_uppercase expressions html_tag :ol, expressions, {type: "A"} end |
#equal(expressions) ⇒ Object
394 395 396 397 398 399 400 401 402 |
# File 'lib/teepee/commander.rb', line 394 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
428 429 430 |
# File 'lib/teepee/commander.rb', line 428 def erf number ensure_numeric Math.erf number.to_number end |
#erfc(number) ⇒ Object
432 433 434 |
# File 'lib/teepee/commander.rb', line 432 def erfc number ensure_numeric Math.erfc number.to_number end |
#false_constant ⇒ Object
436 437 438 |
# File 'lib/teepee/commander.rb', line 436 def false_constant "false" end |
#false_constant?(expression) ⇒ Boolean
147 148 149 |
# File 'lib/teepee/commander.rb', line 147 def false_constant? expression expression.to_s == "false" end |
#floor(number) ⇒ Object
440 441 442 |
# File 'lib/teepee/commander.rb', line 440 def floor number ensure_numeric number.to_number.floor end |
#folder_id(id) ⇒ Object
444 445 446 |
# File 'lib/teepee/commander.rb', line 444 def folder_id id id_command_handler id, :Folder end |
#forum_id(id) ⇒ Object
448 449 450 |
# File 'lib/teepee/commander.rb', line 448 def forum_id id id_command_handler id, :Forum end |
#gamma(number) ⇒ Object
452 453 454 |
# File 'lib/teepee/commander.rb', line 452 def gamma number ensure_numeric Math.gamma number.to_number end |
#get_operator(variable) ⇒ Object
456 457 458 |
# File 'lib/teepee/commander.rb', line 456 def get_operator variable @variables[variable.to_s] end |
#greater_than(numbers) ⇒ Object
460 461 462 463 464 465 466 467 468 |
# File 'lib/teepee/commander.rb', line 460 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
470 471 472 473 474 475 476 477 478 |
# File 'lib/teepee/commander.rb', line 470 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
480 481 482 |
# File 'lib/teepee/commander.rb', line 480 def h1 expressions html_tag :h1, expressions end |
#h2(expressions) ⇒ Object
484 485 486 |
# File 'lib/teepee/commander.rb', line 484 def h2 expressions html_tag :h2, expressions end |
#h3(expressions) ⇒ Object
488 489 490 |
# File 'lib/teepee/commander.rb', line 488 def h3 expressions html_tag :h3, expressions end |
#h4(expressions) ⇒ Object
492 493 494 |
# File 'lib/teepee/commander.rb', line 492 def h4 expressions html_tag :h4, expressions end |
#h5(expressions) ⇒ Object
496 497 498 |
# File 'lib/teepee/commander.rb', line 496 def h5 expressions html_tag :h5, expressions end |
#h6(expressions) ⇒ Object
500 501 502 |
# File 'lib/teepee/commander.rb', line 500 def h6 expressions html_tag :h6, expressions end |
#html_tag(tag, expressions, attribs = nil) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/teepee/commander.rb', line 80 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
561 562 563 |
# File 'lib/teepee/commander.rb', line 561 def hypot numbers ensure_numeric Math.sqrt to_numbers(numbers).map {|n| n**2} end |
#i ⇒ Object
504 505 506 |
# File 'lib/teepee/commander.rb', line 504 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
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/teepee/commander.rb', line 106 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
508 509 510 511 512 513 514 515 516 |
# File 'lib/teepee/commander.rb', line 508 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
518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'lib/teepee/commander.rb', line 518 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
533 534 535 |
# File 'lib/teepee/commander.rb', line 533 def it expressions html_tag :i, expressions end |
#item(expressions) ⇒ Object
537 538 539 |
# File 'lib/teepee/commander.rb', line 537 def item expressions html_tag :li, expressions end |
#itemize(expressions) ⇒ Object
541 542 543 |
# File 'lib/teepee/commander.rb', line 541 def itemize expressions html_tag :ul, expressions end |
#itemize_circle(expressions) ⇒ Object
549 550 551 |
# File 'lib/teepee/commander.rb', line 549 def itemize_circle expressions html_tag :ul, expressions, {style: "list-style-type:circle"} end |
#itemize_disc(expressions) ⇒ Object
545 546 547 |
# File 'lib/teepee/commander.rb', line 545 def itemize_disc expressions html_tag :ul, expressions, {style: "list-style-type:disc"} end |
#itemize_none(expressions) ⇒ Object
557 558 559 |
# File 'lib/teepee/commander.rb', line 557 def itemize_none expressions html_tag :ul, expressions, {style: "list-style-type:none"} end |
#itemize_square(expressions) ⇒ Object
553 554 555 |
# File 'lib/teepee/commander.rb', line 553 def itemize_square expressions html_tag :ul, expressions, {style: "list-style-type:square"} end |
#keyword_id(id) ⇒ Object
565 566 567 |
# File 'lib/teepee/commander.rb', line 565 def keyword_id id id_command_handler id, :Keyword end |
#ld(number) ⇒ Object
569 570 571 |
# File 'lib/teepee/commander.rb', line 569 def ld number ensure_numeric Math.log2 number.to_number end |
#ldexp(fraction, exponent) ⇒ Object
573 574 575 |
# File 'lib/teepee/commander.rb', line 573 def ldexp fraction, exponent ensure_numeric Math.ldexp fraction.to_number, exponent.to_number end |
#left_brace ⇒ Object
577 578 579 |
# File 'lib/teepee/commander.rb', line 577 def left_brace "{" end |
#left_bracket ⇒ Object
581 582 583 |
# File 'lib/teepee/commander.rb', line 581 def left_bracket "[" end |
#left_strip(expressions) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/teepee/commander.rb', line 121 def left_strip expressions while expressions.first.kind_of? WhitespaceToken expressions.shift end expressions end |
#less_than(numbers) ⇒ Object
585 586 587 588 589 590 591 592 593 |
# File 'lib/teepee/commander.rb', line 585 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
595 596 597 598 599 600 601 602 603 |
# File 'lib/teepee/commander.rb', line 595 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
605 606 607 |
# File 'lib/teepee/commander.rb', line 605 def lgamma number ensure_numeric Math::lgamma(number.to_number).first end |
#link(expressions) ⇒ Object
609 610 611 612 613 614 615 616 617 618 619 |
# File 'lib/teepee/commander.rb', line 609 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
621 622 623 |
# File 'lib/teepee/commander.rb', line 621 def link_id id id_command_handler id, :Link end |
#ln(number) ⇒ Object
625 626 627 |
# File 'lib/teepee/commander.rb', line 625 def ln number ensure_numeric Math.log number.to_number end |
#log(base, number) ⇒ Object
629 630 631 632 633 634 635 636 |
# File 'lib/teepee/commander.rb', line 629 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
638 639 640 |
# File 'lib/teepee/commander.rb', line 638 def log10 number ensure_numeric Math.log10 number.to_number end |
#mailto(email_address) ⇒ Object
642 643 644 645 646 647 648 649 |
# File 'lib/teepee/commander.rb', line 642 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
651 652 653 |
# File 'lib/teepee/commander.rb', line 651 def mod numbers ensure_numeric to_numbers(numbers).reduce :% end |
#nbsp(count) ⇒ Object
655 656 657 658 659 660 661 |
# File 'lib/teepee/commander.rb', line 655 def nbsp count if count and count.to_number and count.to_number > 0 " " * count.to_number else " " end end |
#not_equal(numbers) ⇒ Object
663 664 665 666 667 668 669 670 671 |
# File 'lib/teepee/commander.rb', line 663 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
673 674 675 |
# File 'lib/teepee/commander.rb', line 673 def note_id id id_command_handler id, :Note end |
#number_from_word(word) ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/teepee/commander.rb', line 155 def number_from_word word begin word.to_number rescue ArgumentError, NoMethodError nil end end |
#numeric?(*numbers) ⇒ Boolean
58 59 60 |
# File 'lib/teepee/commander.rb', line 58 def numeric? *numbers numbers.all? {|number| number.kind_of? Numeric} end |
#percent_total(numbers) ⇒ Object
211 212 213 |
# File 'lib/teepee/commander.rb', line 211 def percent_total numbers to_numbers(numbers).inject {|total, part| Float(part)/Float(total)*100.0 } end |
#pi ⇒ Object
677 678 679 |
# File 'lib/teepee/commander.rb', line 677 def pi Math::PI end |
#pipe ⇒ Object
681 682 683 |
# File 'lib/teepee/commander.rb', line 681 def pipe "|" end |
#pipe?(expression) ⇒ Boolean
151 152 153 |
# File 'lib/teepee/commander.rb', line 151 def pipe? expression expression.is_a? PipeToken end |
#prog1_operator(expressions) ⇒ Object
685 686 687 |
# File 'lib/teepee/commander.rb', line 685 def prog1_operator expressions expressions.map(&:to_html).first end |
#progn_operator(expressions) ⇒ Object
689 690 691 |
# File 'lib/teepee/commander.rb', line 689 def progn_operator expressions expressions.map(&:to_html).last end |
#radians2degrees(radians) ⇒ Object
693 694 695 |
# File 'lib/teepee/commander.rb', line 693 def radians2degrees radians ensure_numeric(radians.to_number * 180.0 / Math::PI) end |
#right_brace ⇒ Object
697 698 699 |
# File 'lib/teepee/commander.rb', line 697 def right_brace "}" end |
#right_bracket ⇒ Object
701 702 703 |
# File 'lib/teepee/commander.rb', line 701 def right_bracket "]" end |
#right_strip(expressions) ⇒ Object
128 129 130 131 132 133 |
# File 'lib/teepee/commander.rb', line 128 def right_strip expressions while expressions.last.kind_of? WhitespaceToken expressions.pop end expressions end |
#round(number, precision = nil, *_) ⇒ Object
705 706 707 708 709 710 711 |
# File 'lib/teepee/commander.rb', line 705 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
713 714 715 716 |
# File 'lib/teepee/commander.rb', line 713 def set_operator expressions variable, _, value = expressions @variables[variable.to_s] = value end |
#sin(angle) ⇒ Object
718 719 720 |
# File 'lib/teepee/commander.rb', line 718 def sin angle ensure_numeric Math.sin angle.to_number end |
#sinh(angle) ⇒ Object
722 723 724 |
# File 'lib/teepee/commander.rb', line 722 def sinh angle ensure_numeric Math.sinh angle.to_number end |
#small(expressions) ⇒ Object
726 727 728 |
# File 'lib/teepee/commander.rb', line 726 def small expressions html_tag :small, expressions end |
#space ⇒ Object
738 739 740 |
# File 'lib/teepee/commander.rb', line 738 def space " " end |
#span_operator(expressions) ⇒ Object
742 743 744 |
# File 'lib/teepee/commander.rb', line 742 def span_operator expressions html_tag :span, expressions end |
#sqrt(number) ⇒ Object
734 735 736 |
# File 'lib/teepee/commander.rb', line 734 def sqrt number ensure_numeric Math.sqrt number.to_number end |
#squiggle ⇒ Object
730 731 732 |
# File 'lib/teepee/commander.rb', line 730 def squiggle "~" end |
#strip(expressions) ⇒ Object
135 136 137 |
# File 'lib/teepee/commander.rb', line 135 def strip expressions left_strip right_strip expressions end |
#sub(expressions) ⇒ Object
746 747 748 |
# File 'lib/teepee/commander.rb', line 746 def sub expressions html_tag :sub, expressions end |
#subtract_percentage(numbers) ⇒ Object
207 208 209 |
# File 'lib/teepee/commander.rb', line 207 def subtract_percentage numbers to_numbers(numbers).inject {|base, percent| base * (1-percent/100.0) } end |
#sup(expressions) ⇒ Object
750 751 752 |
# File 'lib/teepee/commander.rb', line 750 def sup expressions html_tag :sup, expressions end |
#table(expressions) ⇒ Object
754 755 756 |
# File 'lib/teepee/commander.rb', line 754 def table expressions html_tag :table, expressions end |
#table_data(expressions) ⇒ Object
758 759 760 |
# File 'lib/teepee/commander.rb', line 758 def table_data expressions html_tag :td, expressions end |
#table_header(expressions) ⇒ Object
762 763 764 |
# File 'lib/teepee/commander.rb', line 762 def table_header expressions html_tag :th, expressions end |
#table_row(expressions) ⇒ Object
766 767 768 |
# File 'lib/teepee/commander.rb', line 766 def table_row expressions html_tag :tr, expressions end |
#tag_id(id) ⇒ Object
770 771 772 |
# File 'lib/teepee/commander.rb', line 770 def tag_id id id_command_handler id, :Tag end |
#tan(angle) ⇒ Object
774 775 776 |
# File 'lib/teepee/commander.rb', line 774 def tan angle ensure_numeric Math.tan angle.to_number end |
#tanh(angle) ⇒ Object
778 779 780 |
# File 'lib/teepee/commander.rb', line 778 def tanh angle ensure_numeric Math.tanh angle.to_number end |
#tb_href(target, string) ⇒ Object
102 103 104 |
# File 'lib/teepee/commander.rb', line 102 def tb_href target, string %{<a href="#{TB_COM}/#{target}">#{string}</a>} end |
#to_numbers(words) ⇒ Object
163 164 165 |
# File 'lib/teepee/commander.rb', line 163 def to_numbers words words.map {|word| number_from_word word}.reject &:nil? end |
#true_constant ⇒ Object
782 783 784 |
# File 'lib/teepee/commander.rb', line 782 def true_constant "true" end |
#true_constant?(expression) ⇒ Boolean
143 144 145 |
# File 'lib/teepee/commander.rb', line 143 def true_constant? expression expression.to_s == "true" end |
#tt(expressions) ⇒ Object
786 787 788 |
# File 'lib/teepee/commander.rb', line 786 def tt expressions html_tag :tt, expressions end |
#u(expressions) ⇒ Object
790 791 792 |
# File 'lib/teepee/commander.rb', line 790 def u expressions html_tag :u, expressions end |
#unless_operator(expressions) ⇒ Object
794 795 796 797 798 799 800 801 802 803 804 805 |
# File 'lib/teepee/commander.rb', line 794 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
807 808 809 810 811 812 813 |
# File 'lib/teepee/commander.rb', line 807 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
139 140 141 |
# File 'lib/teepee/commander.rb', line 139 def valid_email_address? email_address email_address =~ /\A[[:graph:]]+@[\w.]+\z/ end |
#valid_uri?(uri) ⇒ Boolean
44 45 46 47 48 |
# File 'lib/teepee/commander.rb', line 44 def valid_uri? uri (!! (u = URI.parse(uri))) and not u.scheme.nil? rescue URI::InvalidURIError false end |
#when_operator(expressions) ⇒ Object
815 816 817 818 819 820 821 822 823 824 825 826 |
# File 'lib/teepee/commander.rb', line 815 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 |