File: commonmarker

package info (click to toggle)
ruby-commonmarker 0.23.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,456 kB
  • sloc: ansic: 10,575; ruby: 1,741; sh: 36; makefile: 22
file content (118 lines) | stat: -rwxr-xr-x 4,115 bytes parent folder | download | duplicates (2)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'optparse'

$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'commonmarker'

root = File.expand_path('..', __dir__)
$LOAD_PATH.unshift File.expand_path('lib', root)

def parse_options
  options = Struct.new(:active_extensions, :active_parse_options, :active_render_options, :output_format, :renderer)
                  .new([], [:DEFAULT], [:DEFAULT], :html)
  extensions = CommonMarker.extensions
  parse_options = CommonMarker::Config::OPTS.fetch(:parse)
  render_options = CommonMarker::Config::OPTS.fetch(:render)
  format_options = CommonMarker::Config::OPTS.fetch(:format)

  option_parser = OptionParser.new do |opts|
    opts.banner = 'Usage: commonmarker [--html-renderer] [--extension=EXTENSION]'
    opts.separator '                    [--to=FORMAT]'
    opts.separator '                    [--parse-option=OPTION]'
    opts.separator '                    [--render-option=OPTION]'
    opts.separator '                    [FILE..]'
    opts.separator ''
    opts.separator 'Convert one or more CommonMark files to HTML and write to standard output.'
    opts.separator 'If no FILE argument is provided, text will be read from STDIN.'
    opts.separator ''

    opts.on('--extension=EXTENSION', Array, 'Use EXTENSION for parsing and HTML output (unless --html-renderer is specified)') do |values|
      values.each do |value|
        if extensions.include?(value)
          options.active_extensions << value.to_sym
        else
          abort("extension '#{value}' not found")
        end
      end
    end

    opts.on('-h', '--help', 'Prints this help') do
      puts opts
      puts
      puts "Available formats: #{format_options.join(', ')}"
      puts "Available extentions: #{extensions.join(', ')}"
      puts "Available parse options: #{parse_options.keys.join(', ')}"
      puts "Available render options: #{render_options.keys.join(', ')}"
      puts
      puts 'See the README for more information on these.'
      exit
    end

    opts.on('-tFORMAT', '--to=FORMAT', String, 'Specify output FORMAT') do |value|
      value = value.to_sym
      if format_options.include?(value)
        options.output_format = value
      else
        abort("format '#{value}' not found")
      end
    end

    opts.on('--html-renderer', 'Use the HtmlRenderer renderer rather than the native C renderer (only valid when format is html)') do
      options.renderer = true
    end

    opts.on('--parse-option=OPTION', Array, 'OPTION passed during parsing') do |values|
      values.each do |value|
        if parse_options.key?(value.to_sym)
          options.active_parse_options << value.to_sym
        else
          abort("parse-option '#{value}' not found")
        end
      end
    end

    opts.on('--render-option=OPTION', Array, 'OPTION passed during rendering') do |values|
      values.each do |value|
        if render_options.key?(value.to_sym)
          options.active_render_options << value.to_sym
        else
          abort("render-option '#{value}' not found")
        end
      end
    end

    opts.on('-v', '--version', 'Version information') do
      puts "commonmarker #{CommonMarker::VERSION}"
      exit
    end
  end

  option_parser.parse!

  options
end

options = parse_options

abort("format '#{options.output_format}' does not support using the HtmlRenderer renderer") if
  options.renderer && options.output_format != :html

doc = CommonMarker.render_doc(ARGF.read, options.active_parse_options, options.active_extensions)

case options.output_format
when :html
  if options.renderer
    renderer = CommonMarker::HtmlRenderer.new(options: options.active_render_options, extensions: options.active_extensions)
    $stdout.write(renderer.render(doc))
  else
    $stdout.write(doc.to_html(options.active_render_options, options.active_extensions))
  end
when :xml
  $stdout.write(doc.to_xml(options.active_render_options))
when :commonmark
  $stdout.write(doc.to_commonmark(options.active_render_options))
when :plaintext
  $stdout.write(doc.to_plaintext(options.active_render_options))
end