Skip to content

Commit

Permalink
Merge pull request #2 from stepheneb/kram_ops
Browse files Browse the repository at this point in the history
add support for passing additional kramdown options
  • Loading branch information
ddubyah committed Mar 3, 2012
2 parents 611ebe0 + f3fd813 commit eeefc12
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ The guard statement defines which guard your configuring and sets any optional p
* :convert_on_start - if true will run all conversions when you start the guard. Defaults to true
* :dry_run - if true won't actually run the conversion process, but it will output the files being watched and the file it would write to. Use it to tweak your watch statements and when you're happy set it to false.

If you want to pass additional options directly to kramdown add them as an additional options hash to `kram_ops`.

For example to generate a table of contents consisting of headers 2 through 6 first make sure that something like the following is in your markdown source file. This serves as a placeholder which will be replaced with the table of contents. See: [Automatic Table of Contents Generation](http://kramdown.rubyforge.org/converter/html.html#toc).

* table of contents
{:toc}

Then include the following in the start of your guard markdown block:

:kram_ops_ => { :toc_levels => [2, 3, 4, 5, 6]}

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.

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"}
Expand Down Expand Up @@ -65,6 +76,5 @@ Oh yeah, I'm using [Kramdown](http://kramdown.rubyforge.org/) for the conversion

* Simplify the required watch statement
* Seems a little wasteful to have to recreate the input path in the regexp. Must find a way around it.
* Allow the passing of Kramdown options into the guard
* Allow the conversion of more doc types using Kramdown

8 changes: 5 additions & 3 deletions lib/guard/markdown.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
module Guard
class Markdown < Guard
# Your code goes here...
attr_reader :kram_ops
def initialize(watchers=[], options={})
super
@options = {
:convert_on_start => true,
:dry_run => false
}.update(options)
@kram_ops = { :input => "kramdown", :output => "html" }
@kram_ops.update(@options[:kram_ops]) if @options[:kram_ops]
end

def start
Expand Down Expand Up @@ -42,10 +45,9 @@ def run_on_change(paths)
target_path = output.gsub(reg,"\\1")
FileUtils.mkpath target_path unless target_path.empty?

kram_ops = { :input => "kramdown", :output => "html" }
kram_ops.update({ :template => template }) unless template.nil?
@kram_ops.update({ :template => template }) unless template.nil?

doc = Kramdown::Document.new(source, kram_ops).to_html
doc = Kramdown::Document.new(source, @kram_ops).to_html


File.open(output, "w") do |f|
Expand Down
36 changes: 35 additions & 1 deletion spec/lib/guard/markdown_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@
@subject.options[:convert_on_start].should be false
@subject.options[:dry_run].should be true
end
it "should also start with default kramdown options" do
@subject.kram_ops[:input].should match "kramdown"
@subject.kram_ops[:output].should match "html"
@subject.kram_ops[:toc_levels].should be nil
end
it "should accept additional kramdown options" do
@subject = Guard::Markdown.new([],{
:kram_ops => { :toc_levels => [2, 3, 4, 5, 6] } })
@subject.kram_ops[:input].should match "kramdown"
@subject.kram_ops[:output].should match "html"
@subject.kram_ops[:toc_levels].should =~ [2, 3, 4, 5, 6]
end
end

describe "start" do
Expand Down Expand Up @@ -106,6 +118,28 @@
end
end

describe "with a template file and additional kramdown options" do
it "should use the additional kramdown options and the template when converting the source file" do
file_double = double()
file_double.should_receive(:read).and_return("#Title")
File.should_receive(:open).with("input.md","rb").and_return(file_double)
kram_doc = double()
kram_doc.should_receive(:to_html)

Kramdown::Document.should_receive(:new).with("#Title", :input => "kramdown", :output => "html",
:toc_levels => [2, 3, 4, 5, 6], :template => "template.html.erb").and_return(kram_doc)

file_out = double()
FileUtils.should_receive(:mkpath)
File.should_receive(:open).with("output.html", "w").and_return(file_out)

Guard::UI.should_receive(:info).with("input.md >> output.html via template.html.erb")

@subject = Guard::Markdown.new([],{ :kram_ops => { :toc_levels => [2, 3, 4, 5, 6] } })
@subject.run_on_change(["input.md|output.html|template.html.erb"])
end
end

describe "run_all" do
it "should call run_on_change for all matching paths" do
#mock Guard.watcher
Expand Down Expand Up @@ -133,4 +167,4 @@ def mock_kramdown text
kram_doc = double()
Kramdown::Document.should_receive(:new).with(text, :input => "kramdown", :output=> "html").and_return(kram_doc)
kram_doc
end
end

0 comments on commit eeefc12

Please sign in to comment.