wordpress-to-kirby 0.0.2 → 0.1.0

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.
Files changed (2) hide show
  1. package/index.js +199 -65
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
  import { readFile, writeFile, mkdir } from 'node:fs/promises';
3
- import { join, dirname, relative } from 'node:path';
3
+ import { join, dirname, extname, relative } from 'node:path';
4
4
 
5
5
  import opsh from 'opsh';
6
6
  import { JSDOM } from 'jsdom';
7
- import { load } from 'js-yaml';
7
+ import { load, dump } from 'js-yaml';
8
8
  import { serialize, unserialize } from 'php-serialize';
9
9
 
10
10
  import { serialize as serialize_item } from './src/kirby-txt.js';
@@ -32,13 +32,24 @@ if (options.v || options.version) {
32
32
  process.exit();
33
33
  }
34
34
 
35
- if (operands.length !== 1) {
36
- console.error(`Exactly one operand is supported, ${operands.length} provided. Exiting.`);
35
+ const [command, ...files ] = operands;
36
+
37
+ if (command !== 'generate' && command !== 'download') {
38
+ console.error(`Supported commands: generate, download; ${command} provided. Exiting.`);
37
39
  process.exit(1);
38
40
  }
39
41
 
40
- const parsers = {};
41
- const serializers = {};
42
+ if (files.length !== 1) {
43
+ console.error(`Exactly one file is supported, ${files.length} provided. Exiting.`);
44
+ process.exit(1);
45
+ }
46
+
47
+ const parsers = {
48
+ 'postmeta_wp_attachment_metadata': 'php'
49
+ };
50
+ const serializers = {
51
+ 'postmeta_wp_attachment_metadata': 'json'
52
+ };
42
53
 
43
54
  function commasep(val) {
44
55
  return (val || '').split(/\s*,\s*/).filter(Boolean);
@@ -64,8 +75,11 @@ commasep(options.serialize).forEach(pair => {
64
75
  serializers[key] = val;
65
76
  });
66
77
 
67
- const keys_to_include = new Set(commasep(options.include));
68
- const keys_to_exclude = new Set(options.include ? [] : commasep(options.exclude));
78
+ const keys_to_include = new Set(commasep(options['include-keys']));
79
+ const keys_to_exclude = new Set(options['include-keys'] ? [] : commasep(options['exclude-keys']));
80
+
81
+ const types_to_include = new Set(commasep(options['include-types']));
82
+ const types_to_exclude = new Set(options['include-types'] ? [] : commasep(options['exclude-types']));
69
83
 
70
84
  function process_item(item) {
71
85
  let entries = Object.entries(item);
@@ -91,21 +105,30 @@ function parse_value(key, value) {
91
105
  try {
92
106
  return unserialize(value);
93
107
  } catch(err) {
94
- console.error(`Can’t serialize PHP value, skipping: ${value}`);
108
+ console.error(`Warn: Can’t serialize PHP value, skipping: ${value}`);
95
109
  return value;
96
110
  }
97
111
  case 'yaml':
98
112
  try {
99
113
  return load(value);
100
114
  } catch (err) {
101
- console.error(`Can’t serialize YAML value, skipping: ${value}`);
115
+ console.error(`Warn: Can’t serialize YAML value, skipping: ${value}`);
102
116
  return value;
103
117
  }
104
118
  case 'json':
105
119
  try {
106
120
  return JSON.parse(value);
107
121
  } catch(err) {
108
- console.error(`Can’t serialize JSON value, skipping: ${value}`);
122
+ console.error(`Warn: Can’t serialize JSON value, skipping: ${value}`);
123
+ return value;
124
+ }
125
+ case 'html':
126
+ try {
127
+ return new JSDOM(
128
+ `<!doctype html><html><head></head><body>${value}</body></html>`
129
+ ).window.document;
130
+ } catch(err) {
131
+ console.error(`Warn: Can’t serialize JSON value, skipping: ${value}`);
109
132
  return value;
110
133
  }
111
134
  default:
@@ -122,21 +145,28 @@ function serialize_value(key, value) {
122
145
  try {
123
146
  return serialize(value);
124
147
  } catch(err) {
125
- console.error(`Can’t stringify PHP value, skipping: ${value}`);
148
+ console.error(`Warn: Can’t stringify PHP value, skipping: ${value}`);
126
149
  return value;
127
150
  }
128
151
  case 'yaml':
129
152
  try {
130
153
  return dump(value);
131
154
  } catch (err) {
132
- console.error(`Can’t stringify YAML value, skipping: ${value}`);
155
+ console.error(`Warn: Can’t stringify YAML value, skipping: ${value}`);
133
156
  return value;
134
157
  }
135
158
  case 'json':
136
159
  try {
137
160
  return JSON.stringify(value);
138
161
  } catch(err) {
139
- console.error(`Can’t stringify JSON value, skipping: ${value}`);
162
+ console.error(`Warn: Can’t stringify JSON value, skipping: ${value}`);
163
+ return value;
164
+ }
165
+ case 'html':
166
+ try {
167
+ return value.body.innerHTML;
168
+ } catch(err) {
169
+ console.error(`Warn: Can’t stringify HTML value, skipping: ${value}`);
140
170
  return value;
141
171
  }
142
172
  default:
@@ -145,13 +175,22 @@ function serialize_value(key, value) {
145
175
  }
146
176
 
147
177
  const document = new JSDOM(
148
- await readFile(operands[0], 'utf8'),
178
+ await readFile(files[0], 'utf8'),
149
179
  {
150
180
  contentType: 'application/xml'
151
181
  }
152
182
  ).window.document;
153
183
 
154
- const items = Array.from(document.querySelectorAll('item')).map(extract);
184
+ const items = Array.from(document.querySelectorAll('item'))
185
+ .map(extract)
186
+ .filter(item => {
187
+ if (types_to_include.size) {
188
+ return types_to_include.has(item.post_type);
189
+ } else if (types_to_exclude.size) {
190
+ return !types_to_exclude.has(item.post_type);
191
+ }
192
+ return true;
193
+ });
155
194
 
156
195
  function extract(item) {
157
196
  const select = (sel, ns, all = false, el) => {
@@ -163,35 +202,34 @@ function extract(item) {
163
202
  return (el ?? item).querySelector(sel);
164
203
  }
165
204
  return {
166
- title: select('title').textContent,
167
- link: select('link').textContent,
168
- pubDate: select('pubDate').textContent,
169
- creator: select('creator', 'dc').textContent,
170
- guid: select('guid').textContent,
171
- guid_isPermaLink: select('guid').getAttribute('isPermaLink'),
172
- description: select('description').textContent,
173
- content: select('encoded', 'content').textContent,
174
- excerpt: select('encoded', 'excerpt').textContent,
175
- post_id: select('post_id', 'wp').textContent,
176
- post_date: select('post_date', 'wp').textContent,
177
- post_date_gmt: select('post_date_gmt', 'wp').textContent,
178
- post_modified: select('post_modified', 'wp').textContent,
179
- post_modified_gmt: select('post_modified_gmt', 'wp').textContent,
180
- comment_status: select('comment_status', 'wp').textContent,
181
- ping_status: select('ping_status', 'wp').textContent,
182
- post_name: select('post_name', 'wp').textContent,
183
- status: select('status', 'wp').textContent,
184
- post_parent: select('post_parent', 'wp').textContent,
185
- menu_order: select('menu_order', 'wp').textContent,
186
- post_type: select('post_type', 'wp').textContent,
187
- post_password: select('post_password', 'wp').textContent,
188
- is_sticky: select('is_sticky', 'wp').textContent,
205
+ title: parse_value('title', select('title').textContent),
206
+ link: parse_value('link', select('link').textContent),
207
+ pubDate: parse_value('pubDate', select('pubDate').textContent),
208
+ creator: parse_value('creator', select('creator', 'dc').textContent),
209
+ guid: parse_value('guid', select('guid').textContent),
210
+ guid_isPermaLink: parse_value('guid_isPermaLink', select('guid').getAttribute('isPermaLink')),
211
+ description: parse_value('description', select('description').textContent),
212
+ content: parse_value('content', select('encoded', 'content').textContent),
213
+ excerpt: parse_value('excerpt', select('encoded', 'excerpt').textContent),
214
+ post_id: parse_value('post_id', select('post_id', 'wp').textContent),
215
+ post_date: parse_value('post_date', select('post_date', 'wp').textContent),
216
+ post_date_gmt: parse_value('post_date_gmt', select('post_date_gmt', 'wp').textContent),
217
+ post_modified: parse_value('post_modified', select('post_modified', 'wp').textContent),
218
+ post_modified_gmt: parse_value('post_modified_gmt', select('post_modified_gmt', 'wp').textContent),
219
+ comment_status: parse_value('comment_status', select('comment_status', 'wp').textContent),
220
+ ping_status: parse_value('ping_status', select('ping_status', 'wp').textContent),
221
+ post_name: parse_value('post_name', select('post_name', 'wp').textContent),
222
+ status: parse_value('status', select('status', 'wp').textContent),
223
+ post_parent: parse_value('post_parent', select('post_parent', 'wp').textContent),
224
+ menu_order: parse_value('menu_order', select('menu_order', 'wp').textContent),
225
+ post_type: parse_value('post_type', select('post_type', 'wp').textContent),
226
+ post_password: parse_value('post_password', select('post_password', 'wp').textContent),
227
+ attachment_url: parse_value('attachment_url', select('attachment_url', 'wp')?.textContent),
228
+ is_sticky: parse_value('is_sticky', select('is_sticky', 'wp').textContent),
189
229
  ...select('postmeta', 'wp', true).reduce((acc, it) => {
190
230
  const key = select('meta_key', 'wp', false, it).textContent;
191
231
  const value = select('meta_value', 'wp', false, it).textContent;
192
- if (!key.match(/^_/)) {
193
- acc['postmeta_' + key] = parse_value('postmeta_' + key, value);
194
- }
232
+ acc['postmeta' + key] = parse_value('postmeta' + key, value);
195
233
  return acc;
196
234
  }, {}),
197
235
  uuid: uuid()
@@ -203,33 +241,111 @@ const outdir = options.o || options.outdir || 'wordpress-to-kirby';
203
241
  function outpath(item) {
204
242
  let parts = (new URL(item.link)).pathname.split('/').filter(Boolean);
205
243
  if (item.post_type === 'attachment') {
206
- parts = ['attachments', ...parts];
244
+ const filename = parts.pop() + (extname(item.attachment_url ?? '') ?? '') + '.txt';
245
+ return join(outdir, 'content', ...parts.filter(p => p !== 'attachment'), filename);
207
246
  }
208
247
  if (!parts.length) {
209
248
  parts = ['_unlinked', item.post_type, item.post_id, ...parts];
210
249
  }
211
- return join(outdir, ...parts, item.post_type + '.txt');
250
+ return join(outdir, 'content', ...parts, item.post_type + '.txt');
212
251
  }
213
252
 
214
- for await (let item of items) {
215
- const destination = outpath(item);
216
- await mkdir(dirname(destination), { recursive: true });
217
- await writeFile(destination, serialize_item(process_item(item)));
218
- };
253
+ if (command === 'generate') {
254
+ for await (let item of items) {
255
+ const destination = outpath(item);
256
+ await mkdir(dirname(destination), { recursive: true });
257
+ await writeFile(destination, serialize_item(process_item(item)));
258
+ };
259
+
260
+ const metadata = {
261
+ output: [],
262
+ types: {}
263
+ };
264
+
265
+ items.forEach(item => {
266
+ metadata.output.push({
267
+ post_id: +item.post_id,
268
+ post_type: item.post_type,
269
+ url: item.attachment_url ?? item.link,
270
+ uuid: item.uuid,
271
+ path: relative(outdir, outpath(item))
272
+ });
273
+ metadata.types[item.post_type] = (metadata.types[item.post_type] || [])
274
+ .concat(Object.keys(item));
275
+ });
219
276
 
220
- const metadata = {
221
- output: []
222
- };
277
+ const blueprints_path = join(outdir, 'site', 'blueprints');
278
+ await mkdir(join(blueprints_path, 'pages'), { recursive: true });
223
279
 
224
- items.forEach(item => {
225
- metadata.output.push({
226
- post_id: +item.post_id,
227
- uuid: item.uuid,
228
- path: relative(outdir, outpath(item))
280
+ Object.keys(metadata.types).forEach(key => {
281
+ metadata.types[key] = Array.from(new Set(metadata.types[key])).sort();
282
+ // Make blueprints
283
+ const blueprint_content = metadata.types[key].reduce((acc, curr) => {
284
+ acc[curr] = {
285
+ type: 'text'
286
+ };
287
+ return acc;
288
+ }, {});
289
+ writeFile(join(blueprints_path, 'pages', key + '.yml'), dump({
290
+ tabs: {
291
+ fields: {
292
+ label: 'Fields',
293
+ sections: {
294
+ fields: {
295
+ type: 'fields',
296
+ fields: blueprint_content
297
+ }
298
+ }
299
+ },
300
+ pages: {
301
+ label: 'Pages',
302
+ sections: {
303
+ pages: {
304
+ type: 'pages'
305
+ }
306
+ }
307
+ },
308
+ files: {
309
+ label: 'Files',
310
+ sections: {
311
+ files: {
312
+ type: 'files'
313
+ }
314
+ }
315
+ }
316
+ }
317
+ }));
229
318
  });
230
- });
231
319
 
232
- await writeFile(join(outdir, 'metadata.json'), JSON.stringify(metadata, null, '\t'));
320
+
321
+ const default_blueprint = {
322
+ tabs: {
323
+ pages: {
324
+ label: 'Pages',
325
+ sections: {
326
+ pages: {
327
+ type: 'pages'
328
+ }
329
+ }
330
+ },
331
+ files: {
332
+ label: 'Files',
333
+ sections: {
334
+ files: {
335
+ type: 'files'
336
+ }
337
+ }
338
+ }
339
+ }
340
+ };
341
+ await writeFile(join(blueprints_path, 'pages', 'default.yml'), dump(default_blueprint));
342
+ await writeFile(join(blueprints_path, 'site.yml'), dump(default_blueprint));
343
+
344
+ await writeFile(join(outdir, 'metadata.json'), JSON.stringify(metadata, null, '\t'));
345
+ } else if (command === 'download') {
346
+ console.error('Not implemented yet, exiting.');
347
+ process.exit(1);
348
+ }
233
349
 
234
350
  function printHelp() {
235
351
  console.log(`wordpress-to-kirby:
@@ -239,7 +355,15 @@ function printHelp() {
239
355
 
240
356
  Usage:
241
357
 
242
- wordpress-to-kirby <options> <xml-file>
358
+ wordpress-to-kirby <command> <xml-file> <options>
359
+
360
+ Commands:
361
+
362
+ generate
363
+ Generate a Kirby CMS file structure from the provided XML file.
364
+
365
+ download
366
+ Download attachments. Not implemented.
243
367
 
244
368
  Options:
245
369
 
@@ -260,6 +384,7 @@ Options:
260
384
  php: deserialize PHP object.
261
385
  yaml: parse as YAML.
262
386
  json: parse as JSON.
387
+ html: parse as HTML.
263
388
  Prefix post meta fields with "postmeta".
264
389
 
265
390
  --serialize=<rules>
@@ -269,14 +394,22 @@ Options:
269
394
  php: serialize as PHP object.
270
395
  yaml: serialize as YAML.
271
396
  json: serialize as JSON.
397
+ html: serialize as HTML.
272
398
  Prefix post meta fields with "postmeta".
273
399
 
274
- --exclude=<keys>
400
+ --exclude-keys=<keys>
275
401
  Exclude certain keys from the output. Comma-separated.
276
402
 
277
- --include=<keys>
403
+ --include-keys=<keys>
278
404
  Only include certain keys from the output. Comma-separated.
279
- When '--include' is specified, any '--exclude' is ignored.
405
+ When '--include-keys' is specified, any '--exclude-keys' is ignored.
406
+
407
+ --exclude-types=<types>
408
+ Exclude certain post types from the output. Comma-separated.
409
+
410
+ --include-types=<types>
411
+ Only include certain post types from the output. Comma-separated.
412
+ When '--include-types' is specified, any '--exclude-types' is ignored.
280
413
 
281
414
  Keys extracted from the XML:
282
415
 
@@ -286,12 +419,13 @@ Keys extracted from the XML:
286
419
  description, content, excerpt, post_id, post_date,
287
420
  post_date_gmt, post_modified, post_modified_gmt,
288
421
  comment_status, ping_status, post_name, status,
289
- post_parent, menu_order, post_type, post_password, is_sticky
422
+ post_parent, menu_order, post_type, post_password, is_sticky,
423
+ attachment_url
290
424
 
291
425
  Post metadata:
292
426
 
293
427
  Fields from <wp:postmeta> not starting with an underscore
294
- are included in the output with the 'postmeta_*' prefix.
428
+ are included in the output with the 'postmeta*' prefix.
295
429
 
296
430
  Keys generated from scratch:
297
431
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wordpress-to-kirby",
3
- "version": "0.0.2",
3
+ "version": "0.1.0",
4
4
  "description": "A CLI to convert WordPress XML to Kirby CMS files",
5
5
  "homepage": "https://github.com/tocmai/wordpress-to-kirby#readme",
6
6
  "bugs": {