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 file path plus any additional options and creates a new converter object.



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

def initialize(*args)
  target = args.shift
  @target  = File.exists?(target) ? File.read(target) : target rescue target
  @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.



41
# File 'lib/rbst.rb', line 41

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
# 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.



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

def self.python_path; @@python_path end

.python_path=(path_to_python) ⇒ Object

Specify a python path or executable.



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

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

Instance Method Details

#convertObject Also known as: to_s

:nodoc:



58
59
60
61
62
63
64
# File 'lib/rbst.rb', line 58

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.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rbst.rb', line 83

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.



68
69
70
71
72
# File 'lib/rbst.rb', line 68

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

#to_latex(*args) ⇒ Object

Converts the object’s input to LaTeX.



75
76
77
78
79
# File 'lib/rbst.rb', line 75

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