Class: Md2html::Parse

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.process_file(file_name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/md2html.rb', line 8

def self.process_file file_name
  if file_name and File.exist? file_name
    html = RDiscount.new( IO.read file_name ).to_html
    document = Hpricot html
    document.search('pre'){ |e|
      e.inner_html = e.search('code').inner_html
      e.set_attribute 'class', 'prettyprint'
    }
    save File.basename(file_name, '.md'), document.to_html
  else
    puts "Error: file doesn't exists"
  end
end

.save(title, body) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/md2html.rb', line 22

def self.save title, body
  script_dir = File.dirname(__FILE__)
  template   = Hpricot IO.read "#{script_dir}/md2html/html/layout.min.html"
  template.at('title').inner_html = title.capitalize
  template.at('#content').inner_html = body

  File.open("#{title}.html", 'w') {|f| f.write(template.to_html) }
  puts "changes saved to #{title}.html"
end

Instance Method Details

#process_code(text) ⇒ Object

This processes code using CodeRay



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/md2html.rb', line 33

def process_code text
  # the first line is the lang identifier
  lang = text.lines.first.scan(/^#!+[\s+]?(\w+)/).flatten.first

  # if found remove first line from text
  if lang
    lang.downcase!
    text = text.lines.to_a[1..-1].join
  else
    lang = 'text'
  end

  # highlight and put into a `div` element
  html = CodeRay.scan(text, lang).div(:css => :class)
end