Class: Plotline::Import::Handlers::MarkdownFile

Inherits:
Base
  • Object
show all
Defined in:
lib/plotline/import/handlers/markdown_file.rb

Constant Summary collapse

FILENAME_SPLIT_PATTERN =
/^(\d{4}-\d{2}-\d{2})-(.*)/
FRONT_MATTER_PATTERN =
/\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
MARKDOWN_EXTENSIONS =
%w(md markdown).freeze

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Plotline::Import::Handlers::Base

Instance Method Details

#import(filename) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/plotline/import/handlers/markdown_file.rb', line 13

def import(filename)
  log "\e[34mImporting:\e[0m #{filename}"

  date, slug = filename_to_date_and_slug(filename)

  if !File.exists?(filename) && entry = Plotline::Entry.find_by(slug: slug)
    log "  \e[31mFile removed, deleting entry\e[0m \e[32m##{entry.id}\e[0m"
    entry.destroy
    return
  end

  full_contents = File.read(filename)
  full_contents = convert_relative_image_paths(filename, full_contents)

  meta, contents = (full_contents)

  if meta['type'].blank?
    raise "\e[31mMissing 'type' attribute in #{filename}\e[0m"
  end

  klass = meta.delete('type').classify.constantize
  entry = klass.find_or_initialize_by(slug: slug)

  process_image_urls(full_contents)
  update_entry(entry, meta, date, contents, full_contents)
end

#supported_file?(filename) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/plotline/import/handlers/markdown_file.rb', line 9

def supported_file?(filename)
  MARKDOWN_EXTENSIONS.include?(File.extname(filename).gsub('.', ''))
end