Skip to content

Commit 08030b2

Browse files
committed
just backin' up
1 parent 62f0d0a commit 08030b2

File tree

2 files changed

+119
-88
lines changed

2 files changed

+119
-88
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"gulp-sass": "^1.3.3",
3434
"gulp-sourcemaps": "^1.5.1",
3535
"gulp-watch": "^4.2.4",
36+
"lazy.js": "^0.4.0",
3637
"node-sass": "^2.1.1"
3738
}
3839
}

scripts/md-import.js

Lines changed: 118 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,134 @@
1-
var fs = require('fs'),
2-
path = require('path');
3-
4-
var argv = process.argv,
5-
markdownPath, jsonOutPath, moreMetaData;
6-
7-
if ( !(markdownPath = argv[2]) ||
8-
!(jsonOutPath = argv[3]) )
9-
return console.error('Usage: <source.md> <destination.json> [<data-to-merge.json> [varInTheMergeData]]');
10-
11-
if (argv[4])
12-
moreMetaData = {
13-
path: argv[4],
14-
id: argv[5] || path.base(argv[4], '.json')
15-
};
16-
17-
fs.readFile(markdownPath, {encoding: 'UTF-8', flag: 'r'}, function (err, data) {
18-
if (err)
19-
return console.error('Couldn\'t open ' + path.resolve(markdownPath));
20-
21-
var allLinks = [],
22-
categories = [];
23-
currentCategory = "",
24-
current = {},
25-
moreMeta = [],
26-
moreMetaIndex = {},
27-
currentMeta = {},
28-
currentMetaIndex = {};
29-
30-
if (moreMetaData) {
31-
try {
32-
var moreMetaString = fs.readFileSync(moreMetaData.path, 'utf8');
33-
}
34-
catch (e) {
35-
return console.error('Couldn\'t open ' + path.resolve(moreMetaData.path));
36-
}
1+
/*
2+
How it _should_ work:
3+
- if the json exists, open and index it (by href?)
4+
- open and cleanup the markdown
5+
- iterate over lines:
6+
- case category: allCategories.push, make current
7+
- case link: merge with old meta, link.categories.push[cat], allLinks.push
8+
- stringify and write
9+
*/
10+
11+
var FS = require('fs'),
12+
Path = require('path'),
13+
_ = require('lodash');
14+
15+
(function md_import() {
16+
var argv = process.argv,
17+
cfg = {
18+
linksID: 'allTheLinks',
19+
catsID: 'categories',
20+
spaces: 2,
21+
source: argv[2],
22+
destination: argv[3]
23+
},
24+
oldTree, newTree, oldTreeIndex, md;
25+
26+
// check cli arguments
27+
if ( !(cfg.source) || !(cfg.destination) )
28+
return console.error('Usage: <source.md> <destination.json>');
29+
30+
// try and import the old JSON data (or at leas create structure)
31+
oldTree = readJson_maybe(cfg.destination);
32+
oldTreeIndex = _.indexBy(oldTree, cfg.linksID);
33+
34+
// read and parse markdown (merging the old JSON)
35+
md = getMdLines(Path.resolve(cfg.source));
36+
newTree = parseMarkdown(md, oldTreeIndex);
37+
38+
// write the JSON
39+
writeJson(cfg.destination, newTree, cfg.spaces);
40+
})();
41+
42+
function readJson_maybe(file) {
43+
var tree;
44+
45+
try {
46+
tree = JSON.parse(FS.readFileSync(file, 'utf8'));
47+
}
48+
catch (e) {
49+
tree = {};
50+
tree[cfg.linksID] = [];
51+
tree[cfg.catsID] = [];
52+
}
53+
54+
return tree;
55+
}
3756

38-
if (!(moreMeta = JSON.parse(moreMetaString)[moreMetaData.id]))
39-
return console.error('Couln\'t get a value of ' + moreMetaData.id + ' from ' + moreMetaData.path);
57+
function getMdLines(file) {
58+
var buffer = '';
4059

41-
moreMeta.forEach(function(el) {
42-
moreMetaIndex[el.title] = el.items;
43-
});
60+
try {
61+
buffer = FS.readFileSync(file, {encoding: 'UTF-8', flag: 'r'});
62+
}
63+
catch (e) {
64+
console.error('Couldn\'t open ' + Path.resolve(file));
65+
process.exit(1);
4466
}
4567

46-
data.split(/\r?\n/).forEach( function(line) {
47-
var match;
68+
return inputCleanup(buffer).split(/\r?\n/);
69+
}
70+
71+
function inputCleanup(string) {
72+
// $TODO
73+
return string;
74+
}
75+
76+
function parseMarkdown(lines, oldJsonIndex) {
77+
var categories = {},
78+
links = {},
79+
cat = '-',
80+
tree = {},
81+
linksIndex = {};
82+
83+
lines.forEach(parseLine);
84+
tree[cfg.linksID] = links;
85+
tree[cfg.catsID] = categories;
86+
return tree;
87+
88+
function parseLine(line) {
89+
var match, link, meta, href;
4890

4991
if ( match = line.match(/^\s*#+\s(.*)$/) ) {
5092
// Category header
51-
currentCategory = match[1];
52-
categories.push(currentCategory);
53-
54-
if (currentMeta = moreMetaIndex[currentCategory]) {
55-
currentMetaIndex = [];
56-
currentMeta.forEach(function(el, i) {
57-
currentMetaIndex[el.title] = el;
58-
});
59-
}
93+
categories.push(cat = match[1]);
6094
} else if ( match = line.match(/^\s*[\*\+\-]\s+\[(.+?)\]\((.+?)\)(\s+(.*))?$/) ) {
61-
current = {
95+
// check for dupes
96+
href = match[2];
97+
if (_.has(linksIndex, href))
98+
return;
99+
100+
link = {
62101
title: match[1],
63-
href: match[2],
102+
href: href,
64103
short_description: match[4],
65-
categories: [currentCategory]
104+
categories: [cat]
66105
};
67106

68-
if (moreMetaData)
69-
enhanceMeta(current, currentMetaIndex[current.title]);
70-
allLinks.push(current);
71-
}
72-
73-
function enhanceMeta(rec, meta) {
74-
if (!meta || typeof meta != 'object')
75-
return;
76-
77-
if (meta.author)
78-
rec.author = meta.author;
79-
80-
if (meta.description[0] &&
81-
( !(rec.short_description) || 0 == rec.short_description.indexOf('by') )
82-
)
83-
rec.short_description = meta.description[0];
107+
// merge old meta
108+
if (meta = oldJsonIndex[href])
109+
link = _.merge(meta, link);
84110

85-
if (meta.description[1])
86-
rec.long_description = meta.description[1];
111+
links.push(link);
112+
linksIndex[href] = link;
113+
}
114+
}
115+
}
87116

88-
if (meta.tags)
89-
rec.tags = meta.tags;
117+
function writeJson(file, tree, spaces) {
118+
spaces || (spaces = 2);
119+
var theBigString = JSON.stringify(tree, null, spaces);
90120

91-
if (meta.level)
92-
rec.level = meta.level;
121+
try {
122+
FS.writeFileSync(file, theBigString);
123+
}
124+
catch (e) {
125+
console.error('Couldn\'t write' + Path.resolve(jsonOutPath));
126+
process.exit(1);
127+
}
128+
}
93129

94-
if (meta.rank)
95-
rec.rank = meta.rank;
96-
}
97-
});
98-
99-
var theBigString = JSON.stringify({allTheLinks: allLinks, linkCategories: categories}, null, 2);
100-
fs.writeFile(jsonOutPath, theBigString, function(err){
101-
if (err)
102-
console.error('Couldn\'t write' + path.resolve(jsonOutPath));
103-
});
104-
});
130+
function t(s,d){
131+
for(var p in d)
132+
s=s.replace(new RegExp('{'+p+'}','g'), d[p]);
133+
return s;
134+
}

0 commit comments

Comments
 (0)