-
Notifications
You must be signed in to change notification settings - Fork 18
/
jekylltoghost.rb
152 lines (113 loc) · 2.75 KB
/
jekylltoghost.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Jekyll-to-Ghost
#
# This Jekyll plugin exports your Markdown posts into a format that can be easily imported by Ghost.
# http://ghost.org
#
# Author: Matt Harzewski
# Copyright: Copyright 2013 Matt Harzewski
# License: GPLv2 or later
# Version: 1.0.0
require 'json'
module Jekyll
class GhostPage < StaticFile
def initialize(site, base, dir, name, contents)
@site = site
@base = base
@dir = dir
@name = name
@contents = contents
end
def write(dest)
File.open(File.join(@site.config['destination'], 'ghost_export.json'), 'w') do |f|
f.write(@contents.to_json.to_s)
end
true
end
end
class JsonFileGenerator < Generator
safe true
def initialize(site)
super
@tags = []
@postTagMap = Hash.new
end
def generate(site)
converter = site.getConverterImpl(Jekyll::Converters::Markdown)
ex_posts = []
id = 0
site.posts.each do |post|
timestamp = post.date.to_i * 1000
ex_post = {
"id" => id,
"title" => post.title,
"slug" => post.slug,
"markdown" => post.content,
"html" => converter.convert(post.content),
"image" => nil,
"featured" => 0,
"page" => 0,
"status" => "published",
"language" => "en_US",
"meta_title" => nil,
"meta_description" => nil,
"author_id" => 1,
"created_at" => timestamp,
"created_by" => 1,
"updated_at" => timestamp,
"updated_by" => 1,
"published_at" => timestamp,
"published_by" => 1
}
ex_posts.push(ex_post)
self.process_tags(id, post.tags, post.categories)
id += 1
end
export_object = {
"meta" => {
"exported_on" => Time.now.to_i * 1000,
"version" => "000"
},
"data" => {
"posts" => ex_posts,
"tags" => self.tag_objects,
"posts_tags" => self.posts_tag_objects
}
}
site.static_files << GhostPage.new(site, site.source, site.config['destination'], 'ghost_export.json', export_object)
end
def process_tags(postId, tags, categories)
unique_tags = tags | categories
unique_tags = unique_tags.map do |t|
t = t.chomp(",")
t = t.downcase
end
@tags = unique_tags | @tags
@postTagMap[postId] = unique_tags
end
def tag_objects
tag_array = []
@tags.each do |tag|
tag_array.push({
"id" => tag_array.size,
"name" => tag,
"slug" => tag.downcase,
"description" => ""
})
end
return tag_array
end
def posts_tag_objects
posts_tag_array = []
@postTagMap.each do |post, tags|
tags.each do |tag|
posts_tag_array.push({
"id" => (posts_tag_array.size + 1),
"post_id" => post,
"tag_id" => @tags.index(tag)
})
end
end
return posts_tag_array
end
end
end