From 758fd07ca92127fb713235ecd91cc0d4ab9a28a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E5=8D=A1=E7=90=B3?= Date: Mon, 16 Sep 2019 14:50:08 +0800 Subject: [PATCH 1/3] add file.codeData to extract user code --- lib/file.js | 20 +++++++++++++++++++- lib/helper.js | 4 ++-- lib/plugins/leetcode.js | 2 +- templates/codeonly.tpl | 2 ++ templates/detailed.tpl | 2 ++ test/test_core.js | 8 ++++++++ test/test_helper.js | 6 +++--- 7 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/file.js b/lib/file.js index 51ea21c3..ab524521 100644 --- a/lib/file.js +++ b/lib/file.js @@ -107,6 +107,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(this.isWindows() ? '\r\n' : '\n'); + } + + return data; +}; + /// templates & metadata /// file.render = function(tpl, data) { const tplfile = path.join(this.codeDir('templates'), tpl + '.tpl'); @@ -145,7 +163,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..b1aae3da 100644 --- a/templates/codeonly.tpl +++ b/templates/codeonly.tpl @@ -3,4 +3,6 @@ ${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..020519d6 100644 --- a/templates/detailed.tpl +++ b/templates/detailed.tpl @@ -15,4 +15,6 @@ ${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..7c23d0e8 100644 --- a/test/test_core.js +++ b/test/test_core.js @@ -159,6 +159,7 @@ describe('core', function() { ' *', ' * [2] Add Two Numbers', ' */', + '// @lc code=start', '/**', ' * Definition for singly-linked list.', ' * struct ListNode {', @@ -173,6 +174,7 @@ describe('core', function() { ' ', ' }', '};', + '// @lc code=end', '' ].join('\n'); @@ -194,6 +196,7 @@ describe('core', function() { ' *', ' * [2] Add Two Numbers', ' */', + '// @lc code=start', '/**', ' * Definition for singly-linked list.', ' * struct ListNode {', @@ -208,6 +211,7 @@ describe('core', function() { ' ', ' }', '};', + '// @lc code=end', '' ].join('\r\n'); @@ -246,6 +250,7 @@ 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 +265,7 @@ describe('core', function() { ' ', ' }', '};', + '// @lc code=end', '' ].join('\n'); @@ -298,6 +304,7 @@ 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 +320,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); From 173b18b64bcf26f025498c70fabe1c27615e5c7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E5=8D=A1=E7=90=B3?= Date: Mon, 16 Sep 2019 20:52:22 +0800 Subject: [PATCH 2/3] add a single empty line between the block comments and the single line comments. --- templates/codeonly.tpl | 1 + templates/detailed.tpl | 1 + test/test_core.js | 4 ++++ 3 files changed, 6 insertions(+) diff --git a/templates/codeonly.tpl b/templates/codeonly.tpl index b1aae3da..ae5b54a7 100644 --- a/templates/codeonly.tpl +++ b/templates/codeonly.tpl @@ -3,6 +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 020519d6..7329a7d7 100644 --- a/templates/detailed.tpl +++ b/templates/detailed.tpl @@ -15,6 +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 7c23d0e8..195f736a 100644 --- a/test/test_core.js +++ b/test/test_core.js @@ -159,6 +159,7 @@ describe('core', function() { ' *', ' * [2] Add Two Numbers', ' */', + '', '// @lc code=start', '/**', ' * Definition for singly-linked list.', @@ -196,6 +197,7 @@ describe('core', function() { ' *', ' * [2] Add Two Numbers', ' */', + '', '// @lc code=start', '/**', ' * Definition for singly-linked list.', @@ -250,6 +252,7 @@ describe('core', function() { ' * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)', ' * Output: 7 -> 0 -> 8', ' */', + '', '// @lc code=start', '/**', ' * Definition for singly-linked list.', @@ -304,6 +307,7 @@ describe('core', function() { '# Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)', '# Output: 7 -> 0 -> 8', '#', + '', '# @lc code=start', '# Definition for singly-linked list.', '# class ListNode', From 9adf07d04e0efc9c5da349e52b672b077ab9c251 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=98=BF=E5=8D=A1=E7=90=B3?= Date: Mon, 16 Sep 2019 20:57:12 +0800 Subject: [PATCH 3/3] use os.EOL instead --- lib/file.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/file.js b/lib/file.js index ab524521..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'); @@ -119,7 +120,7 @@ file.codeData = function(fullpath) { 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(this.isWindows() ? '\r\n' : '\n'); + return lines.slice(start + 1, end).join(os.EOL); } return data;