|
1 | 1 | 'use strict';
|
2 | | -var path = require('path'); |
3 | 2 | var util = require('util');
|
4 | 3 |
|
5 | 4 | var _ = require('underscore');
|
@@ -107,31 +106,26 @@ core.starProblem = function(problem, starred, cb) {
|
107 | 106 | };
|
108 | 107 |
|
109 | 108 | core.exportProblem = function(problem, opts) {
|
110 | | - // copy problem attrs thus we can render it in template |
111 | | - const input = _.extend({}, problem); |
| 109 | + const data = _.extend({}, problem); |
112 | 110 |
|
113 | | - input.code = opts.code.replace(/\r\n/g, '\n'); |
114 | | - input.comment = h.langToCommentStyle(opts.lang); |
115 | | - input.percent = input.percent.toFixed(2); |
116 | | - input.testcase = util.inspect(input.testcase || ''); |
| 111 | + // unify format before rendering |
| 112 | + data.app = require('./config').app; |
| 113 | + if (!data.fid) data.fid = data.id; |
| 114 | + if (!data.lang) data.lang = opts.lang; |
| 115 | + data.code = (opts.code || data.code || '').replace(/\r\n/g, '\n'); |
| 116 | + data.comment = h.langToCommentStyle(data.lang); |
| 117 | + data.percent = data.percent.toFixed(2); |
| 118 | + data.testcase = util.inspect(data.testcase || ''); |
117 | 119 |
|
118 | 120 | if (opts.tpl === 'detailed') {
|
119 | 121 | // NOTE: wordwrap internally uses '\n' as EOL, so here we have to
|
120 | 122 | // remove all '\r' in the raw string.
|
121 | | - const desc = input.desc.replace(/\r\n/g, '\n').replace(/^/mg, ''); |
122 | | - const wrap = require('wordwrap')(79 - input.comment.line.length); |
123 | | - input.desc = wrap(desc).split('\n'); |
| 123 | + const desc = data.desc.replace(/\r\n/g, '\n').replace(/^/mg, ''); |
| 124 | + const wrap = require('wordwrap')(79 - data.comment.line.length); |
| 125 | + data.desc = wrap(desc).split('\n'); |
124 | 126 | }
|
125 | 127 |
|
126 | | - const tplfile = path.join(file.codeDir('templates'), opts.tpl + '.tpl'); |
127 | | - let output = _.template(file.data(tplfile))(input); |
128 | | - |
129 | | - if (file.isWindows()) { |
130 | | - output = output.replace(/\n/g, '\r\n'); |
131 | | - } else { |
132 | | - output = output.replace(/\r\n/g, '\n'); |
133 | | - } |
134 | | - return output; |
| 128 | + return file.render(opts.tpl, data); |
135 | 129 | };
|
136 | 130 |
|
137 | 131 | module.exports = core;
|
0 commit comments