Class: RbST
- Inherits:
-
Object
- Object
- RbST
- Defined in:
- lib/rbst.rb
Constant Summary collapse
- @@python_path =
'python'
- @@executable_path =
File.( 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
-
.convert(*args) ⇒ Object
Takes a string or file path plus any additional options and converts the input.
-
.executables ⇒ Object
Return the executable hash.
-
.executables=(exec_paths = {}) ⇒ Object
Specify custom executables to use instead of the default rst2latex.py and rst2html.py scripts.
-
.html_options ⇒ Object
Print HTML-Specific Options, General Docutils Options and reStructuredText Parser Options.
-
.latex_options ⇒ Object
Print LaTeX-Specific Options, General Docutils Options and reStructuredText Parser Options.
-
.python_path ⇒ Object
Return the python path.
-
.python_path=(path_to_python) ⇒ Object
Specify a python path or executable.
Instance Method Summary collapse
-
#convert ⇒ Object
(also: #to_s)
:nodoc:.
-
#initialize(*args) ⇒ RbST
constructor
Takes a string or array of file paths plus any additional options and creates a new converter object.
-
#print_options(format) ⇒ Object
Formats and prints the options from the docutils help in the way they’d be specified in RbST: strings, symbols and hashes.
-
#to_html(*args) ⇒ Object
Converts the object’s input to HTML.
-
#to_latex(*args) ⇒ Object
Converts the object’s input to LaTeX.
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 |
.executables ⇒ Object
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_options ⇒ Object
Print HTML-Specific Options, General Docutils Options and reStructuredText Parser Options.
27 28 29 |
# File 'lib/rbst.rb', line 27 def self. new.(:html) end |
.latex_options ⇒ Object
Print LaTeX-Specific Options, General Docutils Options and reStructuredText Parser Options.
21 22 23 |
# File 'lib/rbst.rb', line 21 def self. new.(:latex) end |
.python_path ⇒ Object
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
#convert ⇒ Object 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]}" + ) end |
#print_options(format) ⇒ Object
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 (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 |