Skip to content

Commit b361581

Browse files
magic-akarijdneo
authored andcommitted
add file.codeData to extract user code (#23)
1 parent 6ff12d8 commit b361581

File tree

7 files changed

+44
-7
lines changed

7 files changed

+44
-7
lines changed

lib/file.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
var fs = require('fs');
3+
var os = require('os');
34
var path = require('path');
45

56
var _ = require('underscore');
@@ -107,6 +108,24 @@ file.data = function(fullpath) {
107108
return fs.existsSync(fullpath) ? fs.readFileSync(fullpath).toString() : null;
108109
};
109110

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

146165
// first look into the file data
147166
const line = this.data(filename).split('\n')
148-
.find(x => x.indexOf(' @lc ') >= 0) || '';
167+
.find(x => x.indexOf(' @lc app=') >= 0) || '';
149168
line.split(' ').forEach(function(x) {
150169
const v = x.split('=');
151170
if (v.length == 2) {

lib/helper.js

+2-2
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

+1-1
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

+3
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ ${comment.line} @lc app=${app} id=${fid} lang=${lang}
33
${comment.line}
44
${comment.line} [${fid}] ${name}
55
${comment.end}
6+
7+
${comment.singleLine} @lc code=start
68
${code}
9+
${comment.singleLine} @lc code=end

templates/detailed.tpl

+3
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,7 @@ ${comment.line} Testcase Example: ${testcase}
1515
${comment.line}
1616
{{ desc.forEach(function(x) { }}${comment.line} ${x}
1717
{{ }) }}${comment.end}
18+
19+
${comment.singleLine} @lc code=start
1820
${code}
21+
${comment.singleLine} @lc code=end

test/test_core.js

+12
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ describe('core', function() {
159159
' *',
160160
' * [2] Add Two Numbers',
161161
' */',
162+
'',
163+
'// @lc code=start',
162164
'/**',
163165
' * Definition for singly-linked list.',
164166
' * struct ListNode {',
@@ -173,6 +175,7 @@ describe('core', function() {
173175
' ',
174176
' }',
175177
'};',
178+
'// @lc code=end',
176179
''
177180
].join('\n');
178181

@@ -194,6 +197,8 @@ describe('core', function() {
194197
' *',
195198
' * [2] Add Two Numbers',
196199
' */',
200+
'',
201+
'// @lc code=start',
197202
'/**',
198203
' * Definition for singly-linked list.',
199204
' * struct ListNode {',
@@ -208,6 +213,7 @@ describe('core', function() {
208213
' ',
209214
' }',
210215
'};',
216+
'// @lc code=end',
211217
''
212218
].join('\r\n');
213219

@@ -246,6 +252,8 @@ describe('core', function() {
246252
' * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)',
247253
' * Output: 7 -> 0 -> 8',
248254
' */',
255+
'',
256+
'// @lc code=start',
249257
'/**',
250258
' * Definition for singly-linked list.',
251259
' * struct ListNode {',
@@ -260,6 +268,7 @@ describe('core', function() {
260268
' ',
261269
' }',
262270
'};',
271+
'// @lc code=end',
263272
''
264273
].join('\n');
265274

@@ -298,6 +307,8 @@ describe('core', function() {
298307
'# Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)',
299308
'# Output: 7 -> 0 -> 8',
300309
'#',
310+
'',
311+
'# @lc code=start',
301312
'# Definition for singly-linked list.',
302313
'# class ListNode',
303314
'# attr_accessor :val, :next',
@@ -313,6 +324,7 @@ describe('core', function() {
313324
'def add_two_numbers(l1, l2)',
314325
' ',
315326
'end',
327+
'# @lc code=end',
316328
''
317329
].join('\n');
318330

test/test_helper.js

+3-3
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)