literate-programming 1.2.0 → 1.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 75f62b03242c7d1b19adbd5d328d430bba322347
4
- data.tar.gz: e0477f1d1a7988d864afd190dcb22640f25047fb
3
+ metadata.gz: caa6c1610f672cf8ca1c7aaba23fe02f6ff8a1e0
4
+ data.tar.gz: 3890ff2d8c978e7b1d6da14f62e2cc490d309c90
5
5
  SHA512:
6
- metadata.gz: 4f17f6e77b5878728d04119e8d9914c4b93fba4ec574f45eae4f807de0030a8031339b93ca0257af42a5a300aca59c4617b7a9cb0e4106f31599f821fb1cd61d
7
- data.tar.gz: c118b8b572c5af87b2720d2368ca33bc8d64d4fac041d2ec6b38893810adc6d8cdfa32dfae4a4a22e2c2824e25ddf3f122d0173a24e136b7c684fea9d0c6fe6f
6
+ metadata.gz: ccd29169106d20cdc50d702bf5126a01d423dc731d4d321997e37592a7a35e50578be3e3e4ff4438634a127ffa2586444c57de2d3ffb7a1514a9fd2d69821313
7
+ data.tar.gz: 3c4b91aa9bac60784c83af41d7e18d1540ac2d184df42745540cbaaf2e692f98c6a6aaf1008dbfd9d40342e897de5cd1caacf0b829b761c231ed9b9b9d6f9df1
@@ -8,28 +8,67 @@ def usage
8
8
  Usage: rtangle [command]... source.wrb...
9
9
 
10
10
  commands:
11
- -h -? --help --usage show this message and exit
11
+ -c, --concat Read all sources,
12
+ then output to single file.
13
+ default: false
14
+ -e, --expandtab Indent with spaces.
15
+ default: true
16
+ -h, -?, --help, --usage Show this message and exit.
17
+ -oFILE, --output=FILE Set output file name.
18
+ If set this option,
19
+ --concat option will also set.
20
+ -tSTOP, --tabstop=STOP Specifies how many spaces
21
+ per one indent.
22
+ default: 2
12
23
  EOM
13
24
  exit
14
25
  end
15
26
 
16
27
  def main
17
- options = {}
28
+ options = {c: false, e: true, t: 2}
18
29
  begin
19
30
  GetoptLong.new.set_options(
31
+ %w/-c --concat/ << GetoptLong::OPTIONAL_ARGUMENT,
32
+ %w/-e --expandtab/ << GetoptLong::OPTIONAL_ARGUMENT,
20
33
  %w/-h -? --help --usage/ << GetoptLong::NO_ARGUMENT,
34
+ %w/-o --output/ << GetoptLong::REQUIRED_ARGUMENT,
35
+ %w/-t --tabstop/ << GetoptLong::REQUIRED_ARGUMENT,
21
36
  ).each_option do |name, value|
22
- options[name.sub(/^--?/, '').gsub(/-/, '_').to_sym] = value
37
+ option = name.sub(/^--?/, '').gsub(/-/, '_')
38
+ if %w/c e/.member? option then
39
+ case value
40
+ when '', /^t(r(ue?)?)?$/, /^y(es?)?$/
41
+ value = true
42
+ when /^f(a(l(se?)?)?)?$/, /^no?$/
43
+ value = false
44
+ else
45
+ raise "unrecognized value #{name}#{value}"
46
+ end
47
+ elsif %w/t/.member? option then
48
+ value = value.to_i
49
+ end
50
+ options[option.to_sym] = value
23
51
  end
24
52
  rescue GetoptLong::Error
25
53
  usage
26
54
  end
27
55
 
28
56
  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
57
+ if options.member? :o or options[:c] then
58
+ inst = Literate::Programming.new ARGV.collect { |path|
59
+ File.open(path, 'r').read
60
+ }.join($/), tabstop: options[:t], expandtab: options[:e]
61
+ if options.member? :o then
62
+ File.open(options[:o], 'w').write inst.to_ruby
63
+ else
64
+ File.open('a.rb', 'w').write inst.to_ruby
65
+ end
66
+ else
67
+ ARGV.each do |ifname|
68
+ inst = Literate::Programming.new File.open(ifname, 'r').read, tabstop: options[:t], expandtab: options[:e]
69
+ ofname = File.dirname(ifname) + '/' + File.basename(ifname, '.wrb') + '.rb'
70
+ File.open(ofname, 'w').write inst.to_ruby
71
+ end
33
72
  end
34
73
  end
35
74
 
data/bin/rweave CHANGED
@@ -8,9 +8,20 @@ def usage
8
8
  Usage: rweave [command]... source.wrb...
9
9
 
10
10
  commands:
11
- -h, -?, --help, --usage show this message and exit
12
- -fFORMAT, --format=FORMAT specify output format
13
- default: Markdown
11
+ -c, --concat Read all sources,
12
+ then output to single file.
13
+ default: false
14
+ -e, --expandtab Indent with spaces.
15
+ default: true
16
+ -fFORMAT, --format=FORMAT Specifies output format.
17
+ default: md
18
+ -h, -?, --help, --usage Show this message and exit.
19
+ -oFILE, --output=FILE Set output file name.
20
+ If set this option,
21
+ --concat option will also set.
22
+ -tSTOP, --tabstop=STOP Specifies how many spaces
23
+ per indent depth.
24
+ default: 2
14
25
 
15
26
  formats:
16
27
  md Markdown
@@ -20,30 +31,72 @@ Usage: rweave [command]... source.wrb...
20
31
  end
21
32
 
22
33
  def main
23
- options = {}
34
+ options = {c: false, e: true, f: 'md', t: 2}
24
35
  begin
25
36
  GetoptLong.new.set_options(
26
- %w/-h -? --help --usage/ << GetoptLong::NO_ARGUMENT,
37
+ %w/-c --concat/ << GetoptLong::OPTIONAL_ARGUMENT,
38
+ %w/-e --expandtab/ << GetoptLong::OPTIONAL_ARGUMENT,
27
39
  %w/-f --format/ << GetoptLong::REQUIRED_ARGUMENT,
40
+ %w/-h -? --help --usage/ << GetoptLong::NO_ARGUMENT,
41
+ %w/-o --output/ << GetoptLong::REQUIRED_ARGUMENT,
42
+ %w/-t --tabstop/ << GetoptLong::REQUIRED_ARGUMENT,
28
43
  ).each_option do |name, value|
29
- options[name.sub(/^--?/, '').gsub(/-/, '_').to_sym] = value
44
+ option = name.sub(/^--?/, '').gsub(/-/, '_')
45
+ if %w/c e/.member? option then
46
+ case value
47
+ when '', /^t(r(ue?)?)?$/, /^y(es?)?$/
48
+ value = true
49
+ when /^f(a(l(se?)?)?)?$/, /^no?$/
50
+ value = false
51
+ else
52
+ raise "unrecognized value #{name}=#{value}"
53
+ end
54
+ elsif %w/t/.member? option then
55
+ value = value.to_i
56
+ end
57
+ options[option.to_sym] = value
30
58
  end
31
59
  rescue GetoptLong::Error
32
60
  usage
33
61
  end
34
62
 
35
63
  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
64
+ if options.member? :o or options.member? :c then
65
+ inst = Literate::Programming.new ARGV.collect { |path|
66
+ File.open(path, 'r').read
67
+ }.join($/), tabstop: options[:t], expandtab: options[:e]
68
+ if options.member? :o then
69
+ case options[:f]
70
+ when 'md'
71
+ File.open(options[:o], 'w').write inst.to_md
72
+ when 'tex'
73
+ File.open(options[:o], 'w').write inst.to_tex
74
+ else
75
+ raise 'unknown format ' + options[:f]
76
+ end
45
77
  else
46
- raise 'unknown format ' + options[:f]
78
+ case options[:f]
79
+ when 'md'
80
+ File.open('a.md', 'w').write inst.to_md
81
+ when 'tex'
82
+ File.open('a.tex', 'w').write inst.to_tex
83
+ else
84
+ raise 'unknown format ' + options[:f]
85
+ end
86
+ end
87
+ else
88
+ ARGV.each do |ifname|
89
+ inst = Literate::Programming.new File.open(ifname, 'r').read, tabstop: options[:t], expandtab: options[:e]
90
+ case options[:f]
91
+ when 'md'
92
+ ofname = File.dirname(ifname) + '/' + File.basename(ifname, '.wrb') + '.md'
93
+ File.open(ofname, 'w').write inst.to_md
94
+ when 'tex'
95
+ ofname = File.dirname(ifname) + '/' + File.basename(ifname, '.wrb') + '.tex'
96
+ File.open(ofname, 'w').write inst.to_tex
97
+ else
98
+ raise 'unknown format ' + options[:f]
99
+ end
47
100
  end
48
101
  end
49
102
  end
@@ -2,9 +2,12 @@ require "literate/programming/version"
2
2
 
3
3
  module Literate
4
4
  class Programming
5
- def initialize(src = "", source: nil, tabstop: 2)
5
+ attr_writer :tabstop, :expandtab
6
+
7
+ def initialize(src = "", source: nil, tabstop: 2, expandtab: true)
6
8
  @source = source || src
7
9
  @tabstop = tabstop
10
+ @expandtab = expandtab
8
11
  @eval_context = BasicObject.new
9
12
  @eval_context.instance_eval <<-EOC
10
13
  def gensym
@@ -13,6 +16,14 @@ module Literate
13
16
  EOC
14
17
  end
15
18
 
19
+ def inspect
20
+ return "#<Literate::Programming:0x#{object_id.to_s(16)} tabstop=#{@tabstop} expandtab=#{@expandtab}>"
21
+ end
22
+
23
+ def <<(append)
24
+ @source << append
25
+ end
26
+
16
27
  def to_ruby
17
28
  convert[:rb]
18
29
  end
@@ -25,6 +36,7 @@ module Literate
25
36
  convert[:tex]
26
37
  end
27
38
 
39
+ private
28
40
  def convert
29
41
  current_mode = :text
30
42
  md = ""
@@ -94,11 +106,11 @@ module Literate
94
106
  end
95
107
  if current_mode == :code then
96
108
  case operator
97
- when :assign
98
- table[current_label] = current_code
99
- when :append
100
- table[current_label] ||= ''
101
- table[current_label] << $/ << current_code
109
+ when :assign
110
+ table[current_label] = current_code
111
+ when :append
112
+ table[current_label] ||= ''
113
+ table[current_label] << $/ << current_code
102
114
  end
103
115
  md << '```' << $/
104
116
  tex << '\\end{lstlisting}' << $/
@@ -164,7 +176,7 @@ module Literate
164
176
  end
165
177
 
166
178
  def indent_code(code)
167
- indent_level_stack = [-@tabstop]
179
+ indent_level_stack = [-1]
168
180
  ret = ''
169
181
  code.split($/).each do |line|
170
182
  ret, indent_level_stack = pretty_print_ruby ret, indent_level_stack, line
@@ -179,7 +191,7 @@ module Literate
179
191
  text.split($/).each do |line|
180
192
  if mode == :text and line.match /^```/ then
181
193
  mode = :code
182
- indent_level_stack = [-@tabstop]
194
+ indent_level_stack = [-1]
183
195
  ret << line << $/
184
196
  elsif mode == :text then
185
197
  ret << line << $/
@@ -200,7 +212,7 @@ module Literate
200
212
  text.split($/).each do |line|
201
213
  if mode == :text and line.match /^\\begin{lstlisting}/ then
202
214
  mode = :code
203
- indent_level_stack = [-@tabstop]
215
+ indent_level_stack = [-1]
204
216
  ret << line << $/
205
217
  elsif mode == :text then
206
218
  ret << line << $/
@@ -220,12 +232,14 @@ module Literate
220
232
  elsif line.match /\b(end)\b/ then
221
233
  current_indent_level = indent_level_stack.pop
222
234
  else
223
- current_indent_level = indent_level_stack[-1] + @tabstop
235
+ current_indent_level = indent_level_stack[-1] + 1
224
236
  end
225
237
  if line.match /^[ \t]*$/ then
226
238
  buffer << $/
239
+ elsif @expandtab then
240
+ buffer << ' ' * current_indent_level * @tabstop << line << $/
227
241
  else
228
- buffer << ' ' * current_indent_level << line << $/
242
+ buffer << '\t' * current_indent_level << line << $/
229
243
  end
230
244
  if line.match /^[ \t]*(begin|class|def|for|while|module)\b/
231
245
  indent_level_stack.push current_indent_level
@@ -1,5 +1,5 @@
1
1
  module Literate
2
2
  class Programming
3
- VERSION = "1.2.0"
3
+ VERSION = "1.3.0"
4
4
  end
5
5
  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.2.0
4
+ version: 1.3.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-23 00:00:00.000000000 Z
11
+ date: 2016-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler