tbmx 0.0.4 → 0.1.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 +78 -2
  2. metadata +2 -2
data/lib/tbmx.rb CHANGED
@@ -36,6 +36,7 @@
36
36
  # POSSIBILITY OF SUCH DAMAGE.
37
37
 
38
38
  require 'active_support/all'
39
+
39
40
  include ERB::Util
40
41
 
41
42
  module TBMX
@@ -49,9 +50,84 @@ module TBMX
49
50
  @paragraphs = @lines.split ""
50
51
  end
51
52
 
53
+ def evaluate_command(command, input)
54
+ case command
55
+ when "\\"
56
+ "\\ #{input}"
57
+ when "b"
58
+ "<b>#{input}</b>"
59
+ when "i"
60
+ "<i>#{input}</i>"
61
+ when "sub"
62
+ "<sub>#{input}</sub>"
63
+ when "sup"
64
+ "<sup>#{input}</sup>"
65
+ else
66
+ "<b>[UNKNOWN COMMAND #{command}]</b>"
67
+ end
68
+ end
69
+
70
+ def parse_command(command, rest)
71
+ if rest == nil or rest == ""
72
+ return [evaluate_command(command, rest),
73
+ ""]
74
+ elsif rest[0] =~ /\s/
75
+ return [evaluate_command(command, ""),
76
+ rest[1 .. -1]]
77
+ elsif rest[0] == "{"
78
+ if backslash = rest.index("\\")
79
+ if (closing_brace = rest.index("}")) < backslash
80
+ return [evaluate_command(command, rest[1 ... closing_brace]),
81
+ rest[ closing_brace+1 .. -1]]
82
+ elsif closing_brace
83
+ interior = parse_command_body(backslash, rest)
84
+ if new_closing_brace = interior.index("}")
85
+ return [evaluate_command(command,
86
+ interior[1 ... new_closing_brace]),
87
+ interior[ new_closing_brace+1 .. -1]]
88
+ else # Assume an implied closing brace.
89
+ return [evaluate_command(command, interior[1 .. -1]),
90
+ ""]
91
+ end
92
+ else
93
+ return evaluate_command(command, parse_command_body(backslash, rest))
94
+ end
95
+ elsif closing_brace = rest.index("}")
96
+ return [evaluate_command(command, rest[1 ... closing_brace]),
97
+ rest[ closing_brace+1 .. -1]]
98
+ else # Assume an implied closing brace.
99
+ return [evaluate_command(command, rest[1 .. -1]),
100
+ ""]
101
+ end
102
+ else
103
+ raise RuntimeError, "Unreachable: probably a bug in parse_command_name."
104
+ end
105
+ end
106
+
107
+ def parse_command_name(input)
108
+ if end_command = input.index(/[\{\s]/)
109
+ return [input[0 ... end_command],
110
+ input[ end_command .. -1]]
111
+ else
112
+ return [input, ""]
113
+ end
114
+ end
115
+
116
+ def parse_command_body(backslash, input)
117
+ before = input[0 ... backslash]
118
+ command, rest = parse_command_name input[backslash+1 .. -1]
119
+ middle, back = parse_command(command, rest)
120
+ return before + middle + parse_paragraph_body(back)
121
+ end
122
+
52
123
  def parse_paragraph_body(body)
53
- raise ArgumentError if not body.is_a? String
54
- body
124
+ return "" if body.nil? or body == ""
125
+ raise ArgumentError, "Body is #{body.class}" if not body.is_a? String
126
+ if backslash = body.index("\\")
127
+ return parse_command_body backslash, body
128
+ else
129
+ return body
130
+ end
55
131
  end
56
132
 
57
133
  def parse_paragraph(paragraph)
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.0.4
4
+ version: 0.1.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-03 00:00:00.000000000 Z
12
+ date: 2013-12-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport