Class: Conversio::Converter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ Converter

Returns a new instance of Converter.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/conversio/converter.rb', line 9

def initialize(template)
  @template = template
  @table_of_content = false
  @color = false
  # Holds the input Markdown plain text
  @source = nil
  # Hold Markdown rendered to HTML
  @content = nil
  # Hold the finished XHTML document
  @result = nil
  # load the default parser
  @parser = 'bluecloth'
  load_parser(@parser)
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



7
8
9
# File 'lib/conversio/converter.rb', line 7

def color
  @color
end

#table_of_contentObject

Returns the value of attribute table_of_content.



7
8
9
# File 'lib/conversio/converter.rb', line 7

def table_of_content
  @table_of_content
end

Instance Method Details

#load_parser(parser) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/conversio/converter.rb', line 35

def load_parser(parser)
  begin
    case parser
    when 'bluecloth'
      require 'bluecloth'
    when 'kramdown'
      require 'kramdown'
    else
      raise "Parser '#{parser}' is not a known Markdown library"
    end
  rescue LoadError
    raise "Couldn't load #{parser}."
  end
  @parser = parser
end

#markdown_to_xhtml(src, dst) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/conversio/converter.rb', line 24

def markdown_to_xhtml(src,dst)
  @source = open(src).readlines.join
  colorize() if @color 
  parse()    
  generate_table_of_content() if @table_of_content
  render()
  # write the HTML file
  FileUtils::mkdir_p(File.dirname(dst)) unless File.exists?(File.dirname(dst))
  open(dst,'w') { |f| f.write @result }
end