Template:MDN:Common

  • Revision slug: Template:mdn:common
  • Revision title: Template:MDN:Common
  • Revision id: 291415
  • Created:
  • Creator: trevorh
  • Is current revision? No
  • Comment

Revision Content

<% 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; var exists = false; var text = ''; var path_clean = wiki.kumaPath(in_path); var path_less_locale = path_clean.replace(/^\/?\w{2}(?:-\w{2})?/i,''); var path_English = '/en-US' + path_less_locale; // Figure out locale to use (strip any / from in_locale) var locale = env.locale; if (in_locale) { locale = kuma.htmlEscape(in_locale.replace(/\//g,'')); } var path_plus_locale = '/' + locale + path_less_locale; // Get the locale page var pageLocale = wiki.getPage(path_plus_locale); if (pageLocale.title) { exists = true; } // 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 = path_plus_locale; // If the locale version exists, get its title if (pageLocale.title) { text = pageLocale.title; } else { // Try the original path passed, maybe the locale switch did not work if (path_clean != path_plus_locale) { var pageOriginal = wiki.getPage (path_clean); if (pageOriginal.title) { text = pageOriginal.title; } } // Try the English version else if (locale.toLowerCase() != 'en-us') { var pageEnglish = wiki.getPage(path_English); if (pageEnglish.title) { text = pageEnglish.title; } } } } out = ''; } return out; }, /** * Accepts a relative URL or an attachment object * Returns the content of a given file. */ getFileContent: function(fileObjOrUrl) { var url = fileObjOrUrl.url || fileObjOrUrl; if(!url) return ''; var result = '', p = kuma.url.parse(env.url, true), base_url = p.protocol + '//' + p.host; url = base_url + url; return cacheFn('kuma:get_attachment_content:' + md5(url.toLowerCase()), 3600, function(next) { try { request({ method: 'GET', headers: { 'Cache-Control': env.cache_control }, url: url }, function(err, resp, body) { if(resp && 200 == resp.statusCode) { next('[' + body + ']'); } else { next("callback bad :("); } }); } catch(e) { next("error: " + e); } }); } } %>

Revision Source

<% 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;
  
            var exists = false;
            var text = '';
 
            var path_clean = wiki.kumaPath(in_path);
            var path_less_locale = path_clean.replace(/^\/?\w{2}(?:-\w{2})?/i,'');
            var path_English = '/en-US' + path_less_locale;
             
            // Figure out locale to use (strip any / from in_locale)
            var locale = env.locale;
            if (in_locale) {
                locale = kuma.htmlEscape(in_locale.replace(/\//g,''));
            }
            var path_plus_locale = '/' + locale + path_less_locale;
 
            // Get the locale page
            var pageLocale = wiki.getPage(path_plus_locale);
            if (pageLocale.title) {
                exists = true;
            }
 
            // 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 = path_plus_locale;
                // If the locale version exists, get its title
                if (pageLocale.title) {
                    text = pageLocale.title;
                }
                else {
                    // Try the original path passed, maybe the locale switch did not work
                    if (path_clean != path_plus_locale) {
                        var pageOriginal = wiki.getPage (path_clean);
                        if (pageOriginal.title) {
                            text = pageOriginal.title;
                        }
                    }
                    // Try the English version
                    else if (locale.toLowerCase() != 'en-us') {
                        var pageEnglish = wiki.getPage(path_English);
                        if (pageEnglish.title)  {
                            text = pageEnglish.title;
                        }
                    }
                }
            }
            out = '<a href="' + base_url + path_plus_locale + '"';
             // Link title
            if (in_title) {
                out += ' title="' + kuma.htmlEscape(in_title) + '"';
            }
            if (!exists) {
                out += ' class="new"';
            }
            out += '>' + text + '</a>';
        }
        return out;
    },
    /**
     * Accepts a relative URL or an attachment object
     * Returns the content of a given file.
     */
    getFileContent: function(fileObjOrUrl) {
        var url = fileObjOrUrl.url || fileObjOrUrl;
        if(!url) return '';
        
        var result = '',
            p = kuma.url.parse(env.url, true),
            base_url = p.protocol + '//' + p.host;
            
        url = base_url + url;
        return cacheFn('kuma:get_attachment_content:' + md5(url.toLowerCase()), 3600, function(next) {
            try {
                request({
                    method: 'GET',
                    headers: { 'Cache-Control': env.cache_control },
                    url: url
                }, function(err, resp, body) {
                    if(resp && 200 == resp.statusCode) {
                        next('[' + body + ']');
                    }
                    else {
                        next("callback bad :(");
                    }
                });
            } catch(e) {
                next("error: " + e);
            }
        });
    }

} %>
Revert to this revision