Skip to content

Commit 0bfa17f

Browse files
committed
Merge pull request gjtorikian#44 from indirect/master
Add an html-pipeline executable to the gem
2 parents 696ba4d + 5e9688f commit 0bfa17f

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ spec/reports
1515
test/tmp
1616
test/version_tmp
1717
tmp
18-
bin/*
18+
exec/*
1919
vendor/gems

bin/html-pipeline

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env ruby
2+
require 'html/pipeline'
3+
4+
require 'optparse'
5+
6+
# Accept "help", too
7+
ARGV.map!{|a| a == "help" ? "--help" : a }
8+
9+
OptionParser.new do |opts|
10+
opts.banner = <<-HELP.gsub(/^ /, '')
11+
Usage: html-pipeline [-h] [-f]
12+
html-pipeline [FILTER [FILTER [...]]] < file.md
13+
cat file.md | html-pipeline [FILTER [FILTER [...]]]
14+
HELP
15+
16+
opts.separator "Options:"
17+
18+
opts.on("-f", "--filters", "List the available filters") do
19+
filters = HTML::Pipeline.constants.grep(/\w+Filter$/).
20+
map{|f| f.to_s.gsub(/Filter$/,'') }
21+
22+
# Text filter doesn't work, no call method
23+
filters -= ["Text"]
24+
25+
abort <<-HELP.gsub(/^ /, '')
26+
Available filters:
27+
#{filters.join("\n ")}
28+
HELP
29+
end
30+
end.parse!
31+
32+
# Default to a GitHub-ish pipeline
33+
if ARGV.empty?
34+
35+
filters = [
36+
HTML::Pipeline::MarkdownFilter,
37+
HTML::Pipeline::SanitizationFilter,
38+
HTML::Pipeline::ImageMaxWidthFilter,
39+
HTML::Pipeline::EmojiFilter,
40+
HTML::Pipeline::AutolinkFilter,
41+
HTML::Pipeline::TableOfContentsFilter,
42+
]
43+
44+
# Add syntax highlighting if linguist is present
45+
begin
46+
require 'linguist'
47+
filters << HTML::Pipeline::SyntaxHighlightFilter
48+
rescue LoadError
49+
end
50+
51+
else
52+
53+
def filter_named(name)
54+
case name
55+
when "Text"
56+
raise NameError # Text filter doesn't work, no call method
57+
when "Textile"
58+
require "RedCloth" # Textile filter doesn't require RedCloth
59+
end
60+
61+
HTML::Pipeline.const_get("#{name}Filter")
62+
rescue NameError => e
63+
abort "Unknown filter '#{name}'. List filters with the -f option."
64+
end
65+
66+
filters = []
67+
until ARGV.empty?
68+
name = ARGV.shift
69+
filters << filter_named(name)
70+
end
71+
72+
end
73+
74+
context = {
75+
:asset_root => "/assets",
76+
:base_url => "/",
77+
:gfm => true
78+
}
79+
80+
puts HTML::Pipeline.new(filters, context).call(ARGF.read)[:output]

0 commit comments

Comments
 (0)