Module: Indexer::Importer::MarkdownImportation

Included in:
Indexer::Importer
Defined in:
lib/indexer/importer/markdown.rb

Overview

Import metadata from a markdown source.

Instance Method Summary collapse

Instance Method Details

#import(source) ⇒ Object

Markdown import procedure.



12
13
14
15
16
17
18
19
20
21
# File 'lib/indexer/importer/markdown.rb', line 12

def import(source)
  if File.file?(source)
    case File.extname(source)
    when '.md', '.markdown'
      load_markdown(source)
      return true
    end
  end
  super(source) if defined?(super)
end

#load_markdown(file) ⇒ Object

Import metadata from HTML file.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/indexer/importer/markdown.rb', line 26

def load_markdown(file)
  require 'nokogiri'

  text = File.read(file)

  begin
    require 'redcarpet'
    html = render_with_redcarpet(text)
  rescue LoadError
    require 'kramdown'
    html = render_with_kramdown(text)
  end

  doc = Nokogiri::HTML(html)

  load_html(doc)
end

#render_with_kramdown(text) ⇒ Object



52
53
54
# File 'lib/indexer/importer/markdown.rb', line 52

def render_with_kramdown(text)
  Kramdown::Document.new(text).to_html
end

#render_with_redcarpet(text) ⇒ Object



45
46
47
48
49
# File 'lib/indexer/importer/markdown.rb', line 45

def render_with_redcarpet(text)
  renderer = Redcarpet::Render::HTML.new()
  markdown = Redcarpet::Markdown.new(renderer, :autolink=>true, :tables=>true, :space_after_headers=>true)
  markdown.render(text)
end