guard-markdown 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rvmrc
6
+
7
+ ## MAC OS
8
+ .DS_Store
9
+ .Trashes
10
+ .com.apple.timemachine.supported
11
+ .fseventsd
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in guard-krammer.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,18 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb})
6
+ watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # # Rails example
10
+ # watch('spec/spec_helper.rb') { "spec" }
11
+ # watch('config/routes.rb') { "spec/routing" }
12
+ # watch('app/controllers/application_controller.rb') { "spec/controllers" }
13
+ # watch(%r{^spec/.+_spec\.rb})
14
+ # watch(%r{^app/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
15
+ # watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
16
+ # watch(%r{^app/controllers/(.+)_(controller)\.rb}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
17
+
18
+ end
data/LICENSE ADDED
File without changes
data/README.md ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "guard/markdown/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "guard-markdown"
7
+ s.version = Guard::MarkdownVersion::VERSION
8
+ s.authors = ["Darren Wallace"]
9
+ s.email = ["wallace@midweekcrisis.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Markdown folder > html folder conversion}
12
+ s.description = %q{Watches a source folder and converts markdown docs to html docs in a target folder}
13
+
14
+ s.rubyforge_project = "guard-markdown"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ # s.files = Dir.glob('{lib}/**/*')
18
+ #s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ #s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'guard', '>= 0.2.2'
23
+ s.add_dependency 'kramdown', '~> 0.13.3'
24
+
25
+ s.add_development_dependency 'bundler', '~> 1.0'
26
+ s.add_development_dependency 'rspec', '~> 2.6'
27
+ s.add_development_dependency 'guard-rspec'
28
+ s.add_development_dependency 'growl'
29
+ end
@@ -0,0 +1,53 @@
1
+ require 'guard'
2
+ require 'guard/guard'
3
+ require 'guard/watcher'
4
+ require 'Kramdown'
5
+
6
+ module Guard
7
+ class Markdown < Guard
8
+ # Your code goes here...
9
+ def initialize(watchers=[], options={})
10
+ super
11
+ @options = {
12
+ :convert_on_start => true,
13
+ :dry_run => false
14
+ }.update(options)
15
+ end
16
+
17
+ def start
18
+ UI.info("Guard::Markdown has started watching your files")
19
+ run_all if @options[:convert_on_start]
20
+ end
21
+
22
+ def run_all
23
+ files = Dir.glob("**/*.*")
24
+ targets = Watcher.match_files(self, files)
25
+ run_on_change targets
26
+ end
27
+
28
+ # Called on file(s) modifications
29
+ # TODO - this method does far too much. Must refactor to allow
30
+ # - for better testing
31
+ def run_on_change(paths)
32
+ paths.each do |path|
33
+ input, output = path.split("|")
34
+ UI.info "#{input} >> #{output}"
35
+ unless @options[:dry_run]
36
+ source = File.open(input,"rb").read
37
+
38
+ # make sure directory path exists
39
+ reg = /(.+\/).+\.\w+/i
40
+ target_path = output.gsub(reg,"\\1")
41
+ FileUtils.mkpath target_path unless target_path.empty?
42
+
43
+ doc = Kramdown::Document.new(source, :input => "markdown").to_html
44
+
45
+ File.open(output, "w") do |f|
46
+ f.write(doc)
47
+ end
48
+ end
49
+ end
50
+ true
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ guard 'markdown', :convert_on_start => true, :dry_run => true do
2
+ # See README for info on the watch statement below
3
+ # Will not convert while :dry_run is true. Once you're happy with your watch statements remove it
4
+ watch (/source_dir\/(.+\/)*(.+\.)(md|markdown)/i) { |m| "source_dir/#{m[1]}#{m[2]}#{m[3]}|output_dir/#{m[1]}#{m[2]}html"}
5
+ end
@@ -0,0 +1,64 @@
1
+ # Guard::Markdown #
2
+ Markdown guard will watch your markdown documents for changes and convert them to lovely, semantic html. Yay!
3
+
4
+ ## Install ##
5
+
6
+ You're going to need [Guard](https://github.com/guard/guard) too.
7
+
8
+ Install with
9
+
10
+ $ gem install guard-markdown
11
+
12
+ Or add it to your Gemfile
13
+
14
+ gem 'guard-markdown'
15
+
16
+ ## Usage ##
17
+
18
+ Go see the [Guard usage doc](https://github.com/guard/guard#readme) for general instructions
19
+
20
+ ## The Guardfile - where the magic happens
21
+
22
+ The Guardfile is where you define your desired input and output paths.
23
+ Create it with:
24
+
25
+ $ guard init markdown
26
+
27
+ Then tweak the watch statements to your hearts content. It'll look a lot like this:
28
+
29
+ guard 'markdown', :convert_on_start => true, :dry_run => true do
30
+ watch (/source_dir\/(.+\/)*(.+\.)(md|markdown)/i) { |m| "source_dir/#{m[1]}#{m[2]}#{m[3]}|output_dir/#{m[1]}#{m[2]}html"}
31
+ end
32
+
33
+ The guard statement defines which guard your configuring and sets any optional parameters.
34
+
35
+ * :convert_on_start - if true will run all conversions when you start the guard. Defaults to true
36
+ * :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.
37
+
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
+
40
+ watch (/source_dir\/(.+\/)*(.+\.)(md|markdown)/i) { |m| "source_dir/#{m[1]}#{m[2]}#{m[3]}|output_dir/#{m[1]}#{m[2]}html"}
41
+
42
+ ^ ------ input file pattern ----------- ^ ^ ---- input file path -------- ^|^ ----- output file path ---^
43
+
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
+
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
+
48
+ I hope that makes sense :)
49
+
50
+
51
+
52
+ ## Have Fun ##
53
+
54
+ Go see the other [great guards available](https://github.com/guard/guard/wiki/List-of-available-Guards)
55
+
56
+ Oh yeah, I'm using [Kramdown](http://kramdown.rubyforge.org/) for the conversion engine. So if you want to know what markdown syntax it supports, [go here](http://kramdown.rubyforge.org/syntax.html)
57
+
58
+ # TODO #
59
+
60
+ * Simplify the required watch statement
61
+ * Seems a little wasteful to have to recreate the input path in the regexp. Must find a way around it.
62
+ * Allow the passing of Kramdown options into the guard
63
+ * Allow the conversion of more doc types using Kramdown
64
+
@@ -0,0 +1,5 @@
1
+ module Guard
2
+ module MarkdownVersion
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
@@ -0,0 +1,117 @@
1
+ require 'spec_helper'
2
+ require 'Kramdown'
3
+ require "guard/watcher"
4
+ require "guard/ui"
5
+
6
+ describe "Guard-Markdown" do
7
+
8
+ before(:each) do
9
+ @subject = Guard::Markdown.new
10
+ @input_paths = ["input1.md","input2.markdown","dir1/dir2/input3.md"]
11
+ @output_paths = ["output1.html", "output2.html", "dir1/dir2/output3.html"]
12
+ @changed_paths = []
13
+ @input_paths.each_index do |i|
14
+ @changed_paths << "#{@input_paths[i]}|#{@output_paths[i]}"
15
+ end
16
+ end
17
+
18
+ describe "initialize" do
19
+ it "should start with default options" do
20
+ @subject.options[:convert_on_start].should be true
21
+ @subject.options[:dry_run].should be false
22
+ end
23
+
24
+ it "should be possible to overwrite the default options" do
25
+ @subject = Guard::Markdown.new([],{
26
+ :convert_on_start => false,
27
+ :dry_run => true })
28
+ @subject.options[:convert_on_start].should be false
29
+ @subject.options[:dry_run].should be true
30
+ end
31
+ end
32
+
33
+ describe "start" do
34
+ it "should show a welcome message" do
35
+ Guard::UI.should_receive(:info).with("Guard::Markdown has started watching your files")
36
+ @subject.start
37
+ end
38
+
39
+ describe "convert_on_start" do
40
+ it "should run all conversions if convert_on_start is true" do
41
+ @subject.should_receive(:run_all)
42
+ @subject.start
43
+ end
44
+ it "should not convert on start if convert_on_start is false" do
45
+ @subject = Guard::Markdown.new([],{ :convert_on_start => false })
46
+ @subject.should_not_receive(:run_all)
47
+ @subject.start
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ describe "run_on_change" do
54
+ it "should read the changed files markdown and convert it to html " do
55
+ @input_paths.each_index do |i|
56
+ #mock file read
57
+ file_double = double()
58
+ file_double.should_receive(:read).and_return("#Title")
59
+ File.should_receive(:open).with(@input_paths[i],"rb").and_return(file_double)
60
+
61
+ mock_kramdown("#Title").should_receive(:to_html).and_return("<h1>Title</h1>")
62
+
63
+ #test info messages
64
+ Guard::UI.should_receive(:info).with("#{@input_paths[i]} >> #{@output_paths[i]}")
65
+
66
+ #mock file write
67
+ file_out = double()
68
+ target_path = @output_paths[i].gsub(/(.+\/).+\.\w+/i,"\\1")
69
+ FileUtils.should_receive(:mkpath).with(target_path)
70
+ File.should_receive(:open).with(@output_paths[i], "w").and_return(file_out)
71
+
72
+ #TODO Not sure how to test actually writing to the file
73
+
74
+ end
75
+ @subject.run_on_change(@changed_paths)
76
+ end
77
+
78
+ describe "dry run" do
79
+ it "should not permorm a conversion on a dry run" do
80
+ @subject = Guard::Markdown.new([],{ :dry_run=> true })
81
+ FileUtils.should_not_receive(:mkpath)
82
+ File.should_not_receive(:open)
83
+ Kramdown::Document.should_not_receive(:new)
84
+ Guard::UI.should_receive(:info).exactly(3).times
85
+ @subject.run_on_change(@changed_paths)
86
+ end
87
+ end
88
+ end
89
+
90
+ describe "run_all" do
91
+ it "should call run_on_change for all matching paths" do
92
+ #mock Guard.watcher
93
+ mock_watch = double()
94
+
95
+ #mock Dir
96
+ Dir.should_receive(:glob).with("**/*.*").and_return(@input_paths)
97
+
98
+ subject = Guard::Markdown.new(mock_watch)
99
+
100
+ #Guard::Watcher should handle the matching and path manipulation
101
+ ## TODO the following line throws an uninitilizd const error Guard::Guard::Watcher -> don't know why. It'll have to go untested for now
102
+ Guard::Watcher.should_receive(:match_files).with(subject, @input_paths).and_return(@changed_paths)
103
+
104
+ subject.should_receive(:run_on_change).with(@changed_paths)
105
+
106
+ subject.run_all
107
+ end
108
+ end
109
+ end
110
+
111
+ private
112
+
113
+ def mock_kramdown text
114
+ kram_doc = double()
115
+ Kramdown::Document.should_receive(:new).with(text, :input => "markdown").and_return(kram_doc)
116
+ kram_doc
117
+ end
@@ -0,0 +1,2 @@
1
+ require 'guard/markdown'
2
+ ENV["GUARD_ENV"] = 'test'
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guard-markdown
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.1
6
+ platform: ruby
7
+ authors:
8
+ - Darren Wallace
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-06 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: guard
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.2.2
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: kramdown
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 0.13.3
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ prerelease: false
40
+ requirement: &id003 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: "1.0"
46
+ type: :development
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ prerelease: false
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ~>
55
+ - !ruby/object:Gem::Version
56
+ version: "2.6"
57
+ type: :development
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: guard-rspec
61
+ prerelease: false
62
+ requirement: &id005 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ type: :development
69
+ version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: growl
72
+ prerelease: false
73
+ requirement: &id006 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ type: :development
80
+ version_requirements: *id006
81
+ description: Watches a source folder and converts markdown docs to html docs in a target folder
82
+ email:
83
+ - wallace@midweekcrisis.com
84
+ executables: []
85
+
86
+ extensions: []
87
+
88
+ extra_rdoc_files: []
89
+
90
+ files:
91
+ - .gitignore
92
+ - Gemfile
93
+ - Guardfile
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - guard-markdown.gemspec
98
+ - lib/guard/markdown.rb
99
+ - lib/guard/markdown/templates/Guardfile
100
+ - lib/guard/markdown/templates/README.md
101
+ - lib/guard/markdown/version.rb
102
+ - spec/lib/guard/markdown_spec.rb
103
+ - spec/spec_helper.rb
104
+ homepage: ""
105
+ licenses: []
106
+
107
+ post_install_message:
108
+ rdoc_options: []
109
+
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: "0"
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: "0"
124
+ requirements: []
125
+
126
+ rubyforge_project: guard-markdown
127
+ rubygems_version: 1.8.5
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: Markdown folder > html folder conversion
131
+ test_files: []
132
+