diff --git a/lib/file.js b/lib/file.js index 51ea21c3..c2262df1 100644 --- a/lib/file.js +++ b/lib/file.js @@ -1,5 +1,6 @@ 'use strict'; var fs = require('fs'); +var os = require('os'); var path = require('path'); var _ = require('underscore'); @@ -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'); @@ -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) { diff --git a/lib/helper.js b/lib/helper.js index 8806086e..c4f881aa 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -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) { diff --git a/lib/plugins/leetcode.js b/lib/plugins/leetcode.js index cc540ada..057e83ff 100644 --- a/lib/plugins/leetcode.js +++ b/lib/plugins/leetcode.js @@ -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'); diff --git a/templates/codeonly.tpl b/templates/codeonly.tpl index 22ea0a6b..ae5b54a7 100644 --- a/templates/codeonly.tpl +++ b/templates/codeonly.tpl @@ -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 diff --git a/templates/detailed.tpl b/templates/detailed.tpl index c8ac653c..7329a7d7 100644 --- a/templates/detailed.tpl +++ b/templates/detailed.tpl @@ -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 diff --git a/test/test_core.js b/test/test_core.js index 61ea2efd..195f736a 100644 --- a/test/test_core.js +++ b/test/test_core.js @@ -159,6 +159,8 @@ describe('core', function() { ' *', ' * [2] Add Two Numbers', ' */', + '', + '// @lc code=start', '/**', ' * Definition for singly-linked list.', ' * struct ListNode {', @@ -173,6 +175,7 @@ describe('core', function() { ' ', ' }', '};', + '// @lc code=end', '' ].join('\n'); @@ -194,6 +197,8 @@ describe('core', function() { ' *', ' * [2] Add Two Numbers', ' */', + '', + '// @lc code=start', '/**', ' * Definition for singly-linked list.', ' * struct ListNode {', @@ -208,6 +213,7 @@ describe('core', function() { ' ', ' }', '};', + '// @lc code=end', '' ].join('\r\n'); @@ -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 {', @@ -260,6 +268,7 @@ describe('core', function() { ' ', ' }', '};', + '// @lc code=end', '' ].join('\n'); @@ -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', @@ -313,6 +324,7 @@ describe('core', function() { 'def add_two_numbers(l1, l2)', ' ', 'end', + '# @lc code=end', '' ].join('\n'); diff --git a/test/test_helper.js b/test/test_helper.js index b3a8cf08..18a2979d 100644 --- a/test/test_helper.js +++ b/test/test_helper.js @@ -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);