File: html-pipeline

package info (click to toggle)
ruby-html-pipeline 2.14.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: ruby: 2,265; sh: 13; makefile: 6
file content (78 lines) | stat: -rwxr-xr-x 1,746 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env ruby
require 'html/pipeline'

require 'optparse'

# Accept "help", too
.map! { |a| a == 'help' ? '--help' : a }

onParser.new do |opts|
  opts.banner = <<-HELP.gsub(/^    /, '')
    Usage: html-pipeline [-h] [-f]
           html-pipeline [FILTER [FILTER [...]]] < file.md
       cat file.md | html-pipeline [FILTER [FILTER [...]]]
  HELP

 opts.separator 'Options:'

  opts.on('-f', '--filters', 'List the available filters') do
    filters = HTML::Pipeline.constants.grep(/\w+Filter$/)
                            .map { |f| f.to_s.gsub(/Filter$/, '') }

    # Text filter doesn't work, no call method
    filters -= ['Text']

    abort <<-HELP.gsub(/^      /, '')
      Available filters:
        #{filters.join("\n        ")}
    HELP
  end
end.parse!

# Default to a GitHub-ish pipeline
if ARGV.empty?

  filters = [
    HTML::Pipeline::MarkdownFilter,
    HTML::Pipeline::SanitizationFilter,
    HTML::Pipeline::ImageMaxWidthFilter,
    HTML::Pipeline::EmojiFilter,
    HTML::Pipeline::AutolinkFilter,
    HTML::Pipeline::TableOfContentsFilter
  ]

  # Add syntax highlighting if rouge is present
  begin
    require 'rouge'
    filters << HTML::Pipeline::SyntaxHighlightFilter
  rescue LoadError
  end

else

  def filter_named(name)
    case name
    when 'Text'
      raise NameError # Text filter doesn't work, no call method
    end

    HTML::Pipeline.const_get("#{name}Filter")
  rescue NameError => e
    abort "Unknown filter '#{name}'. List filters with the -f option."
  end

  filters = []
  until ARGV.empty?
    name = ARGV.shift
    filters << filter_named(name)
  end

end

context = {
  asset_root: '/assets',
  base_url: '/',
  gfm: true
}

puts HTML::Pipeline.new(filters, context).call(ARGF.read)[:output]