Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fb0270e

Browse files
Refactor template logic.
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent cc0983e commit fb0270e

File tree

7 files changed

+68
-59
lines changed

7 files changed

+68
-59
lines changed

‎lib/commands/show.js‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,18 @@ cmd.handler = function(argv) {
6868

6969
var filename;
7070
if (argv.gen) {
71-
problem.code = template.defaultCode;
72-
7371
// try to use a new filename to avoid overwrite by mistake
7472
filename = problem.id + '.' + problem.slug + h.langToExt(argv.lang);
7573
var i = 0;
7674
while (fs.existsSync(filename))
7775
filename = problem.id + '.' + problem.slug + '.' + (i++) + h.langToExt(argv.lang);
7876

79-
core.exportProblem(problem, filename, !argv.extra);
77+
var opts = {
78+
lang: argv.lang,
79+
code: template.defaultCode,
80+
tpl: argv.extra ? 'detailed' : 'codeonly'
81+
};
82+
fs.writeFileSync(filename, core.exportProblem(problem, opts));
8083

8184
if (argv.editor !== undefined) {
8285
childProcess.spawn(argv.editor || config.EDITOR, [filename], {

‎lib/commands/submission.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,12 @@ function exportSubmission(argv, problem, cb) {
108108
core.getSubmission(submission, function(e, submission) {
109109
if (e) return cb(e);
110110

111-
problem.code = submission.code;
112-
core.exportProblem(problem, filename, !argv.extra);
111+
var opts = {
112+
lang: submission.lang,
113+
code: submission.code,
114+
tpl: argv.extra ? 'detailed' : 'codeonly'
115+
};
116+
fs.writeFileSync(filename, core.exportProblem(problem, opts));
113117

114118
if (submission.status_display === 'Accepted')
115119
cb(null, chalk.green.underline(filename));

‎lib/core.js‎

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var fs = require('fs');
21
var path = require('path');
32
var util = require('util');
43

@@ -48,40 +47,32 @@ core.starProblem = function(problem, starred, cb) {
4847
core.next.starProblem(problem, starred, cb);
4948
};
5049

51-
core.exportProblem = function(problem, f,codeOnly) {
52-
varoutput='';
53-
problem.code=problem.code.replace(/\r\n/g,'\n');
50+
core.exportProblem = function(problem, opts) {
51+
// copy problem attrs thus we can render it in template
52+
varinput=_.extend({},problem);
5453

55-
if (codeOnly) {
56-
output = problem.code;
57-
} else {
58-
var input = {
59-
comment: h.langToCommentStyle(h.extToLang(f))
60-
};
61-
// copy problem attrs thus we can render it in template
62-
_.extend(input, problem);
63-
input.percent = input.percent.toFixed(2);
64-
input.testcase = util.inspect(input.testcase || '');
54+
input.code = opts.code.replace(/\r\n/g, '\n');
55+
input.comment = h.langToCommentStyle(opts.lang);
56+
input.percent = input.percent.toFixed(2);
57+
input.testcase = util.inspect(input.testcase || '');
6558

59+
if (opts.tpl === 'detailed') {
6660
// NOTE: wordwrap internally uses '\n' as EOL, so here we have to
6761
// remove all '\r' in the raw string.
68-
var desc = input.desc.replace(/\r\n/g, '\n')
69-
.replace(/^/mg, '⁠');
70-
62+
var desc = input.desc.replace(/\r\n/g, '\n').replace(/^/mg, '⁠');
7163
var wrap = require('wordwrap')(79 - input.comment.line.length);
7264
input.desc = wrap(desc).split('\n');
73-
74-
var tpl = h.getFileData(path.resolve(__dirname, '../source.tpl'));
75-
output = _.template(tpl)(input);
7665
}
7766

67+
var tplfile = path.resolve(__dirname, '../templates/' + opts.tpl + '.tpl');
68+
var output = _.template(h.getFileData(tplfile))(input);
69+
7870
if (h.isWindows()) {
7971
output = output.replace(/\n/g, '\r\n');
8072
} else {
8173
output = output.replace(/\r\n/g, '\n');
8274
}
83-
84-
fs.writeFileSync(f, output);
75+
return output;
8576
};
8677

8778
module.exports = core;

‎templates/codeonly.tpl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= code %>
File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"state":"ac","id":2,"category":"algorithms","name":"Add Two Numbers","key":"add-two-numbers","link":"https://leetcode.com/problems/add-two-numbers","locked":false,"percent":25.368142876074806,"level":"Medium","starred":true,"totalAC":"195263","totalSubmit":"769711","desc":"You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.\r\n\r\nInput: (2 -> 4 -> 3) + (5 -> 6 -> 4)\r\nOutput: 7 -> 0 -> 8","templates":[{"value":"cpp","text":"C++","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * struct ListNode {\r\n * int val;\r\n * ListNode *next;\r\n * ListNode(int x) : val(x), next(NULL) {}\r\n * };\r\n */\r\nclass Solution {\r\npublic:\r\n ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {\r\n \r\n }\r\n};"},{"value":"java","text":"Java","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * public class ListNode {\r\n * int val;\r\n * ListNode next;\r\n * ListNode(int x) { val = x; }\r\n * }\r\n */\r\npublic class Solution {\r\n public ListNode addTwoNumbers(ListNode l1, ListNode l2) {\r\n \r\n }\r\n}"},{"value":"python","text":"Python","defaultCode":"# Definition for singly-linked list.\r\n# class ListNode(object):\r\n# def __init__(self, x):\r\n# self.val = x\r\n# self.next = None\r\n\r\nclass Solution(object):\r\n def addTwoNumbers(self, l1, l2):\r\n \"\"\"\r\n :type l1: ListNode\r\n :type l2: ListNode\r\n :rtype: ListNode\r\n \"\"\"\r\n "},{"value":"c","text":"C","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * struct ListNode {\r\n * int val;\r\n * struct ListNode *next;\r\n * };\r\n */\r\nstruct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {\r\n \r\n}"},{"value":"csharp","text":"C#","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * public class ListNode {\r\n * public int val;\r\n * public ListNode next;\r\n * public ListNode(int x) { val = x; }\r\n * }\r\n */\r\npublic class Solution {\r\n public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {\r\n \r\n }\r\n}"},{"value":"javascript","text":"JavaScript","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * function ListNode(val) {\r\n * this.val = val;\r\n * this.next = null;\r\n * }\r\n */\r\n/**\r\n * @param {ListNode} l1\r\n * @param {ListNode} l2\r\n * @return {ListNode}\r\n */\r\nvar addTwoNumbers = function(l1, l2) {\r\n \r\n};"},{"value":"ruby","text":"Ruby","defaultCode":"# Definition for singly-linked list.\r\n# class ListNode\r\n# attr_accessor :val, :next\r\n# def initialize(val)\r\n# @val = val\r\n# @next = nil\r\n# end\r\n# end\r\n\r\n# @param {ListNode} l1\r\n# @param {ListNode} l2\r\n# @return {ListNode}\r\ndef add_two_numbers(l1, l2)\r\n \r\nend"},{"value":"swift","text":"Swift","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * public class ListNode {\r\n * public var val: Int\r\n * public var next: ListNode?\r\n * public init(_ val: Int) {\r\n * self.val = val\r\n * self.next = nil\r\n * }\r\n * }\r\n */\r\nclass Solution {\r\n func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {\r\n \r\n }\r\n}"},{"value":"golang","text":"Go","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * type ListNode struct {\r\n * Val int\r\n * Next *ListNode\r\n * }\r\n */\r\nfunc addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {\r\n \r\n}"}],"testcase":"[2,4,3]\n[5,6,4]","testable":true,"code":"class Solution {\r\npublic:\r\n ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {\r\n\r\n }\r\n};"}
1+
{"state":"ac","id":2,"category":"algorithms","name":"Add Two Numbers","key":"add-two-numbers","link":"https://leetcode.com/problems/add-two-numbers","locked":false,"percent":25.368142876074806,"level":"Medium","starred":true,"totalAC":"195263","totalSubmit":"769711","desc":"You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.\r\n\r\nInput: (2 -> 4 -> 3) + (5 -> 6 -> 4)\r\nOutput: 7 -> 0 -> 8","templates":[{"value":"cpp","text":"C++","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * struct ListNode {\r\n * int val;\r\n * ListNode *next;\r\n * ListNode(int x) : val(x), next(NULL) {}\r\n * };\r\n */\r\nclass Solution {\r\npublic:\r\n ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {\r\n \r\n }\r\n};"},{"value":"java","text":"Java","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * public class ListNode {\r\n * int val;\r\n * ListNode next;\r\n * ListNode(int x) { val = x; }\r\n * }\r\n */\r\npublic class Solution {\r\n public ListNode addTwoNumbers(ListNode l1, ListNode l2) {\r\n \r\n }\r\n}"},{"value":"python","text":"Python","defaultCode":"# Definition for singly-linked list.\r\n# class ListNode(object):\r\n# def __init__(self, x):\r\n# self.val = x\r\n# self.next = None\r\n\r\nclass Solution(object):\r\n def addTwoNumbers(self, l1, l2):\r\n \"\"\"\r\n :type l1: ListNode\r\n :type l2: ListNode\r\n :rtype: ListNode\r\n \"\"\"\r\n "},{"value":"c","text":"C","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * struct ListNode {\r\n * int val;\r\n * struct ListNode *next;\r\n * };\r\n */\r\nstruct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {\r\n \r\n}"},{"value":"csharp","text":"C#","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * public class ListNode {\r\n * public int val;\r\n * public ListNode next;\r\n * public ListNode(int x) { val = x; }\r\n * }\r\n */\r\npublic class Solution {\r\n public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {\r\n \r\n }\r\n}"},{"value":"javascript","text":"JavaScript","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * function ListNode(val) {\r\n * this.val = val;\r\n * this.next = null;\r\n * }\r\n */\r\n/**\r\n * @param {ListNode} l1\r\n * @param {ListNode} l2\r\n * @return {ListNode}\r\n */\r\nvar addTwoNumbers = function(l1, l2) {\r\n \r\n};"},{"value":"ruby","text":"Ruby","defaultCode":"# Definition for singly-linked list.\r\n# class ListNode\r\n# attr_accessor :val, :next\r\n# def initialize(val)\r\n# @val = val\r\n# @next = nil\r\n# end\r\n# end\r\n\r\n# @param {ListNode} l1\r\n# @param {ListNode} l2\r\n# @return {ListNode}\r\ndef add_two_numbers(l1, l2)\r\n \r\nend"},{"value":"swift","text":"Swift","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * public class ListNode {\r\n * public var val: Int\r\n * public var next: ListNode?\r\n * public init(_ val: Int) {\r\n * self.val = val\r\n * self.next = nil\r\n * }\r\n * }\r\n */\r\nclass Solution {\r\n func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {\r\n \r\n }\r\n}"},{"value":"golang","text":"Go","defaultCode":"/**\r\n * Definition for singly-linked list.\r\n * type ListNode struct {\r\n * Val int\r\n * Next *ListNode\r\n * }\r\n */\r\nfunc addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {\r\n \r\n}"}],"testcase":"[2,4,3]\n[5,6,4]","testable":true}

‎test/test_core.js‎

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
var fs = require('fs');
2-
3-
var _ = require('underscore');
41
var assert = require('chai').assert;
52
var rewire = require('rewire');
63

@@ -71,33 +68,35 @@ describe('core', function() {
7168
}); // #starProblem
7269

7370
describe('#exportProblem', function() {
74-
function injectVerify(expected, done) {
75-
plugin.__set__('fs', {
76-
writeFileSync: function(f, data) {
77-
assert.equal(data, expected);
78-
done();
79-
},
80-
readFileSync: fs.readFileSync
81-
});
82-
}
83-
84-
it('should ok w/ code only', function(done) {
71+
it('should codeonly ok', function() {
8572
var expected = [
73+
'/**',
74+
' * Definition for singly-linked list.',
75+
' * struct ListNode {',
76+
' * int val;',
77+
' * ListNode *next;',
78+
' * ListNode(int x) : val(x), next(NULL) {}',
79+
' * };',
80+
' */',
8681
'class Solution {',
8782
'public:',
8883
' ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {',
89-
'',
84+
'',
9085
' }',
91-
'};'
86+
'};',
87+
''
9288
].join('\n');
9389

94-
injectVerify(expected, done);
95-
9690
var problem = require('./mock/add-two-numbers.20161015.json');
97-
plugin.exportProblem(problem, 'test.cpp', true);
91+
var opts = {
92+
lang: 'cpp',
93+
code: problem.templates[0].defaultCode,
94+
tpl: 'codeonly'
95+
};
96+
assert.equal(plugin.exportProblem(problem, opts), expected);
9897
});
9998

100-
it('should ok w/ detailed comments', function(done) {
99+
it('should detailed ok', function() {
101100
var expected = [
102101
'/*',
103102
' * [2] Add Two Numbers',
@@ -117,22 +116,33 @@ describe('core', function() {
117116
' * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)',
118117
' * Output: 7 -> 0 -> 8',
119118
' */',
119+
'/**',
120+
' * Definition for singly-linked list.',
121+
' * struct ListNode {',
122+
' * int val;',
123+
' * ListNode *next;',
124+
' * ListNode(int x) : val(x), next(NULL) {}',
125+
' * };',
126+
' */',
120127
'class Solution {',
121128
'public:',
122129
' ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {',
123-
'',
130+
'',
124131
' }',
125132
'};',
126133
''
127134
].join('\n');
128135

129-
injectVerify(expected, done);
130-
131136
var problem = require('./mock/add-two-numbers.20161015.json');
132-
plugin.exportProblem(problem, 'test.cpp', false);
137+
var opts = {
138+
lang: 'cpp',
139+
code: problem.templates[0].defaultCode,
140+
tpl: 'detailed'
141+
};
142+
assert.equal(plugin.exportProblem(problem, opts), expected);
133143
});
134144

135-
it('should ok w/ detailed comments, 2nd', function(done) {
145+
it('should detailed ok, 2nd', function() {
136146
var expected = [
137147
'#',
138148
'# [2] Add Two Numbers',
@@ -170,14 +180,14 @@ describe('core', function() {
170180
''
171181
].join('\n');
172182

173-
injectVerify(expected, done);
174-
175183
var problem = require('./mock/add-two-numbers.20161015.json');
176184
problem.testcase = null;
177-
problem.code = _.find(problem.templates, function(template) {
178-
return template.value === 'ruby';
179-
}).defaultCode;
180-
plugin.exportProblem(problem, 'test.rb', false);
185+
var opts = {
186+
lang: 'ruby',
187+
code: problem.templates[6].defaultCode,
188+
tpl: 'detailed'
189+
};
190+
assert.equal(plugin.exportProblem(problem, opts), expected);
181191
});
182192
}); // #exportProblem
183193

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /