-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
58 lines (42 loc) · 1.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var fs = require('fs'),
pathFn = require('path'),
ejs = require('ejs'),
requestCard = require('mtg-card-sync');
var cardTmplThemeSrc = pathFn.join(hexo.theme_dir, 'plugins/mtg-card-jp/card.ejs');
var cardTmplOriginalSrc = pathFn.join(__dirname, './card.ejs');
var cardTmplSrc = (fs.existsSync(cardTmplThemeSrc)? cardTmplThemeSrc: cardTmplOriginalSrc);
var cardTmpl = ejs.compile(
fs.readFileSync(cardTmplSrc, 'utf8'),
{});
var cardNameRegex = /《([^/(》]*)(\/[^(》]*)?(?:\(([^)]+)\))?》/g
hexo.extend.filter.register('before_post_render', function(data) {
data.content = data.content.replace(
cardNameRegex,
function(content, cardNameA, cardNameB, cardSet) {
var enCardName = (cardNameB? cardNameB.substring(1): cardNameA);
if (!enCardName) {
return content;
}
var card = requestCard(enCardName, cardSet) || {};
return cardTmpl({card: card, content: content}).replace(/(?:\r|\n)/g, '');
});
});
hexo.extend.tag.register('mtg_card', function(args) {
var source = args.join(' ');
var enCardName, cardSet;
var result;
source.replace(cardNameRegex, function(content, cardNameA, cardNameB, cardSet) {
var enCardName = (cardNameB? cardNameB.substring(1): cardNameA);
if (!enCardName || !cardSet) {
return '';
}
var card = requestCard(enCardName, cardSet);
if (!card || !card.multiverseid) {
return '';
}
var imageUrl = 'http://gatherer.wizards.com/Handlers/Image.ashx?multiverseid=' + card.multiverseid + '&type=card';
result = '<img class="mtg_card" src="' + imageUrl + '"/>';
return '';
});
return result;
})