Class: Kramdown::Man::CLI Private

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

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Represents the kramdown-man command's logic.

Since:

  • 1.0.0

Constant Summary collapse

PROGRAM_NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The program name.

Since:

  • 1.0.0

"kramdown-man"
BUG_REPORT_URL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The URL to report bugs to.

Since:

  • 1.0.0

"https://github.com/postmodern/kramdown-man/issues/new"
MAN_PAGE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The path to the man/ directory.

Since:

  • 1.0.0

File.join(__dir__,'..','..','..','man','kramdown-man.1')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the command.

Since:

  • 1.0.0



39
40
41
42
43
# File 'lib/kramdown/man/cli.rb', line 39

def initialize
  @option_parser = option_parser

  @output = nil
end

Instance Attribute Details

#option_parserOptionParser (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The option parser.

Returns:

  • (OptionParser)

Since:

  • 1.0.0



29
30
31
# File 'lib/kramdown/man/cli.rb', line 29

def option_parser
  @option_parser
end

#outputString? (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The optional output file to write to.

Returns:

  • (String, nil)

Since:

  • 1.0.0



34
35
36
# File 'lib/kramdown/man/cli.rb', line 34

def output
  @output
end

Class Method Details

.run(argv) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes and runs the command.

Parameters:

  • argv (Array<String>)

    Command-line arguments.

Returns:

  • (Integer)

    The exit status of the command.

Since:

  • 1.0.0



54
55
56
57
58
59
60
61
62
# File 'lib/kramdown/man/cli.rb', line 54

def self.run(argv)
  new().run(argv)
rescue Interrupt
  # https://tldp.org/LDP/abs/html/exitcodes.html
  return 130
rescue Errno::EPIPE
  # STDOUT pipe broken
  return 0
end

Instance Method Details

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prints a backtrace to stderr.

Parameters:

  • exception (Exception)

    The exception.

Since:

  • 1.0.0



191
192
193
194
195
196
197
198
# File 'lib/kramdown/man/cli.rb', line 191

def print_backtrace(exception)
  $stderr.puts "Oops! Looks like you've found a bug!"
  $stderr.puts "Please report the following text to: #{BUG_REPORT_URL}"
  $stderr.puts
  $stderr.puts "```"
  $stderr.puts "#{exception.full_message}"
  $stderr.puts "```"
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prints an error message to stderr.

Parameters:

  • error (String)

    The error message.

Since:

  • 1.0.0



181
182
183
# File 'lib/kramdown/man/cli.rb', line 181

def print_error(error)
  $stderr.puts "#{PROGRAM_NAME}: #{error}"
end

#run(argv = ARGV) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the command.

Parameters:

  • argv (Array<String>) (defaults to: ARGV)

    Command-line arguments.

Returns:

  • (Integer)

    The return status code.

Since:

  • 1.0.0



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
# File 'lib/kramdown/man/cli.rb', line 73

def run(argv=ARGV)
  argv = begin
           @option_parser.parse(argv)
         rescue OptionParser::ParseError => error
           print_error(error.message)
           return -1
         end

  markdown = case argv.length
             when 1
               path = argv[0]

               unless File.file?(path)
                 print_error "no such file or directory: #{path}"
                 return -1
               end

               File.read(path)
             when 0
               print_error "a MARKDOWN_FILE argument is required"
               return -1
             else
               print_error "too many arguments given"
               return -1
             end

  doc = Kramdown::Document.new(markdown)
  man_page = doc.to_man

  if @output
    File.write(@output,man_page)
  elsif $stdout.tty?
    view_man_page(man_page)
  else
    puts man_page
  end

  return 0
rescue => error
  print_backtrace(error)
  return -1
end

#view_man_page(man_page) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Displays the man page using the man command.

Parameters:

  • man_page (String)

    The man page output.

Since:

  • 1.0.0



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/kramdown/man/cli.rb', line 122

def view_man_page(man_page)
  io  = IO.popen(%w[man -l -],'w')
  pid = io.pid

  begin
    io.puts man_page
  rescue Errno::EPIPE
  ensure
    io.close

    begin
      Process.waitpid(pid)
    rescue Errno::EPIPE, Errno::ECHILD
    end
  end
end