Class: Mediumize::Renderer

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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Renderer

Returns a new instance of Renderer.



10
11
12
13
14
15
16
17
18
19
# File 'lib/mediumize/renderer.rb', line 10

def initialize(opts={})
  @opts = opts
  @normalizers = [
    Normalizers::Title.new
  ]
  if @opts[:base_url]
    @normalizers << Normalizers::Links.new(@opts[:base_url])
  end
  @markdown = Markdown.new
end

Instance Method Details

#render(content, file, opts = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mediumize/renderer.rb', line 21

def render(content, file, opts={})
  if(@opts[:frontmatter])
    content, meta = Frontmatter.parse(content)
    opts[:title] ||= meta["title"]
    opts[:canonical_url] ||= meta["canonical_url"]
  end

  markup = @markdown.render(content)
  doc = Nokogiri::HTML(markup)

  @normalizers.each{|n| doc = n.normalize(doc) }

  doc.title = opts[:title] if opts[:title]
  if(@opts[:headline])
    doc.at('body').children.first.add_previous_sibling("<h1>#{doc.title}</h1")
  end

  doc = {
     :title => doc.title,
     :content => doc.at('body').children.to_html,
     :content_format => "html"
  }

  doc.merge!({:canonical_url => opts[:canonical_url]}) if opts[:canonical_url]

  doc
end