From 14ddfad2beb0fb38910dbfd7035372239ad754da Mon Sep 17 00:00:00 2001 From: Stephen Bannasch Date: Fri, 2 Mar 2012 00:23:35 -0500 Subject: [PATCH 1/2] add support for passing additional kramdown options --- README.md | 12 +++++++++- lib/guard/markdown.rb | 8 ++++--- spec/lib/guard/markdown_spec.rb | 42 ++++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 36eba1d..cb562f1 100644 --- a/README.md +++ b/README.md @@ -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 addition 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"} @@ -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 diff --git a/lib/guard/markdown.rb b/lib/guard/markdown.rb index 929711c..3bae14e 100644 --- a/lib/guard/markdown.rb +++ b/lib/guard/markdown.rb @@ -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 @@ -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| diff --git a/spec/lib/guard/markdown_spec.rb b/spec/lib/guard/markdown_spec.rb index d73b5b3..43013e5 100644 --- a/spec/lib/guard/markdown_spec.rb +++ b/spec/lib/guard/markdown_spec.rb @@ -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 @@ -106,6 +118,28 @@ end end + describe "with additional kram_ops" do + it "should use 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 @@ -133,4 +167,10 @@ 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 \ No newline at end of file +end + +def mock_kramdown_with_kram_ops text + kram_doc = double() + Kramdown::Document.should_receive(:new).with(text, :input => "kramdown", :output=> "html").and_return(kram_doc) + kram_doc +end From f3fd81361f5ff81a088839436eedb8d634573011 Mon Sep 17 00:00:00 2001 From: Stephen Bannasch Date: Fri, 2 Mar 2012 00:48:54 -0500 Subject: [PATCH 2/2] more descriptive spec test names ... clean up --- README.md | 2 +- spec/lib/guard/markdown_spec.rb | 10 ++-------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index cb562f1..232498b 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ 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 addition options hash to `kram_ops`. +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). diff --git a/spec/lib/guard/markdown_spec.rb b/spec/lib/guard/markdown_spec.rb index 43013e5..e303946 100644 --- a/spec/lib/guard/markdown_spec.rb +++ b/spec/lib/guard/markdown_spec.rb @@ -118,8 +118,8 @@ end end - describe "with additional kram_ops" do - it "should use the template when converting the source file" do + 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) @@ -168,9 +168,3 @@ def mock_kramdown text Kramdown::Document.should_receive(:new).with(text, :input => "kramdown", :output=> "html").and_return(kram_doc) kram_doc end - -def mock_kramdown_with_kram_ops text - kram_doc = double() - Kramdown::Document.should_receive(:new).with(text, :input => "kramdown", :output=> "html").and_return(kram_doc) - kram_doc -end