class Makimono::Converter::Markdown

Constants

PRESETS

Public Class Methods

get_markdown_class(class_name) click to toggle source
# File lib/makimono/converter/markdown.rb, line 13
def self.get_markdown_class(class_name)
  case class_name
  when 'CommonMarker'
    CommonMarker
  when 'FujiMarkdown'
    FujiMarkdown
  else
    const_get(class_name.to_s)
  end
rescue NameError
  raise InvalidMarkdownError, "Invalid markdown configuration: #{class_name}"
end
get_template_path(template_config) click to toggle source
# File lib/makimono/converter/markdown.rb, line 26
def self.get_template_path(template_config)
  if PRESETS.include?(template_config)
    File.expand_path("../../templates/#{template_config}.erb", __dir__)
  elsif File.file?(template_config.to_s)
    File.expand_path(template_config.to_s)
  else
    raise InvalidTemplateError, "Template file `#{template_config}` does not exist"
  end
end
new(config, markdown: nil) click to toggle source
# File lib/makimono/converter/markdown.rb, line 36
def initialize(config, markdown: nil)
  @config = config
  @markdown = markdown || self.class.get_markdown_class(config[:markdown]).new(config)
  @template_path = self.class.get_template_path(config[:template])
end

Public Instance Methods

convert(resource) click to toggle source
# File lib/makimono/converter/markdown.rb, line 46
def convert(resource)
  converted = resource.dup
  converted.content = convert_content(resource)
  converted.extname = File.extname(File.basename(@template_path, '.erb'))
  converted
end
convertible?(resource) click to toggle source
# File lib/makimono/converter/markdown.rb, line 42
def convertible?(resource)
  %w[.markdown .mkdown .mkdn .mkd .md].include?(resource.extname)
end

Private Instance Methods

convert_content(resource) click to toggle source
# File lib/makimono/converter/markdown.rb, line 55
def convert_content(resource)
  body = @markdown.render(resource.content)
  ERB.new(template).result_with_hash(
    {
      config: @config,
      resource: resource,
      body: body
    }
  )
end
template() click to toggle source
# File lib/makimono/converter/markdown.rb, line 66
def template
  @template ||= File.read(@template_path)
end