|
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 | +} |
37 | 56 |
|
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 = ''; |
40 | 59 |
|
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); |
44 | 66 | }
|
45 | 67 |
|
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; |
48 | 90 |
|
49 | 91 | if ( match = line.match(/^\s*#+\s(.*)$/) ) {
|
50 | 92 | // 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]); |
60 | 94 | } 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 = { |
62 | 101 | title: match[1],
|
63 |
| - href: match[2], |
| 102 | + href: href, |
64 | 103 | short_description: match[4],
|
65 |
| - categories: [currentCategory] |
| 104 | + categories: [cat] |
66 | 105 | };
|
67 | 106 |
|
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); |
84 | 110 |
|
85 |
| - if (meta.description[1]) |
86 |
| - rec.long_description = meta.description[1]; |
| 111 | + links.push(link); |
| 112 | + linksIndex[href] = link; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
87 | 116 |
|
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); |
90 | 120 |
|
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 | +} |
93 | 129 |
|
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