-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·146 lines (132 loc) · 3.19 KB
/
index.js
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
#! /usr/bin/env node
const fs = require("fs");
const url = require("url");
const fetch = require("node-fetch");
const baseUrl = "http://qiita.com/api/v2/items/";
const template = {
ipynb: {
"cells": null,
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
}
},
"nbformat": 4,
"nbformat_minor": 1
},
markdownCell: {
"cell_type": "markdown",
"metadata": {},
"source": null
},
codeCell: {
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": null
}
};
/**
* @param {String} markdown
* @return {Object}
*/
function md2ipynb(markdown) {
const lines = markdown.split("\n");
const ipynb = Object.assign({}, template.ipynb);
let isBlock = false;
let isCodeBlock = false;
let isMathBlock = false;
let cell = Object.assign({}, template.markdownCell);
ipynb.cells = [];
cell.source = [];
lines.forEach(line => {
if (line.match(/^```/)){
if(isBlock){
// close block
if (isCodeBlock) {
// trim the last line break
cell.source[cell.source.length - 1] = cell.source[cell.source.length - 1].replace(/\n$/, "");
} else if (isMathBlock) {
// do nothing special
} else {
cell.source.push("```");
}
ipynb.cells.push(cell);
cell = Object.assign({}, template.markdownCell);
cell.source = [];
isBlock = false;
isCodeBlock = false;
isMathBlock = false;
} else {
// open block
isBlock = true;
if (!isMathBlock && line.match(/^```(?:py\d?|python\d?|.+\.py)?$/)) {
ipynb.cells.push(cell);
cell = Object.assign({}, template.codeCell);
cell.source = [];
isCodeBlock = true;
isMathBlock = false;
} else if (line.match(/^```math/)) {
ipynb.cells.push(cell);
cell = Object.assign({}, template.markdownCell);
cell.source = [];
isCodeBlock = false;
isMathBlock = true;
} else {
cell.source.push(line + "\n");
}
}
// add cell contents
} else if(isCodeBlock){
cell.source.push(line + "\n");
} else if (isMathBlock) {
cell.source.push("$$ " + line + " $$\n");
} else {
cell.source.push(line.replace(/^(#+)([^#^\s])/, "$1 $2") + "\n");
}
});
ipynb.cells.push(cell);
return ipynb;
}
exports.md2ipynb = md2ipynb;
if (require.main === module) {
const item_id = url.parse(process.argv[2]).pathname.split("/").pop();
const targetUrl = baseUrl + item_id;
console.info(`Fetching: ${targetUrl}`);
fetch(targetUrl)
.then(res => {
return res.json();
})
.then(json => {
const title = json.title;
const markdown = json.body;
const ipynb = md2ipynb(markdown);
const content = JSON.stringify(ipynb, null, " ");
const filename = title + ".ipynb";
// output to file
fs.writeFile(filename, content, err => {
if (err) {
return console.error(err);
}
console.info(`Saved: ${filename}`);
});
})
.catch(err => console.error(err));
}