-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghost-blogger.rb
executable file
·210 lines (178 loc) · 6.31 KB
/
ghost-blogger.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/ruby
require 'json'
require 'nokogiri'
require 'optparse'
require 'set'
require 'time'
# All Ghost timestamps are integer milliseconds.
class Time
def millis
return (self.to_f * 1000).to_i
end
end
# Settings and options parsing.
$settings = (Struct.new(:publish, :verbose, :wrap_html_in_mobiledoc,
:process_ranges)).new()
$settings.process_ranges = []
OptionParser.new {
|opts|
app_name = File.basename($0)
opts.banner = "Usage: #{app_name} [options] <blogger_export.xml>"
opts.on('-pRANGE', '--process=RANGE',
'Which post indices to process. Each value should either be a ' +
'single index, or a ruby-style inclusive or exclusive range. ' +
'May be repeated.') {
|opt|
if opt =~ /^(-?\d+)$/
n = $1.to_i
$settings.process_ranges << Range.new(n, n)
elsif opt =~ /^(-?\d+)\.\.(-?\d+)$/
$settings.process_ranges << Range.new($1.to_i, $2.to_i, exclude_end=false)
elsif opt =~ /^(-?\d+)\.\.\.(-?\d+)$/
$settings.process_ranges << Range.new($1.to_i, $2.to_i, exclude_end=true)
else
raise "Failed to parse --process range \"#{opt}\""
end
}
opts.on('--[no-]publish', 'Whether to mark posts as published or draft') {
|opt|
$settings.publish = opt
}
opts.on("-v", "--[no-]verbose", "Run verbosely") {
|opt|
$settings.verbose = opt
}
opts.on('--[no-]wrap_html_in_mobiledoc',
'If specified, wrap the HTML in a single HTML card in mobiledoc. ' +
'Otherwise, use the "html" field with the expectation of using ' +
'Ghost\'s migration tool') {
|opt|
$settings.wrap_html_in_mobiledoc = opt
}
}.parse!
def wrap_content_html_in_mobiledoc(content)
html_str = content.text
md = {'version' => '0.3.1', 'markups' => [], 'atoms' => [], 'sections' => [[10, 0]]}
md['cards'] = [['html', {'cardName' => 'html', 'html' => html_str}]]
return md
end
class Post < Struct.new(:post_idx, :title, :pub_ts, :update_ts, :slug_tag, :tags, :content, :draft_flag)
def to_json(*args)
hsh = {}
hsh['id'] = post_idx
hsh['title'] = title.text
if hsh['title'].strip.empty?
raise "Post at index #{post_idx} has no title"
end
if $settings.wrap_html_in_mobiledoc
hsh['mobiledoc'] = wrap_content_html_in_mobiledoc(content).to_json
else
hsh['html'] = content.text
end
if slug_tag
orig_url = slug_tag['href']
if orig_url =~ %r{/([^/]+)\.html}
hsh['slug'] = $1
else
$stderr.puts "Failed to parse orig_url: \"#{orig_url}\" for post #{title.text}"
end
else
# Need to create a slug; cross fingers that it's unique :D
hsh['slug'] = hsh['title'].downcase.gsub(/[^a-z0-9]+/, '-')
hsh['slug'].gsub!(/(^-)|(-$)/, '')
end
hsh['featured'] = 0
hsh['page'] = 0
hsh['author_id'] = 1
hsh['created_at'] = pub_ts.millis
hsh['created_by'] = 1
hsh['updated_at'] = update_ts.millis
hsh['updated_by'] = 1
if draft_flag and draft_flag.text == 'yes'
hsh['status'] = 'draft'
if $settings.verbose and $settings.publish
$stderr.puts "Keeping draft post #{hsh['title']} as a draft even though we're in publish mode"
end
elsif $settings.publish
hsh['status'] = 'published'
hsh['published_at'] = pub_ts.millis
hsh['published_by'] = 1
else
hsh['status'] = 'draft'
end
return hsh.to_json(*args)
end
end
class EntryHandler < Struct.new(:entry)
def process()
# Ignore settings, template, and comments for now.
kind_cat = entry.at_css('entry category[scheme="http://schemas.google.com/g/2005#kind"]')
kind = kind_cat['term'].split('#', 2).last
return unless kind == 'post'
title = entry.at_css('entry title')
pub_node = entry.at_css('entry published')
pub_ts = Time.parse(pub_node.text)
update_node = entry.at_css('entry updated')
update_ts = Time.parse(update_node.text)
slug_tag = entry.at_css('entry link[rel="alternate"]')
tags = entry.css('entry category[scheme="http://www.blogger.com/atom/ns#"]')
tags = tags.map {|tag_node| tag_node['term'].gsub(/^"|"$/, '')}
content = entry.at_css('entry content')
draft_flag = entry.at_xpath('.//app:draft', {'app' => 'http://purl.org/atom/app#'})
new_post_idx = $all_posts.length + 1 # posts are 1-indexed.
post = Post.new(new_post_idx, title, pub_ts, update_ts, slug_tag, tags, content, draft_flag)
$all_posts << post
end
end
# Actually start parsing the input file
$all_posts = []
Nokogiri::XML::Reader(File.open(ARGV[0])).each {
|node|
if node.name == 'entry' and node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT
parsed_node = Nokogiri::XML.parse(node.outer_xml)
EntryHandler.new(parsed_node).process
end
}
# Potentially limits which posts are included.
unless $settings.process_ranges.empty?
restricted_posts = []
$settings.process_ranges.each {
|range|
restricted_posts += $all_posts[range]
}
$all_posts = restricted_posts
end
# Find tags from the possibly-restricted set of posts.
$all_tags = Set.new()
$all_posts.each {
|post|
$all_tags |= post.tags
}
# Generate the post-index-to-tag-index mapping.
tag_idx = {}
$all_tags.sort.each_with_index {
|tag, idx|
tag_idx[tag] = {'id' => idx, 'name' => tag}
}
posts_tags = []
$all_posts.each {
|post|
post['tags'].each {
|tag|
pt_pair = {'tag_id' => tag_idx[tag]['id'], 'post_id' => post.post_idx}
posts_tags << pt_pair
}
}
# Pack everything up in a box.
$stderr.puts "Processed #{$all_posts.size} posts and #{$all_tags.size} tags"
if $settings.verbose
$all_posts.each {
|post|
$stderr.puts " #{post.title}"
}
end
data = {'posts' => $all_posts, 'tags' => tag_idx.values,
'posts_tags' => posts_tags}
outer = {'meta' => {'exported_on' => Time.now.millis, 'version' => '4.4.0'},
'data' => data}
puts JSON::pretty_generate(outer)