class Conversio::Converter
Attributes
color[RW]
table_of_content[RW]
Public Class Methods
new(template)
click to toggle source
# File lib/conversio/converter.rb 9 def initialize(template) 10 @template = template 11 @table_of_content = false 12 @color = false 13 # Holds the input Markdown plain text 14 @source = nil 15 # Hold Markdown rendered to HTML 16 @content = nil 17 # Hold the finished XHTML document 18 @result = nil 19 # load the default parser 20 @parser = 'bluecloth' 21 load_parser(@parser) 22 end
Public Instance Methods
load_parser(parser)
click to toggle source
# File lib/conversio/converter.rb 35 def load_parser(parser) 36 begin 37 case parser 38 when 'bluecloth' 39 require 'bluecloth' 40 when 'kramdown' 41 require 'kramdown' 42 else 43 raise "Parser '#{parser}' is not a known Markdown library" 44 end 45 rescue LoadError 46 raise "Couldn't load #{parser}." 47 end 48 @parser = parser 49 end
markdown_to_xhtml(src,dst)
click to toggle source
# File lib/conversio/converter.rb 24 def markdown_to_xhtml(src,dst) 25 @source = open(src).readlines.join 26 colorize() if @color 27 parse() 28 generate_table_of_content() if @table_of_content 29 render() 30 # write the HTML file 31 FileUtils::mkdir_p(File.dirname(dst)) unless File.exists?(File.dirname(dst)) 32 open(dst,'w') { |f| f.write @result } 33 end
Private Instance Methods
colorize()
click to toggle source
# File lib/conversio/converter.rb 71 def colorize 72 @source = Pygmentizer.new.transform_code_blocks(@source) 73 end
generate_table_of_content()
click to toggle source
# File lib/conversio/converter.rb 75 def generate_table_of_content #füge zum geparsten Text ein Inhaltsverzeichnis hinzu -> davor parsen 76 raise "No content to generate table of content - Run the parser first!" if @content == nil 77 @content = HTMLTableOfContent.new(@content).get_html() 78 end
load_template(tpl)
click to toggle source
# File lib/conversio/converter.rb 53 def load_template(tpl) 54 puts "Loading template : "+tpl.to_s 55 raise "Couldn't open ERB template: #{tpl}" unless File.exists?(File.expand_path(tpl)) 56 return open(File.expand_path(tpl)).readlines.join 57 end
parse()
click to toggle source
# File lib/conversio/converter.rb 59 def parse 60 raise "Define source before rendering!" if @source == nil 61 case @parser 62 when 'bluecloth' 63 @content = BlueCloth::new(@source).to_html 64 when 'kramdown' 65 @content = Kramdown::Document.new(@source).to_html 66 else 67 puts "Markdown parser #{@parser} not supported yet" 68 end 69 end
render(values = {})
click to toggle source
# File lib/conversio/converter.rb 80 def render(values = {}) 81 values.store(:content, @content) 82 @result = ERB.new(@template).result(binding) 83 end