let PLUGIN_INFO =
{NAME}
Show/Open related blog
現在ページへリンクしているブログを表示または開くプラグイン
teramako
1.0
MPL 1.1/GPL 2.0/LGPL 2.1
2.0pre
2.0
http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/relatedBlogSearch.js
||
tab relatedblog {URL}
||<
とすることで新規タブに開けます。
また、リストを表示した後、";o" などのヒントからも開けるでしょう。(historyコマンドに似ている)
== 変数 ==
let コマンドで設定してください(しない場合はデフォルト値が使用されます)
:g:blogSearchHeaderTemplate:
リストを表示する時のヘッダとなるXML文字列
省略時は以下が使用されます。
>||
'';
||<
:g:blogSearchBodyTemplate:
リストを表示する時のコンテンツとなるXML文字列
省略時は以下が使用されます。
>||
'%title% | %author% | %content% |
';
||<
また、%で囲まれた特定の文字列が変数として使用されます。
%link%:
対象ページのURL
%title%:
対象ページのタイトル
%author%:
対象ページの著者
%content%:
対象ページの内容(一部)
%rootURI%:
対象ページのトップページのURL
%id%:
不明(ドメイン、日付、パスが含まれる)
%published%:
投稿された日時(%Y-%m-%dT%H:%M:%SZ)
%updated%:
更新された日時(%Y-%m-%dT%H:%M:%SZ)
]]>
;
liberator.plugins.relatedBlogSearch = (function(){
const LANG = window.navigator.language;
const BLOGSEARCH_URL = 'http://blogsearch.google.com/blogsearch_feeds?output=atom&hl=' + LANG + '&q=';
/**
* Serach with Google blogsearch and get entries
* @param {String} name query string
*/
function BlogSearch() { this._init.apply(this, arguments); }
BlogSearch.prototype = {
_init: function(name){
if (!name)
throw Components.results.NS_ERROR_XPC_NOT_ENOUGH_ARGS;
this.date = new Date();
this.name = name;
this.query = BLOGSEARCH_URL + this.name;
let self = this;
this.items = Array.map(
util.httpGet(this.query).responseXML.getElementsByTagName("entry"),
function(entry) self._parse(entry)
);
},
/**
* @param {Element} entryElm Atom entry element
* @return {Object}
*/
_parse: function(entryElm){
let entry = {};
Array.forEach(entryElm.childNodes, function(elm){
let tagName = elm.localName;
switch(tagName){
case 'link':
entry.link = elm.getAttribute('href'); break;
case 'author':
entry.author = elm.childNodes[0].textContent;
entry.rootURI = elm.childNodes[1].textContent;
break;
default:
entry[tagName] = elm.textContent;
}
});
return entry;
},
/**
* @param {RegExp} reg
* @param {String[]} itemNames item's property names
* @return {Object[]} item list
*/
search: function(reg, itemNames){
if (!itemNames) itemNames = [name for (name in this.items[0])];
return this.items.filter(function(item) itemNames.some(function(name) reg.test(item[name])));
},
/**
* @param {String} template
* @param {XMLList} xml header XML
* @param {Object[]} items if omitted, used all items
* template e.g.)
* '
%title% | %author% | %content% |
'
*/
toXMLByTemplate: function(template, xml, items){
if (!items) items = this.items;
function entryToXML(item){
function replacer(all, name) name in item ? item[name] : all;
return new XMLList(template.replace(/%(\w+?)%/g, replacer).replace(/&/g,"&"));
}
items.forEach(function(item){xml.* += entryToXML(item);});
return xml;
},
};
/**
* -query option completion list
* e.g.)
* return [
* ["link:https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects", "-"],
* ["link:https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference", "-"],
* ["link:https://developer.mozilla.org/en", "-"],
* ["link:https://developer.mozilla.org", "-"],
* ]
* when current url is "https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects"
* @return {String[][]}
*/
function getQueryList(){
return buffer.URL.split(/\/(?!\/)/).reduce(function(p,c){
p.length == 0 ? p.push(c) : p.push(p[p.length-1]+"/"+c);
return p;
},[]).splice(1).map(function(url) ["link:" + url, "-"]).reverse();
}
// ----------------------------------------------
// command
// ----------------------------------------------
commands.addUserCommand(["relatedblog"], "search linked blog from google blogsearch",
function(args){
let query = args["-query"] ? args["-query"] : "link:" + buffer.URL;
let entry = manager.getCache(query);
let header = new XMLList(manager.headerTemplate);
if (args.length == 0){
let xml = entry.toXMLByTemplate(manager.bodyTemplate, header);
liberator.echo(xml, true);
} else {
let reg = new RegExp(args[0], "i");
let items = entry.search(reg);
if (items.length > 1){
liberator.echo(entry.toXMLByTemplate(manager.bodyTemplate, header, items),true);
} else if (items.length == 1){
let url = items[0].link;
liberator.open(url);
} else {
liberator.echomsg("related blog is none",5);
}
}
},{
options: [
[["-query", "-q"], commands.OPTION_STRING, null, getQueryList]
],
argCount: "?",
completer: function(context, args){
manager.completer(context, args);
},
},true);
// ----------------------------------------------
// public
// ----------------------------------------------
var manager = {
get bodyTemplate(){
return liberator.globalVariables.blogSearchBodyTemplate ?
liberator.globalVariables.blogSearchBodyTemplate :
'%title% | %author% | %content% |
';
},
get headerTemplate(){
return liberator.globalVariables.blogSearchHeaderTemplate ?
liberator.globalVariables.blogSearchHeaderTemplate :
'';
},
/**
* @type {BlogSerach[]}
*/
cache: [],
/**
* キャッシュからエントリを取得
* キャッシュがないまたは期限切れの場合は新たに取得
* キャッシュのリストは10個まで XXX: 外だししたほうが良いかもしれない
* XXX: 有効期限を変数に持たせた方が良いかもしれない
* @param {String} name query string
* @return {BlogSearch}
*/
getCache: function(name){
let cache = this.cache.filter(function(entry) entry.name == name);
if (cache.length == 0){
let entry = new BlogSearch(name);
if(this.cache.push(entry) > 10)
this.cache.shift();
return entry;
} else if (cache[0].date < Date.now() - 5*60*1000){
cache[0]._init(name);
}
return cache[0];
},
/**
* relatedblog コマンド用の補完関数
* @param {CompletionContext} context
* @param {String[]} args
*/
completer: function(context, args){
let query = args["-query"] ? args["-query"] : "link:" + buffer.URL;
let entry = this.getCache(query)
context.title = ["URL", "Title"];
if (args.length == 0){
context.completions = entry.items.map(function(item) [item.link, item.title]);
} else {
let reg = new RegExp(context.filter, "i");
context.completions = entry.search(reg).map(function(item) [item.link, item.title]);
}
},
};
return manager;
})();
// vim: sw=2 ts=2 et:
f='#n155'>155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|