Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d9e07e4

Browse files
Embed meta in comment instead of filename.
refs #55 #65 Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent 61becf9 commit d9e07e4

File tree

10 files changed

+129
-42
lines changed

10 files changed

+129
-42
lines changed

‎lib/commands/submit.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,13 @@ cmd.handler = function(argv) {
4747
if (!fs.existsSync(argv.filename))
4848
return log.error('File ' + argv.filename + ' not exist!');
4949

50-
// use the 1st section in filename as keyword
51-
// e.g. two-sum.cpp, or two-sum.78502271.ac.cpp
52-
const keyword = file.name(argv.filename).split('.')[0];
50+
const meta = file.meta(argv.filename);
5351

54-
core.getProblem(keyword, function(e, problem) {
52+
core.getProblem(meta.id, function(e, problem) {
5553
if (e) return log.fail(e);
5654

5755
problem.file = argv.filename;
56+
problem.lang = meta.lang;
5857

5958
core.submitProblem(problem, function(e, results) {
6059
if (e) return log.fail(e);

‎lib/commands/test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@ function runTest(argv) {
5757
if (!fs.existsSync(argv.filename))
5858
return log.error('File ' + argv.filename + ' not exist!');
5959

60-
// use the 1st section in filename as keyword
61-
// e.g. two-sum.cpp, or two-sum.78502271.ac.cpp
62-
const keyword = file.name(argv.filename).split('.')[0];
60+
const meta = file.meta(argv.filename);
6361

64-
core.getProblem(keyword, function(e, problem) {
62+
core.getProblem(meta.id, function(e, problem) {
6563
if (e) return log.fail(e);
6664

6765
if (!problem.testable)
@@ -74,6 +72,7 @@ function runTest(argv) {
7472
return log.fail('missing testcase?');
7573

7674
problem.file = argv.filename;
75+
problem.lang = meta.lang;
7776

7877
log.info('\nInput data:');
7978
log.info(problem.testcase);

‎lib/core.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
var path = require('path');
32
var util = require('util');
43

54
var _ = require('underscore');
@@ -107,31 +106,26 @@ core.starProblem = function(problem, starred, cb) {
107106
};
108107

109108
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);
112110

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 || '');
117119

118120
if (opts.tpl === 'detailed') {
119121
// NOTE: wordwrap internally uses '\n' as EOL, so here we have to
120122
// 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');
124126
}
125127

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);
135129
};
136130

137131
module.exports = core;

‎lib/file.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var fs = require('fs');
33
var path = require('path');
44

5+
var _ = require('underscore');
56
var mkdirp = require('mkdirp');
67

78
const file = {}
@@ -61,7 +62,7 @@ file.listCodeDir = function(dir) {
6162
});
6263
};
6364

64-
/// general dirs & files
65+
/// general dirs & files ///
6566
file.mkdir = function(fullpath) {
6667
if (fs.existsSync(fullpath)) return;
6768
mkdirp.sync(fullpath);
@@ -79,4 +80,56 @@ file.data = function(fullpath) {
7980
return fs.existsSync(fullpath) ? fs.readFileSync(fullpath).toString() : null;
8081
};
8182

83+
/// templates & metadata ///
84+
file.render = function(tpl, data) {
85+
const tplfile = path.join(this.codeDir('templates'), tpl + '.tpl');
86+
let result = _.template(this.data(tplfile))(data);
87+
88+
if (this.isWindows()) {
89+
result = result.replace(/\n/g, '\r\n');
90+
} else {
91+
result = result.replace(/\r\n/g, '\n');
92+
}
93+
return result;
94+
};
95+
96+
file.metaByName = function(filename) {
97+
const m = {};
98+
99+
// expect the 1st section in filename as id
100+
// e.g. 1.two-sum.cpp
101+
m.id = file.name(filename).split('.')[0];
102+
103+
// HACK: compatible with old ext
104+
if (filename.endsWith('.py3') || filename.endsWith('.python3.py'))
105+
m.lang = 'python3';
106+
else
107+
m.lang = require('./helper').extToLang(filename);
108+
109+
return m;
110+
};
111+
112+
file.meta = function(filename) {
113+
const m = {};
114+
115+
// first look into the file data
116+
const line = this.data(filename).split('\n')
117+
.find(x => x.indexOf(' @lc ') >= 0) || '';
118+
line.split(' ').forEach(function(x) {
119+
const v = x.split('=');
120+
if (v.length == 2) {
121+
m[v[0]] = v[1];
122+
}
123+
});
124+
125+
// otherwise, look into file name
126+
if (!m.id || !m.lang) {
127+
const olddata = this.metaByName(filename);
128+
m.id = m.id || olddata.id;
129+
m.lang = m.lang || olddata.lang;
130+
}
131+
132+
return m;
133+
};
134+
82135
module.exports = file;

‎lib/helper.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const LANGS = [
4040
{lang: 'kotlin', ext: '.kt', style: 'c'},
4141
{lang: 'mysql', ext: '.sql', style: '#'},
4242
{lang: 'python', ext: '.py', style: '#'},
43-
{lang: 'python3', ext: '.python3.py', style: '#'},
43+
{lang: 'python3', ext: '.py', style: '#'},
4444
{lang: 'ruby', ext: '.rb', style: '#'},
4545
{lang: 'scala', ext: '.scala', style: 'c'},
4646
{lang: 'swift', ext: '.swift', style: 'c'}
@@ -124,14 +124,8 @@ h.langToExt = function(lang) {
124124
};
125125

126126
h.extToLang = function(fullpath) {
127-
// HACK: compatible with old ext
128-
if (fullpath.endsWith('.py3')) return 'python3';
129-
130-
const res = _.chain(LANGS)
131-
.filter(x => fullpath.endsWith(x.ext))
132-
.sortBy(x => -x.ext.length)
133-
.value();
134-
return res.length ? res[0].lang : 'unknown';
127+
const res = LANGS.find(x => fullpath.endsWith(x.ext));
128+
return res ? res.lang : 'unknown';
135129
};
136130

137131
h.langToCommentStyle = function(lang) {

‎lib/plugins/leetcode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ function runCode(opts, problem, cb) {
182182

183183
opts.body = opts.body || {};
184184
_.extendOwn(opts.body, {
185-
lang: h.extToLang(problem.file),
185+
lang: problem.lang,
186186
question_id: parseInt(problem.id, 10),
187187
test_mode: false,
188188
typed_code: file.data(problem.file)

‎templates/detailed.tpl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<%= comment.start %>
2-
<%= comment.line %> [<%= id %>] <%= name %>
2+
<%= comment.line %> @lc app=<%= app %> id=<%= fid %> lang=<%= lang %>
3+
<%= comment.line %>
4+
<%= comment.line %> [<%= fid %>] <%= name %>
35
<%= comment.line %>
46
<%= comment.line %> <%= link %>
57
<%= comment.line %>

‎test/test_core.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ describe('core', function() {
205205
it('should detailed ok with cpp', function() {
206206
const expected = [
207207
'/*',
208+
' * @lc app=leetcode id=2 lang=cpp',
209+
' *',
208210
' * [2] Add Two Numbers',
209211
' *',
210212
' * https://leetcode.com/problems/add-two-numbers',
@@ -250,6 +252,8 @@ describe('core', function() {
250252

251253
it('should detailed ok with ruby', function() {
252254
const expected = [
255+
'#',
256+
'# @lc app=leetcode id=2 lang=ruby',
253257
'#',
254258
'# [2] Add Two Numbers',
255259
'#',

‎test/test_file.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,48 @@ describe('file', function() {
5555
assert.equal(file.data('non-exist'), null);
5656
});
5757
}); // #dirAndFiles
58+
59+
describe('#meta', function() {
60+
it('should meta ok within file content', function() {
61+
file.data = x => [
62+
'/ *',
63+
' * @lc app=leetcode id=123 lang=javascript',
64+
' * /'
65+
].join('\n');
66+
const meta = file.meta('dummy');
67+
assert.equal(meta.app, 'leetcode')
68+
assert.equal(meta.id, '123');
69+
assert.equal(meta.lang, 'javascript');
70+
});
71+
72+
it('should meta ok within file name', function() {
73+
file.data = x => [
74+
'/ *',
75+
' * no meta app=leetcode id=123 lang=javascript',
76+
' * /'
77+
].join('\n');
78+
const meta = file.meta('321.dummy.py');
79+
assert(!meta.app)
80+
assert.equal(meta.id, '321');
81+
assert.equal(meta.lang, 'python');
82+
});
83+
84+
it('should meta ok within deprecated file name', function() {
85+
file.data = x => [
86+
'/ *',
87+
' * no meta app=leetcode id=123 lang=javascript',
88+
' * /'
89+
].join('\n');
90+
91+
var meta = file.meta('111.dummy.py3');
92+
assert(!meta.app)
93+
assert.equal(meta.id, '111');
94+
assert.equal(meta.lang, 'python3');
95+
96+
meta = file.meta('222.dummy.python3.py');
97+
assert(!meta.app)
98+
assert.equal(meta.id, '222');
99+
assert.equal(meta.lang, 'python3');
100+
});
101+
}); // #meta
58102
});

‎test/test_helper.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ describe('helper', function() {
129129
assert.equal(h.langToExt('javascript'), '.js');
130130
assert.equal(h.langToExt('mysql'), '.sql');
131131
assert.equal(h.langToExt('python'), '.py');
132-
assert.equal(h.langToExt('python3'), '.python3.py');
132+
assert.equal(h.langToExt('python3'), '.py');
133133
assert.equal(h.langToExt('ruby'), '.rb');
134134
assert.equal(h.langToExt('scala'), '.scala');
135135
assert.equal(h.langToExt('swift'), '.swift');
@@ -147,8 +147,6 @@ describe('helper', function() {
147147
assert.equal(h.extToLang('file.java'), 'java');
148148
assert.equal(h.extToLang('c:/file.js'), 'javascript');
149149
assert.equal(h.extToLang('c:/Users/skygragon/file.py'), 'python');
150-
assert.equal(h.extToLang('c:/Users/skygragon/file.py3'), 'python3');
151-
assert.equal(h.extToLang('c:/Users/skygragon/file.python3.py'), 'python3');
152150
assert.equal(h.extToLang('~/file.rb'), 'ruby');
153151
assert.equal(h.extToLang('/tmp/file.scala'), 'scala');
154152
assert.equal(h.extToLang('~/leetcode/file.swift'), 'swift');

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /