Class: TBMX::CommandParser
- Inherits:
-
ParserNode
- Object
- ParserNode
- TBMX::CommandParser
- Defined in:
- lib/tbmx.rb
Constant Summary collapse
- @@action_view =
nil- @@controller =
nil
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#expressions ⇒ Object
readonly
Returns the value of attribute expressions.
Class Method Summary collapse
Instance Method Summary collapse
- #bookmarks_folder_id_command_handler ⇒ Object
- #command_error(message) ⇒ Object
- #folder_id_command_handler ⇒ Object
- #html_tag(tag) ⇒ Object
- #id_command_handler(klass, singular = klass.to_s.camelcase_to_snakecase, plural = singular.pluralize, partial = "#{plural}/inline", view = "") ⇒ Object
-
#initialize(command, expressions) ⇒ CommandParser
constructor
A new instance of CommandParser.
- #link_id_command_handler ⇒ Object
- #tag_id_command_handler ⇒ Object
- #tb_href(target, string) ⇒ Object
- #to_html ⇒ Object
- #user_command_handler ⇒ Object
Constructor Details
#initialize(command, expressions) ⇒ CommandParser
Returns a new instance of CommandParser.
238 239 240 241 242 243 244 |
# File 'lib/tbmx.rb', line 238 def initialize(command, expressions) raise ArgumentError if not command.is_a? WordToken @command = command raise ArgumentError if not expressions.is_a? Array expressions.each {|expression| raise ArgumentError if not expression.kind_of? ParserNode} @expressions = expressions end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
236 237 238 |
# File 'lib/tbmx.rb', line 236 def command @command end |
#expressions ⇒ Object (readonly)
Returns the value of attribute expressions.
236 237 238 |
# File 'lib/tbmx.rb', line 236 def expressions @expressions end |
Class Method Details
.action_view=(new) ⇒ Object
401 402 403 |
# File 'lib/tbmx.rb', line 401 def action_view=(new) @@action_view = new end |
.controller=(new) ⇒ Object
405 406 407 |
# File 'lib/tbmx.rb', line 405 def controller=(new) @@controller = new end |
.parse(tokens) ⇒ Object
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'lib/tbmx.rb', line 370 def parse(tokens) expressions = [] rest = tokens backslash, command, left_brace = rest.shift(3) right_brace = nil raise ParseError if not backslash.is_a? BackslashToken raise ParseError if not command.is_a? WordToken if not left_brace.is_a? LeftBraceToken # A command with no interior. rest.unshift left_brace if not left_brace.is_a? WhitespaceToken return [CommandParser.new(command, []), rest] end while rest.length > 0 if rest.first.is_a? WordToken expressions << rest.shift elsif rest.first.is_a? WhitespaceToken expressions << rest.shift elsif rest.first.is_a? BackslashToken result, rest = CommandParser.parse(rest) expressions << result elsif rest.first.is_a? RightBraceToken right_brace = rest.shift return [CommandParser.new(command, expressions), rest] else raise ParseError end end if right_brace.nil? # Allow a forgotten final right brace. return [CommandParser.new(command, expressions), rest] end end |
Instance Method Details
#bookmarks_folder_id_command_handler ⇒ Object
362 363 364 |
# File 'lib/tbmx.rb', line 362 def bookmarks_folder_id_command_handler id_command_handler Folder, "folder", "folders", "folders/bookmarks_inline", "bookmarks" end |
#command_error(message) ⇒ Object
246 247 248 |
# File 'lib/tbmx.rb', line 246 def command_error() %{<span style="color: red">[#{}]</span>} end |
#folder_id_command_handler ⇒ Object
358 359 360 |
# File 'lib/tbmx.rb', line 358 def folder_id_command_handler id_command_handler Folder end |
#html_tag(tag) ⇒ Object
298 299 300 |
# File 'lib/tbmx.rb', line 298 def html_tag(tag) "<#{tag}>" + expressions.map(&:to_html).join + "</#{tag}>" end |
#id_command_handler(klass, singular = klass.to_s.camelcase_to_snakecase, plural = singular.pluralize, partial = "#{plural}/inline", view = "") ⇒ Object
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/tbmx.rb', line 325 def id_command_handler(klass, singular = klass.to_s.camelcase_to_snakecase, plural = singular.pluralize, partial = "#{plural}/inline", view="") id = expressions.select {|expr| expr.is_a? WordToken}.first 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 if @@action_view.kind_of? ActionView::Base thing = klass.find Integer(id.to_s) if thing @@action_view.render partial: partial, locals: {singular.to_sym => thing} else command_error "unknown #{singular} ID #{id.to_s}" end else tb_href "/#{plural}/#{id.to_s}/#{view}", "#{klass} ##{id.to_s}" end end end |
#link_id_command_handler ⇒ Object
350 351 352 |
# File 'lib/tbmx.rb', line 350 def link_id_command_handler id_command_handler Link end |
#tag_id_command_handler ⇒ Object
354 355 356 |
# File 'lib/tbmx.rb', line 354 def tag_id_command_handler id_command_handler Tag end |
#tb_href(target, string) ⇒ Object
302 303 304 |
# File 'lib/tbmx.rb', line 302 def tb_href(target, string) %{<a href="#{TB_COM}/#{target}">#{string}</a>} end |
#to_html ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/tbmx.rb', line 250 def to_html case command.word when "backslash", "bslash" "\\" when "left-brace", "left_brace", "leftbrace", "lbrace", "opening-brace", "opening_brace", "openingbrace", "obrace" "{" when "right-brace", "right_brace", "rightbrace", "rbrace", "closing-brace", "closing_brace", "closingbrace", "cbrace" "}" when "br", "newline" "\n</br>\n" when "bold", "b", "textbf" html_tag :b when "del", "s", "strike", "strikethrough", "strikeout" html_tag :del when "italic", "i", "textit" html_tag :i when "underline", "u" html_tag :u when "tt", "texttt", "teletype", "typewriter" html_tag :tt when "small" html_tag :small when "big" html_tag :big when "subscript", "sub" html_tag :sub when "superscript", "sup" html_tag :sup when "user", "user-id", "user_id" user_command_handler when "link-id", "link_id" link_id_command_handler when "keyword-id", "keyword_id" keyword_id_command_handler when "tag-id", "tag_id" tag_id_command_handler when "folder-id", "folder_id" folder_id_command_handler when "bookmarks-folder-id", "bookmarks_folder_id", "bookmarks_folder-id", "bookmarks-folder_id", "bookmark-folder-id", "bookmark_folder_id", "bookmark_folder-id", "bookmark-folder_id" bookmarks_folder_id_command_handler else command_error "unknown command #{command.to_html}" end end |
#user_command_handler ⇒ Object
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/tbmx.rb', line 306 def user_command_handler user = expressions.select {|expr| expr.is_a? WordToken}.first if not user command_error "user: error: no user specified" else if @@action_view.kind_of? ActionView::Base the_user = User.smart_find user.to_s if the_user @@action_view.render partial: 'users/name_link', locals: {the_user: the_user} else command_error "unknown user #{user.to_s}" end else tb_href "users/#{user}", user.to_s end end end |