livetext 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/bin/livetext +20 -0
  3. data/lib/bookish.rb +212 -0
  4. data/lib/calibre.rb +24 -0
  5. data/lib/liveblog.rb +195 -0
  6. data/lib/livemagick.rb +131 -0
  7. data/lib/livetext.rb +605 -0
  8. data/lib/markdown.rb +30 -0
  9. data/lib/pyggish.rb +194 -0
  10. data/lib/tutorial.rb +89 -0
  11. data/livetext-0.5.2.gem +0 -0
  12. data/livetext.gemspec +99 -4
  13. data/test/cleanup +1 -0
  14. data/test/newtest +10 -0
  15. data/test/rawtext.inc +4 -0
  16. data/test/simple_mixin.rb +3 -0
  17. data/test/simplefile.inc +2 -0
  18. data/test/test.rb +77 -0
  19. data/test/testfiles/basic_formatting/expected-error.txt +0 -0
  20. data/test/testfiles/basic_formatting/expected-output.txt +10 -0
  21. data/test/testfiles/basic_formatting/source.ltx +8 -0
  22. data/test/testfiles/block_comment/expected-error.txt +0 -0
  23. data/test/testfiles/block_comment/expected-output.txt +5 -0
  24. data/test/testfiles/block_comment/source.ltx +19 -0
  25. data/test/testfiles/comments_ignored_1/expected-error.txt +0 -0
  26. data/test/testfiles/comments_ignored_1/expected-output.txt +4 -0
  27. data/test/testfiles/comments_ignored_1/source.ltx +7 -0
  28. data/test/testfiles/copy_is_raw/expected-error.txt +0 -0
  29. data/test/testfiles/copy_is_raw/expected-output.txt +7 -0
  30. data/test/testfiles/copy_is_raw/source.ltx +4 -0
  31. data/test/testfiles/def_method/expected-error.txt +2 -0
  32. data/test/testfiles/def_method/expected-output.txt +5 -0
  33. data/test/testfiles/def_method/source.ltx +10 -0
  34. data/test/testfiles/example_alpha/expected-error.txt +0 -0
  35. data/test/testfiles/example_alpha/expected-output.txt +23 -0
  36. data/test/testfiles/example_alpha/source.ltx +17 -0
  37. data/test/testfiles/example_alpha2/expected-error.txt +0 -0
  38. data/test/testfiles/example_alpha2/expected-output.txt +12 -0
  39. data/test/testfiles/example_alpha2/source.ltx +24 -0
  40. data/test/testfiles/fixit +6 -0
  41. data/test/testfiles/functions/expected-error.txt +0 -0
  42. data/test/testfiles/functions/expected-output.txt +8 -0
  43. data/test/testfiles/functions/source.ltx +11 -0
  44. data/test/testfiles/hello_world/expected-error.txt +0 -0
  45. data/test/testfiles/hello_world/expected-output.txt +2 -0
  46. data/test/testfiles/hello_world/source.ltx +2 -0
  47. data/test/testfiles/more_complex_vars/expected-error.txt +0 -0
  48. data/test/testfiles/more_complex_vars/expected-output.txt +4 -0
  49. data/test/testfiles/more_complex_vars/source.ltx +5 -0
  50. data/test/testfiles/raw_text_block/expected-error.txt +0 -0
  51. data/test/testfiles/raw_text_block/expected-output.txt +14 -0
  52. data/test/testfiles/raw_text_block/source.ltx +16 -0
  53. data/test/testfiles/sigil_can_change/expected-error.txt +0 -0
  54. data/test/testfiles/sigil_can_change/expected-output.txt +6 -0
  55. data/test/testfiles/sigil_can_change/source.ltx +11 -0
  56. data/test/testfiles/simple_copy/expected-error.txt +0 -0
  57. data/test/testfiles/simple_copy/expected-output.txt +7 -0
  58. data/test/testfiles/simple_copy/source.ltx +6 -0
  59. data/test/testfiles/simple_include/expected-error.txt +0 -0
  60. data/test/testfiles/simple_include/expected-output.txt +7 -0
  61. data/test/testfiles/simple_include/source.ltx +6 -0
  62. data/test/testfiles/simple_mixin/expected-error.txt +0 -0
  63. data/test/testfiles/simple_mixin/expected-output.txt +5 -0
  64. data/test/testfiles/simple_mixin/source.ltx +6 -0
  65. data/test/testfiles/simple_vars/expected-error.txt +0 -0
  66. data/test/testfiles/simple_vars/expected-output.txt +6 -0
  67. data/test/testfiles/simple_vars/source.ltx +7 -0
  68. data/test/testfiles/single_raw_line/expected-error.txt +0 -0
  69. data/test/testfiles/single_raw_line/expected-output.txt +10 -0
  70. data/test/testfiles/single_raw_line/source.ltx +8 -0
  71. metadata +76 -9
  72. data/dlt +0 -1
data/lib/markdown.rb ADDED
@@ -0,0 +1,30 @@
1
+ # This file is intended to be used via a Livetext .mixin
2
+ # or the equivalent.
3
+
4
+ def h1; _puts "# #{@_data}"; end # atx style for now
5
+ def h2; _puts "## #{@_data}"; end
6
+ def h3; _puts "### #{@_data}"; end
7
+ def h4; _puts "#### #{@_data}"; end
8
+ def h5; _puts "##### #{@_data}"; end
9
+ def h6; _puts "###### #{@_data}"; end
10
+
11
+ def bq # block quote
12
+ _body {|line| _puts "> #{line}" }
13
+ end
14
+
15
+ # Asterisks, underscores, and double underscores -- difficult, handle later
16
+
17
+ def list
18
+ _body {|line| _puts " * #{line}" }
19
+ end
20
+
21
+ alias nlist olist
22
+
23
+ def olist # Doesn't handle paragraphs yet
24
+ n = 0
25
+ _body do |line|
26
+ n += 1
27
+ _puts "#{n}. #{line}"
28
+ end
29
+ end
30
+
data/lib/pyggish.rb ADDED
@@ -0,0 +1,194 @@
1
+ require 'pygments'
2
+
3
+ # noinspection ALL
4
+ module PygmentFix # Remove CSS for Jutoh
5
+ Styles = {
6
+ :c => "#408080-i", # Comment
7
+ :k => "#008000-b", # Keyword
8
+ :o => "#666666", # Operator
9
+ :cm => "#408080-i", # Comment.Multiline
10
+ :cp => "#BC7A00", # Comment.Preproc
11
+ :c1 => "#408080-i", # Comment.Single
12
+ :cs => "#408080-i", # Comment.Special
13
+ :kc => "#008000-b", # Keyword.Constant
14
+ :kd => "#008000-b", # Keyword.Declaration
15
+ :kn => "#008000-b", # Keyword.Namespace
16
+ :kp => "#008000", # Keyword.Pseudo
17
+ :kr => "#008000-b", # Keyword.Reserved
18
+ :kt => "#B00040", # Keyword.Type
19
+ :m => "#666666", # Literal.Number
20
+ :s => "#BA2121", # Literal.String
21
+ :na => "#7D9029", # Name.Attribute
22
+ :nb => "#008000", # Name.Builtin
23
+ :nc => "#0000FF-b", # Name.Class
24
+ :no => "#880000", # Name.Constant
25
+ :nd => "#AA22FF", # Name.Decorator
26
+ :ni => "#999999-b", # Name.Entity
27
+ :ne => "#D2413A-b", # Name.Exception
28
+ :nf => "#0000FF", # Name.Function
29
+ :nl => "#A0A000", # Name.Label
30
+ :nn => "#0000FF-b", # Name.Namespace
31
+ :nt => "#008000-b", # Name.Tag
32
+ :nv => "#19177C", # Name.Variable
33
+ :ow => "#AA22FF-b", # Operator.Word
34
+ :w => "#bbbbbb", # Text.Whitespace
35
+ :mb => "#666666", # Literal.Number.Bin
36
+ :mf => "#666666", # Literal.Number.Float
37
+ :mh => "#666666", # Literal.Number.Hex
38
+ :mi => "#666666", # Literal.Number.Integer
39
+ :mo => "#666666", # Literal.Number.Oct
40
+ :sb => "#BA2121", # Literal.String.Backtick
41
+ :sc => "#BA2121", # Literal.String.Char
42
+ :sd => "#BA2121-i", # Literal.String.Doc
43
+ :s2 => "#BA2121", # Literal.String.Double
44
+ :se => "#BB6622-b", # Literal.String.Escape
45
+ :sh => "#BA2121", # Literal.String.Heredoc
46
+ :si => "#BB6688-b", # Literal.String.Interpol
47
+ :sx => "#008000", # Literal.String.Other
48
+ :sr => "#BB6688", # Literal.String.Regex
49
+ :s1 => "#BA2121", # Literal.String.Single
50
+ :ss => "#19177C", # Literal.String.Symbol
51
+ :bp => "#008000", # Name.Builtin.Pseudo
52
+ :vc => "#19177C", # Name.Variable.Class
53
+ :vg => "#19177C", # Name.Variable.Global
54
+ :vi => "#19177C", # Name.Variable.Instance
55
+ :il => "#666666" # Literal.Number.Integer.Long
56
+ }
57
+
58
+ def self.pyg_change(code, klass, style)
59
+ color = style[0..6]
60
+ modifier = style[8]
61
+ mod_open = modifier ? "<#{modifier}>" : ""
62
+ mod_close = modifier ? "</#{modifier}>" : ""
63
+ rx = /<span class="#{klass}">(?<cname>[^<]+?)<\/span>/
64
+ loop do
65
+ md = rx.match(code)
66
+ break if md.nil?
67
+ str = md[:cname]
68
+ result = code.sub!(rx, "<font color=#{color}>#{mod_open}#{str}#{mod_close}</font>")
69
+ break if result.nil?
70
+ end
71
+ end
72
+
73
+ def self._codebar_color(lexer)
74
+ color = case lexer
75
+ when :elixir
76
+ "#fc88fc"
77
+ when :ruby
78
+ "#fc8888"
79
+ else
80
+ raise "Unknown lexer"
81
+ end
82
+ end
83
+
84
+ def self.pyg_finalize(code, lexer=:elixir)
85
+ Styles.each_pair {|klass, style| pyg_change(code, klass, style) }
86
+ code.sub!(/<pre>/, "<pre>\n")
87
+ code.gsub!(/<span class="[np]">/, "")
88
+ code.gsub!(/<\/span>/, "")
89
+ color = _codebar_color(lexer)
90
+ code.sub!(/<td class="linenos"/, "<td width=6%></td><td width=5% bgcolor=#{color}")
91
+ code.gsub!(/ class="[^"]*?"/, "") # Get rid of remaining Pygments CSS
92
+ lines = code.split("\n")
93
+ n1 = lines.index {|x| x =~ /<pre>/ }
94
+ n2 = lines.index {|x| x =~ /<\/pre>/ }
95
+ # FIXME ?
96
+ n1 ||= 0
97
+ n2 ||= -1
98
+ lines[n1].sub!(/ 1$/, " 1 ")
99
+ (n1+1).upto(n2) {|n| lines[n].replace(" " + lines[n] + " ") }
100
+ code = lines.join("\n")
101
+ code
102
+ end
103
+ end
104
+
105
+ # Was in 'bookish':
106
+
107
+ include PygmentFix
108
+
109
+ def _process_code(text)
110
+ lines = text.split("\n")
111
+ lines = lines.select {|x| x !~ /##~ omit/ }
112
+ @refs = {}
113
+ lines.each.with_index do |line, i|
114
+ if line =~ /##~ ref/
115
+ frag, name = line.split(/ *##~ ref/)
116
+ @refs[name.strip] = i
117
+ line.replace(frag)
118
+ end
119
+ end
120
+ lines.map! {|line| " " + line }
121
+ text.replace(lines.join("\n"))
122
+ end
123
+
124
+ def _colorize(code, lexer=:elixir)
125
+ text = ::Pygments.highlight(code, lexer: lexer, options: {linenos: "table"})
126
+ _debug "--- in _colorize: text = #{text.inspect}"
127
+ PygmentFix.pyg_finalize(text, lexer)
128
+ text
129
+ end
130
+
131
+ def _colorize!(code, lexer=:elixir)
132
+ text = ::Pygments.highlight(code, lexer: lexer, options: {})
133
+ _debug "--- in _colorize!: text = #{text.inspect}"
134
+ PygmentFix.pyg_finalize(text, lexer)
135
+ text
136
+ end
137
+
138
+ def ruby
139
+ file = @_args.first
140
+ if file.nil?
141
+ code = "# Ruby code\n"
142
+ _body {|line| code << line }
143
+ else
144
+ code = "# Ruby code\n\n" + ::File.read(file)
145
+ end
146
+
147
+ _process_code(code)
148
+ html = _colorize(code, :ruby)
149
+ @output.puts "\n#{html}\n "
150
+ end
151
+
152
+ def elixir
153
+ file = @_args.first
154
+ if file.nil?
155
+ code = ""
156
+ _body {|line| code << line }
157
+ else
158
+ code = ::File.read(file)
159
+ end
160
+
161
+ _process_code(code)
162
+ html = _colorize(code, :elixir)
163
+ @output.puts "\n#{html}\n "
164
+ end
165
+
166
+ def fragment
167
+ # debug
168
+ lexer = @_args.empty? ? :elixir : @_args.first.to_sym # ruby or elixir
169
+ _debug "-- fragment: lexer = #{lexer.inspect}"
170
+ code = ""
171
+ code << "# Ruby code\n\n" if lexer == :ruby
172
+ _body {|line| code << " " + line }
173
+ _debug "code = \n#{code}\n-----"
174
+ params = "(code, lexer: #{lexer.inspect}, options: {})"
175
+ _debug "-- pygments params = #{params}"
176
+ text = _colorize!(code, lexer)
177
+ text ||= "ERROR IN HIGHLIGHTER"
178
+ _debug "text = \n#{text.inspect}\n-----"
179
+ # PygmentFix.pyg_finalize(text, lexer)
180
+ @output.puts text + "\n<br>"
181
+ end
182
+
183
+ def code # FIXME ?
184
+ text = ""
185
+ _body {|line| @output.puts " " + line }
186
+ end
187
+
188
+ def mono
189
+ _puts "<pre>"
190
+ _body {|line| _puts " " + line }
191
+ _puts "</pre>"
192
+ end
193
+
194
+
data/lib/tutorial.rb ADDED
@@ -0,0 +1,89 @@
1
+ require 'cgi'
2
+
3
+ def title
4
+ puts "<center><h2>#{_data}</h2></center>"
5
+ end
6
+
7
+ def section
8
+ puts "<br>" * 2 + "<b><font size=+1>#{_data}</font></b>" + "<br>"
9
+ end
10
+
11
+ def code
12
+ _puts "<pre>"
13
+ _body {|line| _puts " #{::CGI.escape_html(line)}" } # indentation
14
+ _puts "</pre>"
15
+ end
16
+
17
+ def rx(str)
18
+ ::Regexp.compile(::Regexp.escape(str))
19
+ end
20
+
21
+ def list
22
+ puts "<ul>"
23
+ _body {|line| puts "<li>#{_formatting(line)}</li>" }
24
+ _puts "</ul>"
25
+ end
26
+
27
+ def nlist
28
+ puts "<ol>"
29
+ _body {|line| puts "<li>#{_formatting(line)}</li>" }
30
+ _puts "</ol>"
31
+ end
32
+
33
+ def dlist
34
+ delim = "~~"
35
+ _puts "<table>"
36
+ _body do |line|
37
+ # @tty.puts "Line = #{line}"
38
+ line = OLD_formatting(line)
39
+ # @tty.puts "Line = #{line}\n "
40
+ term, defn = line.split(delim)
41
+ _puts "<tr>"
42
+ _puts "<td width=3%><td width=10%>#{term}</td><td>#{defn}</td>"
43
+ _puts "</tr>"
44
+ end
45
+ _puts "</table>"
46
+ end
47
+
48
+ def p
49
+ # puts "<p>"
50
+ _passthru(_data)
51
+ end
52
+
53
+ def inout
54
+ src, out = _args
55
+ t1 = ::File.readlines(src) rescue (abort "t1 = #{src}")
56
+ t2 = ::File.readlines(out) rescue (abort "t2 = #{out}")
57
+ # To pacify markdown for README (FIXME later)
58
+ t1 = t1.map {|x| " " + x.sub(/ +$/,"").gsub(/_/, "\\_") }.join
59
+ t2 = t2.map {|x| " " + x.sub(/ +$/,"").gsub(/_/, "\\_") }.join
60
+ # t1 = ::CGI.escape_html(t1)
61
+ # t2 = ::CGI.escape_html(t2)
62
+
63
+ puts <<-HTML
64
+ <center>
65
+ <table width=80% cellpadding=4>
66
+ <tr>
67
+ <td width=50%><b>Input</b></td>
68
+ <td width=50%><b>Output</b></td>
69
+ </tr>
70
+ <tr>
71
+ <td width=50% bgcolor=#fec0fe valign=top>
72
+ <pre>#{t1}</pre>
73
+ </td>
74
+ <td width=50% bgcolor=lightgray valign=top>
75
+ <pre>#{t2}</pre>
76
+ </td>
77
+ </tr>
78
+ </table>
79
+ </center>
80
+ HTML
81
+ end
82
+
83
+ def testcase
84
+ name = _args.first
85
+ _puts "\n<b>Test: <tt>#{name.gsub(/_/, "\\_")}</tt></b><br>"
86
+ src, exp = "test/testfiles/#{name}/source.ltx", "test/testfiles/#{name}/expected-output.txt"
87
+ @_args = [src, exp] # Better way to do this??
88
+ inout # Weird - only place I've done this yet.
89
+ end
Binary file
data/livetext.gemspec CHANGED
@@ -1,12 +1,107 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'livetext'
3
- s.version = '0.5.2'
4
- s.date = '2017-02-03'
3
+ s.version = '0.5.3'
4
+ s.date = '2017-02-05'
5
5
  s.summary = "A smart processor for text"
6
- s.description = "A smart processor for text"
6
+ s.description = "A smart text processor extensible in Ruby"
7
7
  s.authors = ["Hal Fulton"]
8
8
  s.email = 'rubyhacker@gmail.com'
9
- s.files = Dir["**"]
9
+ s.files = %w[
10
+ ./bin
11
+ ./bin/livetext
12
+ ./lib
13
+ ./lib/bookish.rb
14
+ ./lib/calibre.rb
15
+ ./lib/liveblog.rb
16
+ ./lib/livemagick.rb
17
+ ./lib/livetext.rb
18
+ ./lib/markdown.rb
19
+ ./lib/pyggish.rb
20
+ ./lib/tutorial.rb
21
+ ./livetext-0.5.2.gem
22
+ ./livetext.gemspec
23
+ ./notes.txt
24
+ ./README.html
25
+ ./README.ltx
26
+ ./README.md
27
+ ./test
28
+ ./test/cleanup
29
+ ./test/newtest
30
+ ./test/rawtext.inc
31
+ ./test/simple_mixin.rb
32
+ ./test/simplefile.inc
33
+ ./test/test.rb
34
+ ./test/testfiles
35
+ ./test/testfiles/basic_formatting
36
+ ./test/testfiles/basic_formatting/expected-error.txt
37
+ ./test/testfiles/basic_formatting/expected-output.txt
38
+ ./test/testfiles/basic_formatting/source.ltx
39
+ ./test/testfiles/block_comment
40
+ ./test/testfiles/block_comment/expected-error.txt
41
+ ./test/testfiles/block_comment/expected-output.txt
42
+ ./test/testfiles/block_comment/source.ltx
43
+ ./test/testfiles/comments_ignored_1
44
+ ./test/testfiles/comments_ignored_1/expected-error.txt
45
+ ./test/testfiles/comments_ignored_1/expected-output.txt
46
+ ./test/testfiles/comments_ignored_1/source.ltx
47
+ ./test/testfiles/copy_is_raw
48
+ ./test/testfiles/copy_is_raw/expected-error.txt
49
+ ./test/testfiles/copy_is_raw/expected-output.txt
50
+ ./test/testfiles/copy_is_raw/source.ltx
51
+ ./test/testfiles/def_method
52
+ ./test/testfiles/def_method/expected-error.txt
53
+ ./test/testfiles/def_method/expected-output.txt
54
+ ./test/testfiles/def_method/source.ltx
55
+ ./test/testfiles/example_alpha
56
+ ./test/testfiles/example_alpha/expected-error.txt
57
+ ./test/testfiles/example_alpha/expected-output.txt
58
+ ./test/testfiles/example_alpha/source.ltx
59
+ ./test/testfiles/example_alpha2
60
+ ./test/testfiles/example_alpha2/expected-error.txt
61
+ ./test/testfiles/example_alpha2/expected-output.txt
62
+ ./test/testfiles/example_alpha2/source.ltx
63
+ ./test/testfiles/fixit
64
+ ./test/testfiles/functions
65
+ ./test/testfiles/functions/expected-error.txt
66
+ ./test/testfiles/functions/expected-output.txt
67
+ ./test/testfiles/functions/source.ltx
68
+ ./test/testfiles/hello_world
69
+ ./test/testfiles/hello_world/expected-error.txt
70
+ ./test/testfiles/hello_world/expected-output.txt
71
+ ./test/testfiles/hello_world/source.ltx
72
+ ./test/testfiles/more_complex_vars
73
+ ./test/testfiles/more_complex_vars/expected-error.txt
74
+ ./test/testfiles/more_complex_vars/expected-output.txt
75
+ ./test/testfiles/more_complex_vars/source.ltx
76
+ ./test/testfiles/raw_text_block
77
+ ./test/testfiles/raw_text_block/expected-error.txt
78
+ ./test/testfiles/raw_text_block/expected-output.txt
79
+ ./test/testfiles/raw_text_block/source.ltx
80
+ ./test/testfiles/sigil_can_change
81
+ ./test/testfiles/sigil_can_change/expected-error.txt
82
+ ./test/testfiles/sigil_can_change/expected-output.txt
83
+ ./test/testfiles/sigil_can_change/source.ltx
84
+ ./test/testfiles/simple_copy
85
+ ./test/testfiles/simple_copy/expected-error.txt
86
+ ./test/testfiles/simple_copy/expected-output.txt
87
+ ./test/testfiles/simple_copy/source.ltx
88
+ ./test/testfiles/simple_include
89
+ ./test/testfiles/simple_include/expected-error.txt
90
+ ./test/testfiles/simple_include/expected-output.txt
91
+ ./test/testfiles/simple_include/source.ltx
92
+ ./test/testfiles/simple_mixin
93
+ ./test/testfiles/simple_mixin/expected-error.txt
94
+ ./test/testfiles/simple_mixin/expected-output.txt
95
+ ./test/testfiles/simple_mixin/source.ltx
96
+ ./test/testfiles/simple_vars
97
+ ./test/testfiles/simple_vars/expected-error.txt
98
+ ./test/testfiles/simple_vars/expected-output.txt
99
+ ./test/testfiles/simple_vars/source.ltx
100
+ ./test/testfiles/single_raw_line
101
+ ./test/testfiles/single_raw_line/expected-error.txt
102
+ ./test/testfiles/single_raw_line/expected-output.txt
103
+ ./test/testfiles/single_raw_line/source.ltx
104
+ ]
10
105
  s.homepage = 'https://github.com/Hal9000/livetext'
11
106
  s.license = "Ruby's"
12
107
  end
data/test/cleanup ADDED
@@ -0,0 +1 @@
1
+ find testfiles -name actual-*.txt | xargs rm
data/test/newtest ADDED
@@ -0,0 +1,10 @@
1
+ mkdir testfiles/$1
2
+
3
+ # actual-error.txt actual-output.txt expected-error.txt expected-output.txt source.ltx
4
+
5
+ cd testfiles/$1
6
+
7
+ touch expected-output.txt expected-error.txt
8
+
9
+ vi -O source.ltx exp expected-output.txt expected-error.txt
10
+
data/test/rawtext.inc ADDED
@@ -0,0 +1,4 @@
1
+ This is not a comment:
2
+ .comment woohoo!
3
+ This is not a method:
4
+ .no_such_method