-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathflickrTagUtil.js
105 lines (90 loc) · 2.2 KB
/
flickrTagUtil.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* global hexo */
'use strict';
var util = require('util');
var hexoUtil = require('hexo-util');
var rPhotoId = /\d{5,}/;
var rPhotoSize = /^[sqtmnzcbo-]$/;
var IMG_URL_PATTERN = 'https://farm%s.staticflickr.com/%s/%s_%s%s.%s';
var PHOTO_SIZE = {
's': { width: 75, height: 75 },
'q': { width: 150, height: 150 },
't': { width: 100 },
'm': { width: 240 },
'n': { width: 320 },
'-': { width: 500 },
'z': { width: 640 },
'c': { width: 800 },
'b': { width: 1024 },
'o': {}
};
var flickrTagUtil = {
convertAttr: function(args) {
var attrs = {
classes: [],
id: '',
size: '-',
isWithLink: false
};
var i = 0;
for (i = 0; i < args.length; i++) {
var item = args[i];
if (rPhotoId.test(item)) {
attrs.id = item;
break;
} else {
attrs.classes.push(item);
}
}
args = args.slice(i + 1);
if (args.length) {
if (rPhotoSize.test(args[0])) {
attrs.size = args.shift();
}
// TODO: with link
}
return attrs;
},
imgFormat: function(tag, jsonData) {
var secret = '';
var format = '';
var size,
photoSize;
var imgAttr = {};
switch (tag.size) {
case 'o':
if (typeof jsonData.photo.originalsecret !== 'undefined') {
secret = jsonData.photo.originalsecret;
format = jsonData.photo.originalformat;
} else {
hexo.log.error('Can not access the Flickr id ' + tag.id + ' original size');
}
size = '_' + tag.size;
break;
case '-':
secret = jsonData.photo.secret;
format = 'jpg';
size = '';
break;
default:
secret = jsonData.photo.secret;
format = 'jpg';
size = '_' + tag.size;
}
imgAttr.src = util.format(IMG_URL_PATTERN,
jsonData.photo.farm,
jsonData.photo.server,
jsonData.photo.id,
secret,
size,
format
);
photoSize = PHOTO_SIZE[tag.size];
for (var key in photoSize) {
imgAttr[key] = photoSize[key];
}
imgAttr.class = tag.classes.join(' ');
imgAttr.alt = hexoUtil.escapeHTML(jsonData.photo.title._content);
return imgAttr;
}
};
module.exports = flickrTagUtil;