literate-programming 1.1.1 → 1.2.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.
- checksums.yaml +4 -4
- data/bin/rtangle +32 -5
- data/bin/rweave +47 -5
- data/lib/literate/programming.rb +43 -2
- data/lib/literate/programming/version.rb +1 -1
- data/spec/lib/literate/programming_spec.rb +199 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 75f62b03242c7d1b19adbd5d328d430bba322347
|
4
|
+
data.tar.gz: e0477f1d1a7988d864afd190dcb22640f25047fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f17f6e77b5878728d04119e8d9914c4b93fba4ec574f45eae4f807de0030a8031339b93ca0257af42a5a300aca59c4617b7a9cb0e4106f31599f821fb1cd61d
|
7
|
+
data.tar.gz: c118b8b572c5af87b2720d2368ca33bc8d64d4fac041d2ec6b38893810adc6d8cdfa32dfae4a4a22e2c2824e25ddf3f122d0173a24e136b7c684fea9d0c6fe6f
|
data/bin/rtangle
CHANGED
@@ -1,9 +1,36 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
+
require 'getoptlong'
|
3
4
|
require 'literate/programming'
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
def usage
|
7
|
+
print <<-EOM
|
8
|
+
Usage: rtangle [command]... source.wrb...
|
9
|
+
|
10
|
+
commands:
|
11
|
+
-h -? --help --usage show this message and exit
|
12
|
+
EOM
|
13
|
+
exit
|
14
|
+
end
|
15
|
+
|
16
|
+
def main
|
17
|
+
options = {}
|
18
|
+
begin
|
19
|
+
GetoptLong.new.set_options(
|
20
|
+
%w/-h -? --help --usage/ << GetoptLong::NO_ARGUMENT,
|
21
|
+
).each_option do |name, value|
|
22
|
+
options[name.sub(/^--?/, '').gsub(/-/, '_').to_sym] = value
|
23
|
+
end
|
24
|
+
rescue GetoptLong::Error
|
25
|
+
usage
|
26
|
+
end
|
27
|
+
|
28
|
+
usage if ARGV.length == 0 or options.member? :h
|
29
|
+
ARGV.each do |ifname|
|
30
|
+
inst = Literate::Programming.new File.open(ifname, 'r').read
|
31
|
+
ofname = File.dirname(ifname) + '/' + File.basename(ifname, '.wrb') + '.rb'
|
32
|
+
File.open(ofname, 'w').write inst.to_ruby
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
main
|
data/bin/rweave
CHANGED
@@ -1,9 +1,51 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
+
require 'getoptlong'
|
3
4
|
require 'literate/programming'
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
def usage
|
7
|
+
print <<-EOM
|
8
|
+
Usage: rweave [command]... source.wrb...
|
9
|
+
|
10
|
+
commands:
|
11
|
+
-h, -?, --help, --usage show this message and exit
|
12
|
+
-fFORMAT, --format=FORMAT specify output format
|
13
|
+
default: Markdown
|
14
|
+
|
15
|
+
formats:
|
16
|
+
md Markdown
|
17
|
+
tex LaTeX
|
18
|
+
EOM
|
19
|
+
exit
|
20
|
+
end
|
21
|
+
|
22
|
+
def main
|
23
|
+
options = {}
|
24
|
+
begin
|
25
|
+
GetoptLong.new.set_options(
|
26
|
+
%w/-h -? --help --usage/ << GetoptLong::NO_ARGUMENT,
|
27
|
+
%w/-f --format/ << GetoptLong::REQUIRED_ARGUMENT,
|
28
|
+
).each_option do |name, value|
|
29
|
+
options[name.sub(/^--?/, '').gsub(/-/, '_').to_sym] = value
|
30
|
+
end
|
31
|
+
rescue GetoptLong::Error
|
32
|
+
usage
|
33
|
+
end
|
34
|
+
|
35
|
+
usage if ARGV.length == 0 or options.member? :h
|
36
|
+
ARGV.each do |ifname|
|
37
|
+
inst = Literate::Programming.new File.open(ifname, 'r').read
|
38
|
+
case options[:f]
|
39
|
+
when 'md', nil
|
40
|
+
ofname = File.dirname(ifname) + '/' + File.basename(ifname, '.wrb') + '.md'
|
41
|
+
File.open(ofname, 'w').write inst.to_md
|
42
|
+
when 'tex'
|
43
|
+
ofname = File.dirname(ifname) + '/' + File.basename(ifname, '.wrb') + '.tex'
|
44
|
+
File.open(ofname, 'w').write inst.to_tex
|
45
|
+
else
|
46
|
+
raise 'unknown format ' + options[:f]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
main
|
data/lib/literate/programming.rb
CHANGED
@@ -21,9 +21,19 @@ module Literate
|
|
21
21
|
convert[:md]
|
22
22
|
end
|
23
23
|
|
24
|
+
def to_tex
|
25
|
+
convert[:tex]
|
26
|
+
end
|
27
|
+
|
24
28
|
def convert
|
25
29
|
current_mode = :text
|
26
30
|
md = ""
|
31
|
+
tex = <<-TEX
|
32
|
+
\\documentclass{report}
|
33
|
+
\\usepackage{listings}
|
34
|
+
\\begin{document}
|
35
|
+
\\lstset{language=Ruby}
|
36
|
+
TEX
|
27
37
|
table = {}
|
28
38
|
current_code = nil
|
29
39
|
current_label = nil
|
@@ -62,17 +72,23 @@ module Literate
|
|
62
72
|
table[current_label] << $/ << current_code
|
63
73
|
end
|
64
74
|
md << '```' << $/ << $/
|
75
|
+
tex << '\\end{lstlisting}' << $/ << $/
|
65
76
|
else
|
66
77
|
md << line << $/
|
78
|
+
tex << line << $/
|
67
79
|
end
|
68
80
|
when :code
|
69
81
|
if old_mode == :code then
|
70
82
|
current_code << line << $/
|
71
83
|
md << line << $/
|
84
|
+
tex << line << $/
|
72
85
|
else
|
73
86
|
md << '```ruby:' << current_label
|
74
87
|
md << ' append' if operator == :append
|
75
88
|
md << $/
|
89
|
+
tex << '\\begin{lstlisting}[frame=lines,caption=' << current_label
|
90
|
+
tex << ' append' if operator == :append
|
91
|
+
tex << ']' << $/
|
76
92
|
end
|
77
93
|
end
|
78
94
|
end
|
@@ -85,9 +101,13 @@ module Literate
|
|
85
101
|
table[current_label] << $/ << current_code
|
86
102
|
end
|
87
103
|
md << '```' << $/
|
104
|
+
tex << '\\end{lstlisting}' << $/
|
88
105
|
end
|
89
106
|
table['*'] ||= ''
|
90
|
-
|
107
|
+
tex << <<-TEX
|
108
|
+
\\end{document}
|
109
|
+
TEX
|
110
|
+
return {rb: indent_code(expand(table, '*')), md: indent_markdown(md), tex: indent_tex(tex)}
|
91
111
|
end
|
92
112
|
|
93
113
|
def expand(table, label)
|
@@ -152,7 +172,7 @@ module Literate
|
|
152
172
|
return ret
|
153
173
|
end
|
154
174
|
|
155
|
-
def
|
175
|
+
def indent_markdown(text)
|
156
176
|
mode = :text
|
157
177
|
ret = ''
|
158
178
|
indent_level_stack = nil
|
@@ -173,6 +193,27 @@ module Literate
|
|
173
193
|
return ret
|
174
194
|
end
|
175
195
|
|
196
|
+
def indent_tex(text)
|
197
|
+
mode = :text
|
198
|
+
ret = ''
|
199
|
+
indent_level_stack = nil
|
200
|
+
text.split($/).each do |line|
|
201
|
+
if mode == :text and line.match /^\\begin{lstlisting}/ then
|
202
|
+
mode = :code
|
203
|
+
indent_level_stack = [-@tabstop]
|
204
|
+
ret << line << $/
|
205
|
+
elsif mode == :text then
|
206
|
+
ret << line << $/
|
207
|
+
elsif mode == :code and line.match /^\\end{lstlisting}/ then
|
208
|
+
mode = :text
|
209
|
+
ret << line << $/
|
210
|
+
else
|
211
|
+
ret, indent_level_stack = pretty_print_ruby ret, indent_level_stack, line
|
212
|
+
end
|
213
|
+
end
|
214
|
+
return ret
|
215
|
+
end
|
216
|
+
|
176
217
|
def pretty_print_ruby(buffer, indent_level_stack, line)
|
177
218
|
if line.match /^[ \t]*(else|elsif|ensure|rescue|when)\b/ then
|
178
219
|
current_indent_level = indent_level_stack[-1]
|
@@ -6,14 +6,35 @@ describe 'literate-programming' do
|
|
6
6
|
inst = Literate::Programming.new
|
7
7
|
expect(inst.to_ruby).to eq ""
|
8
8
|
expect(inst.to_md).to eq ""
|
9
|
+
expect(inst.to_tex).to eq <<-TEX
|
10
|
+
\\documentclass{report}
|
11
|
+
\\usepackage{listings}
|
12
|
+
\\begin{document}
|
13
|
+
\\lstset{language=Ruby}
|
14
|
+
\\end{document}
|
15
|
+
TEX
|
9
16
|
|
10
17
|
inst = Literate::Programming.new ""
|
11
18
|
expect(inst.to_ruby).to eq ""
|
12
19
|
expect(inst.to_md).to eq ""
|
20
|
+
expect(inst.to_tex).to eq <<-TEX
|
21
|
+
\\documentclass{report}
|
22
|
+
\\usepackage{listings}
|
23
|
+
\\begin{document}
|
24
|
+
\\lstset{language=Ruby}
|
25
|
+
\\end{document}
|
26
|
+
TEX
|
13
27
|
|
14
28
|
inst = Literate::Programming.new $/
|
15
29
|
expect(inst.to_ruby).to eq ""
|
16
30
|
expect(inst.to_md).to eq ""
|
31
|
+
expect(inst.to_tex).to eq <<-TEX
|
32
|
+
\\documentclass{report}
|
33
|
+
\\usepackage{listings}
|
34
|
+
\\begin{document}
|
35
|
+
\\lstset{language=Ruby}
|
36
|
+
\\end{document}
|
37
|
+
TEX
|
17
38
|
end
|
18
39
|
|
19
40
|
it 'simple case 1' do
|
@@ -33,6 +54,18 @@ Simple Case 1.
|
|
33
54
|
p 'Hello, world!'
|
34
55
|
```
|
35
56
|
EOM
|
57
|
+
|
58
|
+
expect(inst.to_tex).to eq <<-EOM
|
59
|
+
\\documentclass{report}
|
60
|
+
\\usepackage{listings}
|
61
|
+
\\begin{document}
|
62
|
+
\\lstset{language=Ruby}
|
63
|
+
Simple Case 1.
|
64
|
+
\\begin{lstlisting}[frame=lines,caption=*]
|
65
|
+
p 'Hello, world!'
|
66
|
+
\\end{lstlisting}
|
67
|
+
\\end{document}
|
68
|
+
EOM
|
36
69
|
end
|
37
70
|
|
38
71
|
it 'simple case 2' do
|
@@ -61,6 +94,23 @@ The main program says hello!
|
|
61
94
|
p [[hello-world]]
|
62
95
|
```
|
63
96
|
EOM
|
97
|
+
|
98
|
+
expect(inst.to_tex).to eq <<-TEX
|
99
|
+
\\documentclass{report}
|
100
|
+
\\usepackage{listings}
|
101
|
+
\\begin{document}
|
102
|
+
\\lstset{language=Ruby}
|
103
|
+
Simple Case 2.
|
104
|
+
\\begin{lstlisting}[frame=lines,caption=hello-world]
|
105
|
+
'Hello, world!'
|
106
|
+
\\end{lstlisting}
|
107
|
+
|
108
|
+
The main program says hello!
|
109
|
+
\\begin{lstlisting}[frame=lines,caption=*]
|
110
|
+
p [[hello-world]]
|
111
|
+
\\end{lstlisting}
|
112
|
+
\\end{document}
|
113
|
+
TEX
|
64
114
|
end
|
65
115
|
|
66
116
|
it 'simple case 3' do
|
@@ -115,6 +165,35 @@ Finally, call the main function.
|
|
115
165
|
main
|
116
166
|
```
|
117
167
|
EOC
|
168
|
+
|
169
|
+
expect(inst.to_tex).to eq <<-TEX
|
170
|
+
\\documentclass{report}
|
171
|
+
\\usepackage{listings}
|
172
|
+
\\begin{document}
|
173
|
+
\\lstset{language=Ruby}
|
174
|
+
Simple Case 3.
|
175
|
+
\\begin{lstlisting}[frame=lines,caption=*]
|
176
|
+
def main
|
177
|
+
[[main-body]]
|
178
|
+
end
|
179
|
+
\\end{lstlisting}
|
180
|
+
|
181
|
+
The main function says hello-world
|
182
|
+
\\begin{lstlisting}[frame=lines,caption=main-body]
|
183
|
+
p [[hello-world]]
|
184
|
+
\\end{lstlisting}
|
185
|
+
|
186
|
+
Definition of the hello-world is a 'Hello, world!'
|
187
|
+
\\begin{lstlisting}[frame=lines,caption=hello-world]
|
188
|
+
'Hello, world!'
|
189
|
+
\\end{lstlisting}
|
190
|
+
|
191
|
+
Finally, call the main function.
|
192
|
+
\\begin{lstlisting}[frame=lines,caption=* append]
|
193
|
+
main
|
194
|
+
\\end{lstlisting}
|
195
|
+
\\end{document}
|
196
|
+
TEX
|
118
197
|
end
|
119
198
|
|
120
199
|
it 'template case 1' do
|
@@ -411,6 +490,57 @@ Finally, call the main function.
|
|
411
490
|
main
|
412
491
|
```
|
413
492
|
EOC
|
493
|
+
|
494
|
+
expect(inst.to_tex).to eq <<-TEX
|
495
|
+
\\documentclass{report}
|
496
|
+
\\usepackage{listings}
|
497
|
+
\\begin{document}
|
498
|
+
\\lstset{language=Ruby}
|
499
|
+
Template Case 1.
|
500
|
+
\\begin{lstlisting}[frame=lines,caption=*]
|
501
|
+
def main
|
502
|
+
[[main-body]]
|
503
|
+
end
|
504
|
+
\\end{lstlisting}
|
505
|
+
|
506
|
+
Sing a song ''99 bottles of beer''
|
507
|
+
\\begin{lstlisting}[frame=lines,caption=main-body]
|
508
|
+
[[bottles:100]]
|
509
|
+
\\end{lstlisting}
|
510
|
+
|
511
|
+
General case, sing below
|
512
|
+
\\begin{lstlisting}[frame=lines,caption=bottles:@]
|
513
|
+
puts '@0 bottles of beer on the wall, @0 bottles of beer.'
|
514
|
+
puts 'Take one down and pass it around, @@(@0 - 1) bottles of beer on the wall.'
|
515
|
+
[[bottles:@@(@0 - 1)]]
|
516
|
+
\\end{lstlisting}
|
517
|
+
|
518
|
+
When number of bottles == 2
|
519
|
+
\\begin{lstlisting}[frame=lines,caption=bottles:2]
|
520
|
+
puts '2 bottles of beer on the wall, 2 bottles of beer.'
|
521
|
+
puts 'Take one down and pass it around, 1 bottle of beer on the wall.'
|
522
|
+
[[bottles:1]]
|
523
|
+
\\end{lstlisting}
|
524
|
+
|
525
|
+
When number of bottles == 1
|
526
|
+
\\begin{lstlisting}[frame=lines,caption=bottles:1]
|
527
|
+
puts '1 bottle of beer on the wall, 1 bottle of beer.'
|
528
|
+
puts 'Take one down and pass it around, no more bottles of beer on the wall.'
|
529
|
+
[[bottles:0]]
|
530
|
+
\\end{lstlisting}
|
531
|
+
|
532
|
+
When no rest bottles...
|
533
|
+
\\begin{lstlisting}[frame=lines,caption=bottles:0]
|
534
|
+
puts 'No more bottles of beer on the wall, no more bottles of beer.'
|
535
|
+
puts 'Go to the store and buy some more, 99 bottles of beer on the wall.'
|
536
|
+
\\end{lstlisting}
|
537
|
+
|
538
|
+
Finally, call the main function.
|
539
|
+
\\begin{lstlisting}[frame=lines,caption=* append]
|
540
|
+
main
|
541
|
+
\\end{lstlisting}
|
542
|
+
\\end{document}
|
543
|
+
TEX
|
414
544
|
end
|
415
545
|
|
416
546
|
it 'template case 2' do
|
@@ -494,6 +624,46 @@ Finally, instanciate Main and run it!
|
|
494
624
|
Main.new.run
|
495
625
|
```
|
496
626
|
EOC
|
627
|
+
|
628
|
+
expect(inst.to_tex).to eq <<-TEX
|
629
|
+
\\documentclass{report}
|
630
|
+
\\usepackage{listings}
|
631
|
+
\\begin{document}
|
632
|
+
\\lstset{language=Ruby}
|
633
|
+
Template Case 2.
|
634
|
+
\\begin{lstlisting}[frame=lines,caption=*]
|
635
|
+
class Main
|
636
|
+
[[main-body]]
|
637
|
+
end
|
638
|
+
\\end{lstlisting}
|
639
|
+
|
640
|
+
It is free to use @ unless it is followed by a number.
|
641
|
+
It is also free to use @@ unless it is followed by left parent.
|
642
|
+
\\begin{lstlisting}[frame=lines,caption=main-body]
|
643
|
+
def initialize
|
644
|
+
@x = @@default_x
|
645
|
+
end
|
646
|
+
\\end{lstlisting}
|
647
|
+
|
648
|
+
If @@ is followed by left parent, it will evaluated by the rtangle.
|
649
|
+
For example, an rtangled below one will equals to '@@default_x = 10'
|
650
|
+
\\begin{lstlisting}[frame=lines,caption=main-body append]
|
651
|
+
@@default_x = @@(1 + 2 + 3 + 4)
|
652
|
+
\\end{lstlisting}
|
653
|
+
|
654
|
+
How Main.new.run works?
|
655
|
+
\\begin{lstlisting}[frame=lines,caption=main-body append]
|
656
|
+
def run
|
657
|
+
p 'Hello!'
|
658
|
+
end
|
659
|
+
\\end{lstlisting}
|
660
|
+
|
661
|
+
Finally, instanciate Main and run it!
|
662
|
+
\\begin{lstlisting}[frame=lines,caption=* append]
|
663
|
+
Main.new.run
|
664
|
+
\\end{lstlisting}
|
665
|
+
\\end{document}
|
666
|
+
TEX
|
497
667
|
end
|
498
668
|
|
499
669
|
it '*before* label' do
|
@@ -549,5 +719,34 @@ Finally, call the main function.
|
|
549
719
|
main
|
550
720
|
```
|
551
721
|
EOC
|
722
|
+
|
723
|
+
expect(inst.to_tex).to eq <<-TEX
|
724
|
+
\\documentclass{report}
|
725
|
+
\\usepackage{listings}
|
726
|
+
\\begin{document}
|
727
|
+
\\lstset{language=Ruby}
|
728
|
+
If you want to do something that requires many sentence,
|
729
|
+
you can use *before* label;
|
730
|
+
It will be expanded and be evaluated by rtangle to help to write.
|
731
|
+
\\begin{lstlisting}[frame=lines,caption=*]
|
732
|
+
def main
|
733
|
+
@@(helper_function)
|
734
|
+
end
|
735
|
+
\\end{lstlisting}
|
736
|
+
|
737
|
+
For example, *before* label likes below;
|
738
|
+
Note: the *before*before* label, the *before*before*before* label, and so on, are also exists.
|
739
|
+
\\begin{lstlisting}[frame=lines,caption=*before*]
|
740
|
+
def helper_function
|
741
|
+
return "p 'Hello, world!'"
|
742
|
+
end
|
743
|
+
\\end{lstlisting}
|
744
|
+
|
745
|
+
Finally, call the main function.
|
746
|
+
\\begin{lstlisting}[frame=lines,caption=* append]
|
747
|
+
main
|
748
|
+
\\end{lstlisting}
|
749
|
+
\\end{document}
|
750
|
+
TEX
|
552
751
|
end
|
553
752
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: literate-programming
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pixie-grasper
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|