Skip to content

Commit 4003229

Browse files
committed
Refactor code generation.
* use '\r\n' on windows, '\n' on linux. Signed-off-by: Eric Wang <[email protected]>
1 parent b45693d commit 4003229

File tree

6 files changed

+37
-34
lines changed

6 files changed

+37
-34
lines changed

Diff for: lib/core.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -154,30 +154,37 @@ core.starProblem = function(problem, starred, cb) {
154154

155155
core.exportProblem = function(problem, f, codeOnly) {
156156
var output = '';
157+
problem.code = problem.code.replace(/\r\n/g, '\n');
157158

158159
if (codeOnly) {
159160
output = problem.code;
160161
} else {
161-
var input = h.langToCommentStyle(h.extToLang(f));
162+
var input = {
163+
comment: h.langToCommentStyle(h.extToLang(f))
164+
};
162165
// copy problem attrs thus we can render it in template
163166
_.extend(input, problem);
164167
input.percent = input.percent.toFixed(2);
165168
input.testcase = util.inspect(input.testcase || '');
166169

167170
// NOTE: wordwrap internally uses '\n' as EOL, so here we have to
168171
// remove all '\r' in the raw string.
169-
// FIXME: while in template file we still use '\r\n' for the sake
170-
// of windows, is it really necessary?
171172
var desc = input.desc.replace(/\r\n/g, '\n')
172173
.replace(/^ /mg, '⁠');
173174

174-
var wrap = require('wordwrap')(79 - input.commentLine.length);
175+
var wrap = require('wordwrap')(79 - input.comment.line.length);
175176
input.desc = wrap(desc).split('\n');
176177

177178
var tpl = fs.readFileSync(path.resolve(__dirname, '../source.tpl'), 'utf-8');
178179
output = _.template(tpl)(input);
179180
}
180181

182+
if (h.isWindows()) {
183+
output = output.replace(/\n/g, '\r\n');
184+
} else {
185+
output = output.replace(/\r\n/g, '\n');
186+
}
187+
181188
fs.writeFileSync(f, output);
182189
};
183190

Diff for: lib/helper.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ var LANGS = [
4646

4747
var h = {};
4848

49+
h.isWindows = function() {
50+
return process.platform === 'win32';
51+
};
52+
4953
h.prettyState = function(state) {
5054
switch (state) {
5155
case 'ac': return this.prettyText('', true);
@@ -119,8 +123,8 @@ h.langToCommentStyle = function(lang) {
119123
});
120124

121125
return (res && res.style === '#') ?
122-
{commentHeader: '#', commentLine: '#', commentFooter: '#'} :
123-
{commentHeader: '/*', commentLine: ' *', commentFooter: ' */'};
126+
{start: '#', line: '#', end: '#'} :
127+
{start: '/*', line: ' *', end: ' */'};
124128
};
125129

126130
h.getFileData = function(p) {

Diff for: lib/icon.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var icons = {
1414
};
1515

1616
icons.setTheme = function(name) {
17-
var defaultName = process.platform === 'win32' ? 'win7' : 'default';
17+
var defaultName = h.isWindows() ? 'win7' : 'default';
1818
var theme = this.themes[name] || this.themes[defaultName] || {};
1919
_.extendOwn(this, theme);
2020
};

Diff for: source.tpl

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
<%= commentHeader %>
2-
<%= commentLine %> [<%= id %>] <%= name %>
3-
<%= commentLine %>
4-
<%= commentLine %> <%= link %>
5-
<%= commentLine %>
6-
<%= commentLine %> <%= category %>
7-
<%= commentLine %> <%= level %> (<%= percent %>%)
8-
<%= commentLine %> Total Accepted: <%= totalAC %>
9-
<%= commentLine %> Total Submissions: <%= totalSubmit %>
10-
<%= commentLine %> Testcase Example: <%= testcase %>
11-
<%= commentLine %>
12-
<% _.each(desc, function(x) { %><%= commentLine %> <%= x %>
13-
<% }) %><%= commentFooter %>
14-
<%= code %>
1+
<%= comment.start %>
2+
<%= comment.line %> [<%= id %>] <%= name %>
3+
<%= comment.line %>
4+
<%= comment.line %> <%= link %>
5+
<%= comment.line %>
6+
<%= comment.line %> <%= category %>
7+
<%= comment.line %> <%= level %> (<%= percent %>%)
8+
<%= comment.line %> Total Accepted: <%= totalAC %>
9+
<%= comment.line %> Total Submissions: <%= totalSubmit %>
10+
<%= comment.line %> Testcase Example: <%= testcase %>
11+
<%= comment.line %>
12+
<% _.each(desc, function(x) { %><%= comment.line %> <%= x %>
13+
<% }) %><%= comment.end %>
14+
<%= code %>

Diff for: test/test_core.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ describe('core', function() {
364364
'',
365365
' }',
366366
'};'
367-
].join('\r\n');
367+
].join('\n');
368368

369369
injectVerify(expected, done);
370370

@@ -399,7 +399,7 @@ describe('core', function() {
399399
' }',
400400
'};',
401401
''
402-
].join('\r\n');
402+
].join('\n');
403403

404404
injectVerify(expected, done);
405405

@@ -443,7 +443,7 @@ describe('core', function() {
443443
' ',
444444
'end',
445445
''
446-
].join('\r\n');
446+
].join('\n');
447447

448448
injectVerify(expected, done);
449449

Diff for: test/test_helper.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,8 @@ describe('helper', function() {
134134

135135
describe('#langToCommentStyle', function() {
136136
it('should ok', function() {
137-
var C_STYLE = {
138-
commentHeader: '/*',
139-
commentLine: ' *',
140-
commentFooter: ' */'
141-
};
142-
var RUBY_STYLE = {
143-
commentHeader: '#',
144-
commentLine: '#',
145-
commentFooter: '#'
146-
};
137+
var C_STYLE = {start: '/*', line: ' *', end: ' */'};
138+
var RUBY_STYLE = {start: '#', line: '#', end: '#'};
147139

148140
assert.deepEqual(h.langToCommentStyle('bash'), RUBY_STYLE);
149141
assert.deepEqual(h.langToCommentStyle('c'), C_STYLE);

0 commit comments

Comments
 (0)