md_inc 0.2.7 → 0.2.8

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.
@@ -1,7 +1,5 @@
1
1
  module MdInc
2
2
  module Commands
3
- public :instance_eval
4
-
5
3
  class << self
6
4
  def root(path)
7
5
  @root = path
@@ -11,13 +9,25 @@ module MdInc
11
9
  @root ? File.join(@root, path) : path
12
10
  end
13
11
 
12
+ def process(content)
13
+ output = process_lines(content.split("\n"))
14
+ output.flatten.join("\n")
15
+ end
16
+
17
+ def process_lines(lines)
18
+ lines.map do |line|
19
+ (line[0] == '.') ? instance_eval(line[1..-1]) : line
20
+ end
21
+ end
22
+
14
23
  def x(*args)
15
24
  []
16
25
  end
17
26
 
18
- def inc(path)
27
+ def inc(path, recursive=true)
19
28
  lines = File.readlines(full_path(path))
20
- lines.map &:rstrip!
29
+ lines.map! &:rstrip
30
+ recursive ? process_lines(lines) : lines
21
31
  end
22
32
 
23
33
  def code_inc(path, language=nil, re1=nil, re2=nil)
@@ -29,10 +39,10 @@ module MdInc
29
39
  end
30
40
 
31
41
  def code(language, lines)
32
- unless language.nil?
33
- ["```#{language}"] + lines + ["```"]
34
- else
42
+ if language.nil?
35
43
  lines.map {|l| l.rstrip.prepend(' ')}
44
+ else
45
+ ["```#{language}"] + lines + ["```"]
36
46
  end
37
47
  end
38
48
 
@@ -1,3 +1,3 @@
1
1
  module MdInc
2
- VERSION = "0.2.7"
2
+ VERSION = "0.2.8"
3
3
  end
data/lib/md_inc.rb CHANGED
@@ -3,30 +3,20 @@ require 'md_inc/md_inc_commands'
3
3
 
4
4
  module MdInc
5
5
  class TextProcessor
6
- def process_stream(s)
7
- process(s.read)
6
+ def root(path)
7
+ Commands.root(path)
8
8
  end
9
9
 
10
- def process_file(path)
11
- process(File.read(path))
10
+ def process(string)
11
+ Commands.process(string)
12
12
  end
13
13
 
14
- def process(content)
15
- output = []
16
- content.split("\n").each do |line|
17
- if /^\./ =~ line
18
- output << process_command(line)
19
- else
20
- output << line
21
- end
22
- end
23
- out = output.flatten.join("\n")
24
- out
14
+ def process_stream(stream)
15
+ Commands.process(stream.read)
25
16
  end
26
17
 
27
- def process_command(command_line)
28
- cmd = command_line[1..-1]
29
- Commands.instance_eval(cmd)
18
+ def process_file(path)
19
+ Commands.process(File.read(path))
30
20
  end
31
21
  end
32
22
  end
data/spec/md_inc_spec.rb CHANGED
@@ -1,6 +1,70 @@
1
1
  require 'md_inc'
2
2
  require 'fileutils'
3
3
 
4
+ describe MdInc::Commands do
5
+ context '#code' do
6
+ it 'uses git style tagging if a language is supplied' do
7
+ output = MdInc::Commands.code("java", %w{foo})
8
+ output.should == [ "```java", "foo", "```"]
9
+ end
10
+
11
+ it 'uses traditional code indenting if a language is not supplied' do
12
+ output = MdInc::Commands.code(nil, %w{foo})
13
+ output.should == [ " foo"]
14
+ end
15
+ end
16
+
17
+ context '#skip' do
18
+ let(:lines) { %w{foo skip1 skip2 bar skip3 baz skip4 skip5} }
19
+
20
+ it 'skips the lines that match the regular expression' do
21
+ output = MdInc::Commands.skip(/skip/, lines)
22
+ output.should == %w{foo bar baz}
23
+ end
24
+
25
+ it 'doesnt skip the lines that down match the regular expression' do
26
+ output = MdInc::Commands.skip(/no match/, lines)
27
+ output.should == lines
28
+ end
29
+ end
30
+
31
+ context '#between' do
32
+ let(:lines) { %w{aaa bbb ccc ddd eee} }
33
+
34
+ it 'returns the lines between the patterns, exclusive' do
35
+ output = MdInc::Commands.between(/aaa/, /ddd/, lines)
36
+ output.should == %w{bbb ccc}
37
+ end
38
+
39
+ it 'will skip the whole output if first re doesnt match' do
40
+ output = MdInc::Commands.between(/no match/, /ccc/, lines)
41
+ output.should == []
42
+ end
43
+ end
44
+
45
+ context '#normalize_indent' do
46
+ it 'does nothing to non-indented lines' do
47
+ lines = %w{aaa, bbb, ccc}
48
+ output = MdInc::Commands.normalize_indent(lines)
49
+ output.should == lines
50
+ end
51
+
52
+ it 'does nothing to lines with at least one non-indented line' do
53
+ lines = [' aaa', 'bbb', ' ccc']
54
+ output = MdInc::Commands.normalize_indent(lines)
55
+ output.should == lines
56
+ end
57
+
58
+ it 'unindents so that the least indented line has no indent' do
59
+ lines = [' aaa', ' bbb', ' ccc']
60
+ output = MdInc::Commands.normalize_indent(lines)
61
+ output.should == ['aaa', ' bbb', ' ccc']
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+
4
68
  describe MdInc::TextProcessor do
5
69
  let(:mdi) { MdInc::TextProcessor.new }
6
70
 
@@ -28,11 +92,18 @@ describe MdInc::TextProcessor do
28
92
  File.open("temp2",'w') do |f|
29
93
  1.upto(10) {|n| f.puts "line #{n}" }
30
94
  end
95
+
96
+ File.open("temp3",'w') do |f|
97
+ f.puts "temp3 line1"
98
+ f.puts ".inc 'temp1'"
99
+ f.puts "temp3 line3"
100
+ end
31
101
  end
32
102
 
33
103
  after :each do
34
104
  FileUtils.rm_f("temp1")
35
105
  FileUtils.rm_f("temp2")
106
+ FileUtils.rm_f("temp3")
36
107
  end
37
108
 
38
109
  it 'can pull in the contents of another file with inc' do
@@ -61,5 +132,11 @@ describe MdInc::TextProcessor do
61
132
  output = mdi.process(text)
62
133
  output.should == "first\nAAA\nBBB\nlast"
63
134
  end
135
+
136
+ it 'can do recursive inclusion' do
137
+ text = "first\n.inc 'temp3'\nlast"
138
+ output = mdi.process(text)
139
+ output.should == "first\ntemp3 line1\naaa\nbbb\ntemp3 line3\nlast"
140
+ end
64
141
  end
65
142
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: md_inc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-13 00:00:00.000000000 Z
12
+ date: 2013-02-14 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: MdInc is a simple text inclusion utility (it sucks in files) intended
15
15
  for use with markdown and similar utilities.