Class: Redcarpet::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/redcarpet/cli.rb

Overview

This class aims at easing the creation of custom binary for your needs. For example, you can add new options or change the existing ones. The parsing is handled by Ruby's OptionParser. For instance:

class Custom::CLI < Redcarpet::CLI
  def self.options_parser
    super.tap do |opts|
      opts.on("--rainbow") do
        @@options[:rainbow] = true
      end
    end
  end

  def self.render_object
    @@options[:rainbow] ? RainbowRender : super
  end
end

Class Method Summary collapse

Class Method Details

.legacy_parse!(args) ⇒ Object

:nodoc:


76
77
78
79
80
81
82
83
84
# File 'lib/redcarpet/cli.rb', line 76

def self.legacy_parse!(args) # :nodoc:
  # Workaround for backward compatibility as OptionParser
  # doesn't support the --flag-OPTION syntax.
  args.select {|a| a =~ /--(parse|render)-/ }.each do |arg|
    args.delete(arg)
    arg = arg.partition(/\b-/)
    args.push(arg.first, arg.last)
  end
end

.options_parserObject


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
# File 'lib/redcarpet/cli.rb', line 24

def self.options_parser
  @@options = {
    render_extensions: {},
    parse_extensions: {},
    smarty_pants: false
  }

  OptionParser.new do |opts|
    opts.banner = "Usage: redcarpet [--parse <extension>...] " \
                  "[--render <extension>...] [--smarty] <file>..."

    opts.on("--parse EXTENSION", "Enable a parsing extension") do |ext|
      ext = ext.gsub('-', '_').to_sym
      @@options[:parse_extensions][ext] = true
    end

    opts.on("--render EXTENSION", "Enable a rendering extension") do |ext|
      ext = ext.gsub('-', '_').to_sym
      @@options[:render_extensions][ext] = true
    end

    opts.on("--smarty", "Enable Smarty Pants") do
      @@options[:smarty_pants] = true
    end

    opts.on_tail("-v", "--version", "Display the current version") do
      STDOUT.write "Redcarpet #{Redcarpet::VERSION}"
      exit
    end

    opts.on_tail("-h", "--help", "Display this help message") do
      puts opts
      exit
    end
  end
end

.parser_objectObject


71
72
73
74
# File 'lib/redcarpet/cli.rb', line 71

def self.parser_object
  renderer = render_object.new(@@options[:render_extensions])
  Redcarpet::Markdown.new(renderer, @@options[:parse_extensions])
end

.process(args) ⇒ Object


61
62
63
64
65
# File 'lib/redcarpet/cli.rb', line 61

def self.process(args)
  self.legacy_parse!(args)
  self.options_parser.parse!(args)
  STDOUT.write parser_object.render(ARGF.read)
end

.render_objectObject


67
68
69
# File 'lib/redcarpet/cli.rb', line 67

def self.render_object
  @@options[:smarty_pants] ? Render::SmartyHTML : Render::HTML
end