-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp2ghost.coffee
163 lines (141 loc) · 5.03 KB
/
wp2ghost.coffee
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
fs = require 'fs'
xml = require 'xml2js'
csv = require 'csv-parse'
slugs = require 'slug'
uuid = require 'uuid'
tomd = require('to-markdown').toMarkdown
Promise = (require 'bluebird').Promise
_ = require('lodash')
posts = fs.readFileSync('posts.xml')
users = {}
now = () ->
return new Date().getTime()
findID = (user) ->
if user is ''
return 1
if users[user]?
return parseInt(users[user].ID)
throw "No user found"
toTitleCase = (str) ->
str.replace /\w\S*/g, (txt) ->
txt[0].toUpperCase() + txt[1..txt.length - 1].toLowerCase()
parseUser = (user) ->
if user.first_name == undefined && user.display_name == undefined
return
givenName = 'NO NAME'
if (user.first_name && user.first_name.length && user.last_name && user.last_name.length)
fullName = ("#{user.first_name} #{user.last_name}")
givenName = toTitleCase(fullName)
else
if (user.display_name.split(' ').length > 1)
givenName = toTitleCase(user.display_name)
else
givenName = user.display_name
if !user.user_email || user.user_email.length == 0
user.user_email = '[email protected]';
givenName = '__' + givenName;
return {
id: 1000 + parseInt(user.ID)
name: givenName
slug: user.user_nicename
email: user.user_email
image: null
cover: null
bio: if user.description then user.description.substring(0, 200) else ''
website: user.user_url
location: null
accessibility: null
status: 'active'
language: 'en_US'
meta_title: null
meta_description: null
last_login: null
created_at: new Date(user.user_registered).getTime()
created_by: 1
updated_at: new Date(user.user_registered).getTime()
updated_by: 1
}
parsePost = (post) ->
creator = 1000 + findID(post['dc:creator'][0])
post_content = post['content:encoded'][0]
post_title = post.title[0]
if post_content.length == 0
return
if post_title.lengt == 0
post.title[0] = 'NO TITLE ' + uuid.v4()
regexes = [
/\[caption.*?width=["'](\d+)["'].*?(<([a-z]+)\s+.+?(?:\/>)(?:.+?\/\3>)?)\s*(.*?)\[\/caption\]/g,
/\[caption.*?width=["'](\d+)["'].*?caption=["']([^"']*)["'].*?(<[a-z]+\s+.+?\/>)\[\/caption\]/g,
/(.*?)<(b|em|i|small|strong|sub|sup|ins|del|mark)>([\s\n]*)(.*?)([\s\n]*)<\/\2>(.*)/g,
/(<div.*?>)+(<img.*\/>)(<\/div>)+/g,
/<div><\/div>/g,
]
replacements = [
'<center>$2\n<small>$4</small></center>',
'<center>$3\n<small>$2</small></center>',
'$1$3<$2>$4</$2>$5$6',
'$2',
'',
]
_.forEach(regexes, (regex, i) ->
post_content = post_content.replace(regex, replacements[i])
)
return {
id: parseInt(post['wp:post_id'][0])
title: post.title[0]
slug: slugs(post.title[0])
markdown: tomd(post_content)
html: post_content
image: null
featured: 0
page: 0
status: 'published'
language: 'en_US'
meta_title: null
meta_description: null
author_id: creator
created_at: new Date(post['wp:post_date'][0]).getTime()
created_by: creator
updated_at: new Date(post['wp:post_date'][0]).getTime()
updated_by: creator
published_at: new Date(post['wp:post_date'][0]).getTime()
published_by: creator
}
xml.parseString posts, (err, result) ->
data = result.rss.channel[0]
posts = data.item.filter((item) -> item['wp:status'][0] == 'publish')
csv fs.readFileSync('users.csv'), {columns: true}, (err, result) ->
users = result.reduce ((dict, obj) -> dict[obj['user_login']] = obj if obj['user_login']?; return dict;), {}
tags = {}
post_tags = []
tag_id = 1
pushTag = (tag) ->
if not tags[tag]?
tags[tag] = {
id: tag_id
name: tag
slug: slugs(tag)
description: ''
}
tag_id++
(((((pushTag(cat.$.nicename); post_tags.push({tag_id: tags[cat.$.nicename].id, post_id: parseInt(post['wp:post_id'][0])})) if cat.$.domain is 'post_tag') for cat in post.category) if post.category?) for post in posts)
parsedUsers = _.compact(parseUser(users[user]) for user in Object.keys(users))
parsedPosts = _.compact(parsePost(post) for post in posts)
usersWithPosts = _.filter(parsedUsers, (user) ->
user_id = user.id
for post in parsedPosts
if user.id == post.author_id
return true
return false
)
output =
meta:
exported_on: now()
version: '003'
data:
posts: parsedPosts
tags: (tags[tag] for tag in Object.keys(tags))
posts_tags: post_tags
users: usersWithPosts
roles_users: []
console.log JSON.stringify(output)