tbmx 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/tbmx.rb +80 -61
  2. metadata +4 -4
data/lib/tbmx.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  # -*- mode: Ruby -*-
3
3
 
4
- # Copyright © 2013, Christopher Mark Gore,
4
+ # Copyright © 2013-2014, Christopher Mark Gore,
5
5
  # Soli Deo Gloria,
6
6
  # All rights reserved.
7
7
  #
@@ -12,16 +12,16 @@
12
12
  # Redistribution and use in source and binary forms, with or without
13
13
  # modification, are permitted provided that the following conditions are met:
14
14
  #
15
- # * Redistributions of source code must retain the above copyright
16
- # notice, this list of conditions and the following disclaimer.
15
+ # * Redistributions of source code must retain the above copyright
16
+ # notice, this list of conditions and the following disclaimer.
17
17
  #
18
- # * Redistributions in binary form must reproduce the above copyright
19
- # notice, this list of conditions and the following disclaimer in the
20
- # documentation and/or other materials provided with the distribution.
18
+ # * Redistributions in binary form must reproduce the above copyright
19
+ # notice, this list of conditions and the following disclaimer in the
20
+ # documentation and/or other materials provided with the distribution.
21
21
  #
22
- # * Neither the name of Christopher Mark Gore nor the names of other
23
- # contributors may be used to endorse or promote products derived from
24
- # this software without specific prior written permission.
22
+ # * Neither the name of Christopher Mark Gore nor the names of other
23
+ # contributors may be used to endorse or promote products derived from
24
+ # this software without specific prior written permission.
25
25
  #
26
26
  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
27
  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@@ -41,6 +41,8 @@ require 'monkey-patch'
41
41
  include ERB::Util
42
42
 
43
43
  module TBMX
44
+ TB_COM = "http://thinkingbicycle.com"
45
+
44
46
  class ParseError < RuntimeError
45
47
  end
46
48
 
@@ -176,6 +178,31 @@ module TBMX
176
178
  end
177
179
  end
178
180
 
181
+ class NumberToken < Token
182
+ attr_reader :number, :text
183
+
184
+ def initialize(text)
185
+ raise ArgumentError if not text.is_a? String
186
+ @text = text
187
+ end
188
+
189
+ def parse
190
+ end
191
+
192
+ def to_s
193
+ number.to_s
194
+ end
195
+
196
+ def to_html
197
+ to_s
198
+ end
199
+
200
+ class << self
201
+ def matches? text
202
+ end
203
+ end
204
+ end
205
+
179
206
  class Tokenizer
180
207
  attr_reader :text, :tokens
181
208
  def initialize(text)
@@ -192,6 +219,7 @@ module TBMX
192
219
  result = RightBraceToken.matches?(rest) or
193
220
  result = EmptyNewlinesToken.matches?(rest) or # String Tokens
194
221
  result = WhitespaceToken.matches?(rest) or
222
+ result = NumberToken.matches?(rest) or
195
223
  result = WordToken.matches?(rest)
196
224
  then
197
225
  @tokens << result[0]
@@ -223,9 +251,11 @@ module TBMX
223
251
  case command.word
224
252
  when "backslash", "bslash"
225
253
  "\\"
226
- when "left-brace", "left_brace", "leftbrace", "lbrace"
254
+ when "left-brace", "left_brace", "leftbrace", "lbrace", "opening-brace", "opening_brace",
255
+ "openingbrace", "obrace"
227
256
  "{"
228
- when "right-brace", "right_brace", "rightbrace", "rbrace"
257
+ when "right-brace", "right_brace", "rightbrace", "rbrace", "closing-brace", "closing_brace",
258
+ "closingbrace", "cbrace"
229
259
  "}"
230
260
  when "br", "newline"
231
261
  "\n</br>\n"
@@ -251,10 +281,15 @@ module TBMX
251
281
  user_command_handler
252
282
  when "link-id", "link_id"
253
283
  link_id_command_handler
284
+ when "keyword-id", "keyword_id"
285
+ keyword_id_command_handler
254
286
  when "tag-id", "tag_id"
255
287
  tag_id_command_handler
256
- when "bookmark-folder-id", "bookmark_folder_id", "bookmark_folder-id", "bookmark-folder_id"
257
- bookmark_folder_id_command_handler
288
+ when "folder-id", "folder_id"
289
+ folder_id_command_handler
290
+ when "bookmarks-folder-id", "bookmarks_folder_id", "bookmarks_folder-id", "bookmarks-folder_id",
291
+ "bookmark-folder-id", "bookmark_folder_id", "bookmark_folder-id", "bookmark-folder_id"
292
+ bookmarks_folder_id_command_handler
258
293
  else
259
294
  command_error "unknown command #{command.to_html}"
260
295
  end
@@ -264,6 +299,10 @@ module TBMX
264
299
  "<#{tag}>" + expressions.map(&:to_html).join + "</#{tag}>"
265
300
  end
266
301
 
302
+ def tb_href(target, string)
303
+ %{<a href="#{TB_COM}/#{target}">#{string}</a>}
304
+ end
305
+
267
306
  def user_command_handler
268
307
  user = expressions.select {|expr| expr.is_a? WordToken}.first
269
308
  if not user
@@ -278,70 +317,50 @@ module TBMX
278
317
  command_error "unknown user #{user.to_s}"
279
318
  end
280
319
  else
281
- %{<a href="http://thinkingbicycle.com/users/#{user}">#{user.to_s}</a>}
320
+ tb_href "users/#{user}", user.to_s
282
321
  end
283
322
  end
284
323
  end
285
324
 
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"
325
+ def id_command_handler(klass,
326
+ singular = klass.to_s.camelcase_to_snakecase,
327
+ plural = singular.pluralize,
328
+ partial = "#{plural}/inline",
329
+ view="")
330
+ id = expressions.select {|expr| expr.is_a? WordToken}.first
331
+ if not id
332
+ command_error "#{singular}_id: error: no #{singular} ID specified"
333
+ elsif not id.to_s =~ /\A[0-9]+\z/
334
+ command_error "#{singular}_id: error: invalid #{singular} ID specified"
292
335
  else
293
336
  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}
337
+ thing = klass.find Integer(id.to_s)
338
+ if thing
339
+ @@action_view.render partial: partial,
340
+ locals: {singular.to_sym => thing}
298
341
  else
299
- command_error "unknown link ID #{link_id.to_s}"
342
+ command_error "unknown #{singular} ID #{id.to_s}"
300
343
  end
301
344
  else
302
- %{<a href="http://thinkingbicycle.com/links/#{link_id.to_s}">Link ##{link_id.to_s}</a>}
345
+ tb_href "/#{plural}/#{id.to_s}/#{view}", "#{klass} ##{id.to_s}"
303
346
  end
304
347
  end
305
348
  end
306
349
 
350
+ def link_id_command_handler
351
+ id_command_handler Link
352
+ end
353
+
307
354
  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
355
+ id_command_handler Tag
325
356
  end
326
357
 
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
344
- end
358
+ def folder_id_command_handler
359
+ id_command_handler Folder
360
+ end
361
+
362
+ def bookmarks_folder_id_command_handler
363
+ id_command_handler Folder, "folder", "folders", "folders/bookmarks_inline", "bookmarks"
345
364
  end
346
365
 
347
366
  class << self
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.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-16 00:00:00.000000000 Z
12
+ date: 2014-02-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -43,7 +43,7 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
- description: TBMX is a markup language based on TeX for ThinkingBicycle.com.
46
+ description: TBMX is a markup language, loosely based on Lisp and TeX, for ThinkingBicycle.com.
47
47
  email: cgore@cgore.com
48
48
  executables: []
49
49
  extensions: []
@@ -73,5 +73,5 @@ rubyforge_project:
73
73
  rubygems_version: 1.8.23
74
74
  signing_key:
75
75
  specification_version: 3
76
- summary: TBMX is a markup language based on TeX for ThinkingBicycle.com.
76
+ summary: TBMX is a markup language for ThinkingBicycle.com.
77
77
  test_files: []