UNPKG

1.68 kBJavaScriptView Raw
1'use strict';
2
3var fs = require('fs');
4var path = require('path');
5var utils = require('./utils');
6var obj = require('./object');
7
8/**
9 * Embed code from an external file as preformatted text.
10 *
11 * ```js
12 * <%= embed('path/to/file.js') %>
13 *
14 * // specify the language to use
15 * <%= embed('path/to/file.hbs', 'html') %>
16 * ```
17 *
18 * @param {String} `fp` filepath to the file to embed.
19 * @param {String} `language` Optionally specify the language to use for syntax highlighting.
20 * @return {String}
21 * @api public
22 */
23
24exports.embed = function embed(fp, ext) {
25 ext = typeof ext !== 'string' ? path.extname(fp).slice(1) : ext;
26 var code = fs.readFileSync(fp, 'utf8');
27
28 // if the string is markdown, escape backticks
29 if (ext === 'markdown' || ext === 'md') {
30 code = code.split('`').join('&#x60');
31 }
32
33 return utils.block(code, ext) + '\n';
34};
35
36/**
37 * Generate the HTML for a jsFiddle link with the given `params`
38 *
39 * ```js
40 * <%= jsfiddle({id: '0dfk10ks', {tabs: true}}) %>
41 * ```
42 *
43 * @param {Object} `params`
44 * @return {String}
45 * @api public
46 */
47
48exports.jsfiddle = function jsFiddle(attr) {
49 if (!attr || !obj.isPlainObject(attr)) return '';
50 attr.id = 'http://jsfiddle.net/' + (attr.id || '');
51 attr.width = attr.width || '100%';
52 attr.height = attr.height || '300';
53 attr.skin = attr.skin || '/presentation/';
54 attr.tabs = (attr.tabs || 'result,js,html,css') + attr.skin;
55 attr.src = attr.id + '/embedded/' + attr.tabs;
56 attr.allowfullscreen = attr.allowfullscreen || 'allowfullscreen';
57 attr.frameborder = attr.frameborder || '0';
58
59 attr = obj.omit(attr, ['id', 'tabs', 'skin']);
60 return '<iframe ' + utils.toAttributes(attr) + '></iframe>';
61};