Class: Makimono::Converter::Markdown

Inherits:
Object
  • Object
show all
Defined in:
lib/makimono/converter/markdown.rb,
lib/makimono/converter/markdown/commonmarker.rb,
lib/makimono/converter/markdown/fuji_markdown.rb

Defined Under Namespace

Classes: CommonMarker, FujiMarkdown

Constant Summary collapse

PRESETS =
%w[default.xhtml empty.txt].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, markdown: nil) ⇒ Markdown

Returns a new instance of Markdown.



36
37
38
39
40
# 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

Class Method Details

.get_markdown_class(class_name) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# 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) ⇒ Object



26
27
28
29
30
31
32
33
34
# 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

Instance Method Details

#convert(resource) ⇒ Object



46
47
48
49
50
51
# 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) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/makimono/converter/markdown.rb', line 42

def convertible?(resource)
  %w[.markdown .mkdown .mkdn .mkd .md].include?(resource.extname)
end