|
2 | 2 |
|
3 | 3 | 'use strict';
|
4 | 4 |
|
5 |
| -var fs = require('fs'); |
| 5 | +var fs = require ('fs'); |
6 | 6 |
|
7 |
| -var program = require('commander'); |
8 |
| -var R = require('ramda'); |
| 7 | +var program = require ('commander'); |
| 8 | +var R = require ('ramda'); |
9 | 9 |
|
10 |
| -var pkg = require('../package.json'); |
| 10 | +var pkg = require ('../package.json'); |
11 | 11 |
|
12 | 12 |
|
13 | 13 | //. esc :: String -> String
|
14 |
| -var esc = R.pipe(R.replace(/&/g, '&'), |
15 |
| - R.replace(/</g, '<'), |
16 |
| - R.replace(/"/g, '"')); |
| 14 | +var esc = R.pipe (R.replace (/&/g, '&'), |
| 15 | + R.replace (/</g, '<'), |
| 16 | + R.replace (/"/g, '"')); |
17 | 17 |
|
18 | 18 |
|
19 | 19 | //. nbsp :: String -> String
|
20 |
| -var nbsp = R.replace(/[ ]/g, '\u00A0'); |
| 20 | +var nbsp = R.replace (/[ ]/g, '\u00A0'); |
21 | 21 |
|
22 | 22 |
|
23 | 23 | //. controlWrapping :: String -> String
|
24 | 24 | var controlWrapping =
|
25 |
| -R.pipe(R.split(' :: '), |
26 |
| - R.map(R.split(' => ')), |
27 |
| - R.map(R.map(R.split(/([(][^()]+[)])/))), |
28 |
| - R.map(R.map(R.append(''))), |
29 |
| - R.map(R.map(R.splitEvery(2))), |
30 |
| - R.map(R.map(R.map(R.over(R.lensIndex(1), nbsp)))), |
31 |
| - R.map(R.map(R.unnest)), |
32 |
| - R.map(R.map(R.map(R.split(' -> ')))), |
33 |
| - R.map(R.map(R.map(R.map(nbsp)))), |
34 |
| - R.map(R.map(R.map(R.join(' -> ')))), |
35 |
| - R.map(R.map(R.join(''))), |
36 |
| - R.map(R.join(' => ')), |
37 |
| - R.join(' :: '), |
38 |
| - R.replace(/->/g, '-\u2060>')); |
| 25 | +R.pipe (R.split (' :: '), |
| 26 | + R.map (R.split (' => ')), |
| 27 | + R.map (R.map (R.split (/([(][^()]+[)])/))), |
| 28 | + R.map (R.map (R.append (''))), |
| 29 | + R.map (R.map (R.splitEvery (2))), |
| 30 | + R.map (R.map (R.map (R.over (R.lensIndex (1), nbsp)))), |
| 31 | + R.map (R.map (R.unnest)), |
| 32 | + R.map (R.map (R.map (R.split (' -> ')))), |
| 33 | + R.map (R.map (R.map (R.map (nbsp)))), |
| 34 | + R.map (R.map (R.map (R.join (' -> ')))), |
| 35 | + R.map (R.map (R.join (''))), |
| 36 | + R.map (R.join (' => ')), |
| 37 | + R.join (' :: '), |
| 38 | + R.replace (/->/g, '-\u2060>')); |
39 | 39 |
|
40 | 40 |
|
41 | 41 | //. formatSignature :: Options -> String -> Number -> String -> String
|
42 |
| -var formatSignature = R.curry(function(options, filename, line, signature) { |
43 |
| - return '######'.slice(0, options.headingLevel) + ' ' + |
44 |
| - '<a name="' + esc(signature.split(' :: ')[0]) + '"' + |
45 |
| - ' href="' + esc(options.url.replace('{filename}', filename) |
46 |
| - .replace('{line}', line)) + '">' + |
47 |
| - '`' + controlWrapping(signature) + '`' + |
| 42 | +var formatSignature = R.curry (function(options, filename, line, signature) { |
| 43 | + return '######'.slice (0, options.headingLevel) + ' ' + |
| 44 | + '<a name="' + esc ((signature.split (' :: '))[0]) + '"' + |
| 45 | + ' href="' + esc (options.url.replace ('{filename}', filename) |
| 46 | + .replace ('{line}', line)) + '">' + |
| 47 | + '`' + controlWrapping (signature) + '`' + |
48 | 48 | '</a>\n';
|
49 | 49 | });
|
50 | 50 |
|
51 | 51 |
|
52 | 52 | //. parseLine :: Options -> String -> Number -> String -> String
|
53 |
| -var parseLine = R.curry(function(options, filename, line, value) { |
54 |
| - return R.pipe( |
55 |
| - R.replace(/^\s+/, ''), |
56 |
| - R.cond([[R.pipe(R.indexOf(options.headingPrefix), R.equals(0)), |
57 |
| - R.pipe(R.drop(R.length(options.headingPrefix)), |
58 |
| - R.replace(/^[ ]/, ''), |
59 |
| - formatSignature(options, filename, line))], |
60 |
| - [R.pipe(R.indexOf(options.prefix), R.equals(0)), |
61 |
| - R.pipe(R.drop(R.length(options.prefix)), |
62 |
| - R.replace(/^[ ]/, ''), |
63 |
| - R.concat(R.__, '\n'))], |
64 |
| - [R.T, |
65 |
| - R.always('\n')]]) |
66 |
| - )(value); |
| 53 | +var parseLine = R.curry (function(options, filename, line, value) { |
| 54 | + return R.pipe ( |
| 55 | + R.replace (/^\s+/, ''), |
| 56 | + R.cond ([[R.pipe (R.indexOf (options.headingPrefix), R.equals (0)), |
| 57 | + R.pipe (R.drop (R.length (options.headingPrefix)), |
| 58 | + R.replace (/^[ ]/, ''), |
| 59 | + formatSignature (options, filename, line))], |
| 60 | + [R.pipe (R.indexOf (options.prefix), R.equals (0)), |
| 61 | + R.pipe (R.drop (R.length (options.prefix)), |
| 62 | + R.replace (/^[ ]/, ''), |
| 63 | + R.concat (R.__, '\n'))], |
| 64 | + [R.T, |
| 65 | + R.always ('\n')]]) |
| 66 | + ) (value); |
67 | 67 | });
|
68 | 68 |
|
69 | 69 |
|
70 | 70 | //. parseFile :: Options -> String -> String
|
71 |
| -var parseFile = R.curry(function(options, filename) { |
72 |
| - return R.pipe( |
| 71 | +var parseFile = R.curry (function(options, filename) { |
| 72 | + return R.pipe ( |
73 | 73 | fs.readFileSync,
|
74 | 74 | String,
|
75 |
| - R.match(/^.*$/gm), |
76 |
| - R.lift(R.zip)(R.pipe(R.length, R.inc, R.range(1)), R.identity), |
77 |
| - R.map(R.apply(parseLine(options, filename))), |
78 |
| - R.join('') |
79 |
| - )(filename); |
| 75 | + R.match (/^.*$/gm), |
| 76 | + R.lift (R.zip) (R.pipe (R.length, R.inc, R.range (1)), R.identity), |
| 77 | + R.map (R.apply (parseLine (options, filename))), |
| 78 | + R.join ('') |
| 79 | + ) (filename); |
80 | 80 | });
|
81 | 81 |
|
82 | 82 |
|
83 | 83 | //. transcribe :: Options -> [String] -> String
|
84 |
| -var transcribe = R.curry(function(options, filenames) { |
85 |
| - return R.pipe( |
86 |
| - R.map(parseFile(options)), |
87 |
| - R.join('\n\n'), |
88 |
| - R.replace(/\n{3,}/g, '\n\n'), |
89 |
| - R.replace(/^\n+/, ''), |
90 |
| - R.replace(/\n+$/, '\n') |
91 |
| - )(filenames); |
| 84 | +var transcribe = R.curry (function(options, filenames) { |
| 85 | + return R.pipe ( |
| 86 | + R.map (parseFile (options)), |
| 87 | + R.join ('\n\n'), |
| 88 | + R.replace (/\n{3,}/g, '\n\n'), |
| 89 | + R.replace (/^\n+/, ''), |
| 90 | + R.replace (/\n+$/, '\n') |
| 91 | + ) (filenames); |
92 | 92 | });
|
93 | 93 |
|
94 | 94 |
|
95 | 95 | program
|
96 |
| -.version(pkg.version) |
97 |
| -.usage('[options] <file ...>') |
98 |
| -.description(pkg.description) |
99 |
| -.option('--heading-level <num>', 'heading level in range [1, 6] (default: 3)') |
100 |
| -.option('--heading-prefix <str>', 'prefix for heading lines (default: "//#")') |
101 |
| -.option('--insert-into <str>', 'name of a file into which Transcribe will' + |
102 |
| - ' insert generated output') |
103 |
| -.option('--prefix <str>', 'prefix for non-heading lines (default: "//.")') |
104 |
| -.option('--url <str>', 'source URL with {filename} and {line} placeholders') |
105 |
| -.parse(process.argv); |
| 96 | +.version (pkg.version) |
| 97 | +.usage ('[options] <file ...>') |
| 98 | +.description (pkg.description) |
| 99 | +.option ('--heading-level <num>', 'heading level in range [1, 6] (default: 3)') |
| 100 | +.option ('--heading-prefix <str>', 'prefix for heading lines (default: "//#")') |
| 101 | +.option ('--insert-into <str>', |
| 102 | + 'name of a file into which Transcribe will insert generated output') |
| 103 | +.option ('--prefix <str>', 'prefix for non-heading lines (default: "//.")') |
| 104 | +.option ('--url <str>', 'source URL with {filename} and {line} placeholders') |
| 105 | +.parse (process.argv); |
106 | 106 |
|
107 | 107 | var valid = true;
|
108 | 108 |
|
109 |
| -if (!(program.headingLevel == null || /^[1-6]$/.test(program.headingLevel))) { |
110 |
| - process.stderr.write('Invalid --heading-level\n'); |
| 109 | +if (!(program.headingLevel == null || /^[1-6]$/.test (program.headingLevel))) { |
| 110 | + process.stderr.write ('Invalid --heading-level\n'); |
111 | 111 | valid = false;
|
112 | 112 | }
|
113 | 113 |
|
114 | 114 | if (program.url == null) {
|
115 |
| - process.stderr.write('No --url template specified\n'); |
| 115 | + process.stderr.write ('No --url template specified\n'); |
116 | 116 | valid = false;
|
117 | 117 | }
|
118 | 118 |
|
119 | 119 | if (!valid) {
|
120 |
| - process.exit(1); |
| 120 | + process.exit (1); |
121 | 121 | }
|
122 | 122 |
|
123 | 123 | //. options :: { headingLevel, headingPrefix, insertInto, prefix, url }
|
124 | 124 | var options = {
|
125 |
| - headingLevel: Number(R.defaultTo('3', program.headingLevel)), |
126 |
| - headingPrefix: R.defaultTo('//#', program.headingPrefix), |
127 |
| - insertInto: R.defaultTo(null, program.insertInto), |
128 |
| - prefix: R.defaultTo('//.', program.prefix), |
| 125 | + headingLevel: Number (R.defaultTo ('3', program.headingLevel)), |
| 126 | + headingPrefix: R.defaultTo ('//#', program.headingPrefix), |
| 127 | + insertInto: R.defaultTo (null, program.insertInto), |
| 128 | + prefix: R.defaultTo ('//.', program.prefix), |
129 | 129 | url: program.url
|
130 | 130 | };
|
131 | 131 |
|
132 |
| -var output = transcribe(options, program.args); |
| 132 | +var output = transcribe (options, program.args); |
133 | 133 | if (options.insertInto == null) {
|
134 |
| - process.stdout.write(output); |
| 134 | + process.stdout.write (output); |
135 | 135 | } else {
|
136 | 136 | // Read the file, insert the output, and write to the file again
|
137 |
| - fs.writeFileSync(options.insertInto, R.replace( |
| 137 | + fs.writeFileSync (options.insertInto, R.replace ( |
138 | 138 | /(<!--transcribe-->)[\s\S]*?(<!--[/]transcribe-->)/,
|
139 | 139 | '$1\n\n' + output + '\n$2',
|
140 |
| - fs.readFileSync(options.insertInto, {encoding: 'utf8'}) |
| 140 | + fs.readFileSync (options.insertInto, {encoding: 'utf8'}) |
141 | 141 | ));
|
142 | 142 | }
|
0 commit comments