Movatterモバイル変換


[0]ホーム

URL:


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

Commitdfbb5b1

Browse files
authored
feat: Added support for opting out endpoint translation (#56)
1 parentdd8ba8b commitdfbb5b1

17 files changed

+125
-70
lines changed

‎lib/cache.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ cache.init = function() {
99
file.mkdir(file.cacheDir());
1010
};
1111

12+
cache.deleteAll=function(){
13+
cache.list().forEach(value=>{
14+
cache.del(value.name);
15+
})
16+
};
17+
1218
cache.get=function(k){
1319
constfullpath=file.cacheFile(k);
1420
if(!file.exist(fullpath))returnnull;

‎lib/commands/list.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ const cmd = {
2828
default:false,
2929
describe:'Show extra details: category, companies, tags.'
3030
})
31+
.option('T',{
32+
alias:'dontTranslate',
33+
type:'boolean',
34+
default:false,
35+
describe:'Set to true to disable endpoint\'s translation',
36+
})
3137
.positional('keyword',{
3238
type:'string',
3339
default:'',

‎lib/commands/show.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ const cmd = {
5757
default:false,
5858
describe:'Show extra question details in source code'
5959
})
60+
.option('T',{
61+
alias:'dontTranslate',
62+
type:'boolean',
63+
default:false,
64+
describe:'Set to true to disable endpoint\'s translation',
65+
})
6066
.positional('keyword',{
6167
type:'string',
6268
default:'',
@@ -175,7 +181,7 @@ cmd.handler = function(argv) {
175181
session.argv=argv;
176182
if(argv.keyword.length>0){
177183
// show specific one
178-
core.getProblem(argv.keyword,function(e,problem){
184+
core.getProblem(argv.keyword,!argv.dontTranslate,function(e,problem){
179185
if(e)returnlog.fail(e);
180186
showProblem(problem,argv);
181187
});
@@ -194,7 +200,7 @@ cmd.handler = function(argv) {
194200
if(problems.length===0)returnlog.fail('Problem not found!');
195201

196202
constproblem=_.sample(problems);
197-
core.getProblem(problem,function(e,problem){
203+
core.getProblem(problem,!argv.dontTranslate,function(e,problem){
198204
if(e)returnlog.fail(e);
199205
showProblem(problem,argv);
200206
});

‎lib/commands/star.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const cmd = {
2929

3030
cmd.handler=function(argv){
3131
session.argv=argv;
32-
core.getProblem(argv.keyword,function(e,problem){
32+
// translation doesn't affect question lookup
33+
core.getProblem(argv.keyword,true,function(e,problem){
3334
if(e)returnlog.fail(e);
3435

3536
core.starProblem(problem,!argv.delete,function(e,starred){

‎lib/commands/submission.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ const cmd = {
4242
default:false,
4343
describe:'Show extra question details in submission code'
4444
})
45+
.option('T',{
46+
alias:'dontTranslate',
47+
type:'boolean',
48+
default:false,
49+
describe:'Set to true to disable endpoint\'s translation',
50+
})
4551
.positional('keyword',{
4652
type:'string',
4753
default:'',
@@ -69,7 +75,7 @@ function doTask(problem, queue, cb) {
6975

7076
if(argv.extra){
7177
// have to get problem details, e.g. problem description.
72-
core.getProblem(problem.fid,function(e,problem){
78+
core.getProblem(problem.fid,!argv.dontTranslate,function(e,problem){
7379
if(e)returncb(e);
7480
exportSubmission(problem,argv,onTaskDone);
7581
});
@@ -135,7 +141,7 @@ cmd.handler = function(argv) {
135141
if(!argv.keyword)
136142
returnlog.fail('missing keyword?');
137143

138-
core.getProblem(argv.keyword,function(e,problem){
144+
core.getProblem(argv.keyword,!argv.dontTranslate,function(e,problem){
139145
if(e)returnlog.fail(e);
140146
q.addTask(problem).run();
141147
});

‎lib/commands/submit.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ cmd.handler = function(argv) {
4949

5050
constmeta=file.meta(argv.filename);
5151

52-
core.getProblem(meta.id,function(e,problem){
52+
// translation doesn't affect problem lookup
53+
core.getProblem(meta.id,true,function(e,problem){
5354
if(e)returnlog.fail(e);
5455

5556
problem.file=argv.filename;

‎lib/commands/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function runTest(argv) {
6060

6161
constmeta=file.meta(argv.filename);
6262

63-
core.getProblem(meta.id,function(e,problem){
63+
core.getProblem(meta.id,true,function(e,problem){
6464
if(e)returnlog.fail(e);
6565

6666
if(!problem.testable)

‎lib/core.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const QUERY_HANDLERS = {
6161
};
6262

6363
core.filterProblems=function(opts,cb){
64-
this.getProblems(function(e,problems){
64+
this.getProblems(!opts.dontTranslate,function(e,problems){
6565
if(e)returncb(e);
6666

6767
for(letqof(opts.query||'').split('')){
@@ -82,11 +82,11 @@ core.filterProblems = function(opts, cb) {
8282
});
8383
};
8484

85-
core.getProblem=function(keyword,cb){
85+
core.getProblem=function(keyword,needTranslation,cb){
8686
if(keyword.id)
87-
returncore.next.getProblem(keyword,cb);
87+
returncore.next.getProblem(keyword,needTranslation,cb);
8888

89-
this.getProblems(function(e,problems){
89+
this.getProblems(needTranslation,function(e,problems){
9090
if(e)returncb(e);
9191

9292
keyword=Number(keyword)||keyword;
@@ -95,7 +95,7 @@ core.getProblem = function(keyword, cb) {
9595
returnx.fid+''===keyword+''||x.fid+''===metaFid+''||x.name===keyword||x.slug===keyword;
9696
});
9797
if(!problem)returncb('Problem not found!');
98-
core.next.getProblem(problem,cb);
98+
core.next.getProblem(problem,needTranslation,cb);
9999
});
100100
};
101101

‎lib/helper.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ const LANGS = [
5252
consth={};
5353

5454
h.KEYS={
55-
user:'../user',
56-
stat:'../stat',
57-
plugins:'../../plugins',
58-
problems:'problems',
59-
problem:p=>p.fid+'.'+p.slug+'.'+p.category
55+
user:'../user',
56+
stat:'../stat',
57+
plugins:'../../plugins',
58+
problems:'problems',
59+
translation:'translationConfig',
60+
problem:p=>p.fid+'.'+p.slug+'.'+p.category
6061
};
6162

6263
h.prettyState=function(state){

‎lib/plugins/cache.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,37 @@ var session = require('../session');
99

1010
constplugin=newPlugin(50,'cache','','Plugin to provide local cache.');
1111

12-
plugin.getProblems=function(cb){
12+
// this function will clear all caches if needTranslation is different than stored
13+
// it will also store the new needTranslation into cache automatically
14+
functionclearCacheIfTchanged(needTranslation){
15+
consttranslationConfig=cache.get(h.KEYS.translation);
16+
if(!translationConfig||translationConfig['useEndpointTranslation']!=needTranslation){
17+
// cache doesn't have the key => old cache version, need to update
18+
// or cache does have the key but it contains a different value
19+
cache.deleteAll();
20+
cache.set(h.KEYS.translation,{useEndpointTranslation:needTranslation});
21+
log.debug('cache cleared: -T option changed');
22+
}
23+
}
24+
25+
plugin.getProblems=function(needTranslation,cb){
26+
clearCacheIfTchanged(needTranslation);
1327
constproblems=cache.get(h.KEYS.problems);
1428
if(problems){
1529
log.debug('cache hit: problems.json');
1630
returncb(null,problems);
1731
}
1832

19-
plugin.next.getProblems(function(e,problems){
33+
plugin.next.getProblems(needTranslation,function(e,problems){
2034
if(e)returncb(e);
2135

2236
cache.set(h.KEYS.problems,problems);
2337
returncb(null,problems);
2438
});
2539
};
2640

27-
plugin.getProblem=function(problem,cb){
41+
plugin.getProblem=function(problem,needTranslation,cb){
42+
clearCacheIfTchanged(needTranslation);
2843
constk=h.KEYS.problem(problem);
2944
const_problem=cache.get(k);
3045
if(_problem){
@@ -42,7 +57,7 @@ plugin.getProblem = function(problem, cb) {
4257
}
4358
}
4459

45-
plugin.next.getProblem(problem,function(e,_problem){
60+
plugin.next.getProblem(problem,needTranslation,function(e,_problem){
4661
if(e)returncb(e);
4762

4863
plugin.saveProblem(_problem);

‎lib/plugins/company.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,8 +1511,8 @@ var TAGS = {
15111511
'1148':['math']
15121512
};
15131513

1514-
plugin.getProblems=function(cb){
1515-
plugin.next.getProblems(function(e,problems){
1514+
plugin.getProblems=function(needTranslation,cb){
1515+
plugin.next.getProblems(needTranslation,function(e,problems){
15161516
if(e)returncb(e);
15171517

15181518
problems.forEach(function(problem){

‎lib/plugins/leetcode.cn.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,29 @@ function checkError(e, resp, expectedStatus) {
7070
returne;
7171
}
7272

73-
plugin.getProblems=function(cb){
74-
plugin.next.getProblems(function(e,problems){
73+
// overloading getProblems here to make sure everything related
74+
// to listing out problems can have a chance to be translated.
75+
// NOTE: Details of the problem is translated inside leetcode.js
76+
plugin.getProblems=function(needTranslation,cb){
77+
plugin.next.getProblems(needTranslation,function(e,problems){
7578
if(e)returncb(e);
7679

77-
plugin.getProblemsTitle(function(e,titles){
78-
if(e)returncb(e);
80+
if(needTranslation){
81+
// only translate titles of the list if user requested
82+
plugin.getProblemsTitle(function(e,titles){
83+
if(e)returncb(e);
7984

80-
problems.forEach(function(problem){
81-
consttitle=titles[problem.id];
82-
if(title)
83-
problem.name=title;
84-
});
85+
problems.forEach(function(problem){
86+
consttitle=titles[problem.id];
87+
if(title)
88+
problem.name=title;
89+
});
8590

91+
returncb(null,problems);
92+
});
93+
}else{
8694
returncb(null,problems);
87-
});
95+
}
8896
});
8997
};
9098

‎lib/plugins/leetcode.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ plugin.init = function() {
5454
config.app='leetcode';
5555
};
5656

57-
plugin.getProblems=function(cb){
57+
plugin.getProblems=function(needTranslation,cb){
5858
log.debug('running leetcode.getProblems');
5959
letproblems=[];
6060
constgetCategory=function(category,queue,cb){
@@ -117,7 +117,7 @@ plugin.getCategoryProblems = function(category, cb) {
117117
});
118118
};
119119

120-
plugin.getProblem=function(problem,cb){
120+
plugin.getProblem=function(problem,needTranslation,cb){
121121
log.debug('running leetcode.getProblem');
122122
constuser=session.getUser();
123123
if(problem.locked&&!user.paid)returncb('failed to load locked problem!');
@@ -161,7 +161,7 @@ plugin.getProblem = function(problem, cb) {
161161
problem.likes=q.likes;
162162
problem.dislikes=q.dislikes;
163163

164-
problem.desc=q.translatedContent ?q.translatedContent :q.content;
164+
problem.desc=(q.translatedContent&&needTranslation) ?q.translatedContent :q.content;
165165

166166
problem.templates=JSON.parse(q.codeDefinition);
167167
problem.testcase=q.sampleTestCase;

‎lib/plugins/solution.discuss.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ function getSolution(problem, lang, cb) {
7171
});
7272
}
7373

74-
plugin.getProblem=function(problem,cb){
75-
plugin.next.getProblem(problem,function(e,problem){
74+
plugin.getProblem=function(problem,needTranslation,cb){
75+
plugin.next.getProblem(problem,needTranslation,function(e,problem){
7676
if(e||!session.argv.solution)returncb(e,problem);
7777

7878
varlang=session.argv.lang;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp