livetext 0.9.25 → 0.9.30

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/README.lt3 +3 -2
  3. data/imports/bookish.rb +1 -2
  4. data/lib/livetext/errors.rb +3 -0
  5. data/lib/livetext/expansion.rb +106 -0
  6. data/lib/livetext/formatter.rb +104 -0
  7. data/lib/livetext/functions.rb +9 -0
  8. data/lib/livetext/global_helpers.rb +5 -0
  9. data/lib/livetext/handler/import.rb +2 -6
  10. data/lib/livetext/handler/mixin.rb +2 -6
  11. data/lib/livetext/helpers.rb +30 -27
  12. data/lib/livetext/html.rb +73 -0
  13. data/lib/livetext/more.rb +178 -0
  14. data/lib/livetext/parser/general.rb +1 -1
  15. data/lib/livetext/parser/set.rb +10 -3
  16. data/lib/livetext/parser/string.rb +14 -5
  17. data/lib/livetext/processor.rb +8 -1
  18. data/lib/livetext/skeleton.rb +2 -1
  19. data/lib/livetext/standard.rb +30 -16
  20. data/lib/livetext/userapi.rb +61 -22
  21. data/lib/livetext/version.rb +1 -1
  22. data/lib/livetext.rb +2 -152
  23. data/plugin/bootstrap_menu.rb +140 -0
  24. data/plugin/misc/navbar.rb +162 -0
  25. data/test/snapshots/basic_formatting/expected-output.txt +1 -2
  26. data/test/snapshots/{import_bookish/toc.tmp → bootstrap_menu/expected-error.txt} +0 -0
  27. data/test/snapshots/bootstrap_menu/expected-output.txt +4 -0
  28. data/test/snapshots/bootstrap_menu/source.lt3 +17 -0
  29. data/test/snapshots/error_invalid_name/foo +5 -0
  30. data/test/snapshots/import_bookish/expected-output.txt +4 -4
  31. data/test/snapshots/more_functions/expected-output.txt +1 -1
  32. data/test/snapshots/more_functions/source.lt3 +1 -1
  33. data/test/snapshots/subset.txt +50 -46
  34. data/test/snapshots/{mixin_bookish/toc.tmp → var_into_func/expected-error.txt} +0 -0
  35. data/test/snapshots/var_into_func/expected-output.txt +16 -0
  36. data/test/snapshots/var_into_func/source.lt3 +16 -0
  37. data/test/unit/all.rb +2 -1
  38. data/test/unit/lineparser.rb +359 -0
  39. data/test/unit/new_lineparser.rb +359 -0
  40. data/test/unit/parser/general.rb +2 -2
  41. data/test/unit/parser/set.rb +12 -20
  42. metadata +16 -11
  43. data/lib/livetext/formatline.rb +0 -321
  44. data/lib/livetext/funcall.rb +0 -84
  45. data/test/snapshots/error_inc_line_num/OUT +0 -17
  46. data/test/snapshots/error_no_such_copy/duh +0 -26
  47. data/test/snapshots/error_no_such_copy/mystery.txt +0 -36
  48. data/test/testlines.rb +0 -37
  49. data/test/unit/formatline.rb +0 -769
@@ -1,321 +0,0 @@
1
-
2
- require_relative 'parsing'
3
- require_relative 'funcall'
4
-
5
- # Class FormatLine handles the parsing of comments, dot commands, and
6
- # simple formatting characters.
7
-
8
- class Livetext::FormatLine < StringParser
9
- include Livetext::ParsingConstants
10
- include Livetext::FormatLine::FunCall
11
-
12
- attr_reader :out
13
- attr_reader :tokenlist
14
-
15
- def initialize(line)
16
- super
17
- @token = Null.dup
18
- @tokenlist = []
19
- end
20
-
21
- def self.parse!(line)
22
- return nil if line.nil?
23
- x = self.new(line.chomp)
24
- t = x.tokenize
25
- x.evaluate
26
- end
27
-
28
- def tokenize
29
- loop do
30
- case peek
31
- when Escape; grab; add peek; grab
32
- when "$"
33
- dollar
34
- when "*", "_", "`", "~"
35
- marker peek
36
- add peek
37
- when LF
38
- break if eos?
39
- when nil
40
- break
41
- else
42
- add peek
43
- end
44
- grab
45
- end
46
- add_token(:str)
47
- @tokenlist
48
- end
49
-
50
- def terminate?(terminators, ch)
51
- if terminators.is_a? Regexp
52
- terminators === ch
53
- else
54
- terminators.include?(ch)
55
- end
56
- end
57
-
58
- def var_func_parse
59
- char = self.peek
60
- loop do
61
- char = self.grab
62
- break if char == LF || char == nil
63
- self.handle_escaping if char == Escape
64
- self.dollar if char == "$" # Could be $$
65
- self.add char
66
- end
67
- self.add_token(:str)
68
- result = self.evaluate
69
- result
70
- end
71
-
72
- def self.var_func_parse(str)
73
- return nil if str.nil?
74
- x = self.new(str.chomp)
75
- char = x.peek
76
- loop do
77
- char = x.grab
78
- break if char == LF || char == nil
79
- x.handle_escaping if char == Escape
80
- x.dollar if char == "$" # Could be $$
81
- x.add char
82
- end
83
- x.add_token(:str)
84
- result = x.evaluate
85
- result
86
- end
87
-
88
- def handle_escaping
89
- grab
90
- add grab
91
- end
92
-
93
- def embed(sym, str)
94
- pre, post = SimpleFormats[sym]
95
- pre + str + post
96
- end
97
-
98
- def evaluate(tokens = @tokenlist)
99
- @out = ""
100
- return "" if tokens.empty?
101
- gen = tokens.each
102
- token = gen.next
103
- loop do
104
- break if token.nil?
105
- sym, val = *token
106
- case sym
107
- when :str; eval_str(val)
108
- when :var; eval_var(val)
109
- when :func; eval_func(val, gen)
110
- when *BITS; eval_bits(sym, val)
111
- else
112
- add_token :str
113
- end
114
- token = gen.next
115
- end
116
- @out
117
- end
118
-
119
- def add(str)
120
- @token << str unless str.nil?
121
- end
122
-
123
- def add_token(kind, token = @token)
124
- return if token.nil?
125
- @tokenlist << [kind, token] unless token.empty?
126
- @token = Null.dup
127
- end
128
-
129
- def grab_alpha
130
- str = Null.dup
131
- grab
132
- loop do
133
- break if eos?
134
- str << peek
135
- break if terminate?(NoAlpha, lookahead)
136
- grab
137
- end
138
- str
139
- end
140
-
141
- def grab_alpha_dot
142
- str = Null.dup
143
- grab
144
- loop do
145
- break if peek.nil? # eos?
146
- str << peek
147
- break if terminate?(NoAlphaDot, lookahead)
148
- grab
149
- end
150
- str
151
- end
152
-
153
- def dollar
154
- grab
155
- case peek
156
- when LF; add "$"; add_token :str
157
- when " "; add "$ "; add_token :str
158
- when nil; add "$"; add_token :str
159
- when "$"; double_dollar
160
- # when "."; dollar_dot
161
- when /[A-Za-z]/
162
- add_token :str
163
- var = peek + grab_alpha_dot
164
- add_token(:var, var)
165
- else
166
- add "$" + peek
167
- add_token(:string)
168
- end
169
- end
170
-
171
- # def dollar_dot
172
- # add_token :ddot, @line[@i..-1]
173
- # end
174
-
175
- def marker(char)
176
- add_token :str
177
- sym = Syms[char]
178
- if embedded?
179
- # add char # ??? add_token "*", :string
180
- return
181
- end
182
-
183
- grab
184
- case peek
185
- when Space
186
- add char + " "
187
- add_token :str
188
- grab
189
- when LF, nil
190
- add char
191
- add_token :str
192
- when char; double_marker(char)
193
- when LBrack; long_marker(char)
194
- else
195
- str = peek + collect!(sym, Blank)
196
- add str
197
- add_token sym, str
198
- grab
199
- end
200
- end
201
-
202
- def double_marker(char)
203
- sym = Syms[char]
204
- kind = sym
205
- case lookahead # first char after **
206
- when Space, LF, nil
207
- pre, post = SimpleFormats[sym]
208
- add_token kind
209
- else
210
- str = collect!(sym, Punc)
211
- add_token kind, str
212
- grab
213
- end
214
- end
215
-
216
- def long_marker(char)
217
- sym = Syms[char]
218
- # grab # skip left bracket
219
- kind = sym # "param_#{sym}".to_sym
220
- arg = collect!(sym, Param, true)
221
- add_token kind, arg
222
- end
223
-
224
- def collect_bracketed(sym, terminators)
225
- str = Null.dup # next is not " ","*","["
226
- grab # ZZZ
227
- loop do
228
- if peek == Escape
229
- grab
230
- str << grab
231
- next
232
- end
233
- if terminate?(terminators, peek)
234
- break
235
- end
236
- str << peek # not a terminator
237
- grab
238
- end
239
-
240
- if peek == "]" # skip right bracket
241
- grab
242
- end
243
- add str
244
- str
245
- rescue => err
246
- ::STDERR.puts "ERR = #{err}\n#{err.backtrace}"
247
- end
248
-
249
- def escaped
250
- grab # Eat the backslash
251
- ch = grab # Take next char
252
- ch
253
- end
254
-
255
- def collect!(sym, terminators, bracketed=nil)
256
- return collect_bracketed(sym, terminators) if bracketed
257
-
258
- str = Null.dup # next is not " ","*","["
259
- grab # ZZZ
260
- loop do
261
- case
262
- when peek.nil?
263
- return str
264
- when peek == Escape
265
- str << escaped
266
- next
267
- when terminate?(terminators, peek)
268
- break
269
- else
270
- str << peek # not a terminator
271
- end
272
- grab
273
- end
274
- ungrab
275
- add str
276
- str
277
- rescue => err
278
- ::STDERR.puts "ERR = #{err}\n#{err.backtrace}"
279
- end
280
-
281
- def varsub(name)
282
- live = Livetext.new
283
- value = live.vars[name]
284
- result = value || "[#{name} is undefined]"
285
- result
286
- end
287
-
288
- def embedded?
289
- ! (['"', "'", " ", nil].include? prev)
290
- end
291
-
292
- private
293
-
294
- def eval_bits(sym, val)
295
- val = Livetext.interpolate(val)
296
- @out << embed(sym, val)
297
- end
298
-
299
- def eval_func(val, gen)
300
- param = nil
301
- arg = gen.peek rescue :bogus
302
- unless arg == :bogus
303
- if [:colon, :brackets].include? arg[0]
304
- arg = gen.next # for real
305
- param = arg[1]
306
- # FIXME - unsure - interpolate again??
307
- # param = Livetext.interpolate(param)
308
- end
309
- end
310
- @out << funcall(val, param)
311
- end
312
-
313
- def eval_var(val)
314
- @out << varsub(val)
315
- end
316
-
317
- def eval_str(val)
318
- @out << val unless val == "\n" # BUG
319
- end
320
-
321
- end
@@ -1,84 +0,0 @@
1
-
2
- require_relative '../livetext'
3
-
4
- # Parse function calls
5
-
6
- module Livetext::FormatLine::FunCall
7
-
8
- include Livetext::ParsingConstants
9
-
10
- def param_loop(char)
11
- param = ""
12
- loop do
13
- case lookahead
14
- when Escape
15
- param << escaped
16
- when char, LF, nil
17
- break
18
- else
19
- param << lookahead
20
- grab
21
- end
22
- end
23
- param = nil if param.empty?
24
- param
25
- end
26
-
27
- def grab_colon_param
28
- grab # grab :
29
- param = param_loop(Space)
30
- end
31
-
32
- def grab_bracket_param
33
- grab # [
34
- param = param_loop("]")
35
- grab # "]"
36
- param
37
- end
38
-
39
- def funcall(name, param)
40
- err = "[Error evaluating $$#{name}(#{param})]"
41
- func_name = name # "func_" + name.to_s
42
- result =
43
- if self.send?(func_name, param) # self.respond_to?(func_name)
44
- # do nothing
45
- else
46
- fobj = ::Livetext::Functions.new
47
- fobj.send(name, param) rescue err
48
- end
49
- result.to_s
50
- end
51
-
52
- def double_dollar
53
- case lookahead
54
- when Space; add_token :string, "$$ "; grab; return
55
- when LF, nil; add "$$"; add_token :str
56
- when Alpha
57
- add_token(:str, @token)
58
- func = grab_alpha
59
- add_token(:func, func)
60
- param = grab_func_param # may be null/missing
61
- else
62
- grab; add_token :str, "$$" + peek; return
63
- end
64
- end
65
-
66
- def grab_func_param
67
- case lookahead
68
- when "["
69
- param = grab_bracket_param
70
- add_token(:brackets, param)
71
- when ":"
72
- param = grab_colon_param
73
- add_token(:colon, param)
74
- else # do nothing
75
- end
76
- end
77
-
78
- def escaped
79
- grab # Eat the backslash
80
- ch = grab # Take next char
81
- ch
82
- end
83
-
84
- end
@@ -1,17 +0,0 @@
1
- This is my
2
- source file
3
- which includes file2 here:
4
- This is file2
5
- which has an error
6
- about an unknown command
7
- in line 5
8
- This is my
9
- source file
10
- which includes file2 here:
11
- This is file2
12
- which has an error
13
- about an unknown command
14
- in line 5
15
- <p>
16
-
17
- And this is file2 line 7.
@@ -1,26 +0,0 @@
1
- ####
2
- self =
3
- #<Processor:0x00007fd5ec14d290
4
- @parent=#<Livetext:0x00007fd5ec14d358
5
- @source=nil,
6
- @_mixins=[],
7
- @_imports=[],
8
- @_outdir=".",
9
- @no_puts=false,
10
- @body="Make sure a\nnonexistent file with .copy\ngives an error.\n<p>\n\n",
11
- @main=#<Processor:0x00007fd5ec14d290 ...>,
12
- @indentation=[0],
13
- @_vars={"User"=>"Hal", :User=>"Hal", "Version"=>"0.9.24", :Version=>"0.9.24",
14
- "File"=>"source.lt3", :File=>"source.lt3",
15
- "FileDir"=>"/Users/Hal/Dropbox/topx/git/livetext/test/snapshots/error_no_such_copy",
16
- :FileDir=>"/Users/Hal/Dropbox/topx/git/livetext/test/snapshots/error_no_such_copy"},
17
- @backtrace=true>,
18
- @_nopass=false,
19
- @_nopara=false,
20
- @output=#<IO:<STDOUT>>,
21
- @sources=[[#<Enumerator: ["Make sure a", "nonexistent file with .copy", "gives an error.", "\n", ".copy nosuchfile.txt\n", "\n", "Nothing to\n", "see here.\n"]:each>, "source.lt3", 5]],
22
- @indentation=nil,
23
- @_mixins=[],
24
- @_imports=[],
25
- @_data="nosuchfile.txt",
26
- @_args=["nosuchfile.txt"]>
@@ -1,36 +0,0 @@
1
- livetext:78:in `parse_command_line'
2
- livetext:78:in `loop'
3
- livetext:92:in `block in parse_command_line'
4
- helpers.rb:72:in `process_file'
5
- helpers.rb:72:in `loop'
6
- helpers.rb:75:in `block in process_file'
7
- helpers.rb:95:in `process_line'
8
- helpers.rb:124:in `handle_dotcmd'
9
- standard.rb:273:in `copy': Error: file 'nosuchfile.txt' not found (FileNotFound)
10
-
11
-
12
- parse_command_line
13
- process_file
14
- process_line
15
- handle_dotcmd
16
- copy
17
-
18
-
19
-
20
-
21
- livetext "main" rescue unexpected
22
- process_file ret Bool, print warning, rescue unex, no raise
23
- process_line ret Bool, no rescue, no raise
24
- handle_dotcmd ret Bool, no rescue, 2 custom exceptions
25
- copy ret Bool, rescue only unexpected
26
- check_file_exists returns Boolean, no rescue, no raise
27
-
28
-
29
- METHOD RETURNS RESCUE RAISES OTHER
30
- ------------------- ----------------- ----------------- ----------------- -----------------
31
- parse_command_line nothing only unexpected
32
- process_file
33
- process_line
34
- handle_dotcmd
35
- copy
36
-
data/test/testlines.rb DELETED
@@ -1,37 +0,0 @@
1
- require '../lib/livetext'
2
- require '../lib/formatline'
3
-
4
- def red(str)
5
- "" + str + ""
6
- end
7
-
8
- input = ARGV.first || "test/data/lines.txt"
9
- data = File.readlines(input)
10
-
11
- pass = fail = 0
12
- data.each_slice(4).with_index do |lines, i|
13
- title, input, expected, blank = *lines
14
- lnum = i*4 + 1
15
- input.chomp!
16
- expected.chomp!
17
- expected = eval(expected) if expected[0] == "/"
18
-
19
-
20
- actual = FormatLine.parse!(input)
21
- if expected === actual
22
- pass += 1
23
- # puts "PASS: #{title}"
24
- next
25
- end
26
-
27
- fail += 1
28
- puts "----------------------------- (line #{lnum})"
29
- puts "Test: #{title}"
30
- puts "Input: #{input}"
31
- puts " #{red('FAIL Expected: ')} #{expected.inspect}"
32
- puts " #{red(' Actual : ')} #{actual.inspect}"
33
- puts
34
- end
35
-
36
- puts
37
- puts "#{pass} passes #{fail} fails"