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

API:

  • private

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

API:

  • private

"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

API:

  • private

"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

API:

  • private

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

API:

  • private



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.

Since:

  • 1.0.0

Returns:

API:

  • private



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.

Since:

  • 1.0.0

Returns:

API:

  • private



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.

Since:

  • 1.0.0

Parameters:

  • Command-line arguments.

Returns:

  • The exit status of the command.

API:

  • private



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.

Since:

  • 1.0.0

Parameters:

  • The exception.

API:

  • private



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.

Since:

  • 1.0.0

Parameters:

  • The error message.

API:

  • private



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.

Since:

  • 1.0.0

Parameters:

  • (defaults to: ARGV)

    Command-line arguments.

Returns:

  • The return status code.

API:

  • private



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.

Since:

  • 1.0.0

Parameters:

  • The man page output.

API:

  • private



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