html2markdown-cli 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/html2markdown-cli +3 -0
- data/lib/html2markdown/html2markdown_cli.rb +36 -0
- data/lib/html2markdown/options_parser.rb +62 -0
- data/lib/runner.rb +16 -0
- metadata +83 -0
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'reverse-markdown'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
class Html2MarkdownCli
|
5
|
+
attr_accessor :output_dir
|
6
|
+
|
7
|
+
def initialize(output_dir = "#{Dir.pwd}/output")
|
8
|
+
@output_dir = output_dir
|
9
|
+
create_dir(output_dir)
|
10
|
+
end
|
11
|
+
|
12
|
+
def convert_file(file_name)
|
13
|
+
reverse_md = ReverseMarkdown.new
|
14
|
+
contents = File.open(file_name).read
|
15
|
+
markdown = reverse_md.parse_string(contents)
|
16
|
+
write_file(file_name, markdown, output_dir)
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def create_dir(dir)
|
21
|
+
unless Dir.exists?(dir)
|
22
|
+
FileUtils.mkdir(dir)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def valid_file?(file)
|
27
|
+
file.include?(".html") || file.include?(".htm")
|
28
|
+
end
|
29
|
+
|
30
|
+
def write_file(file_name, markdown_string, output_dir)
|
31
|
+
file_name = file_name.gsub(".html", ".markdown")
|
32
|
+
file = File.new("#{output_dir}/#{File.basename(file_name)}", "w")
|
33
|
+
file.write(markdown_string)
|
34
|
+
file.close
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require_relative 'html2markdown_cli'
|
2
|
+
|
3
|
+
class OptionsParser
|
4
|
+
attr_accessor :options, :html2md
|
5
|
+
|
6
|
+
def initialize(options)
|
7
|
+
@options = options
|
8
|
+
|
9
|
+
if option?(:output_dir)
|
10
|
+
@html2md = Html2MarkdownCli.new(options[:output_dir])
|
11
|
+
else
|
12
|
+
@html2md = Html2MarkdownCli.new
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def parse
|
17
|
+
if option?(:file)
|
18
|
+
parse_file
|
19
|
+
elsif option?(:dir)
|
20
|
+
parse_dir
|
21
|
+
else
|
22
|
+
puts "No options specified. See --help for options"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def option?(name)
|
29
|
+
options["#{name}_given".to_sym]
|
30
|
+
end
|
31
|
+
|
32
|
+
def parse_file
|
33
|
+
file = options[:file]
|
34
|
+
if File.exists?(file)
|
35
|
+
convert(file)
|
36
|
+
else
|
37
|
+
puts "File doesn't exist. Use --help for options"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def parse_dir
|
42
|
+
dir = options[:dir]
|
43
|
+
if Dir.exists?(dir)
|
44
|
+
files = File.join(dir, "*.html")
|
45
|
+
Dir.glob(files).each do |file|
|
46
|
+
convert(file)
|
47
|
+
end
|
48
|
+
else
|
49
|
+
puts "Directory doesn't exist. Use --help for options"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def convert(file)
|
54
|
+
begin
|
55
|
+
puts "Converting: #{file}"
|
56
|
+
html2md.convert_file(file)
|
57
|
+
rescue RuntimeError => e
|
58
|
+
puts "Failed to convert: #{file}: #{e}"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
data/lib/runner.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'trollop'
|
2
|
+
require_relative 'html2markdown/options_parser'
|
3
|
+
|
4
|
+
p = Trollop::Parser.new do
|
5
|
+
opt :dir, "Directory containing html files to convert", :type => :string
|
6
|
+
opt :file, "The file to convert", :type => :string
|
7
|
+
opt :output_dir, "The output location of the converted files", :type => :string
|
8
|
+
end
|
9
|
+
|
10
|
+
opts = Trollop::with_standard_exception_handling p do
|
11
|
+
raise Trollop::HelpNeeded if ARGV.empty?
|
12
|
+
p.parse ARGV
|
13
|
+
end
|
14
|
+
|
15
|
+
OptionsParser.new(opts).parse
|
16
|
+
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: html2markdown-cli
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Austen Ito
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-05-27 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70173079967240 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70173079967240
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: reverse-markdown
|
27
|
+
requirement: &70173079966780 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70173079966780
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: trollop
|
38
|
+
requirement: &70173079966340 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70173079966340
|
47
|
+
description: Command-line utility to convert HTML files into Markdown
|
48
|
+
email: austen.dev@gmail.com
|
49
|
+
executables:
|
50
|
+
- html2markdown-cli
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- lib/html2markdown/html2markdown_cli.rb
|
55
|
+
- lib/html2markdown/options_parser.rb
|
56
|
+
- lib/runner.rb
|
57
|
+
- !binary |-
|
58
|
+
YmluL2h0bWwybWFya2Rvd24tY2xp
|
59
|
+
homepage: https://github.com/austenito/html2md-cli
|
60
|
+
licenses: []
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.8.17
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: Command-line utility to convert HTML files into Markdown
|
83
|
+
test_files: []
|