livetext 0.7.7 → 0.7.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1bf150349564c9f613698a94db648859a5b03d3b
4
- data.tar.gz: cea49f36d5db27d1fdff4d0c5484686044126104
3
+ metadata.gz: a7abb70149d3ce8d437180e80e90ff923284202f
4
+ data.tar.gz: e006a124c7ee1524774b996d6632203bc4d1f8be
5
5
  SHA512:
6
- metadata.gz: c8ec7ac2f7028ef1fef91e3e1937e62e7f91fbd467a0a3e0c7c58d51796b05ada956fb89a04ecc8b2487d53591fa5ab211d18bfd095ddc4c5404ce92d178afad
7
- data.tar.gz: 106ef65fea37b5d262d574f3adf7435ff95397508be3241d7e3711a050f2019f3303bce8bdf9f1b308518568d9c49dc3b5f9cf21066021050513ceb04798b7b9
6
+ metadata.gz: 162c8d389a83695a457046a6f57b1e4991f643c15c272a65d6689e5ba171933bcf83cb8fae9407b7cca2abeffe71f6f23085e397024ad1e0c0e2f0e64b7c488b
7
+ data.tar.gz: 9d1435ef7cb23a5bcd0293de2649d8d2cc3b4e5e9ff21213f7682621284a011b1a5357110b50c7ebfb0d604588fff6c425eb49600ae992b791358c9390703235
data/lib/formatline.rb ADDED
@@ -0,0 +1,197 @@
1
+
2
+ class FormatLine
3
+ EOL = :eol
4
+ Alpha = /[a-z]/
5
+ Alpha2 = /[a-z0-9_]/
6
+ Other = Object
7
+
8
+ SimpleFormats = {}
9
+ SimpleFormats[:b] = %w[<b> </b>]
10
+ SimpleFormats[:i] = %w[<i> </i>]
11
+ SimpleFormats[:t] = %w[<tt> </tt>]
12
+ SimpleFormats[:s] = %w[<strike> </strike>]
13
+
14
+ def initialize
15
+ @buffer, @vname, @fname, @param, @substr = "", "", "", "", ""
16
+ end
17
+
18
+ def peek
19
+ @enum.peek
20
+ rescue StopIteration
21
+ EOL
22
+ end
23
+
24
+ def grab
25
+ @enum.next
26
+ rescue StopIteration
27
+ EOL
28
+ end
29
+
30
+ def skip
31
+ @enum.next
32
+ @enum.peek
33
+ rescue StopIteration
34
+ EOL
35
+ end
36
+
37
+ def keep(initial = "")
38
+ @buffer << initial
39
+ @buffer << @enum.next
40
+ rescue StopIteration
41
+ EOL
42
+ end
43
+
44
+ def emit(str = "")
45
+ @buffer << str
46
+ end
47
+
48
+ def varsub(name)
49
+ Livetext::Vars[name]
50
+ end
51
+
52
+ def funcall(name, param)
53
+ fobj = ::Livetext::Functions.new
54
+ fobj.send(name, param)
55
+ end
56
+
57
+ def vsub
58
+ @buffer << varsub(@vname)
59
+ @vname = ""
60
+ end
61
+
62
+ def fcall
63
+ @buffer << funcall(@fname, @param)
64
+ @fname, @param = "", ""
65
+ end
66
+
67
+ def bold
68
+ d0, d1 = SimpleFormats[:b]
69
+ @buffer << "#{d0}#@substr#{d1}"
70
+ @substr = ""
71
+ end
72
+
73
+ def ttype
74
+ d0, d1 = SimpleFormats[:t]
75
+ @buffer << "#{d0}#@substr#{d1}"
76
+ @substr = ""
77
+ end
78
+
79
+ def italics
80
+ d0, d1 = SimpleFormats[:i]
81
+ @buffer << "#{d0}#@substr#{d1}"
82
+ @substr = ""
83
+ end
84
+
85
+ def parse(line)
86
+ @enum = line.chomp.each_char
87
+ @buffer = ""
88
+ @substr = ""
89
+ @fname = ""
90
+ @vname = ""
91
+ @param = ""
92
+
93
+ loop do # starting state
94
+ char = peek
95
+ # puts "char = #{char.inspect}"
96
+ case char
97
+ when EOL
98
+ break
99
+ when "$" # var or func or $
100
+ case skip
101
+ when EOL
102
+ emit("$")
103
+ break
104
+ when Alpha
105
+ loop { @vname << grab; break unless Alpha2 === peek }
106
+ vsub
107
+ when "$"
108
+ case skip
109
+ when Alpha
110
+ loop { @fname << grab; break unless Alpha2 === peek }
111
+ case peek
112
+ when " " # no param - just call
113
+ fcall # no param? Hmm
114
+ when "[" # long param - may have spaces - can hit eol
115
+ skip
116
+ loop { break if ["]", EOL].include?(peek); @param << grab }
117
+ fcall
118
+ when ":" # param (single token or to-eol)
119
+ case skip
120
+ when ":" # param to eol
121
+ skip
122
+ loop { break if peek == EOL; @param << grab }
123
+ when Other # grab until space or eol
124
+ loop { @param << grab; break if [" ", EOL].include?(peek) }
125
+ fcall
126
+ end
127
+ when Other # no param - just call
128
+ fcall
129
+ end
130
+ when Other
131
+ emit "$$"
132
+ end
133
+ when Other
134
+ emit "$"
135
+ end
136
+ when "*"
137
+ case skip
138
+ when EOL
139
+ emit "*"
140
+ when " "
141
+ emit "*"
142
+ when "["
143
+ skip
144
+ loop { break if ["]", EOL].include?(peek); @substr << grab }
145
+ skip
146
+ bold
147
+ when Other
148
+ loop { @substr << grab; break if [" ", EOL].include?(peek) }
149
+ bold
150
+ end
151
+ when "_"
152
+ case skip
153
+ when EOL
154
+ emit "_"
155
+ when " "
156
+ emit "_"
157
+ when "["
158
+ skip
159
+ loop { break if ["]", EOL].include?(peek); @substr << grab }
160
+ skip
161
+ italics
162
+ when "_" # doubled...
163
+ skip
164
+ loop { break if [".", ",", ")", EOL].include?(peek); @substr << grab } # ";" ?? FIXME
165
+ italics
166
+ when Other
167
+ loop { @substr << grab; break if [" ", EOL].include?(peek) }
168
+ italics
169
+ end
170
+ when "`"
171
+ case skip
172
+ when EOL
173
+ emit "`"
174
+ when " "
175
+ emit "`"
176
+ when "["
177
+ skip
178
+ loop { break if ["]", EOL].include?(peek); @substr << grab }
179
+ skip
180
+ ttype
181
+ when "`" # doubled...
182
+ skip
183
+ loop { break if [".", ",", ")", EOL].include?(peek); @substr << grab } # ";" ?? FIXME
184
+ ttype
185
+ when Other
186
+ loop { @substr << grab; break if [" ", EOL].include?(peek) }
187
+ ttype
188
+ end
189
+ when Other
190
+ keep
191
+ end
192
+ end
193
+
194
+ @buffer
195
+ end
196
+ end
197
+
data/lib/livetext.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class Livetext
2
- VERSION = "0.7.7"
2
+ VERSION = "0.7.8"
3
3
  end
4
4
 
5
5
  require 'fileutils'
@@ -11,6 +11,7 @@ $: << Path
11
11
  require 'functions'
12
12
  require 'userapi'
13
13
  require 'standard'
14
+ require 'formatline'
14
15
 
15
16
  Plugins = File.expand_path(File.join(File.dirname(__FILE__), "../dsl"))
16
17
 
@@ -21,6 +22,8 @@ TTY = ::File.open("/dev/tty", "w")
21
22
 
22
23
  class Livetext
23
24
 
25
+ Vars = {}
26
+
24
27
  class Processor
25
28
  include Livetext::Standard
26
29
  include Livetext::UserAPI
@@ -43,7 +46,6 @@ class Livetext
43
46
  @_nopass = false
44
47
  @_nopara = false
45
48
  @output = output
46
- @vars = {}
47
49
  @sources = []
48
50
  end
49
51
 
@@ -89,6 +91,8 @@ class Livetext
89
91
 
90
92
  Space = " "
91
93
 
94
+ attr_reader :main
95
+
92
96
  def initialize(output = ::STDOUT)
93
97
  @source = nil
94
98
  @_mixins = []
data/lib/standard.rb CHANGED
@@ -151,7 +151,7 @@ module Livetext::Standard
151
151
  var, val = a.split("=")
152
152
  val = val[1..-2] if val[0] == ?" and val[-1] == ?"
153
153
  val = val[1..-2] if val[0] == ?' and val[-1] == ?'
154
- @vars[var] = val
154
+ Livetext::Vars[var] = val
155
155
  end
156
156
  _optional_blank_line
157
157
  end
data/lib/userapi.rb CHANGED
@@ -1,7 +1,11 @@
1
1
  # User API
2
2
 
3
+ require 'formatline'
4
+
3
5
  module Livetext::UserAPI
4
6
 
7
+ Parser = ::FormatLine.new
8
+
5
9
  def _check_existence(file, msg)
6
10
  _error! msg unless File.exist?(file)
7
11
  # puts "ERROR" unless File.exist?(file)
@@ -80,7 +84,9 @@ module Livetext::UserAPI
80
84
  _body(sigil).join("\n")
81
85
  end
82
86
 
83
- def _new_format(line, delim, sym)
87
+ def _full_format(line)
88
+ Parser.parse(line)
89
+ =begin
84
90
  d0, d1 = Livetext::Standard::SimpleFormats[sym]
85
91
  s = line.each_char
86
92
  c = s.next
@@ -113,6 +119,7 @@ module Livetext::UserAPI
113
119
  getch.call
114
120
  end
115
121
  buffer
122
+ =end
116
123
  end
117
124
 
118
125
  def _basic_format(line, delim, tag)
@@ -159,12 +166,13 @@ module Livetext::UserAPI
159
166
 
160
167
  def _formatting(line)
161
168
  # l2 = _basic_format(line, "_", "i")
162
- l2 = _new_format(line, "_", :i)
169
+ # l2 = _new_format(line, "_", :i)
163
170
  # l2 = _basic_format(l2, "*", "b")
164
- l2 = _new_format(l2, "*", :b)
171
+ # l2 = _new_format(l2, "*", :b)
165
172
  # l2 = _basic_format(l2, "`", "tt")
166
- l2 = _new_format(l2, "`", :t)
173
+ # l2 = _new_format(l2, "`", :t)
167
174
  # Do strikethrough?
175
+ l2 = _full_format(line)
168
176
  l2 = _handle_escapes(l2, "_*`")
169
177
  line.replace(l2)
170
178
  end
@@ -197,7 +205,7 @@ module Livetext::UserAPI
197
205
  return if @_nopass
198
206
  _puts "<p>" if line == "\n" and ! @_nopara
199
207
  _formatting(line)
200
- _substitution(line)
208
+ # _substitution(line)
201
209
  _puts line
202
210
  end
203
211
 
data/test/ftest.rb ADDED
@@ -0,0 +1,109 @@
1
+ require 'minitest/autorun'
2
+
3
+ $LOAD_PATH << "./lib"
4
+ require 'livetext'
5
+
6
+ # How these tests work - see the block comment at the bottom.
7
+
8
+ class TestingLivetext < MiniTest::Test
9
+
10
+ TTY = File.open("/dev/tty","w")
11
+
12
+ TestLines = []
13
+
14
+ f = File.open("test/testfiles/lines.txt")
15
+ loop do
16
+ item = []
17
+ 4.times { item << f.gets.chomp }
18
+ raise "Oops? #{item.inspect}" unless item.last == ""
19
+ TestLines << item
20
+ break if f.eof?
21
+ end
22
+
23
+ STDERR.puts TestLines.inspect
24
+
25
+ TestLines.each.with_index do |item, i|
26
+ msg, src, exp, blank = *item
27
+ define_method("test_formatting_#{i}") do
28
+ STDERR.puts caller
29
+ x = FormatLine.new
30
+ actual = x.parse(src)
31
+ assert_equal(exp, actual, msg)
32
+ end
33
+ end
34
+
35
+ # def external_files
36
+ # tag = caller[0]
37
+ # n1, n2 = tag.index("`")+6, tag.index("'")-1
38
+ # base = tag[n1..n2]
39
+ # origin = Dir.pwd
40
+ # Dir.chdir("test/testfiles/#{base}") do
41
+ # src, out, exp = "source.ltx", "actual-output.txt", "expected-output.txt"
42
+ # err, erx = "actual-error.txt", "expected-error.txt"
43
+ # cmd = "../../../bin/livetext #{src} >#{out} 2>#{err}"
44
+ # system(cmd)
45
+ # output, expected, errors, errexp = File.read(out), File.read(exp), File.read(err), File.read(erx)
46
+ #
47
+ # out_ok = output == expected
48
+ # err_ok = errors == errexp
49
+ # bad_out = "--- Expected: \n#{expected}\n--- Output: \n#{output}\n"
50
+ # bad_err = "--- Error Expected: \n#{errexp}\n--- Error Output: \n#{errors}\n"
51
+ #
52
+ # assert(out_ok, bad_out)
53
+ # assert(err_ok, bad_err)
54
+ # system("rm -f #{out} #{err}") # only on success
55
+ # end
56
+ # end
57
+ #
58
+ # def test_hello_world; external_files end
59
+ # def test_basic_formatting; external_files end
60
+ #
61
+ # def test_comments_ignored_1; external_files end
62
+ # def test_block_comment; external_files end
63
+ #
64
+ # def test_error_line_num; external_files end
65
+ # def test_error_inc_line_num; external_files end
66
+ # def test_error_invalid_name; external_files end
67
+ # def test_error_no_such_mixin; external_files end
68
+ # def test_error_no_such_inc; external_files end
69
+ # def test_error_no_such_copy; external_files end
70
+ # def test_error_name_not_permitted; external_files end
71
+ # def test_error_missing_end; external_files end
72
+ # def test_error_mismatched_end; external_files end
73
+ #
74
+ # def test_simple_vars; external_files end
75
+ # def test_more_complex_vars; external_files end
76
+ #
77
+ # def test_def_method; external_files end
78
+ #
79
+ # def test_single_raw_line; external_files end
80
+ #
81
+ # def test_simple_include; external_files end
82
+ # def test_simple_mixin; external_files end
83
+ # def test_simple_copy; external_files end
84
+ # def test_copy_is_raw; external_files end
85
+ # def test_raw_text_block; external_files end
86
+ #
87
+ # def test_example_alpha; external_files end
88
+ # def test_example_alpha2; external_files end
89
+ #
90
+ # def test_functions; external_files end
91
+ end
92
+
93
+
94
+ =begin
95
+
96
+ You can add any ordinary test method above. But so far, all these tests simply
97
+ call external_files.
98
+
99
+ The external_files method works this way:
100
+ - If the test (caller) method is test_my_silly_feature, then we will
101
+ look for a directory called testfiles/my_silly_feature
102
+ - In here, there must be a source.ltx, expected-output.txt, and expected-error.txt
103
+ - Technically, any of these can be empty
104
+ - We run livetext on the source and compare actual vs expected (stdout, stderr)
105
+ - The "real" output gets checked first
106
+ - Of course, both must compare correctly for the test to pass
107
+
108
+ =end
109
+
data/test/test.rb CHANGED
@@ -1,14 +1,36 @@
1
1
  require 'minitest/autorun'
2
2
 
3
3
  $LOAD_PATH << "./lib"
4
+
4
5
  require 'livetext'
5
6
 
6
7
  # How these tests work - see the block comment at the bottom.
7
8
 
8
- class TestingLiveText < MiniTest::Test
9
+ class TestingLivetext < MiniTest::Test
9
10
 
10
11
  TTY = File.open("/dev/tty","w")
11
12
 
13
+ TestLines = []
14
+
15
+ f = File.open("test/testfiles/lines.txt")
16
+ loop do
17
+ item = []
18
+ 4.times { item << f.gets.chomp }
19
+ raise "Oops? #{item.inspect}" unless item.last == ""
20
+ TestLines << item
21
+ break if f.eof?
22
+ end
23
+
24
+ TestLines.each.with_index do |item, i|
25
+ msg, src, exp, blank = *item
26
+ define_method("test_formatting_#{i}") do
27
+ x = FormatLine.new
28
+ actual = x.parse(src)
29
+ # raise "Hello?"
30
+ assert_equal(exp, actual, msg)
31
+ end
32
+ end
33
+
12
34
  def external_files
13
35
  tag = caller[0]
14
36
  n1, n2 = tag.index("`")+6, tag.index("'")-1
@@ -4,7 +4,7 @@ and <tt>code font</tt>.
4
4
  <p>
5
5
 
6
6
  Here are some random punctuation marks:
7
- # . @ <b> <i></b> <tt></i></tt> : ; % ^ & $
7
+ # . @ * _ ` : ; % ^ & $
8
8
  <p>
9
9
 
10
- Oops, forgot to escape these: * _ `
10
+ No need to escape these: * _ `
@@ -1,8 +1,8 @@
1
1
  Here are examples of *boldface and _italics and `code
2
- as well as *(more complex) examples of _(italicized text)
3
- and `(code font).
2
+ as well as *[more complex] examples of _[italicized text]
3
+ and `[code font].
4
4
 
5
5
  Here are some random punctuation marks:
6
6
  # . @ * _ ` : ; % ^ & $
7
7
 
8
- Oops, forgot to escape these: \* \_ \`
8
+ No need to escape these: * _ `
@@ -2,7 +2,7 @@ Testing out
2
2
  some functions
3
3
  here...
4
4
 
5
- .func myfunc
5
+ .func myfunc(x)
6
6
  "Eureka!"
7
7
  .end
8
8
 
@@ -0,0 +1,96 @@
1
+ tick: Single token, beginning of line
2
+ `def is like a reserved word
3
+ <tt>def</tt> is like a reserved word
4
+
5
+ tick: Single token, middle of line
6
+ They say `def is like a reserved word
7
+ They say <tt>def</tt> is like a reserved word
8
+
9
+ tick: Single token, end of line
10
+ like a reserved word, is `def
11
+ like a reserved word, is <tt>def</tt>
12
+
13
+ tick: Bracketed, normal
14
+ This is `[code font] here
15
+ This is <tt>code font</tt> here
16
+
17
+ tick: Bracketed, hits EOL
18
+ This is `[code font here
19
+ This is <tt>code font here</tt>
20
+
21
+ tick: followed by space
22
+ This is not ` code
23
+ This is not ` code
24
+
25
+ tick: followed by EOL
26
+ This also isn't code: `
27
+ This also isn't code: `
28
+
29
+ tick: Doubled, ignores comma
30
+ This is ``code, of course
31
+ This is <tt>code</tt>, of course
32
+
33
+ tick: Doubled, ignores period
34
+ This is ``code. Hooray, I guess.
35
+ This is <tt>code</tt>. Hooray, I guess.
36
+
37
+ tick: Doubled, ignores right paren
38
+ (By the way, this is ``code)
39
+ (By the way, this is <tt>code</tt>)
40
+
41
+ tick: Doubled, ignores EOL
42
+ Up to EOL, this is ``code
43
+ Up to EOL, this is <tt>code</tt>
44
+
45
+ uscore: Single token, beginning of line
46
+ _This is italics
47
+ <i>This</i> is italics
48
+
49
+ uscore: Single token, middle of line
50
+ And _this is italics
51
+ And <i>this</i> is italics
52
+
53
+ uscore: Single token, end of line
54
+ And so is _this
55
+ And so is <i>this</i>
56
+
57
+ uscore: Bracketed, normal
58
+ The _[USS Enterprise] was here
59
+ The <i>USS Enterprise</i> was here
60
+
61
+ uscore: Bracketed, hits EOL
62
+ Hey, it's the _[USS Enterprise
63
+ Hey, it's the <i>USS Enterprise</i>
64
+
65
+ uscore: followed by space
66
+ This: _ is just an underscore
67
+ This: _ is just an underscore
68
+
69
+ uscore: followed by EOL
70
+ This is just an underscore: _
71
+ This is just an underscore: _
72
+
73
+ uscore: Doubled, ignores comma
74
+ Just a __second, ok?
75
+ Just a <i>second</i>, ok?
76
+
77
+ uscore: Doubled, ignores period
78
+ Just a __second. OK?
79
+ Just a <i>second</i>. OK?
80
+
81
+ uscore: Doubled, ignores right paren
82
+ I'll (just a __second) be right there.
83
+ I'll (just a <i>second</i>) be right there.
84
+
85
+ uscore: Doubled, ignores EOL
86
+ Be there in a __second
87
+ Be there in a <i>second</i>
88
+
89
+ Tick has precedence over underscore
90
+ This is a `piece_of_code here
91
+ This is a <tt>piece_of_code</tt> here
92
+
93
+ under: Starts after double quote?
94
+ He said, "_Please go away."
95
+ He said, "<i>Please</i> go away."
96
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: livetext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.7
4
+ version: 0.7.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hal Fulton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-30 00:00:00.000000000 Z
11
+ date: 2017-04-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A smart text processor extensible in Ruby
14
14
  email: rubyhacker@gmail.com
@@ -28,6 +28,7 @@ files:
28
28
  - dsl/markdown.rb
29
29
  - dsl/pyggish.rb
30
30
  - dsl/tutorial.rb
31
+ - lib/formatline.rb
31
32
  - lib/functions.rb
32
33
  - lib/livetext.rb
33
34
  - lib/standard.rb
@@ -36,6 +37,7 @@ files:
36
37
  - test/affirm/kbks.jpg
37
38
  - test/affirm/lm-kbks.lt
38
39
  - test/cleanup
40
+ - test/ftest.rb
39
41
  - test/newtest
40
42
  - test/test.rb
41
43
  - test/testfiles/basic_formatting/expected-error.txt
@@ -96,6 +98,7 @@ files:
96
98
  - test/testfiles/hello_world/expected-error.txt
97
99
  - test/testfiles/hello_world/expected-output.txt
98
100
  - test/testfiles/hello_world/source.ltx
101
+ - test/testfiles/lines.txt
99
102
  - test/testfiles/more_complex_vars/expected-error.txt
100
103
  - test/testfiles/more_complex_vars/expected-output.txt
101
104
  - test/testfiles/more_complex_vars/source.ltx