mdexport 0.0.4 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c85cb29c8b8d26848fe5dd50a3e1d6a3d65b4f6c
4
- data.tar.gz: 27c9fb2d07c8f1f9acbb0faf0ef08237bfafe144
3
+ metadata.gz: c04a31d10dcad1ab0c7e39c781c4a2311edb0495
4
+ data.tar.gz: 076fa1ea74a9bd53fdd57e0b4e41c141ef0b95b9
5
5
  SHA512:
6
- metadata.gz: a284d5bfbc650d1787c6c1702ea965d4f7a2767588e49d96457cfa5d010526ca727eef69eb02ba57626d94e7f00ad054d3e79f5ea0d961ea6701c27b2170d728
7
- data.tar.gz: 6802a52ee7e8333c1750efdc1ef48b79ea37c4741dcdac84ddc43d3ed73a3461cc8a92d503186563459a8b5289e60afc73655a59382245013461fdb3ce128800
6
+ metadata.gz: 9b615f810e58e15004eacb010c5c4dc6b698da8a44a8c4e9a7de81946948b915dfdab0022a7210b5ca0c35487b89671f0170064ae17d7304613a61c807de9de7
7
+ data.tar.gz: 0820c5d8afe7e82ef7fa96afbcfa7e56fa22c5534ededd6dbc93859160bc731dc7cdc404e68507c96da693668a0dbce45a351e547d97ba57514915fd2dffa4ea
@@ -4,4 +4,4 @@ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
4
4
 
5
5
  require 'mdexport'
6
6
 
7
- Mdexport.run
7
+ Mdexport.new.run
@@ -0,0 +1,9 @@
1
+ require 'fileutils'
2
+
3
+ class File
4
+
5
+ def self.remove(path)
6
+ FileUtils.rm(path) if File.exist?(path)
7
+ end
8
+
9
+ end
@@ -1,93 +1,144 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'fileutils'
4
3
  require 'filewatcher'
5
4
  require 'string'
5
+ require 'file'
6
6
  require 'markdown'
7
7
  require 'mustache'
8
+ require 'commander'
9
+ require 'rubygems'
8
10
 
9
11
  class Mdexport
12
+ include Commander::Methods
10
13
 
11
- def self.run
12
- folder = nil
13
- watching = false
14
- cleaning = false
14
+ def run
15
+ program :name, 'mdexport'
16
+ program :version, '0.0.5'
17
+ program :description, 'mdexport is a command line that exports markdown files into html files.'
15
18
 
16
- if ARGV.size > 0
17
- ARGV.each do |param|
18
- if param == '-w' || param == '--watch'
19
- puts 'Watching file changes.'
20
- watching = true
21
- end
19
+ default_command :run
20
+
21
+ command :run do |c|
22
+ c.syntax = 'mdexport [--path /path/to/folder] [--watch] [--clean] [--output /path/to/output_file]'
23
+ c.description = 'Exports the markdown files from folder to html files.'
24
+ c.option '--path STRING', String, 'Path to folder that contains the markdown files.'
25
+ c.option '--output STRING', String, 'Path to output file.'
26
+ c.option '--watch', 'Watch markdown file changes.'
27
+ c.option '--clean', 'Clean html files on folder.'
28
+ c.option '--merge', 'Merge markdown files in a given output file.'
29
+
30
+ c.action do |args, opt|
31
+ opt.default :path => '.'
32
+ opt.default :output => nil
33
+ opt.default :watch => false
34
+ opt.default :clean => false
35
+ opt.default :merge => false
22
36
 
23
- if param == '-c' || param == '--clean'
24
- puts 'Removing html files.'
25
- cleaning = true
26
- end
37
+ opt.path = File.expand_path opt.path
38
+ opt.output = File.expand_path opt.output if opt.output
39
+
40
+ check_path(opt.path)
41
+ #check_path(opt.output) if opt.output
27
42
 
28
- if folder == nil && File.exist?(param)
29
- folder = File.expand_path param
30
- end
43
+ error "Incompatible params: merge, output." if opt.merge == true && opt.output == nil
44
+ error "Incompatible params: clean, watch." if opt.clean == true && opt.watch == true
45
+ error "Incompatible params: output, watch." if opt.output != nil && opt.watch == true
46
+
47
+ process opt.path, opt.clean, opt.watch, opt.output, opt.merge
31
48
  end
32
49
  end
50
+
51
+ run!
52
+ end
53
+
54
+ private
55
+
56
+ def check_path(path)
57
+ error "Invalid path '#{path}'." unless File.exist?(path)
58
+ end
59
+
60
+ def error(message)
61
+ puts message; exit 1
62
+ end
33
63
 
34
- unless folder
35
- folder = File.expand_path "."
36
- end
37
-
38
- pattern = "#{folder}/**/*.md"
64
+ def process(path, clean, watch, output, merge)
65
+ extension = clean ? "html" : "md"
66
+ pattern = "#{path}/**/*.#{extension}"
39
67
 
40
68
  files = Dir[pattern] || Array.new
41
69
 
42
70
  if files.size == 0
43
- puts "There is no markdown files here."; exit 1
71
+ type = clean ? "html" : "markdown"
72
+ error "There is no #{type} files to process."
44
73
  end
45
-
74
+
75
+ content = ""
76
+
46
77
  files.each do |file_path|
47
- if cleaning
48
- html_file = file_path.html_file_path
49
- FileUtils.rm(html_file) if File.exist?(html_file)
78
+ if clean
79
+ File.remove(file_path)
50
80
  else
51
- self.generate_html_for(file_path)
81
+ file_content = File.read(file_path)
82
+
83
+ if merge
84
+ content += file_content
85
+ else
86
+ content += Markdown.render(file_content)
87
+ end
88
+
89
+ unless output
90
+ generate_html(content, file_path.html)
91
+ content = ""
92
+ end
52
93
  end
53
94
  end
54
95
 
55
- if watching
56
- FileWatcher.new(pattern).watch do |file_path|
57
- self.generate_html_for(file_path)
58
- self.refresh_page(file_path.basename)
96
+ if output != nil
97
+ generate_file(content, output)
98
+ end
99
+
100
+ if watch
101
+ FileWatcher.new(pattern).watch do |file_path, event|
102
+ if [:changed, :new].include? event
103
+ file_content = File.read(file_path)
104
+ content = Markdown.render(file_content)
105
+ generate_html(content, file_path.html)
106
+ refresh_page(file_path.basename)
107
+ elsif event.to_sym == :delete
108
+ File.remove(file_path.html)
109
+ end
59
110
  end
60
111
  end
61
112
  end
62
113
 
63
- def self.refresh_page keyword
64
- %x{osascript<<ENDGAME
65
- tell application "Safari"
66
- set windowList to every window
67
- repeat with aWindow in windowList
68
- set tabList to every tab of aWindow
69
- repeat with atab in tabList
70
- if (URL of atab contains "#{keyword}") then
71
- tell atab to do javascript "window.location.reload()"
72
- end if
73
- end repeat
74
- end repeat
75
- end tell
76
- ENDGAME
77
- }
114
+ def refresh_page keyword
115
+ template = File.read("lib/templates/refresh.mustache")
116
+ command = Mustache.render(template, :keyword => keyword)
117
+ system(command)
78
118
  end
79
-
80
- def self.generate_html_for(file_path)
81
- file_content = File.read(file_path)
82
- html_body = Markdown.render(file_content)
83
119
 
84
- title = file_path.basename
120
+ def generate_html(content, path)
121
+ title = get_title(content) || path.basename
122
+
85
123
  template = File.read("lib/templates/page.mustache")
86
- content = Mustache.render(template, :title => title, :yield => html_body)
124
+ content = Mustache.render(template, :title => title, :yield => content)
125
+
126
+ generate_file(content, path)
127
+ end
128
+
129
+ def generate_file(content, path)
130
+ File.remove(path)
131
+ File.write(path, content)
132
+ end
133
+
134
+ def get_title(content)
135
+ title = nil
87
136
 
88
- html_file_path = file_path.html_file_path
89
- FileUtils.rm(html_file_path) if File.exist?(html_file_path)
90
- File.write(html_file_path, content)
137
+ if content =~ /<[hH][1-6].*?>(\w.*?)<\/.*?[hH][1-6]>/
138
+ title = Regexp.last_match[1]
139
+ end
140
+
141
+ title
91
142
  end
92
143
 
93
144
  end # end class
@@ -13,7 +13,7 @@ class String
13
13
  File.basename(self, extension)
14
14
  end
15
15
 
16
- def html_file_path
16
+ def html
17
17
  dirname = Pathname.new(self).dirname
18
18
  filename = self.filename.gsub(extension, '.html')
19
19
  "#{dirname}/#{filename}"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mdexport
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Madson Cardoso
@@ -38,6 +38,26 @@ dependencies:
38
38
  - - ~>
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.6.8
41
+ - !ruby/object:Gem::Dependency
42
+ name: commander
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '4.3'
48
+ - - '>='
49
+ - !ruby/object:Gem::Version
50
+ version: 4.3.4
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: '4.3'
58
+ - - '>='
59
+ - !ruby/object:Gem::Version
60
+ version: 4.3.4
41
61
  - !ruby/object:Gem::Dependency
42
62
  name: mustache
43
63
  requirement: !ruby/object:Gem::Requirement
@@ -87,6 +107,7 @@ extensions: []
87
107
  extra_rdoc_files: []
88
108
  files:
89
109
  - bin/mdexport
110
+ - lib/file.rb
90
111
  - lib/markdown.rb
91
112
  - lib/mdexport.rb
92
113
  - lib/string.rb