LiterateRuby 0.2.0 → 0.2.1
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/README.md +15 -0
- data/Rakefile +5 -0
- data/bin/lruby +9 -2
- data/lib/literate-ruby/file.rb +66 -35
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e447d2337768c6fd301b6187acdc96ab2bfad880
|
4
|
+
data.tar.gz: 48cf19a95f428bc1e423abd3b05ec51f62cd97b8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 850ad55aacad8835c4884902349635d6fea355bda572637cfbbda296b9615a1e292ceb93ecac20dab88b194bc43e408b8972204544595cb86b9ef36dbf8f50be
|
7
|
+
data.tar.gz: 5fbe2061d2155277d0f672f9699ddc1bbfc20e004ed9c1f03b248a73f57a1b10401d17b9052843d93724f1b4a5851a60e8ba29abe3082ea415750c28319f962b
|
data/README.md
CHANGED
@@ -5,6 +5,21 @@ Version](https://badge.fury.io/rb/LiterateRuby.svg)](https://badge.fury.io/rb/Li
|
|
5
5
|
## Name
|
6
6
|
The name is inspired by Literate Haskell.
|
7
7
|
|
8
|
+
## Installation
|
9
|
+
This gem can be install using `gem` as follows:
|
10
|
+
|
11
|
+
```bash
|
12
|
+
gem install LiterateRuby
|
13
|
+
```
|
14
|
+
|
15
|
+
## Executable
|
16
|
+
|
17
|
+
The executable for this is called `lruby`. It can be used as such
|
18
|
+
|
19
|
+
```bash
|
20
|
+
lruby file.lrb
|
21
|
+
```
|
22
|
+
|
8
23
|
## Usage
|
9
24
|
|
10
25
|
A file like this
|
data/Rakefile
CHANGED
data/bin/lruby
CHANGED
@@ -4,10 +4,17 @@ require 'literate-ruby'
|
|
4
4
|
outputs = []
|
5
5
|
exit_status = 0
|
6
6
|
|
7
|
+
flags = ARGV.select { |i| i[0] == '-' }
|
8
|
+
eval_flag = flags.include?('-e')
|
9
|
+
ARGV.reject! { |i| i[0] == '-' }
|
10
|
+
|
7
11
|
ARGV.each do |f|
|
8
12
|
if f[-4..-1] != '.lrb'
|
9
13
|
STDERR.puts "Error: #{f} is not a literate ruby file."
|
10
14
|
exit_status = 1
|
15
|
+
elsif eval_flag
|
16
|
+
j = LiterateRuby::File.new(f)
|
17
|
+
outputs << j.to_eval_markdown
|
11
18
|
else
|
12
19
|
j = LiterateRuby::File.new(f)
|
13
20
|
outputs << j.parse
|
@@ -16,6 +23,6 @@ ARGV.each do |f|
|
|
16
23
|
end
|
17
24
|
end
|
18
25
|
|
19
|
-
puts
|
20
|
-
outputs.each{|i| puts " x - #{i}"}
|
26
|
+
puts 'Files created:'
|
27
|
+
outputs.each { |i| puts " x - #{i}" }
|
21
28
|
exit exit_status
|
data/lib/literate-ruby/file.rb
CHANGED
@@ -1,58 +1,89 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
|
1
3
|
module LiterateRuby
|
2
|
-
# This class is the file class for literate ruby.
|
3
|
-
#
|
4
|
+
# This class is the file class for literate ruby. It extends Ruby's
|
5
|
+
# +File+ class.
|
4
6
|
#
|
5
|
-
#
|
6
|
-
# parses a literate ruby file (extension `.lrb`).
|
7
|
+
# @attr base [String] the base name of the file
|
7
8
|
class File < File
|
9
|
+
attr_accessor :base
|
10
|
+
|
11
|
+
# The constructor for +LiterateRuby::File+
|
12
|
+
#
|
13
|
+
# @param *args the arguments that are passed to super.
|
14
|
+
def initialize(*args)
|
15
|
+
super(*args)
|
16
|
+
@base = File.basename(path, '.*')
|
17
|
+
end
|
18
|
+
|
8
19
|
# This method parses a literate ruby file, (which should have the
|
9
|
-
# extension
|
10
|
-
# extension
|
20
|
+
# extension +.lrb+), to a normal ruby file (which will have the
|
21
|
+
# extension +.rb+).
|
11
22
|
#
|
12
23
|
# @return [String] the file_path to the normal ruby file.
|
13
24
|
def parse
|
14
|
-
base = File.basename(path, '.*')
|
15
25
|
`touch #{base}.rb`
|
16
26
|
file = read
|
17
27
|
File.open("#{base}.rb", 'w') do |f|
|
18
28
|
file.each_line do |line|
|
19
|
-
f.write(line[2..-1]) if line[0
|
29
|
+
f.write(line[2..-1]) if line[0..1] == '> '
|
20
30
|
end
|
21
31
|
end
|
22
32
|
"#{base}.rb"
|
23
33
|
end
|
24
34
|
|
25
35
|
# This method parses a literate ruby file, (which should have the
|
26
|
-
# extension
|
27
|
-
# extension
|
36
|
+
# extension +.lrb+), to a markdown file (which will have the
|
37
|
+
# extension +.md+).
|
28
38
|
#
|
29
|
-
# @param [Boolean] ghfm if true then it will be written in github
|
30
|
-
# flavored markdown. Otherwise it will be in a more traditional
|
31
|
-
# markdown format
|
32
39
|
# @return [String] the file_path to the markdown file.
|
33
|
-
def to_markdown
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
f.write("\n#{line}")
|
47
|
-
elsif line =~ /^\s+$/
|
48
|
-
code_block = true
|
49
|
-
f.write(line)
|
50
|
-
else
|
51
|
-
f.write(line)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
40
|
+
def to_markdown
|
41
|
+
f = File.new("#{base}.md", 'w')
|
42
|
+
block = false
|
43
|
+
read.each_line { |l| block = to_markdown_helper(l, f, block) }
|
44
|
+
"#{base}.md"
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_eval_markdown
|
48
|
+
f = File.new("#{base}.md", 'w+')
|
49
|
+
tmp_f = File.new("#{base}_tmp.rb", 'w+')
|
50
|
+
block = false
|
51
|
+
read.each_line { |l| block = to_eval_helper(l, f, tmp_f, block) }
|
52
|
+
`yes | rm #{base}_tmp.rb`
|
55
53
|
"#{base}.md"
|
56
54
|
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def to_markdown_helper(l, f, block)
|
59
|
+
if l[0..1] == '> ' && block then f.write(" #{l[2..-1]}")
|
60
|
+
elsif block then block = !f.write("\n#{l}")
|
61
|
+
elsif l =~ /^\s+$/ then block = !f.write(l).nil?
|
62
|
+
else f.write(l)
|
63
|
+
end
|
64
|
+
block
|
65
|
+
end
|
66
|
+
|
67
|
+
def to_eval_helper(l, f, tmp_f, block)
|
68
|
+
if l[0..1] == '> ' && block then tmp_f.write(l[2..-1].to_s)
|
69
|
+
elsif block
|
70
|
+
block = eval_string(tmp_f, f)
|
71
|
+
$stdout = STDOUT
|
72
|
+
elsif l =~ /^\s+$/ then block = !f.write(l).nil?
|
73
|
+
else f.write(l)
|
74
|
+
end
|
75
|
+
block
|
76
|
+
end
|
77
|
+
|
78
|
+
def eval_string(tmp_f, f)
|
79
|
+
s = ''
|
80
|
+
IO.popen('ruby', 'w+') do |i|
|
81
|
+
i.write(tmp_f.read)
|
82
|
+
i.close_write
|
83
|
+
s = i.read
|
84
|
+
end
|
85
|
+
f.puts " #{s}"
|
86
|
+
false
|
87
|
+
end
|
57
88
|
end
|
58
89
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: LiterateRuby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eli Sadoff
|
@@ -10,8 +10,10 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2017-01-11 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
14
|
-
|
13
|
+
description: |
|
14
|
+
This gem is hightly inspired by literate haskell. It currently will support
|
15
|
+
"bird style" which is described in full <a href="https://wiki.haskell.org/Literate_programming#Bird_Style">here</a>
|
16
|
+
Note: The <pre>lruby -e</pre> feature does not yet work.
|
15
17
|
email: snood1205@gmail.com
|
16
18
|
executables:
|
17
19
|
- lruby
|