Class: RbST

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

Constant Summary

@@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

- (RbST) initialize(*args)

Takes a string or file path plus any additional options and creates a new converter object.



39
40
41
42
43
# File 'lib/rbst.rb', line 39

def initialize(*args)
  target = args.shift
  @target  = File.exists?(target) ? File.read(target) : target rescue target
  @options = args
end

Class Method Details

+ (Object) convert(*args)

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



12
13
14
# File 'lib/rbst.rb', line 12

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

+ (Object) executables

Return the executable hash.



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

def self.executables; @@executables end

+ (Object) executables=(exec_paths = {})

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.



29
30
31
32
33
34
# File 'lib/rbst.rb', line 29

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

+ (Object) html_options

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



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

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

+ (Object) latex_options

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



17
18
19
# File 'lib/rbst.rb', line 17

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

Instance Method Details

- (Object) convert Also known as: to_s

:nodoc:



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

def convert # :nodoc:
  @output_format ||= :html
  execute "python #{@@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.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rbst.rb', line 66

def print_options(format)
  help = execute("python #{@@executables[format]} --help")
  # non-hyphenated long options to symbols
  help.gsub!(/(\-\-)([A-Za-z0-9]+)([=|\s])/, ':\2\3')
  # hyphenated long options to quoted strings
  help.gsub!(/(\-\-)([\w|\-]+)(\n)?[^$|^=|\]]?/, '\'\2\'\3')
  # equal to hashrocket
  help.gsub!(/\=/, ' => ')
  # hort options to symbols
  help.gsub!(/([^\w])\-(\w)([^\w])/, '\1:\2\1')
  # short options with args get a hashrocket
  help.gsub!(/(:\w) </, '\1 => <')
  puts help
end

- (Object) to_html(*args)

Converts the object's input to HTML.



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

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

- (Object) to_latex(*args)

Converts the object's input to LaTeX.



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

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