guard-markdown 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +9 -3
- data/lib/guard/markdown.rb +11 -5
- data/lib/guard/markdown/templates/Guardfile +1 -0
- data/lib/guard/markdown/version.rb +1 -1
- data/spec/lib/guard/markdown_spec.rb +19 -0
- metadata +1 -1
data/README.md
CHANGED
@@ -37,14 +37,20 @@ The guard statement defines which guard your configuring and sets any optional p
|
|
37
37
|
|
38
38
|
The watch statement - ok, it may look a little intimidating. You'll need to know your regular expressions. But this is what it's doing.
|
39
39
|
|
40
|
-
watch (/source_dir\/(.+\/)*(.+\.)(md|markdown)/i) { |m| "source_dir/#{m[1]}#{m[2]}#{m[3]}|output_dir/#{m[1]}#{m[2]}html"}
|
40
|
+
watch (/source_dir\/(.+\/)*(.+\.)(md|markdown)/i) { |m| "source_dir/#{m[1]}#{m[2]}#{m[3]}|output_dir/#{m[1]}#{m[2]}html|optional_template.html.erb"}
|
41
41
|
|
42
|
-
^ ------ input file pattern ----------- ^ ^ ---- input file path -------- ^|^ ----- output file path
|
42
|
+
^ ------ input file pattern ----------- ^ ^ ---- input file path -------- ^|^ ----- output file path ---^|^ --- template path ---- ^
|
43
43
|
|
44
44
|
The "input file pattern" is a regular expression that is used to determine which files are watched by the guard. It'll be applied recursively to all files and folders starting in the current working directory.
|
45
45
|
|
46
|
-
Any matches are passed into the block and used to construct the conversion command. The conversion command is a string containing the path to the source file and the desired path to the output file separated by a "|"
|
46
|
+
Any matches are passed into the block and used to construct the conversion command. The conversion command is a string containing the path to the source file and the desired path to the output file separated by a "|".
|
47
|
+
You can also provide an optional template file. This file, if provided will be used by kramdown to wrap the converted output.
|
48
|
+
The template file is _typically_ an html file, and you define where the converted content will be placed by adding <%= @body %> in the desired location. e.g.
|
47
49
|
|
50
|
+
<div id = "main">
|
51
|
+
<%= @body %>
|
52
|
+
</div>
|
53
|
+
|
48
54
|
I hope that makes sense :)
|
49
55
|
|
50
56
|
|
data/lib/guard/markdown.rb
CHANGED
@@ -30,17 +30,23 @@ module Guard
|
|
30
30
|
# - for better testing
|
31
31
|
def run_on_change(paths)
|
32
32
|
paths.each do |path|
|
33
|
-
input, output = path.split("|")
|
34
|
-
|
33
|
+
input, output, template = path.split("|")
|
34
|
+
info = "#{input} >> #{output}"
|
35
|
+
info = "#{info} via #{template}" unless template.nil?
|
36
|
+
UI.info info
|
35
37
|
unless @options[:dry_run]
|
36
|
-
source = File.open(input,"rb").read
|
38
|
+
source = File.open(input,"rb").read
|
37
39
|
|
38
40
|
# make sure directory path exists
|
39
41
|
reg = /(.+\/).+\.\w+/i
|
40
42
|
target_path = output.gsub(reg,"\\1")
|
41
43
|
FileUtils.mkpath target_path unless target_path.empty?
|
42
|
-
|
43
|
-
|
44
|
+
|
45
|
+
kram_ops = { :input => "markdown" }
|
46
|
+
kram_ops.update({ :template => template }) unless template.nil?
|
47
|
+
|
48
|
+
doc = Kramdown::Document.new(source, kram_ops).to_html
|
49
|
+
|
44
50
|
|
45
51
|
File.open(output, "w") do |f|
|
46
52
|
f.write(doc)
|
@@ -2,4 +2,5 @@ guard 'markdown', :convert_on_start => true, :dry_run => true do
|
|
2
2
|
# See README for info on the watch statement below
|
3
3
|
# Will not convert while :dry_run is true. Once you're happy with your watch statements remove it
|
4
4
|
watch (/source_dir\/(.+\/)*(.+\.)(md|markdown)/i) { |m| "source_dir/#{m[1]}#{m[2]}#{m[3]}|output_dir/#{m[1]}#{m[2]}html"}
|
5
|
+
watch (/source_dir\/(.+\/)*(.+\.)(md|markdown)/i) { |m| "source_dir/#{m[1]}#{m[2]}#{m[3]}|output_dir/#{m[1]}#{m[2]}html|optional_template.html.erb"}
|
5
6
|
end
|
@@ -84,6 +84,25 @@ describe "Guard-Markdown" do
|
|
84
84
|
Guard::UI.should_receive(:info).exactly(3).times
|
85
85
|
@subject.run_on_change(@changed_paths)
|
86
86
|
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe "with a template file" do
|
90
|
+
it "should use the template when converting the source file" do
|
91
|
+
file_double = double()
|
92
|
+
file_double.should_receive(:read).and_return("#Title")
|
93
|
+
File.should_receive(:open).with("input.md","rb").and_return(file_double)
|
94
|
+
kram_doc = double()
|
95
|
+
kram_doc.should_receive(:to_html)
|
96
|
+
Kramdown::Document.should_receive(:new).with("#Title", :input => "markdown", :template => "template.html.erb").and_return(kram_doc)
|
97
|
+
|
98
|
+
file_out = double()
|
99
|
+
FileUtils.should_receive(:mkpath)
|
100
|
+
File.should_receive(:open).with("output.html", "w").and_return(file_out)
|
101
|
+
|
102
|
+
Guard::UI.should_receive(:info).with("input.md >> output.html via template.html.erb")
|
103
|
+
|
104
|
+
@subject.run_on_change(["input.md|output.html|template.html.erb"])
|
105
|
+
end
|
87
106
|
end
|
88
107
|
end
|
89
108
|
|