Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 758fd07

Browse files
committed
add file.codeData to extract user code
1 parent 6ff12d8 commit 758fd07

File tree

7 files changed

+37
-7
lines changed

7 files changed

+37
-7
lines changed

lib/file.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,24 @@ file.data = function(fullpath) {
107107
return fs.existsSync(fullpath) ? fs.readFileSync(fullpath).toString() : null;
108108
};
109109

110+
file.codeData = function(fullpath) {
111+
const data = this.data(fullpath);
112+
113+
if (data === null) {
114+
return null;
115+
}
116+
117+
const lines = data.split(/\r\n|\n|\r/);
118+
const start = lines.findIndex(x => x.indexOf('@lc code=start') !== -1);
119+
const end = lines.findIndex(x => x.indexOf('@lc code=end') !== -1);
120+
121+
if (start !== -1 && end !== -1 && start + 1 <= end) {
122+
return lines.slice(start + 1, end).join(this.isWindows() ? '\r\n' : '\n');
123+
}
124+
125+
return data;
126+
};
127+
110128
/// templates & metadata ///
111129
file.render = function(tpl, data) {
112130
const tplfile = path.join(this.codeDir('templates'), tpl + '.tpl');
@@ -145,7 +163,7 @@ file.meta = function(filename) {
145163

146164
// first look into the file data
147165
const line = this.data(filename).split('\n')
148-
.find(x => x.indexOf(' @lc ') >= 0) || '';
166+
.find(x => x.indexOf(' @lc app=') >= 0) || '';
149167
line.split(' ').forEach(function(x) {
150168
const v = x.split('=');
151169
if (v.length == 2) {

lib/helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ h.langToCommentStyle = function(lang) {
134134
const res = LANGS.find(x => x.lang === lang);
135135

136136
return (res && res.style === 'c') ?
137-
{start: '/*', line: ' *', end: ' */'} :
138-
{start: res.style, line: res.style, end: res.style};
137+
{start: '/*', line: ' *', end: ' */', singleLine: '//'} :
138+
{start: res.style, line: res.style, end: res.style, singleLine: res.style};
139139
};
140140

141141
h.readStdin = function(cb) {

lib/plugins/leetcode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ function runCode(opts, problem, cb) {
185185
lang: problem.lang,
186186
question_id: parseInt(problem.id, 10),
187187
test_mode: false,
188-
typed_code: file.data(problem.file)
188+
typed_code: file.codeData(problem.file)
189189
});
190190

191191
const spin = h.spin('Sending code to judge');

templates/codeonly.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ ${comment.line} @lc app=${app} id=${fid} lang=${lang}
33
${comment.line}
44
${comment.line} [${fid}] ${name}
55
${comment.end}
6+
${comment.singleLine} @lc code=start
67
${code}
8+
${comment.singleLine} @lc code=end

templates/detailed.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ ${comment.line} Testcase Example: ${testcase}
1515
${comment.line}
1616
{{ desc.forEach(function(x) { }}${comment.line} ${x}
1717
{{ }) }}${comment.end}
18+
${comment.singleLine} @lc code=start
1819
${code}
20+
${comment.singleLine} @lc code=end

test/test_core.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ describe('core', function() {
159159
' *',
160160
' * [2] Add Two Numbers',
161161
' */',
162+
'// @lc code=start',
162163
'/**',
163164
' * Definition for singly-linked list.',
164165
' * struct ListNode {',
@@ -173,6 +174,7 @@ describe('core', function() {
173174
' ',
174175
' }',
175176
'};',
177+
'// @lc code=end',
176178
''
177179
].join('\n');
178180

@@ -194,6 +196,7 @@ describe('core', function() {
194196
' *',
195197
' * [2] Add Two Numbers',
196198
' */',
199+
'// @lc code=start',
197200
'/**',
198201
' * Definition for singly-linked list.',
199202
' * struct ListNode {',
@@ -208,6 +211,7 @@ describe('core', function() {
208211
' ',
209212
' }',
210213
'};',
214+
'// @lc code=end',
211215
''
212216
].join('\r\n');
213217

@@ -246,6 +250,7 @@ describe('core', function() {
246250
' * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)',
247251
' * Output: 7 -> 0 -> 8',
248252
' */',
253+
'// @lc code=start',
249254
'/**',
250255
' * Definition for singly-linked list.',
251256
' * struct ListNode {',
@@ -260,6 +265,7 @@ describe('core', function() {
260265
' ',
261266
' }',
262267
'};',
268+
'// @lc code=end',
263269
''
264270
].join('\n');
265271

@@ -298,6 +304,7 @@ describe('core', function() {
298304
'# Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)',
299305
'# Output: 7 -> 0 -> 8',
300306
'#',
307+
'# @lc code=start',
301308
'# Definition for singly-linked list.',
302309
'# class ListNode',
303310
'# attr_accessor :val, :next',
@@ -313,6 +320,7 @@ describe('core', function() {
313320
'def add_two_numbers(l1, l2)',
314321
' ',
315322
'end',
323+
'# @lc code=end',
316324
''
317325
].join('\n');
318326

test/test_helper.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ describe('helper', function() {
163163

164164
describe('#langToCommentStyle', function() {
165165
it('should ok', function() {
166-
const C_STYLE = {start: '/*', line: ' *', end: ' */'};
167-
const RUBY_STYLE = {start: '#', line: '#', end: '#'};
168-
const SQL_STYLE = {start: '--', line: '--', end: '--'};
166+
const C_STYLE = {start: '/*', line: ' *', end: ' */', singleLine: '//'};
167+
const RUBY_STYLE = {start: '#', line: '#', end: '#', singleLine: '#'};
168+
const SQL_STYLE = {start: '--', line: '--', end: '--', singleLine: '--'};
169169

170170
assert.deepEqual(h.langToCommentStyle('bash'), RUBY_STYLE);
171171
assert.deepEqual(h.langToCommentStyle('c'), C_STYLE);

0 commit comments

Comments
 (0)