Class: JekyllImport::Importers::Ghost

Inherits:
JekyllImport::Importer show all
Defined in:
lib/jekyll-import/importers/ghost.rb

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.fetch_posts(dbfile) ⇒ Object



31
32
33
34
35
# File 'lib/jekyll-import/importers/ghost.rb', line 31

def fetch_posts(dbfile)
  db = Sequel.sqlite(dbfile)
  query = "SELECT `title`, `slug`, `markdown`, `created_at`, `published_at`, `status`, `page` FROM posts"
  db[query]
end

.process(options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/jekyll-import/importers/ghost.rb', line 18

def self.process(options)
  posts = fetch_posts(options.fetch("dbfile", "ghost.db"))
  unless posts.empty?
    FileUtils.mkdir_p("_posts")
    FileUtils.mkdir_p("_drafts")
    posts.each do |post|
      write_post_to_file(post)
    end
  end
end

.require_depsObject



8
9
10
11
12
13
14
15
16
# File 'lib/jekyll-import/importers/ghost.rb', line 8

def self.require_deps
  JekyllImport.require_with_fallback(%w(
    rubygems
    sequel
    sqlite3
    fileutils
    safe_yaml
  ))
end

.specify_options(c) ⇒ Object



4
5
6
# File 'lib/jekyll-import/importers/ghost.rb', line 4

def self.specify_options(c)
  c.option "dbfile", "--dbfile", "Database file (default: ghost.db)"
end

.write_file(filename, frontmatter, content) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/jekyll-import/importers/ghost.rb', line 71

def write_file(filename, frontmatter, content)
  File.open(filename, "w") do |f|
    f.puts frontmatter
    f.puts "---"
    f.puts content
  end
end

.write_post_to_file(post) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/jekyll-import/importers/ghost.rb', line 37

def write_post_to_file(post)
  # detect if the post is a draft
  draft = post[:status].eql?("draft")

  # detect if the post is considered a static page
  page = post[:page]

  # the publish date if the post has been published, creation date otherwise
  date = Time.at(post[draft ? :created_at : :published_at].to_i)

  if page
    # the filename under which the page is stored
    filename = "#{post[:slug]}.markdown"
  else
    # the directory where the file will be saved to. either _drafts or _posts
    directory = draft ? "_drafts" : "_posts"

    # the filename under which the post is stored
    filename = File.join(directory, "#{date.strftime("%Y-%m-%d")}-#{post[:slug]}.markdown")
  end

  # the YAML FrontMatter
  frontmatter = {
    "layout" => page ? "page" : "post",
    "title"  => post[:title],
  }
  frontmatter["date"] = date if !page && !draft # only add the date to the frontmatter when the post is published
  frontmatter["published"] = false if page && draft # set published to false for draft pages
  frontmatter.delete_if { |_k, v| v.nil? || v == "" } # removes empty fields

  # write the posts to disk
  write_file(filename, frontmatter.to_yaml, post[:markdown])
end