md_inc 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +76 -0
- data/Rakefile +10 -0
- data/lib/md_inc/md_inc_commands.rb +58 -0
- data/lib/md_inc/version.rb +3 -0
- data/lib/md_inc.rb +24 -0
- data/md_inc.gemspec +17 -0
- data/spec/md_inc_spec.rb +65 -0
- data/spec/new_commands.rb +11 -0
- metadata +59 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# MdInc
|
2
|
+
|
3
|
+
MdInc is a text utility that allows you to suck text from one file into another.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'md_inc'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install md_inc
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
MdInc is a simple text inclusion filter intended for use
|
22
|
+
with markdown and similar text formatting utilities.
|
23
|
+
MdInc provides simple 'include this other file' kind
|
24
|
+
of processing. Using md_inc is straight forwark: Just require
|
25
|
+
it in and use the process method:
|
26
|
+
|
27
|
+
require 'md_inc'
|
28
|
+
|
29
|
+
v = MdInc::TextProcessor.new
|
30
|
+
output = v.process 'The quick brown fox'
|
31
|
+
|
32
|
+
In the simple case like the one above, md_inc simply
|
33
|
+
returns the text unchanged. The interesting bit is
|
34
|
+
when your input text includes commands that md_inc
|
35
|
+
recognizes. MdInc commands all start with a . in
|
36
|
+
the first column of a line. The most basic is
|
37
|
+
`.inc`. Here is some input that includes an `.inc`
|
38
|
+
command:
|
39
|
+
|
40
|
+
Here is the first line.
|
41
|
+
Now I'm going to include another file.
|
42
|
+
.inc 'some_other_file.md'
|
43
|
+
And the last line.
|
44
|
+
|
45
|
+
Run the file above through md_inc and the output
|
46
|
+
will include the contents of `some_other_file.md`
|
47
|
+
embedded in it.
|
48
|
+
|
49
|
+
You can also pluck out only part of the included
|
50
|
+
file based on a pair of regular expressions:
|
51
|
+
|
52
|
+
Here we are going to include only part of file1,
|
53
|
+
just the lines between START and END.
|
54
|
+
.between(/START/, /END/, inc('file1'))
|
55
|
+
Note that the lines matching START and END are
|
56
|
+
not included in the output.
|
57
|
+
|
58
|
+
And you can exclude lines based on a regular expression:
|
59
|
+
|
60
|
+
Pull in the contents of file1, skipping any
|
61
|
+
lines that contain DONTWANT
|
62
|
+
.skip(/DONTWANT/, inc('file1'))
|
63
|
+
|
64
|
+
As you can probably guess from this last example,
|
65
|
+
the MdInc dot commands are really just inline Ruby
|
66
|
+
code that gets executed during file processing.
|
67
|
+
Because of this it's easy to extend MdInc with
|
68
|
+
your own commands. See the specs for an example.
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
1. Fork it
|
73
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
74
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
75
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
76
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
module MdInc
|
2
|
+
module Commands
|
3
|
+
public :instance_eval
|
4
|
+
|
5
|
+
class << self
|
6
|
+
attr_accessor :root
|
7
|
+
|
8
|
+
def full_path(path)
|
9
|
+
@root ? File.join(@root, path) : path
|
10
|
+
end
|
11
|
+
|
12
|
+
def x(*args)
|
13
|
+
[]
|
14
|
+
end
|
15
|
+
|
16
|
+
def inc(path)
|
17
|
+
lines = File.readlines(full_path(path))
|
18
|
+
lines.map &:rstrip!
|
19
|
+
end
|
20
|
+
|
21
|
+
def code_inc(path, re1=nil, re2=nil)
|
22
|
+
if re1
|
23
|
+
code(between(re1, re2, inc(path)))
|
24
|
+
else
|
25
|
+
code(inc(path))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def code(lines)
|
30
|
+
lines.map {|l| l.rstrip.prepend(' ')}
|
31
|
+
end
|
32
|
+
|
33
|
+
def between(re1, re2, lines)
|
34
|
+
state = :outside
|
35
|
+
output = []
|
36
|
+
lines.each do |l|
|
37
|
+
if state == :outside && re1 =~ l
|
38
|
+
state = :inside
|
39
|
+
elsif state == :inside && re2 =~ l
|
40
|
+
state = :outside
|
41
|
+
else
|
42
|
+
output << l if state==:inside
|
43
|
+
end
|
44
|
+
end
|
45
|
+
STDERR.puts "Warning: no output from included file" if output.empty?
|
46
|
+
output
|
47
|
+
end
|
48
|
+
|
49
|
+
def skip(re, lines)
|
50
|
+
output = []
|
51
|
+
lines.each do |l|
|
52
|
+
output << l unless l =~ re
|
53
|
+
end
|
54
|
+
output
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/md_inc.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'md_inc/version'
|
2
|
+
require 'md_inc/md_inc_commands'
|
3
|
+
|
4
|
+
module MdInc
|
5
|
+
class TextProcessor
|
6
|
+
def process(content)
|
7
|
+
output = []
|
8
|
+
content.split("\n").each do |line|
|
9
|
+
if /^\./ =~ line
|
10
|
+
output << process_command(line)
|
11
|
+
else
|
12
|
+
output << line
|
13
|
+
end
|
14
|
+
end
|
15
|
+
out = output.flatten.join("\n")
|
16
|
+
out
|
17
|
+
end
|
18
|
+
|
19
|
+
def process_command(command_line)
|
20
|
+
cmd = command_line[1..-1]
|
21
|
+
Commands.instance_eval(cmd)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/md_inc.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/md_inc/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Russ Olsen"]
|
6
|
+
gem.email = ["russ@russolsen.com"]
|
7
|
+
gem.description = %q{MdInc is a simple text inclusion utility (it sucks in files) intended for use with markdown and similar utilities.}
|
8
|
+
gem.summary = %q{A simple text inclusion utility}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "md_inc"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = MdInc::VERSION
|
17
|
+
end
|
data/spec/md_inc_spec.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'md_inc'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
describe MdInc::TextProcessor do
|
5
|
+
let(:mdi) { MdInc::TextProcessor.new }
|
6
|
+
|
7
|
+
context 'basic processing' do
|
8
|
+
it 'copies input to output by default' do
|
9
|
+
text = "aaa\nbbb\nccc"
|
10
|
+
mdi.process(text).should == text
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'handles empty input' do
|
14
|
+
text = ''
|
15
|
+
mdi.process(text).should == text
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'can require in plain old ruby files' do
|
19
|
+
text = '.x require "set"'
|
20
|
+
mdi.process(text).should == ''
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'commands' do
|
25
|
+
before :each do
|
26
|
+
File.open("temp1",'w') {|f| f.puts("aaa\nbbb\n")}
|
27
|
+
|
28
|
+
File.open("temp2",'w') do |f|
|
29
|
+
1.upto(10) {|n| f.puts "line #{n}" }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
after :each do
|
34
|
+
FileUtils.rm_f("temp1")
|
35
|
+
FileUtils.rm_f("temp2")
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'can pull in the contents of another file with inc' do
|
39
|
+
text = "first\n.inc 'temp1'\nlast"
|
40
|
+
output = mdi.process(text)
|
41
|
+
output.should == "first\naaa\nbbb\nlast"
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'can do include just the lines between two matching lines' do
|
45
|
+
text = "first\n.between(/line 4/, /line 7/, inc('temp2') )\nlast"
|
46
|
+
output = mdi.process(text)
|
47
|
+
output.should == "first\nline 5\nline 6\nlast"
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'can skip lines matching a regular expression' do
|
51
|
+
text = "first\n.skip(/1|2/, inc('temp2'))\nlast"
|
52
|
+
output = mdi.process(text)
|
53
|
+
output.should_not match("line 1")
|
54
|
+
output.should_not match("line 2")
|
55
|
+
output.should match("line 3")
|
56
|
+
output.should match("line 4")
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'can allows for new commands to be added' do
|
60
|
+
text = ".x require 'new_commands'\nfirst\n.inc_up 'temp1'\nlast"
|
61
|
+
output = mdi.process(text)
|
62
|
+
output.should == "first\nAAA\nBBB\nlast"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# This is an example of what you need to do to
|
2
|
+
# Add a new command to MdInc: Just add a new
|
3
|
+
# module level method to the Vacuum::Commands
|
4
|
+
# module and off you go.
|
5
|
+
module MdInc
|
6
|
+
module Commands
|
7
|
+
def self.inc_up(path)
|
8
|
+
inc(path).map {|s| s.upcase!}
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: md_inc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Russ Olsen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-12 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: MdInc is a simple text inclusion utility (it sucks in files) intended
|
15
|
+
for use with markdown and similar utilities.
|
16
|
+
email:
|
17
|
+
- russ@russolsen.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/md_inc.rb
|
28
|
+
- lib/md_inc/md_inc_commands.rb
|
29
|
+
- lib/md_inc/version.rb
|
30
|
+
- md_inc.gemspec
|
31
|
+
- spec/md_inc_spec.rb
|
32
|
+
- spec/new_commands.rb
|
33
|
+
homepage: ''
|
34
|
+
licenses: []
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
requirements: []
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.8.10
|
54
|
+
signing_key:
|
55
|
+
specification_version: 3
|
56
|
+
summary: A simple text inclusion utility
|
57
|
+
test_files:
|
58
|
+
- spec/md_inc_spec.rb
|
59
|
+
- spec/new_commands.rb
|