Skip to content

Instantly share code, notes, and snippets.

@darkwing
Created July 19, 2012 14:47
Show Gist options
  • Save darkwing/3144448 to your computer and use it in GitHub Desktop.
Save darkwing/3144448 to your computer and use it in GitHub Desktop.
// Document
{{ GetAttachmentContent() }}
// Intermediate template
<%- mdn.getFileContent(env.files[0]) %>
// Complete template:mdn:common
<% module.exports = {
/**
* Given a set of strings like this:
* { "en-US": "Foo", "de": "Bar", "es": "Baz" }
* Return the one which matches the current locale.
*/
localString: function (strings) {
var lang = env.locale;
if (!(lang in strings)) lang = 'en-US';
return strings[lang];
},
/**
* DEVELOPMENTAL - DO NOT USE THIS IN PRODUCTION
* Creates a localized internal link
* path - path of the document to create link to. (Will handle old style links if necessary)
* text (optional) - text of the link, defaults to page title
* title (optional) - link title
* locale (optional) - locale to link to. Only use if the link MUST be to another locale
*/
linkInternal: function (in_path, in_text, in_title, in_locale) {
var wiki = require("DekiScript:Wiki");
var out = '(Missing path)';
// No use doing anything if there is no path
if (in_path) {
var p = kuma.url.parse(env.url, true);
var base_url = p.protocol + '//' + p.host;
// Fix path - remove any locale, add /docs, replace space with underscore and do a htmlEscape
var path = '/docs/' + kuma.htmlEscape(in_path.replace(/^\/?(?:\w{2}(?:-\w{2})?)?\/(?:docs\/)?/i,'').replace(/ /g,'_').replace(/%20/g,'_'));
// Figure out locale
var locale = env.locale;
if (in_locale) {
locale = kuma.htmlEscape(in_locale.replace(/\//g,''));
}
out = '<a href="' + base_url + '/' + locale + path + '"';
// Link title
if (in_title) {
out += ' title="' + kuma.htmlEscape(in_title) + '"';
}
// Get text of link (title of page), if no override
var text = '';
if (in_text) {
text = kuma.htmlEscape(in_text);
}
else {
// Use path as fallback
text = '/' + locale + path;
// If the locale version exists, get its title
var pageLocale = wiki.getPage ('/' + locale + path);
if (pageLocale.title) {
text = pageLocale.title;
}
else if (locale.toLowerCase() != 'en-us') {
// Try the English version
var pageEnglish = wiki.getPage('/en-US' + path);
if (pageEnglish.title) {
text = pageEnglish.title;
}
}
}
out += '>' + text + '</a>';
}
return out;
},
/* returns file content */
getFileContent: function(fileObjOrUrl) {
// Ensure we have something to work with
var url = fileObjOrUrl.url || fileObjOrUrl;
if(!fileObjOrUrl) return '';
// Try to load it
var key = 'kuma:get_attachment_content:' + md5(url.toLowerCase());
return cacheFn(key, 3600, function() {
var result = '';
try {
request({
method: 'GET',
headers: { 'Cache-Control': env.cache_control },
url: url
}, function(err, resp, body) {
if(resp && 200 == resp.statusCode) {
result = body;
}
});
} catch(e) {}
return result;
});
}
} %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment