wiki style document editor
Révision | 6e7abdeb3a4fbc1aa6fb6772afd30eeb4987118b (tree) |
---|---|
l'heure | 2014-09-17 23:32:46 |
Auteur | Hiromichi Matsushima <hylom@Hiro...> |
Commiter | Hiromichi Matsushima |
add index generation script
@@ -0,0 +1,69 @@ | ||
1 | +// make_index.js | |
2 | + | |
3 | +var config = require('../../config'); | |
4 | +var path = require('path'); | |
5 | +var fs = require('fs'); | |
6 | +var util = require('util'); | |
7 | + | |
8 | +var tocParser = require('../export/toc-parser'); | |
9 | + | |
10 | +var usage = 'node make_index.js <output_file>'; | |
11 | + | |
12 | +function getSectionTitle(filename) { | |
13 | + var content = fs.readFileSync(filename, {encoding: 'utf8'}); | |
14 | + var rex = /■(.*)/g; | |
15 | + var matches = rex.exec(content); | |
16 | + if (matches === null) { | |
17 | + rex = /●(.*)/; | |
18 | + matches = rex.exec(content); | |
19 | + if (matches === null) { | |
20 | + return ''; | |
21 | + } | |
22 | + return [ matches[1] ]; | |
23 | + } | |
24 | + | |
25 | + var titles = []; | |
26 | + while (matches !== null) { | |
27 | + titles.push(matches[1]); | |
28 | + matches = rex.exec(content); | |
29 | + } | |
30 | + return titles; | |
31 | +} | |
32 | + | |
33 | +function titleToPath(title, directory) { | |
34 | + var basePath = path.join(config.rootDir, directory, title); | |
35 | + return basePath + '.txt'; | |
36 | +} | |
37 | + | |
38 | +function makeIndex(output) { | |
39 | + var toc = readToc(); | |
40 | + toc.forEachDirectories(function (directory) { | |
41 | + var pageNum = directory.pageStart; | |
42 | + directory.contents.forEach(function (content) { | |
43 | + var pagePath = titleToPath(content.title, directory.name); | |
44 | + var pages = content.pages; | |
45 | + var pageTitle = getSectionTitle(pagePath); | |
46 | + console.log(pageNum, pageTitle); | |
47 | + pageNum += pages; | |
48 | + }); | |
49 | + }); | |
50 | +} | |
51 | + | |
52 | +function readToc() { | |
53 | + var tocPath = path.join(config.rootDir, 'index.toc'); | |
54 | + var text = fs.readFileSync(tocPath, {encoding: 'utf8'}); | |
55 | + var toc = tocParser.parseToc(text); | |
56 | + return toc; | |
57 | +} | |
58 | + | |
59 | +// main action | |
60 | +if (require.main == module) { | |
61 | + if (process.argv.length < 3) { | |
62 | + process.stdout.write(usage + '\n'); | |
63 | + process.exit(1); | |
64 | + } | |
65 | + var output = process.argv[2]; | |
66 | + makeIndex(output); | |
67 | +} | |
68 | + | |
69 | + |