tbmx 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/tbmx.rb +126 -6
  2. metadata +1 -2
data/lib/tbmx.rb CHANGED
@@ -82,12 +82,17 @@ module TBMX
82
82
 
83
83
  class StringToken < Token
84
84
  attr_reader :text
85
+
85
86
  def initialize(text)
86
87
  raise ArgumentError if not text.is_a? String
87
88
  raise ArgumentError if not text =~ self.class.full_match_regex
88
89
  @text = text
89
90
  end
90
91
 
92
+ def to_s
93
+ @text
94
+ end
95
+
91
96
  def to_html
92
97
  @text
93
98
  end
@@ -210,6 +215,10 @@ module TBMX
210
215
  @expressions = expressions
211
216
  end
212
217
 
218
+ def command_error(message)
219
+ %{<span style="color: red">[#{message}]</span>}
220
+ end
221
+
213
222
  def to_html
214
223
  case command.word
215
224
  when "backslash", "bslash"
@@ -221,21 +230,124 @@ module TBMX
221
230
  when "br", "newline"
222
231
  "\n</br>\n"
223
232
  when "bold", "b", "textbf"
224
- "<b>" + expressions.map(&:to_html).join + "</b>"
233
+ html_tag :b
234
+ when "del", "s", "strike", "strikethrough", "strikeout"
235
+ html_tag :del
225
236
  when "italic", "i", "textit"
226
- "<i>" + expressions.map(&:to_html).join + "</i>"
237
+ html_tag :i
227
238
  when "underline", "u"
228
- "<u>" + expressions.map(&:to_html).join + "</u>"
239
+ html_tag :u
240
+ when "tt", "texttt", "teletype", "typewriter"
241
+ html_tag :tt
242
+ when "small"
243
+ html_tag :small
244
+ when "big"
245
+ html_tag :big
229
246
  when "subscript", "sub"
230
- "<sub>" + expressions.map(&:to_html).join + "</sub>"
247
+ html_tag :sub
231
248
  when "superscript", "sup"
232
- "<sup>" + expressions.map(&:to_html).join + "</sup>"
249
+ html_tag :sup
250
+ when "user", "user-id", "user_id"
251
+ user_command_handler
252
+ when "link-id", "link_id"
253
+ link_id_command_handler
254
+ when "tag-id", "tag_id"
255
+ tag_id_command_handler
256
+ when "bookmark-folder-id", "bookmark_folder_id", "bookmark_folder-id", "bookmark-folder_id"
257
+ bookmark_folder_id_command_handler
233
258
  else
234
- %{<span style="color: red">[UNKNOWN COMMAND #{command.to_html}]</span>}
259
+ command_error "unknown command #{command.to_html}"
260
+ end
261
+ end
262
+
263
+ def html_tag(tag)
264
+ "<#{tag}>" + expressions.map(&:to_html).join + "</#{tag}>"
265
+ end
266
+
267
+ def user_command_handler
268
+ user = expressions.select {|expr| expr.is_a? WordToken}.first
269
+ if not user
270
+ command_error "user: error: no user specified"
271
+ else
272
+ if @@action_view.kind_of? ActionView::Base
273
+ the_user = User.smart_find user.to_s
274
+ if the_user
275
+ @@action_view.render partial: 'users/name_link',
276
+ locals: {the_user: the_user}
277
+ else
278
+ command_error "unknown user #{user.to_s}"
279
+ end
280
+ else
281
+ %{<a href="http://thinkingbicycle.com/users/#{user}">#{user.to_s}</a>}
282
+ end
283
+ end
284
+ end
285
+
286
+ def link_id_command_handler
287
+ link_id = expressions.select {|expr| expr.is_a? WordToken}.first
288
+ if not link_id
289
+ command_error "link_id: error: no link ID specified"
290
+ elsif not link_id.to_s =~ /\A[0-9]+\z/
291
+ command_error "link_id: error: invalid link ID specified"
292
+ else
293
+ if @@action_view.kind_of? ActionView::Base
294
+ link = Link.find Integer(link_id.to_s)
295
+ if link
296
+ @@action_view.render partial: 'links/inline',
297
+ locals: {link: link}
298
+ else
299
+ command_error "unknown link ID #{link_id.to_s}"
300
+ end
301
+ else
302
+ %{<a href="http://thinkingbicycle.com/links/#{link_id.to_s}">Link ##{link_id.to_s}</a>}
303
+ end
304
+ end
305
+ end
306
+
307
+ def tag_id_command_handler
308
+ tag_id = expressions.select {|expr| expr.is_a? WordToken}.first
309
+ if not tag_id
310
+ command_error "tag_id: error: no tag ID specified"
311
+ elsif not tag_id.to_s =~ /\A[0-9]+\z/
312
+ command_error "tag_id: error: invalid tag ID specified"
313
+ else
314
+ if @@action_view.kind_of? ActionView::Base
315
+ tag = Tag.find Integer(tag_id.to_s)
316
+ if tag
317
+ "<br/>" + @@action_view.render(partial: 'tags/show',locals: {tag: tag}) + "<br/>"
318
+ else
319
+ command_error "unknown tag ID #{tag_id.to_s}"
320
+ end
321
+ else
322
+ %{<a href="http://thinkingbicycle.com/tags/#{tag_id.to_s}">Tag ##{tag_id.to_s}</a>}
323
+ end
324
+ end
325
+ end
326
+
327
+ def bookmark_folder_id_command_handler
328
+ bookmark_folder_id = expressions.select {|expr| expr.is_a? WordToken}.first
329
+ if not bookmark_folder_id
330
+ command_error "bookmark_folder_id: error: no bookmark folder ID specified"
331
+ elsif not bookmark_folder_id.to_s =~ /\A[0-9]+\z/
332
+ command_error "bookmark_folder_id: error: invalid bookmark folder ID specified"
333
+ else
334
+ if @@action_view.kind_of? ActionView::Base
335
+ bookmark_folder = BookmarkFolder.find Integer(bookmark_folder_id.to_s)
336
+ if bookmark_folder
337
+ %{<a href="/bookmark_folders/#{bookmark_folder.id}">Bookmark Folder ##{bookmark_folder.id}</a>}
338
+ else
339
+ command_error "unknown bookmark folder ID #{bookmark_folder_id.to_s}"
340
+ end
341
+ else
342
+ %{<a href="http://thinkingbicycle.com/bookmark_folders/#{bookmark_folder_id.to_s}">Bookmark Folder ##{bookmark_folder_id.to_s}</a>}
343
+ end
235
344
  end
236
345
  end
237
346
 
238
347
  class << self
348
+ @@action_view = nil
349
+ @@controller = nil
350
+
239
351
  def parse(tokens)
240
352
  expressions = []
241
353
  rest = tokens
@@ -266,6 +378,14 @@ module TBMX
266
378
  return [CommandParser.new(command, expressions), rest]
267
379
  end
268
380
  end
381
+
382
+ def action_view=(new)
383
+ @@action_view = new
384
+ end
385
+
386
+ def controller=(new)
387
+ @@controller = new
388
+ end
269
389
  end
270
390
  end
271
391
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tbmx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -75,4 +75,3 @@ signing_key:
75
75
  specification_version: 3
76
76
  summary: TBMX is a markup language based on TeX for ThinkingBicycle.com.
77
77
  test_files: []
78
- has_rdoc: