Skip to content

Commit

Permalink
add file.codeData to extract user code (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
magic-akari authored and jdneo committed Sep 17, 2019
1 parent 6ff12d8 commit b361581
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 7 deletions.
21 changes: 20 additions & 1 deletion lib/file.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
var fs = require('fs');
var os = require('os');
var path = require('path');

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

file.codeData = function(fullpath) {
const data = this.data(fullpath);

if (data === null) {
return null;
}

const lines = data.split(/\r\n|\n|\r/);
const start = lines.findIndex(x => x.indexOf('@lc code=start') !== -1);
const end = lines.findIndex(x => x.indexOf('@lc code=end') !== -1);

if (start !== -1 && end !== -1 && start + 1 <= end) {
return lines.slice(start + 1, end).join(os.EOL);
}

return data;
};

/// templates & metadata ///
file.render = function(tpl, data) {
const tplfile = path.join(this.codeDir('templates'), tpl + '.tpl');
Expand Down Expand Up @@ -145,7 +164,7 @@ file.meta = function(filename) {

// first look into the file data
const line = this.data(filename).split('\n')
.find(x => x.indexOf(' @lc ') >= 0) || '';
.find(x => x.indexOf(' @lc app=') >= 0) || '';
line.split(' ').forEach(function(x) {
const v = x.split('=');
if (v.length == 2) {
Expand Down
4 changes: 2 additions & 2 deletions lib/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ h.langToCommentStyle = function(lang) {
const res = LANGS.find(x => x.lang === lang);

return (res && res.style === 'c') ?
{start: '/*', line: ' *', end: ' */'} :
{start: res.style, line: res.style, end: res.style};
{start: '/*', line: ' *', end: ' */', singleLine: '//'} :
{start: res.style, line: res.style, end: res.style, singleLine: res.style};
};

h.readStdin = function(cb) {
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/leetcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function runCode(opts, problem, cb) {
lang: problem.lang,
question_id: parseInt(problem.id, 10),
test_mode: false,
typed_code: file.data(problem.file)
typed_code: file.codeData(problem.file)
});

const spin = h.spin('Sending code to judge');
Expand Down
3 changes: 3 additions & 0 deletions templates/codeonly.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ ${comment.line} @lc app=${app} id=${fid} lang=${lang}
${comment.line}
${comment.line} [${fid}] ${name}
${comment.end}

${comment.singleLine} @lc code=start
${code}
${comment.singleLine} @lc code=end
3 changes: 3 additions & 0 deletions templates/detailed.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ ${comment.line} Testcase Example: ${testcase}
${comment.line}
{{ desc.forEach(function(x) { }}${comment.line} ${x}
{{ }) }}${comment.end}

${comment.singleLine} @lc code=start
${code}
${comment.singleLine} @lc code=end
12 changes: 12 additions & 0 deletions test/test_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ describe('core', function() {
' *',
' * [2] Add Two Numbers',
' */',
'',
'// @lc code=start',
'/**',
' * Definition for singly-linked list.',
' * struct ListNode {',
Expand All @@ -173,6 +175,7 @@ describe('core', function() {
' ',
' }',
'};',
'// @lc code=end',
''
].join('\n');

Expand All @@ -194,6 +197,8 @@ describe('core', function() {
' *',
' * [2] Add Two Numbers',
' */',
'',
'// @lc code=start',
'/**',
' * Definition for singly-linked list.',
' * struct ListNode {',
Expand All @@ -208,6 +213,7 @@ describe('core', function() {
' ',
' }',
'};',
'// @lc code=end',
''
].join('\r\n');

Expand Down Expand Up @@ -246,6 +252,8 @@ describe('core', function() {
' * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)',
' * Output: 7 -> 0 -> 8',
' */',
'',
'// @lc code=start',
'/**',
' * Definition for singly-linked list.',
' * struct ListNode {',
Expand All @@ -260,6 +268,7 @@ describe('core', function() {
' ',
' }',
'};',
'// @lc code=end',
''
].join('\n');

Expand Down Expand Up @@ -298,6 +307,8 @@ describe('core', function() {
'# Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)',
'# Output: 7 -> 0 -> 8',
'#',
'',
'# @lc code=start',
'# Definition for singly-linked list.',
'# class ListNode',
'# attr_accessor :val, :next',
Expand All @@ -313,6 +324,7 @@ describe('core', function() {
'def add_two_numbers(l1, l2)',
' ',
'end',
'# @lc code=end',
''
].join('\n');

Expand Down
6 changes: 3 additions & 3 deletions test/test_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ describe('helper', function() {

describe('#langToCommentStyle', function() {
it('should ok', function() {
const C_STYLE = {start: '/*', line: ' *', end: ' */'};
const RUBY_STYLE = {start: '#', line: '#', end: '#'};
const SQL_STYLE = {start: '--', line: '--', end: '--'};
const C_STYLE = {start: '/*', line: ' *', end: ' */', singleLine: '//'};
const RUBY_STYLE = {start: '#', line: '#', end: '#', singleLine: '#'};
const SQL_STYLE = {start: '--', line: '--', end: '--', singleLine: '--'};

assert.deepEqual(h.langToCommentStyle('bash'), RUBY_STYLE);
assert.deepEqual(h.langToCommentStyle('c'), C_STYLE);
Expand Down

0 comments on commit b361581

Please sign in to comment.