markdown 0.4.0 → 0.5.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,9 @@ README.markdown
4
4
  Rakefile
5
5
  bin/markdown
6
6
  lib/markdown.rb
7
+ lib/markdown/cli/gen.rb
8
+ lib/markdown/cli/opts.rb
9
+ lib/markdown/cli/runner.rb
7
10
  lib/markdown/config.rb
8
11
  lib/markdown/engines/bluecloth.rb
9
12
  lib/markdown/engines/kramdown.rb
@@ -12,6 +15,6 @@ lib/markdown/engines/pandoc_ruby.rb
12
15
  lib/markdown/engines/rdiscount.rb
13
16
  lib/markdown/engines/redcarpet.rb
14
17
  lib/markdown/engines/rpeg_markdown.rb
15
- lib/markdown/gen.rb
18
+ lib/markdown/version.rb
16
19
  lib/markdown/wrapper.rb
17
20
  test/test_kramdown.rb
@@ -1,71 +1,145 @@
1
- # Markdown Engine Wrapper - Use Your Markdown Library of Choice
1
+ # Markdown Engine Wrapper - Use Your Markdown Library of Choice in Ruby
2
2
 
3
3
  * [geraldb.github.com/markdown](http://geraldb.github.com/markdown)
4
4
 
5
- ## Description
6
-
7
5
  The Markdown Engine Wrapper (`markdown`) Ruby gem lets you use
8
- your markdown library of choice. Preconfigured markdown libraries include:
6
+ your markdown library of choice. Preconfigured markdown libraries include
9
7
 
10
8
  * `kramdown`
9
+ * `redcarpet`
11
10
  * `bluecloth`
12
11
  * `maruku`
13
12
  * `rpeg-markdown`
14
13
  * `rdiscount`
15
14
  * `pandoc-ruby`
16
15
 
17
-
18
- ## Usage
16
+ ## Usage - Ruby Code
19
17
 
20
18
  require 'markdown'
21
19
 
22
20
  Markdown.new( 'Hello World' ).to_html
21
+
22
+ # => "<p>Hello World</p>\n"
23
23
 
24
24
 
25
- ## Install
25
+ ## Usage - Command Line
26
26
 
27
- Just install the gem:
27
+ The `markdown` gem includes a little command line tool. Try `markdown -h` for details:
28
28
 
29
- $ gem install markdown
29
+ ```
30
+ markdown - Lets you convert plain text documents to hypertext with your Markdown engine of choice
31
+ and preprocessing text filters.
30
32
 
33
+ Usage: markdown [options] files_or_dirs
34
+ -o, --output PATH Output Path
35
+ -v, --verbose Show debug trace
31
36
 
32
- ## Configuration - Markdown Engine Loading Order
33
37
 
34
- The default fallback loading order is:
38
+ Examples:
39
+ markdown # Process all documents in working folder (that is, .)
40
+ markdown quickref # Process document or folder using Markdown
41
+ markdown quickref.text # Process document using Markdown
42
+ markdown -o site quickref # Output documents to site folder
43
+ ```
35
44
 
36
- `pandoc-ruby`, `kramdown`, `bluecloth`, `maruku`, `rpeg-markdown`, `rdiscount`
45
+ ## Configuration - Markdown Engine Loading Order
37
46
 
38
- To use your markdown engine of choice configure the wrapper. The wrapper
39
- uses the following lookup order to find the markdown engine to configure:
47
+ The default (fallback) Markdown library is `kramdown`. To use your markdown engine of choice
48
+ configure the wrapper. The wrapper
49
+ uses the following lookup order to find the markdown engine:
40
50
 
41
- 1) `MARKDOWN_LIB` environment variable
51
+ ### 1) `MARKDOWN_LIB` Environment Variable
42
52
 
43
53
  Example:
44
54
 
45
55
  set MARKDOWN_LIB=kramdown
46
56
 
47
- 2) `lib` property (in `./markdown.yml` or `~/markdown.yml`)
57
+ ### 2) `lib` Property (in `./markdown.yml` or `~/markdown.yml`)
48
58
 
49
59
  Example:
50
60
 
51
61
  lib: kramdown
52
62
 
53
- 3) `libs` property (in `./markdown.yml` or `~/markdown.yml`) - first-come first-serve markdown engine loading list (defaults to builtin list).
63
+ ### 3) `libs` Property (in `./markdown.yml` or `~/markdown.yml`)
54
64
 
55
65
  Example:
56
66
 
57
67
  libs:
58
- - rdiscount
68
+ - redcarpet
59
69
  - kramdown
60
70
 
61
71
 
62
- ## Converters
72
+ Markdown libraries in the list get loaded on a first-come first-serve principle,
73
+ that is, the first library `require`'d successfully will get used.
63
74
 
64
- The Markdown Wrapper lets you configure different converter methods for each markdown engine. By default
65
- the converter method *<lib>*_to_html gets used
66
- (for example, the default converter for `kramdown` is `kramdown_to_html`).
67
75
 
68
- ...
76
+
77
+ ## Configuration - Markdown Engine Options
78
+
79
+ You can also pass along options to your Markdown library. Example:
80
+
81
+ ## Let's use the Redcarpet library
82
+
83
+ lib: redcarpet
84
+
85
+ redcarpet:
86
+ extensions:
87
+ - no_intra_emphasis
88
+ - fenced_code_blocks
89
+ - tables
90
+ - strikethrough
91
+
92
+
93
+ ## Configuration -Filters (Command Line Only)
94
+
95
+ For the command line tool only you can configure preprocessing filters to
96
+ allow comments, Ruby helpers, and much more. Example:
97
+
98
+ ## Let's use percent style comments
99
+
100
+ filters:
101
+ - comments-percent-style
102
+
103
+ Now the filter will strip comment lines starting with percent (that is, %). Example:
104
+
105
+ %%%%%%%%%%%%%%%%
106
+ % Some Headers
107
+
108
+ Title: Web Services REST-Style: Universal Identifiers, Formats & Protocols
109
+
110
+ Becomes
111
+
112
+ Title: Web Services REST-Style: Universal Identifiers, Formats & Protocols
113
+
114
+ before the text gets passed along to the markdown engine. The filter
115
+ also supports multiline comments with `%begin`|`comment`|`comments`/`%end` pairs. Example:
116
+
117
+ %begin
118
+ Using modern browser such as Firefox, Chrome and Safari you can
119
+ now theme your slide shows using using "loss-free" vector graphics
120
+ in plain old CSS. Thanks to gradient support in backgrounds in CSS3.
121
+ %end
122
+
123
+ or
124
+
125
+ %comment
126
+ Using modern browser such as Firefox, Chrome and Safari you can
127
+ now theme your slide shows using using "loss-free" vector graphics
128
+ in plain old CSS. Thanks to gradient support in backgrounds in CSS3.
129
+ %end
130
+
131
+ Note: As a shortcut using a single `%end` directive (that is, without a leading `%begin`)
132
+ will skip everything until the end of the document.
133
+
134
+ For more about filters see the [`textutils`](http://geraldb.github.com/textutils) gem.
135
+
136
+
137
+ ## Configuration - Converters
138
+
139
+ The Markdown wrapper lets you configure different converter methods
140
+ for each markdown engine. By default
141
+ the converter method `<lib>_to_html` gets used
142
+ (for example, the default converter for `kramdown` is `kramdown_to_html`).
69
143
 
70
144
  Example:
71
145
 
@@ -73,19 +147,24 @@ Example:
73
147
  converter: pandoc-ruby-to-s5
74
148
 
75
149
 
150
+ ## Install
151
+
152
+ Just install the gem:
153
+
154
+ $ gem install markdown
155
+
76
156
 
77
157
  ## Real World Usage
78
158
 
79
159
  The [`slideshow`](http://slideshow.rubyforge.org) (also known as Slide Show (S9)) gem
80
160
  that lets you create slide shows
81
- and author slides in plain text using a wiki-style markup language that's easy-to-write and easy-to-read
82
- ships with the `markdown` gem.
161
+ and author slides in plain text using a wiki-style markup language that's easy-to-write and easy-to-read.
83
162
 
84
163
 
85
164
  ## Alternatives
86
165
 
87
- [`multi_markdown`](https://github.com/postmodern/multi_markdown) gem by Hal Brodigan
88
-
166
+ * [`multi_markdown`](https://github.com/postmodern/multi_markdown) gem by Hal Brodigan (aka postmodern)
167
+ * [`markdown_meta`](https://github.com/headius/markdown_meta) gem by Charles Oliver Nutter (aka headius)
89
168
 
90
169
  ## Questions? Comments?
91
170
 
data/Rakefile CHANGED
@@ -1,5 +1,5 @@
1
1
  require 'hoe'
2
- require './lib/markdown.rb'
2
+ require './lib/markdown/version.rb'
3
3
 
4
4
  Hoe.spec 'markdown' do
5
5
 
@@ -28,6 +28,7 @@ require 'textutils' # text filters and helpers
28
28
 
29
29
  # our own code
30
30
 
31
+ require 'markdown/version'
31
32
  require 'markdown/config'
32
33
  require 'markdown/engines/bluecloth'
33
34
  require 'markdown/engines/kramdown'
@@ -37,16 +38,17 @@ require 'markdown/engines/rdiscount'
37
38
  require 'markdown/engines/redcarpet'
38
39
  require 'markdown/engines/rpeg_markdown'
39
40
  require 'markdown/wrapper'
40
- require 'markdown/gen'
41
41
 
42
+ require 'markdown/cli/gen'
43
+ require 'markdown/cli/opts'
44
+ require 'markdown/cli/runner'
42
45
 
43
- module Markdown
44
46
 
45
- VERSION = '0.4.0'
47
+ module Markdown
46
48
 
47
49
  # version string for generator meta tag (includes ruby version)
48
50
  def self.banner
49
- "Markdown #{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
51
+ "markdown #{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
50
52
  end
51
53
 
52
54
  def self.main
@@ -59,7 +61,7 @@ module Markdown
59
61
  args += markdownopt.split if markdownopt
60
62
  args += ARGV.dup
61
63
 
62
- Gen.new.run(args)
64
+ Runner.new.run(args)
63
65
  end
64
66
 
65
67
  end # module Markdown
@@ -0,0 +1,89 @@
1
+ module Markdown
2
+
3
+ class Gen
4
+
5
+ include TextUtils::Filter # include filters such as comments_percent_style, etc. (see textutils gem)
6
+
7
+ attr_reader :logger
8
+ attr_reader :opts
9
+
10
+ def initialize( logger, opts )
11
+ @logger = logger
12
+ @opts = opts
13
+ end
14
+
15
+ def create_doc( fn )
16
+ dirname = File.dirname( fn )
17
+ basename = File.basename( fn, '.*' )
18
+ extname = File.extname( fn )
19
+
20
+ logger.debug "dirname=#{dirname}, basename=#{basename}, extname=#{extname}"
21
+
22
+ if opts.output_path == '.'
23
+ # expand output path in current dir
24
+ outpath = File.expand_path( dirname )
25
+ else
26
+ # expand output path in user supplied dir and make sure output path exists
27
+ outpath = File.expand_path( opts.output_path )
28
+ FileUtils.makedirs( outpath ) unless File.directory? outpath
29
+ end
30
+ logger.debug "outpath=#{outpath}"
31
+
32
+ # todo: add a -c option to commandline? to let you set cwd?
33
+
34
+
35
+ # change working dir to sourcefile dir (that is, dirname); push working folder/dir
36
+ newcwd = File.expand_path( dirname )
37
+ oldcwd = File.expand_path( Dir.pwd )
38
+
39
+ unless newcwd == oldcwd
40
+ logger.debug "oldcwd=>#{oldcwd}<, newcwd=>#{newcwd}<"
41
+ Dir.chdir( newcwd )
42
+ end
43
+
44
+ inname = "#{basename}#{extname}"
45
+ outname = "#{basename}.html"
46
+
47
+ logger.debug "inname=#{inname}, outname=#{outname}"
48
+
49
+ puts "*** #{inname} (#{dirname}) => #{outname} (#{(opts.output_path == '.') ? dirname : opts.output_path})..."
50
+
51
+ content = File.read( inname )
52
+
53
+ # step 1) run (optional) preprocessing text filters
54
+ Markdown.filters.each do |filter|
55
+ mn = filter.tr( '-', '_' ).to_sym # construct method name (mn)
56
+ content = send( mn, content ) # call filter e.g. include_helper_hack( content )
57
+ end
58
+
59
+ # step 2) convert light-weight markup to hypertext
60
+ content = Markdown.new( content ).to_html
61
+
62
+
63
+ ## todo: add Markdown.lib_options inspect/dump to banner
64
+
65
+ banner =<<EOS
66
+ <!-- ======================================================================
67
+ generated by #{Markdown.banner}
68
+ on #{Time.now} with Markdown engine '#{Markdown.lib}'
69
+ ====================================================================== -->
70
+ EOS
71
+
72
+ out = File.new( File.join( outpath, outname ), "w+" )
73
+ #### out << banner
74
+ out << content
75
+ out.flush
76
+ out.close
77
+
78
+ ## pop/restore working folder/dir
79
+ unless newcwd == oldcwd
80
+ logger.debug "oldcwd=>#{oldcwd}<, newcwd=>#{newcwd}<"
81
+ Dir.chdir( oldcwd )
82
+ end
83
+
84
+ end # method create_doc
85
+
86
+
87
+
88
+ end # class Gen
89
+ end # module Markdown
@@ -0,0 +1,14 @@
1
+ module Markdown
2
+
3
+ class Opts
4
+
5
+ def output_path=(value)
6
+ @output_path = value
7
+ end
8
+
9
+ def output_path
10
+ @output_path || '.'
11
+ end
12
+
13
+ end # class Opts
14
+ end # module Markdown
@@ -0,0 +1,173 @@
1
+ module Markdown
2
+
3
+ class Runner
4
+
5
+ attr_reader :logger
6
+ attr_reader :opts
7
+
8
+ def initialize
9
+ @logger = Logger.new(STDOUT)
10
+ @logger.level = Logger::INFO
11
+ @opts = Opts.new
12
+ end
13
+
14
+ def has_markdown_extension?( fn )
15
+ dirname = File.dirname( fn )
16
+ basename = File.basename( fn, '.*' )
17
+ extname = File.extname( fn )
18
+ logger.debug "dirname=#{dirname}, basename=#{basename}, extname=#{extname}"
19
+
20
+ return false if extname.empty? # no extension
21
+
22
+ Markdown.extnames.include?( extname.downcase )
23
+ end
24
+
25
+ def find_file_with_markdown_extension( fn )
26
+ dirname = File.dirname( fn )
27
+ basename = File.basename( fn, '.*' )
28
+ extname = File.extname( fn )
29
+ logger.debug "dirname=#{dirname}, basename=#{basename}, extname=#{extname}"
30
+
31
+ Markdown.extnames.each do |e|
32
+ newname = File.join( dirname, "#{basename}#{e}" )
33
+ logger.debug "File.exists? #{newname}"
34
+ return newname if File.exists?( newname )
35
+ end # each extension (e)
36
+
37
+ nil # not found; return nil
38
+ end
39
+
40
+
41
+ def find_files( file_or_dir_or_pattern )
42
+
43
+ filtered_files = []
44
+
45
+ # assume pattern if includes * or ? or {} or []
46
+ if file_or_dir_or_pattern =~ /[*?{}\[\]]/
47
+ puts "searching glob pattern '#{file_or_dir_or_pattern}'..."
48
+ Dir.glob( file_or_dir_or_pattern ).each do |file|
49
+ if File.directory?( file ) # skip (sub)directories
50
+ puts " skipping folder '#{file}'..."
51
+ next
52
+ else
53
+ if has_markdown_extension?( file )
54
+ logger.debug " adding file '#{file}'..."
55
+ filtered_files << file
56
+ else
57
+ puts " skipping file '#{file}'..."
58
+ end
59
+ end
60
+ end
61
+ elsif File.directory?(file_or_dir_or_pattern)
62
+ puts "searching folder '#{file_or_dir_or_pattern}'..."
63
+ Dir.entries( file_or_dir_or_pattern ).each do |entry|
64
+ next if entry == '.' || entry == '..' # silently skip current and up dirs
65
+
66
+ if file_or_dir_or_pattern == '.'
67
+ file = entry
68
+ else # add dir (if not working dir)
69
+ file = File.join( file_or_dir_or_pattern, entry )
70
+ end
71
+
72
+ if File.directory?( file ) # skip (sub)directories
73
+ puts " skipping folder '#{file}'..."
74
+ next
75
+ else
76
+ if has_markdown_extension?( file )
77
+ logger.debug " adding file '#{file}'..."
78
+ filtered_files << file
79
+ else
80
+ puts " skipping file '#{file}'..."
81
+ end
82
+ end
83
+ end
84
+ else # assume it's a single file (check for missing extension)
85
+ if File.exists?( file_or_dir_or_pattern )
86
+ file = file_or_dir_or_pattern
87
+ if has_markdown_extension?( file )
88
+ logger.debug " adding file '#{file}'..."
89
+ filtered_files << file
90
+ else
91
+ puts " skipping file '#{file}'..."
92
+ end
93
+ else # check for existing file w/ missing extension
94
+ file = find_file_with_markdown_extension( file_or_dir_or_pattern )
95
+ if file.nil?
96
+ puts " skipping missing file '#{file_or_dir_or_pattern}{#{Markdown.extnames.join(',')}}'..."
97
+ else
98
+ logger.debug " adding file '#{file}'..."
99
+ filtered_files << file
100
+ end
101
+ end
102
+ end
103
+
104
+ filtered_files
105
+ end # find_files
106
+
107
+
108
+ def run( args )
109
+ opt=OptionParser.new do |cmd|
110
+
111
+ cmd.banner = "Usage: markdown [options] files_or_dirs"
112
+
113
+ cmd.on( '-o', '--output PATH', "Output Path (default is #{opts.output_path})" ) { |path| opts.output_path = path }
114
+
115
+ cmd.on( '-v', '--version', "Show version" ) do
116
+ puts Markdown.banner
117
+ exit
118
+ end
119
+
120
+ cmd.on( "--verbose", "Show debug trace" ) do
121
+ logger.datetime_format = "%H:%H:%S"
122
+ logger.level = Logger::DEBUG
123
+ end
124
+
125
+ ## todo: add markdown.lib options (e.g. extensions,etc)
126
+
127
+ cmd.on_tail( "-h", "--help", "Show this message" ) do
128
+ puts <<EOS
129
+
130
+ markdown - Lets you convert plain text documents (#{Markdown.extnames.join(', ')}) to hypertext (.html) with your Markdown engine of choice (#{Markdown.lib}) and preprocessing text filters (#{Markdown.filters.join(', ')}).
131
+
132
+ #{cmd.help}
133
+
134
+ Examples:
135
+ markdown # Process all documents in working folder (that is, .)
136
+ markdown quickref # Process document or folder using Markdown
137
+ markdown quickref.text # Process document using Markdown
138
+ markdown -o site quickref # Output documents to site folder
139
+
140
+ Further information:
141
+ http://geraldb.github.com/markdown
142
+
143
+ EOS
144
+ exit
145
+ end
146
+ end
147
+
148
+ opt.parse!( args )
149
+
150
+ puts Markdown.banner
151
+
152
+ # force loading of config
153
+ Markdown.lib
154
+
155
+ logger.debug "args.length: #{args.length}"
156
+ logger.debug "args: >#{args.join(',')}<"
157
+
158
+ # if no file args given; default to working folder (that is, .)
159
+ args = ['.'] if args.length == 0
160
+
161
+ args.each do |arg|
162
+ files = find_files( arg )
163
+ files.each do |file|
164
+ Gen.new( logger, opts ).create_doc( file )
165
+ end
166
+ end
167
+
168
+ puts "Done."
169
+
170
+ end # method run
171
+
172
+ end # class Runner
173
+ end # module Markdown
@@ -0,0 +1,3 @@
1
+ module Markdown
2
+ VERSION = '0.5.0.beta1'
3
+ end
metadata CHANGED
@@ -1,13 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markdown
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
5
- prerelease:
4
+ hash: 123249068
5
+ prerelease: 6
6
6
  segments:
7
7
  - 0
8
- - 4
8
+ - 5
9
9
  - 0
10
- version: 0.4.0
10
+ - beta
11
+ - 1
12
+ version: 0.5.0.beta1
11
13
  platform: ruby
12
14
  authors:
13
15
  - Gerald Bauer
@@ -15,7 +17,7 @@ autorequire:
15
17
  bindir: bin
16
18
  cert_chain: []
17
19
 
18
- date: 2012-06-10 00:00:00 Z
20
+ date: 2012-06-18 00:00:00 Z
19
21
  dependencies:
20
22
  - !ruby/object:Gem::Dependency
21
23
  name: props
@@ -110,6 +112,9 @@ files:
110
112
  - Rakefile
111
113
  - bin/markdown
112
114
  - lib/markdown.rb
115
+ - lib/markdown/cli/gen.rb
116
+ - lib/markdown/cli/opts.rb
117
+ - lib/markdown/cli/runner.rb
113
118
  - lib/markdown/config.rb
114
119
  - lib/markdown/engines/bluecloth.rb
115
120
  - lib/markdown/engines/kramdown.rb
@@ -118,7 +123,7 @@ files:
118
123
  - lib/markdown/engines/rdiscount.rb
119
124
  - lib/markdown/engines/redcarpet.rb
120
125
  - lib/markdown/engines/rpeg_markdown.rb
121
- - lib/markdown/gen.rb
126
+ - lib/markdown/version.rb
122
127
  - lib/markdown/wrapper.rb
123
128
  - test/test_kramdown.rb
124
129
  - .gemtest
@@ -143,12 +148,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
148
  required_rubygems_version: !ruby/object:Gem::Requirement
144
149
  none: false
145
150
  requirements:
146
- - - ">="
151
+ - - ">"
147
152
  - !ruby/object:Gem::Version
148
- hash: 3
153
+ hash: 25
149
154
  segments:
150
- - 0
151
- version: "0"
155
+ - 1
156
+ - 3
157
+ - 1
158
+ version: 1.3.1
152
159
  requirements: []
153
160
 
154
161
  rubyforge_project: markdown
@@ -1,260 +0,0 @@
1
- module Markdown
2
-
3
-
4
- class Opts
5
-
6
- def output_path=(value)
7
- @output_path = value
8
- end
9
-
10
- def output_path
11
- @output_path ||= '.'
12
- end
13
-
14
- end # class Opts
15
-
16
-
17
- class Gen
18
-
19
- include TextUtils::Filter # include filters such as comments_percent_style, etc. (see textutils gem)
20
-
21
- attr_reader :logger
22
- attr_reader :opts
23
-
24
- def initialize
25
- @logger = Logger.new(STDOUT)
26
- @logger.level = Logger::INFO
27
- @opts = Opts.new
28
- end
29
-
30
- def create_doc( fn )
31
- dirname = File.dirname( fn )
32
- basename = File.basename( fn, '.*' )
33
- extname = File.extname( fn )
34
-
35
- logger.debug "dirname=#{dirname}, basename=#{basename}, extname=#{extname}"
36
-
37
- if opts.output_path == '.'
38
- # expand output path in current dir
39
- outpath = File.expand_path( dirname )
40
- else
41
- # expand output path in user supplied dir and make sure output path exists
42
- outpath = File.expand_path( opts.output_path )
43
- FileUtils.makedirs( outpath ) unless File.directory? outpath
44
- end
45
- logger.debug "outpath=#{outpath}"
46
-
47
- # todo: add a -c option to commandline? to let you set cwd?
48
-
49
-
50
- # change working dir to sourcefile dir (that is, dirname); push working folder/dir
51
- newcwd = File.expand_path( dirname )
52
- oldcwd = File.expand_path( Dir.pwd )
53
-
54
- unless newcwd == oldcwd
55
- logger.debug "oldcwd=>#{oldcwd}<, newcwd=>#{newcwd}<"
56
- Dir.chdir( newcwd )
57
- end
58
-
59
- inname = "#{basename}#{extname}"
60
- outname = "#{basename}.html"
61
-
62
- logger.debug "inname=#{inname}, outname=#{outname}"
63
-
64
- puts "*** #{inname} (#{dirname}) => #{outname} (#{(opts.output_path == '.') ? dirname : opts.output_path})..."
65
-
66
- content = File.read( inname )
67
-
68
- # step 1) run (optional) preprocessing text filters
69
- Markdown.filters.each do |filter|
70
- mn = filter.tr( '-', '_' ).to_sym # construct method name (mn)
71
- content = send( mn, content ) # call filter e.g. include_helper_hack( content )
72
- end
73
-
74
- # step 2) convert light-weight markup to hypertext
75
- content = Markdown.new( content ).to_html
76
-
77
-
78
- ## todo: add Markdown.lib_options inspect/dump to banner
79
-
80
- banner =<<EOS
81
- <!-- ======================================================================
82
- generated by #{Markdown.banner}
83
- on #{Time.now} with Markdown engine '#{Markdown.lib}'
84
- ====================================================================== -->
85
- EOS
86
-
87
- out = File.new( File.join( outpath, outname ), "w+" )
88
- #### out << banner
89
- out << content
90
- out.flush
91
- out.close
92
-
93
- ## pop/restore working folder/dir
94
- unless newcwd == oldcwd
95
- logger.debug "oldcwd=>#{oldcwd}<, newcwd=>#{newcwd}<"
96
- Dir.chdir( oldcwd )
97
- end
98
-
99
- end # method create_doc
100
-
101
-
102
- def has_markdown_extension?( fn )
103
- dirname = File.dirname( fn )
104
- basename = File.basename( fn, '.*' )
105
- extname = File.extname( fn )
106
- logger.debug "dirname=#{dirname}, basename=#{basename}, extname=#{extname}"
107
-
108
- return false if extname.empty? # no extension
109
-
110
- Markdown.extnames.include?( extname.downcase )
111
- end
112
-
113
- def find_file_with_markdown_extension( fn )
114
- dirname = File.dirname( fn )
115
- basename = File.basename( fn, '.*' )
116
- extname = File.extname( fn )
117
- logger.debug "dirname=#{dirname}, basename=#{basename}, extname=#{extname}"
118
-
119
- Markdown.extnames.each do |e|
120
- newname = File.join( dirname, "#{basename}#{e}" )
121
- logger.debug "File.exists? #{newname}"
122
- return newname if File.exists?( newname )
123
- end # each extension (e)
124
-
125
- nil # not found; return nil
126
- end
127
-
128
-
129
- def find_files( file_or_dir_or_pattern )
130
-
131
- filtered_files = []
132
-
133
- # assume pattern if includes * or ? or {} or []
134
- if file_or_dir_or_pattern =~ /[*?{}\[\]]/
135
- puts "searching glob pattern '#{file_or_dir_or_pattern}'..."
136
- Dir.glob( file_or_dir_or_pattern ).each do |file|
137
- if File.directory?( file ) # skip (sub)directories
138
- puts " skipping folder '#{file}'..."
139
- next
140
- else
141
- if has_markdown_extension?( file )
142
- logger.debug " adding file '#{file}'..."
143
- filtered_files << file
144
- else
145
- puts " skipping file '#{file}'..."
146
- end
147
- end
148
- end
149
- elsif File.directory?(file_or_dir_or_pattern)
150
- puts "searching folder '#{file_or_dir_or_pattern}'..."
151
- Dir.entries( file_or_dir_or_pattern ).each do |entry|
152
- next if entry == '.' || entry == '..' # silently skip current and up dirs
153
-
154
- if file_or_dir_or_pattern == '.'
155
- file = entry
156
- else # add dir (if not working dir)
157
- file = File.join( file_or_dir_or_pattern, entry )
158
- end
159
-
160
- if File.directory?( file ) # skip (sub)directories
161
- puts " skipping folder '#{file}'..."
162
- next
163
- else
164
- if has_markdown_extension?( file )
165
- logger.debug " adding file '#{file}'..."
166
- filtered_files << file
167
- else
168
- puts " skipping file '#{file}'..."
169
- end
170
- end
171
- end
172
- else # assume it's a single file (check for missing extension)
173
- if File.exists?( file_or_dir_or_pattern )
174
- file = file_or_dir_or_pattern
175
- if has_markdown_extension?( file )
176
- logger.debug " adding file '#{file}'..."
177
- filtered_files << file
178
- else
179
- puts " skipping file '#{file}'..."
180
- end
181
- else # check for existing file w/ missing extension
182
- file = find_file_with_markdown_extension( file_or_dir_or_pattern )
183
- if file.nil?
184
- puts " skipping missing file '#{file_or_dir_or_pattern}{#{Markdown.extnames.join(',')}}'..."
185
- else
186
- logger.debug " adding file '#{file}'..."
187
- filtered_files << file
188
- end
189
- end
190
- end
191
-
192
- filtered_files
193
- end # find_files
194
-
195
- def run( args )
196
- opt=OptionParser.new do |cmd|
197
-
198
- cmd.banner = "Usage: markdown [options] files_or_dirs"
199
-
200
- cmd.on( '-o', '--output PATH', 'Output Path' ) { |path| opts.output_path = path }
201
-
202
- # todo: find different letter for debug trace switch (use v for version?)
203
- cmd.on( "-v", "--verbose", "Show debug trace" ) do
204
- logger.datetime_format = "%H:%H:%S"
205
- logger.level = Logger::DEBUG
206
- end
207
-
208
-
209
- ## todo: add markdown.lib options (e.g. extensions,etc)
210
-
211
- usage =<<EOS
212
-
213
- markdown - Lets you convert plain text documents (#{Markdown.extnames.join(', ')}) to hypertext (.html) with your Markdown engine of choice (#{Markdown.lib}) and preprocessing text filters (#{Markdown.filters.join(', ')}).
214
-
215
- #{cmd.help}
216
-
217
- Examples:
218
- markdown # Process all documents in working folder (that is, .)
219
- markdown quickref # Process document or folder using Markdown
220
- markdown quickref.text # Process document using Markdown
221
- markdown -o site quickref # Output documents to site folder
222
-
223
- Further information:
224
- http://geraldb.github.com/markdown
225
-
226
- EOS
227
-
228
- cmd.on_tail( "-h", "--help", "Show this message" ) do
229
- puts usage
230
- exit
231
- end
232
- end
233
-
234
- opt.parse!( args )
235
-
236
- puts Markdown.banner
237
-
238
- # force loading of config
239
- Markdown.lib
240
-
241
- logger.debug "args.length: #{args.length}"
242
- logger.debug "args: >#{args.join(',')}<"
243
-
244
- # if no file args given; default to working folder (that is, .)
245
- args = ['.'] if args.length == 0
246
-
247
- args.each do |arg|
248
- files = find_files( arg )
249
- files.each do |file|
250
- create_doc( file )
251
- end
252
- end
253
-
254
- puts "Done."
255
-
256
- end # method run
257
-
258
- end # class Gen
259
-
260
- end # module Markdown