wordpress-to-kirby 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,3 +1,7 @@
1
- # Temporary Holding Version
1
+ # wordpress-to-kirby
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A Node.js-based command-line utility to convert a WordPress XML export to a directory of Kirby CMS files.
4
+
5
+ ## Options
6
+
7
+ Use `wordpress-to-kirby --help` for a list of options.
package/index.js ADDED
@@ -0,0 +1,249 @@
1
+ #!/usr/bin/env node
2
+ import { readFile, writeFile, mkdir } from 'node:fs/promises';
3
+ import { join, dirname } from 'node:path';
4
+
5
+ import opsh from 'opsh';
6
+ import { JSDOM } from 'jsdom';
7
+ import { load } from 'js-yaml';
8
+ import { unserialize } from 'php-serialize';
9
+
10
+ import { serialize } from './src/kirby-txt.js';
11
+ import { uuid } from './src/uuid.js';
12
+
13
+ async function getPackage() {
14
+ return JSON.parse(
15
+ await readFile(new URL('./package.json', import.meta.url))
16
+ );
17
+ }
18
+
19
+ const { operands, options } = opsh(
20
+ process.argv.slice(2),
21
+ ['help', 'h', 'version', 'v']
22
+ );
23
+
24
+ if (options.h || options.help) {
25
+ await printHelp();
26
+ process.exit();
27
+ }
28
+
29
+ if (options.v || options.version) {
30
+ const pkg = await getPackage();
31
+ console.log(pkg.version);
32
+ process.exit();
33
+ }
34
+
35
+ if (operands.length !== 1) {
36
+ console.error(`Exactly one operand is supported, ${operands.length} provided. Exiting.`);
37
+ process.exit(1);
38
+ }
39
+
40
+ const parsers = {};
41
+ const serializers = {};
42
+
43
+ function commasep(val) {
44
+ return (val || '').split(/\s*,\s*/).filter(Boolean);
45
+ }
46
+
47
+ commasep(options.parse).forEach(pair => {
48
+ let [key, val] = pair.split('=');
49
+ key = key.trim();
50
+ val = val.trim();
51
+ if (!key || !val) {
52
+ return;
53
+ }
54
+ parsers[key] = val;
55
+ });
56
+
57
+ commasep(options.serialize).forEach(pair => {
58
+ let [key, val] = pair.split('=');
59
+ key = key.trim();
60
+ val = val.trim();
61
+ if (!key || !val) {
62
+ return;
63
+ }
64
+ serializers[key] = val;
65
+ });
66
+
67
+ const keys_to_include = new Set(commasep(options.include));
68
+ const keys_to_exclude = new Set(options.include ? [] : commasep(options.exclude));
69
+
70
+ function pluck(item) {
71
+ if (keys_to_include.size) {
72
+ return Object.fromEntries(
73
+ Object.entries(item)
74
+ .filter(entry => keys_to_include.has(entry[0]))
75
+ );
76
+ }
77
+ if (keys_to_exclude.size) {
78
+ return Object.fromEntries(
79
+ Object.entries(item)
80
+ .filter(entry => !keys_to_exclude.has(entry[0]))
81
+ );
82
+ }
83
+ return item;
84
+ }
85
+
86
+ function parse(key, value) {
87
+ if (!value) {
88
+ return value;
89
+ }
90
+ switch(parsers[key]) {
91
+ case 'php':
92
+ try {
93
+ return unserialize(value);
94
+ } catch(err) {
95
+ console.error(`Can’t unserialize PHP value, skipping: ${value}`);
96
+ return value;
97
+ }
98
+ case 'yaml':
99
+ try {
100
+ return load(value);
101
+ } catch (err) {
102
+ console.error(`Can’t parse YAML value, skipping: ${value}`);
103
+ return value;
104
+ }
105
+ case 'json':
106
+ try {
107
+ return JSON.parse(value);
108
+ } catch(err) {
109
+ console.error(`Can’t parse JSON value, skipping: ${value}`);
110
+ return value;
111
+ }
112
+ default:
113
+ return value;
114
+ }
115
+ }
116
+
117
+ const document = new JSDOM(
118
+ await readFile(operands[0], 'utf8'),
119
+ {
120
+ contentType: 'application/xml'
121
+ }
122
+ ).window.document;
123
+
124
+ const items = Array.from(document.querySelectorAll('item')).map(extract);
125
+
126
+ function extract(item) {
127
+ const select = (sel, ns, all = false, el) => {
128
+ if (ns) {
129
+ const candidates = Array.from((el ?? item).querySelectorAll(sel) ?? [])
130
+ .filter(it => it.nodeName === `${ns}:${sel}`);
131
+ return all ? candidates : candidates[0];
132
+ }
133
+ return (el ?? item).querySelector(sel);
134
+ }
135
+ return {
136
+ title: select('title').textContent,
137
+ link: select('link').textContent,
138
+ pubDate: select('pubDate').textContent,
139
+ creator: select('creator', 'dc').textContent,
140
+ guid: select('guid').textContent,
141
+ guid_isPermaLink: select('guid').getAttribute('isPermaLink'),
142
+ description: select('description').textContent,
143
+ content: select('encoded', 'content').textContent,
144
+ excerpt: select('encoded', 'excerpt').textContent,
145
+ post_id: select('post_id', 'wp').textContent,
146
+ post_date: select('post_date', 'wp').textContent,
147
+ post_date_gmt: select('post_date_gmt', 'wp').textContent,
148
+ post_modified: select('post_modified', 'wp').textContent,
149
+ post_modified_gmt: select('post_modified_gmt', 'wp').textContent,
150
+ comment_status: select('comment_status', 'wp').textContent,
151
+ ping_status: select('ping_status', 'wp').textContent,
152
+ post_name: select('post_name', 'wp').textContent,
153
+ status: select('status', 'wp').textContent,
154
+ post_parent: select('post_parent', 'wp').textContent,
155
+ menu_order: select('menu_order', 'wp').textContent,
156
+ post_type: select('post_type', 'wp').textContent,
157
+ post_password: select('post_password', 'wp').textContent,
158
+ is_sticky: select('is_sticky', 'wp').textContent,
159
+ ...select('postmeta', 'wp', true).reduce((acc, it) => {
160
+ const key = select('meta_key', 'wp', false, it).textContent;
161
+ const value = select('meta_value', 'wp', false, it).textContent;
162
+ if (!key.match(/^_/)) {
163
+ acc['postmeta_' + key] = parse('postmeta_' + key, value);
164
+ }
165
+ return acc;
166
+ }, {}),
167
+ uuid: uuid()
168
+ }
169
+ }
170
+
171
+ const outdir = options.o || options.outdir || 'wordpress-to-kirby';
172
+
173
+ function outpath(item) {
174
+ let parts = (new URL(item.link)).pathname.split('/').filter(Boolean);
175
+ if (item.post_type === 'attachment') {
176
+ parts = ['attachments', ...parts];
177
+ }
178
+ if (!parts.length) {
179
+ parts = ['_unlinked', item.post_type, item.post_id, ...parts];
180
+ }
181
+ return join(outdir, ...parts, item.post_type + '.txt');
182
+ }
183
+
184
+ for await (let item of items) {
185
+ const destination = outpath(item);
186
+ await mkdir(dirname(destination), { recursive: true });
187
+ await writeFile(destination, serialize(pluck(item)));
188
+ };
189
+
190
+ function printHelp() {
191
+ console.log(`wordpress-to-kirby:
192
+
193
+ transform a WordPress XML export
194
+ into a directory of Kirby CMS files.
195
+
196
+ Usage:
197
+
198
+ wordpress-to-kirby <options> <xml-file>
199
+
200
+ Options:
201
+
202
+ -h, --help
203
+ Print out this help information.
204
+
205
+ -v, --version
206
+ Show the program version.
207
+
208
+ -o, --outdir
209
+ Root directory to output to, relative to CWD.
210
+ Default: 'wordpress-to-kirby'
211
+
212
+ --parse=<rules>
213
+ Instructions to parse some fields, in the form of:
214
+ "<key>=<parser>,<key>=<parser>,…"
215
+ Supported parsers:
216
+ php: deserialize PHP object.
217
+ yaml: parse as YAML.
218
+ json: parse as JSON.
219
+ Prefix post meta fields with "postmeta".
220
+
221
+ --serialize=<rules>
222
+ Not implemented yet.
223
+
224
+ --exclude=<keys>
225
+ Exclude certain keys from the output. Comma-separated.
226
+
227
+ --include=<keys>
228
+ Only include certain keys from the output. Comma-separated.
229
+ When '--include' is specified, any '--exclude' is ignored.
230
+
231
+ Keys extracted from the XML:
232
+
233
+ Top-level:
234
+
235
+ title, link, pubDate, creator, guid, guid_isPermaLink,
236
+ description, content, excerpt, post_id, post_date,
237
+ post_date_gmt, post_modified, post_modified_gmt,
238
+ comment_status, ping_status, post_name, status,
239
+ post_parent, menu_order, post_type, post_password, is_sticky
240
+
241
+ Post metadata:
242
+
243
+ postmeta_*
244
+
245
+ Keys generated from scratch:
246
+
247
+ uuid — sixteen random alphanumeric characters
248
+ `);
249
+ }
package/package.json CHANGED
@@ -1,6 +1,27 @@
1
1
  {
2
2
  "name": "wordpress-to-kirby",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.1",
4
+ "description": "A CLI to convert WordPress XML to Kirby CMS files",
5
+ "homepage": "https://github.com/tocmai/wordpress-to-kirby#readme",
6
+ "bugs": {
7
+ "url": "https://github.com/tocmai/wordpress-to-kirby/issues"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/tocmai/wordpress-to-kirby.git"
12
+ },
13
+ "scripts": {
14
+ "test": "node --test 'test/**/*.test.js'"
15
+ },
16
+ "bin": {
17
+ "wordpress-to-kirby": "index.js"
18
+ },
19
+ "type": "module",
20
+ "main": "index.js",
21
+ "dependencies": {
22
+ "js-yaml": "^5.5.0",
23
+ "jsdom": "^31.0.1",
24
+ "opsh": "^1.1.0",
25
+ "php-serialize": "^5.1.3"
26
+ }
27
+ }
@@ -0,0 +1,46 @@
1
+ import { load, dump } from 'js-yaml';
2
+
3
+ // Based on: https://github.com/getkirby/kirby/blob/main/src/Data/Txt.php
4
+
5
+ // https://danburzo.ro/snippets/slugify/
6
+ function slugify(str = '') {
7
+ return str
8
+ .trim()
9
+ .normalize('NFD')
10
+ .replace(/[\u0300-\u036f]/g, '')
11
+ .toLowerCase()
12
+ .replace(/[^a-z0-9]+/g, '-')
13
+ .replace(/(^\-)|(\-$)/g, '');
14
+ }
15
+
16
+ function cap(str = '') {
17
+ return str[0].toUpperCase() + str.slice(1);
18
+ }
19
+
20
+ export function parse(str = '') {
21
+ const data = {};
22
+ str.split(/\n----\s*\n*/).forEach(field => {
23
+ let [_, key, value] = field.match(/\s*([^:]+?)\s*:([^:]*)/) ?? [];
24
+ if (!key) {
25
+ return;
26
+ }
27
+ data[key
28
+ .toLowerCase()
29
+ .replace(/[ -]/g, '_')
30
+ ] = value.trim().replace(/(\n|^)\\\\----/, '----');
31
+ });
32
+ return data;
33
+ }
34
+
35
+ export function serialize(data = {}) {
36
+ return Object.entries(data)
37
+ .filter(entry => entry[0] && entry[1])
38
+ .map(entry => {
39
+ const value = (Array.isArray(entry[1]) ? dump(entry[1]) : entry[1] + '')
40
+ .replace(/(?:\n|^)----/g, '\\----')
41
+ .trim();
42
+ const key = cap(slugify(entry[0]));
43
+ return key + (value.indexOf('\n') > -1 ? ':\n\n' : ': ') + value;
44
+ })
45
+ .join("\n\n----\n\n");
46
+ }
package/src/uuid.js ADDED
@@ -0,0 +1,12 @@
1
+ /* generate random UUIDs */
2
+ import { randomBytes } from 'node:crypto';
3
+
4
+ export function uuid(len = 16) {
5
+ let res = '';
6
+ while (res.length < len) {
7
+ res += randomBytes(len)
8
+ .toString('base64')
9
+ .replace(/[^a-z0-9]/gi, '');
10
+ }
11
+ return res.slice(0, len);
12
+ }
@@ -0,0 +1,16 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert';
3
+
4
+ import { parse, serialize } from '../src/kirby-txt.js';
5
+
6
+ test('parse()', () => {
7
+ assert.deepEqual(parse('Hello: world'), {
8
+ hello: 'world'
9
+ });
10
+ });
11
+
12
+ test('serialize()', () => {
13
+ assert.deepEqual(serialize({
14
+ hello: 'worldwide\nweb'
15
+ }), 'Hello:\n\nworldwide\nweb');
16
+ });
@@ -0,0 +1,8 @@
1
+ import assert from 'node:assert';
2
+ import test from 'node:test';
3
+
4
+ import { uuid } from '../src/uuid.js';
5
+
6
+ test('uuid()', () => {
7
+ assert.ok(uuid().match(/^[a-z0-9]{16}$/i));
8
+ });