LiterateRuby 0.2.0 → 0.2.1

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: b08338d5e80fbb76ac865adee47d0806f117b5c0
4
- data.tar.gz: b9b722beadec98f4ec61129b27f48ba1837136b6
3
+ metadata.gz: e447d2337768c6fd301b6187acdc96ab2bfad880
4
+ data.tar.gz: 48cf19a95f428bc1e423abd3b05ec51f62cd97b8
5
5
  SHA512:
6
- metadata.gz: 483c41a6bfcb163c678a6f80f2c743b7b4bb7c51f0bdf22ad03c1a8d289a533ace263d8d0f164269093442c56f4b6f2dbbf21c545e6672a9fc1a3f3226adf042
7
- data.tar.gz: a56c1ecd7fb721ce91f4d89155f323bd8240d36d144ad94a0614cbc4c51cfb40ad62b84e14510db44d9e018c12a130d82f2ddb2843c5c1c76acc0feb0a2920bb
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
@@ -12,6 +12,11 @@ task :clean do
12
12
  `rm *.gem`
13
13
  end
14
14
 
15
+ task :install do
16
+ `gem build literate-ruby.gemspec`
17
+ `gem install *.gem`
18
+ end
19
+
15
20
  task :build do
16
21
  `gem build literate-ruby.gemspec`
17
22
  end
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 "Files created:"
20
- outputs.each{|i| puts " x - #{i}"}
26
+ puts 'Files created:'
27
+ outputs.each { |i| puts " x - #{i}" }
21
28
  exit exit_status
@@ -1,58 +1,89 @@
1
+ require 'stringio'
2
+
1
3
  module LiterateRuby
2
- # This class is the file class for literate ruby.
3
- # It mostly just inherits from Ruby's default File class.
4
+ # This class is the file class for literate ruby. It extends Ruby's
5
+ # +File+ class.
4
6
  #
5
- # The method in this class that is added is `parse`. This method
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 `.lrb`), to a normal ruby file (which will have the
10
- # extension `.rb`).
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...2] == '> '
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 `.lrb`), to a markdown file (which will have the
27
- # extension `.md`).
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(ghfm = false)
34
- base = File.basename(path, '.*')
35
- `touch #{base}.md`
36
- file = read
37
- File.open("#{base}.md", 'w') do |f|
38
- code_block = false
39
- file.each_line do |line|
40
- if line[0...2] == '> ' && code_block && !ghfm
41
- f.write(" #{line[2..-1]}")
42
- elsif line[0...2] == '> ' && code_block
43
- f.write("#{line[2..-1]}")
44
- elsif code_block
45
- code_block = false
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.0
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: "This gem is hightly inspired by literate haskell. It currently will
14
- support \n\"bird style\" which is described in full [here](https://wiki.haskell.org/Literate_programming#Bird_Style).\n"
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