wiki style document editor
Revision | 8e1928cccc1d0e77866cd89a1394e57a5f976fed (tree) |
---|---|
Time | 2014-08-26 19:52:39 |
Author | hylom <hylom@hylo...> |
Commiter | hylom |
refactor export.js
@@ -10,6 +10,7 @@ var fs = require('fs'); | ||
10 | 10 | |
11 | 11 | var HTML_HEADER = 'header.txt'; |
12 | 12 | var HTML_FOOTER = 'footer.txt'; |
13 | +var IMAGE_DIR = 'figure'; | |
13 | 14 | |
14 | 15 | var headerFullPath = path.join(path.dirname(module.filename), HTML_HEADER); |
15 | 16 | var footerFullPath = path.join(path.dirname(module.filename), HTML_FOOTER); |
@@ -20,29 +21,21 @@ var HTML_FOOT = fs.readFileSync(footerFullPath, {encoding: 'utf8'}); | ||
20 | 21 | var usage = 'node export.js <target directory> <output directory>'; |
21 | 22 | |
22 | 23 | |
23 | - | |
24 | - | |
25 | 24 | function exportSingle(target, output) { |
26 | 25 | fs.readFile(target, 'utf8', function (err, data) { |
27 | 26 | if (err) { |
28 | 27 | console.log('Cannot read file: ' + target); |
29 | 28 | return; |
30 | 29 | } |
31 | - var result = stripText(data); | |
32 | -/* | |
33 | - console.log(result.text.join('\n')); | |
34 | - console.log('-------------------------'); | |
35 | - console.log(result.caption.join('\n')); | |
36 | - console.log('-------------------------'); | |
37 | - console.log(result.html.join('\n')); | |
38 | -*/ | |
39 | - result.caption = parseCaption(result.caption, target); | |
30 | + | |
31 | + var result = parseText(data); | |
40 | 32 | result.html = makeCaptionHtml(result.figure); |
41 | 33 | |
42 | 34 | fs.writeFileSync(path.join(output, 'honmon.txt'), |
43 | 35 | result.text.join('\n')); |
44 | 36 | fs.writeFileSync(path.join(output, 'caption.txt'), |
45 | 37 | result.caption.join('\n')); |
38 | + | |
46 | 39 | var out = fs.createWriteStream(path.join(output, 'figures.html'), {flags: 'w', encoding: 'utf8', mode: 0666}); |
47 | 40 | out.write(HTML_HEAD); |
48 | 41 | out.write('\n'); |
@@ -50,6 +43,7 @@ function exportSingle(target, output) { | ||
50 | 43 | out.write('\n'); |
51 | 44 | out.write(HTML_FOOT); |
52 | 45 | out.end(); |
46 | + | |
53 | 47 | }); |
54 | 48 | } |
55 | 49 |
@@ -92,7 +86,8 @@ function makeCaptionHtml(text) { | ||
92 | 86 | return results; |
93 | 87 | } |
94 | 88 | |
95 | -function parseCaption(text, targetPath) { | |
89 | +// replace image file name to full pathname | |
90 | +function replaceImageToPathname(text, targetPath) { | |
96 | 91 | var results = []; |
97 | 92 | for (var i = 0; i < text.length; i++) { |
98 | 93 |
@@ -110,7 +105,8 @@ function parseCaption(text, targetPath) { | ||
110 | 105 | return results; |
111 | 106 | } |
112 | 107 | |
113 | -function stripText(data) { | |
108 | +// parse main text to separate body text and captions/tables | |
109 | +function parseText(data) { | |
114 | 110 | var lines = data.split('\n'); |
115 | 111 | var counter = 0; |
116 | 112 | var texts = []; |
@@ -155,12 +151,13 @@ function stripText(data) { | ||
155 | 151 | } |
156 | 152 | var result = {}; |
157 | 153 | result.text = texts; |
158 | - result.caption = captions; | |
154 | + result.caption = replaceImageToPathname(captions, target); | |
159 | 155 | result.figure = figures; |
160 | 156 | return result; |
161 | 157 | } |
162 | 158 | |
163 | 159 | |
160 | +// main action | |
164 | 161 | if (require.main == module) { |
165 | 162 | if (process.argv.length < 4) { |
166 | 163 | process.stdout.write(usage + '\n'); |