Skip to content

Commit 9fc68a8

Browse files
committedNov 28, 2017
Separate cli & module
1 parent d6a6ce0 commit 9fc68a8

File tree

3 files changed

+86
-75
lines changed

3 files changed

+86
-75
lines changed
 

‎cli.js

+2-73
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,6 @@
11
#!/usr/bin/env node
22
'use strict';
3-
const fs = require('fs');
4-
const path = require('path');
5-
const md5 = require('md5');
6-
const xml2js = require('xml2js');
7-
const yaml = require('js-yaml');
8-
const fspath = require('fs-path');
93

10-
const args = process.argv.slice(2);
11-
const parser = new xml2js.Parser();
12-
let i = 0;
4+
const importComments = require('./src/main');
135

14-
console.log('Starting to find comments...');
15-
16-
const createFile = (c, slug) => {
17-
const timestamp = new Date(c.date).getTime();
18-
const filename = path.join(args[1], '/comments/', slug,
19-
'/comment-' + timestamp + '.yml');
20-
const fileInput = yaml.safeDump(c);
21-
22-
fspath.writeFile(path.resolve(filename), fileInput, err => {
23-
i += 1;
24-
console.log(`Adding comment ${i}, by ${c.name}...`);
25-
console.log(`To: ${filename}`);
26-
if (err) {
27-
throw err;
28-
}
29-
console.log('Done.');
30-
});
31-
};
32-
33-
const parseComments = (comments, slug) => {
34-
for (const index in comments) {
35-
if (Object.prototype.hasOwnProperty.call(comments, index)) {
36-
const comment = comments[index];
37-
38-
if (comment['wp:comment_approved'][0] === '1') {
39-
const commentObj = {
40-
name: comment['wp:comment_author'][0],
41-
date: comment['wp:comment_date'][0],
42-
url: comment['wp:comment_author_url'][0],
43-
message: comment['wp:comment_content'][0],
44-
email: md5(comment['wp:comment_author_email'][0])
45-
};
46-
47-
createFile(commentObj, slug);
48-
}
49-
}
50-
}
51-
};
52-
53-
fs.readFile(path.resolve(args[0]), (err, data) => {
54-
if (err) {
55-
throw err;
56-
}
57-
parser.parseString(data, (err, result) => {
58-
if (err) {
59-
throw err;
60-
}
61-
62-
const posts = result.rss.channel[0].item;
63-
64-
for (const index in posts) {
65-
if (Object.prototype.hasOwnProperty.call(posts, index)) {
66-
const post = posts[index];
67-
68-
if (post['wp:comment']) {
69-
const slug = post['wp:post_name'][0];
70-
const comments = post['wp:comment'];
71-
72-
parseComments(comments, slug);
73-
}
74-
}
75-
}
76-
});
77-
});
6+
importComments();

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "wordpress-comments-jekyll-staticman",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Convert Wordpress comment into Jekyll for Staticman.",
5-
"main": "cli.js",
5+
"main": "src/main.js",
66
"scripts": {
77
"test": "node cli test/wordpress-export.xml ."
88
},

‎src/main.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const md5 = require('md5');
4+
const xml2js = require('xml2js');
5+
const yaml = require('js-yaml');
6+
const fspath = require('fs-path');
7+
8+
const args = process.argv.slice(2);
9+
const parser = new xml2js.Parser();
10+
let i = 0;
11+
12+
console.log('Starting to find comments...');
13+
14+
const createFile = (c, slug) => {
15+
const timestamp = new Date(c.date).getTime();
16+
const filename = path.join(args[1], '/comments/', slug,
17+
'/comment-' + timestamp + '.yml');
18+
const fileInput = yaml.safeDump(c);
19+
20+
fspath.writeFile(path.resolve(filename), fileInput, err => {
21+
i += 1;
22+
console.log(`Adding comment ${i}, by ${c.name}...`);
23+
console.log(`To: ${filename}`);
24+
if (err) {
25+
throw err;
26+
}
27+
console.log('Done.');
28+
});
29+
};
30+
31+
const parseComments = (comments, slug) => {
32+
for (const index in comments) {
33+
if (Object.prototype.hasOwnProperty.call(comments, index)) {
34+
const comment = comments[index];
35+
36+
if (comment['wp:comment_approved'][0] === '1') {
37+
const commentObj = {
38+
name: comment['wp:comment_author'][0],
39+
date: comment['wp:comment_date'][0],
40+
url: comment['wp:comment_author_url'][0],
41+
message: comment['wp:comment_content'][0],
42+
email: md5(comment['wp:comment_author_email'][0])
43+
};
44+
45+
createFile(commentObj, slug);
46+
}
47+
}
48+
}
49+
};
50+
51+
const start = (source, dest) => {
52+
source = source || args[0];
53+
dest = dest || args[1];
54+
55+
fs.readFile(path.resolve(args[0]), (err, data) => {
56+
if (err) {
57+
throw err;
58+
}
59+
parser.parseString(data, (err, result) => {
60+
if (err) {
61+
throw err;
62+
}
63+
64+
const posts = result.rss.channel[0].item;
65+
66+
for (const index in posts) {
67+
if (Object.prototype.hasOwnProperty.call(posts, index)) {
68+
const post = posts[index];
69+
70+
if (post['wp:comment']) {
71+
const slug = post['wp:post_name'][0];
72+
const comments = post['wp:comment'];
73+
74+
parseComments(comments, slug);
75+
}
76+
}
77+
}
78+
});
79+
});
80+
}
81+
82+
module.exports = start;

0 commit comments

Comments
 (0)
Please sign in to comment.