Class: RbST

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

Constant Summary collapse

@@python_path =
'python'
@@executable_path =
File.expand_path(
  File.join(File.dirname(__FILE__), 'rst2parts')
)
@@executables =
{
  html:  File.join(@@executable_path, 'rst2html.py'),
  latex: File.join(@@executable_path, 'rst2latex.py')
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RbST

Takes a string or array of file paths plus any additional options and creates a new converter object.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rbst.rb', line 59

def initialize(*args)
  target = args.shift
  if target.is_a?(String)
    @target = target
  elsif target.is_a?(Array)
    @target = ''
    target.each_with_index do |path, i|
      @target += "\n" if i.positive?
      @target += begin
                   File.exist?(path) ? File.read(path) : path
                 rescue StandardError
                   path
                 end
    end
  end
  @options = args
end

Class Method Details

.convert(*args) ⇒ Object

Takes a string or file path plus any additional options and converts the input.



15
16
17
# File 'lib/rbst.rb', line 15

def self.convert(*args)
  new(*args).convert
end

.executablesObject

Return the executable hash.



43
44
45
# File 'lib/rbst.rb', line 43

def self.executables
  @@executables
end

.executables=(exec_paths = {}) ⇒ Object

Specify custom executables to use instead of the default rst2latex.py and rst2html.py scripts. Takes a hash with two possible keys, :html and :latex, which should contain a full path to the alternative executable.



34
35
36
37
38
39
40
# File 'lib/rbst.rb', line 34

def self.executables=(exec_paths = {})
  if exec_paths.empty? || (exec_paths.keys & [:html, :latex]).empty?
    raise ArgumentError, 'Custom executable format must be :html or :latex'
  end

  @@executables = @@executables.merge(exec_paths)
end

.html_optionsObject

Print HTML-Specific Options, General Docutils Options and reStructuredText Parser Options.



27
28
29
# File 'lib/rbst.rb', line 27

def self.html_options
  new.print_options(:html)
end

.latex_optionsObject

Print LaTeX-Specific Options, General Docutils Options and reStructuredText Parser Options.



21
22
23
# File 'lib/rbst.rb', line 21

def self.latex_options
  new.print_options(:latex)
end

.python_pathObject

Return the python path.



53
54
55
# File 'lib/rbst.rb', line 53

def self.python_path
  @@python_path
end

.python_path=(path_to_python) ⇒ Object

Specify a python path or executable.



48
49
50
# File 'lib/rbst.rb', line 48

def self.python_path=(path_to_python)
  @@python_path = path_to_python
end

Instance Method Details

#convertObject Also known as: to_s

:nodoc:



77
78
79
80
81
82
83
# File 'lib/rbst.rb', line 77

def convert # :nodoc:
  @output_format ||= :html
  execute(
    "#{@@python_path} #{@@executables[@output_format]}" +
    convert_options
  )
end

Formats and prints the options from the docutils help in the way they’d be specified in RbST: strings, symbols and hashes.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rbst.rb', line 102

def print_options(format)
  help = execute("#{@@python_path} #{@@executables[format]} --help")
  # Convert non-hyphenated long options to symbols
  help.gsub!(/(\-\-)([A-Za-z0-9]+)([=|\s])/, ':\2\3')
  # Convert hyphenated long options to quoted strings
  help.gsub!(/(\-\-)([\w|\-]+)(\n)?[^=|\]]?/, '\'\2\'\3')

  # Convert equal signs to hashrocket
  help.gsub!(/\=/, ' => ')
  # Convert short options to symbols
  help.gsub!(/([^\w])\-(\w)([^\w])/, '\1:\2\1')
  # Convert short options with args get a hashrocket
  help.gsub!(/(:\w) </, '\1 => <')
  # Print converted help text
  puts help
end

#to_html(*args) ⇒ Object

Converts the object’s input to HTML.



87
88
89
90
91
# File 'lib/rbst.rb', line 87

def to_html(*args)
  @output_format = :html
  @options += args
  convert
end

#to_latex(*args) ⇒ Object

Converts the object’s input to LaTeX.



94
95
96
97
98
# File 'lib/rbst.rb', line 94

def to_latex(*args)
  @output_format = :latex
  @options += args
  convert
end