new flat style: cleanup.

- The information stored in `makeImage`'s `options` parameter is better served
  in its `data` parameter,
- Remove the (no longer used) `colorscheme` parameter to `makeTemplate`,
- We can already get to the contents of the URL query parameter `style` through
  that `data` parameter in `getRequest`,
- Relocation of the code to set badge data related to the flat style into
  `getBadgeData`,
- Code style (adding semicolons).

Related to issue #159.
This commit is contained in:
Thaddee Tyl 2014-04-19 22:25:35 +00:00
parent 519aa5e6cd
commit d578b65513
4 changed files with 26 additions and 39 deletions

View File

@ -15,20 +15,20 @@ try {
} catch(e) {}
canvasContext.font = '11px Verdana, "DejaVu Sans"';
// cache templates
var templates = {}
var templateFiles = fs.readdirSync('templates')
// cache templates.
var templates = {};
var templateFiles = fs.readdirSync('templates');
templateFiles.forEach(function(file) {
templates[file] = fs.readFileSync(path.join('templates', file)).toString()
})
templates[file] = fs.readFileSync(
path.join(__dirname, 'templates', file)).toString();
});
var colorscheme = require('./colorscheme.json');
var colorscheme = require(path.join(__dirname, 'colorscheme.json'));
function makeTemplate(colorscheme, template) {
function makeTemplate(template) {
// Template crafting action below.
var template = templates[(template || 'default') + '-template.svg'];
var imageTemplate = dot.template(''+template);
return imageTemplate;
return dot.template(template);
}
var defaultTemplate = makeTemplate();
@ -38,14 +38,10 @@ function optimize(string, callback) {
svgo.optimize(string, callback);
}
function makeImage(data, options, cb) {
if (typeof options === 'function') {
cb = options
options = {}
}
function makeImage(data, cb) {
var template = defaultTemplate;
if (options.colorscheme || options.template) {
template = makeTemplate(options.colorscheme, options.template);
if (data.template) {
template = makeTemplate(data.template);
}
if (data.colorscheme) {
data.colorA = colorscheme[data.colorscheme].colorA;

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="109" height="18"><linearGradient id="a" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-opacity=".3"/><stop offset="1" stop-opacity=".5"/></linearGradient><rect rx="4" width="109" height="18" fill="#555"/><rect rx="4" x="63" width="46" height="18" fill="#a4a61d"/><path fill="#a4a61d" d="M63 0h4v18h-4z"/><rect rx="4" width="109" height="18" fill="url(#a)"/><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="32.5" y="13" fill="#010101" fill-opacity=".3">coverage</text><text x="32.5" y="12">coverage</text><text x="85" y="13" fill="#010101" fill-opacity=".3">80.8%</text><text x="85" y="12">80.8%</text></g></svg>
<svg xmlns="http://www.w3.org/2000/svg" width="110" height="18"><linearGradient id="a" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-opacity=".3"/><stop offset="1" stop-opacity=".5"/></linearGradient><rect rx="4" width="110" height="18" fill="#555"/><rect rx="4" x="63" width="47" height="18" fill="#a4a61d"/><path fill="#a4a61d" d="M63 0h4v18h-4z"/><rect rx="4" width="110" height="18" fill="url(#a)"/><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="32.5" y="13" fill="#010101" fill-opacity=".3">coverage</text><text x="32.5" y="12">coverage</text><text x="85.5" y="13" fill="#010101" fill-opacity=".3">82.3%</text><text x="85.5" y="12">82.3%</text></g></svg>

Before

Width:  |  Height:  |  Size: 823 B

After

Width:  |  Height:  |  Size: 827 B

View File

@ -42,13 +42,10 @@ var status = process.argv[3];
var color = process.argv[4] || ':green';
var colorA = process.argv[5];
var badgeOpts = {}
if (style) {
badgeOpts.template = style;
}
var badgeData = {text: [subject, status]};
if (style) {
badgeData.template = style;
}
if (color[0] === ':') {
color = color.slice(1);
@ -63,7 +60,7 @@ if (color[0] === ':') {
if (colorA) { badgeData.colorA = colorA; }
}
badge(badgeData, badgeOpts, function produceOutput(svg) {
badge(badgeData, function produceOutput(svg) {
if (format === 'svg') {
console.log(svg);
} else if (/png|jpg|gif/.test(format)) {

View File

@ -5,7 +5,6 @@ var camp = require('camp').start({
var https = require('https');
var request = require('request');
var fs = require('fs');
var url = require('url');
var badge = require('./badge.js');
var svg2img = require('./svg-to-img.js');
var serverStartTime = new Date((new Date()).toGMTString());
@ -99,25 +98,15 @@ var cacheFromIndex = Object.create(null);
function cache(f) {
return function getRequest(data, match, end, ask) {
// parse querystring for specifying template
var reqURL = url.parse(ask.req.url, true);
var badgeOpts = {
template: 'default'
};
var style = reqURL.query.style || 'default';
if (style && validTemplates.indexOf(style) > -1) {
badgeOpts.template = style;
};
// Cache management - no cache, so it won't be cached by GitHub's CDN.
ask.res.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
incrMonthlyAnalytics(analytics.vendorMonthly);
var cacheIndex = match[0] + '?label=' + data.label;
var cacheIndex = match[0] + '?label=' + data.label + '&style=' + data.style;
// Should we return the data right away?
var cached = cacheFromIndex[cacheIndex];
if (cached != null) {
badge(cached.badgeData, badgeOpts, makeSend(cached.format, ask.res, end));
badge(cached.badgeData, makeSend(cached.format, ask.res, end));
return;
}
@ -127,7 +116,7 @@ function cache(f) {
var badgeData = getBadgeData('vendor', data);
badgeData.text[1] = 'unresponsive';
serverUnresponsive = true;
badge(badgeData, badgeOpts, makeSend('svg', ask.res, end));
badge(badgeData, makeSend('svg', ask.res, end));
}, 25000);
f(data, match, function sendBadge(format, badgeData) {
@ -952,7 +941,12 @@ function getLabel(label, data) {
function getBadgeData(defaultLabel, data) {
var label = getLabel(defaultLabel, data);
return {text:[label, 'n/a'], colorscheme:'lightgrey'};
var template = data.style || 'default';
if (data.style && validTemplates.indexOf(data.style) > -1) {
template = data.style;
};
return {text:[label, 'n/a'], colorscheme:'lightgrey', template:template};
}
function makeSend(format, askres, end) {