'use strict';

var path = require('path');
var concat = require('helper-concat');
var block = require('to-gfm-code-block');
var utils = require('./utils');
var obj = require('./object');

/**
 * Embed code from an external file as preformatted text.
 *
 * ```js
 * <%= embed('path/to/file.js') %>
 *
 * // specify the language to use
 * <%= embed('path/to/file.hbs', 'html') %>
 * ```
 *
 * @param {String} `fp` filepath to the file to embed.
 * @param {String} `language` Optionally specify the language to use for syntax highlighting.
 * @return {String}
 * @api public
 */

exports.embed = function embed(fp, ext) {
  ext = typeof ext !== 'string'
    ? path.extname(fp).slice(1)
    : ext;

  var code = concat.sync(fp);

  if (ext === 'markdown' || ext === 'md') {
    // if the string is markdown, escape backticks
    code = code.split('`').join('&#x60');
  }
  return block(code, ext) + '\n';
};

/**
 * Generate the HTML for a jsFiddle link with the given `params`
 *
 * ```js
 * <%= jsfiddle({id: '0dfk10ks', {tabs: true}}) %>
 * ```
 *
 * @param {Object} `params`
 * @return {String}
 * @api public
 */

exports.jsfiddle = function jsFiddle(attr) {
  if (!attr || !obj.isPlainObject(attr)) return '';
  attr.id = 'http://jsfiddle.net/' + (attr.id || '');
  attr.width = attr.width || '100%';
  attr.height = attr.height || '300';
  attr.skin = attr.skin || '/presentation/';
  attr.tabs = (attr.tabs || 'result,js,html,css') + attr.skin;
  attr.src = attr.id + '/embedded/' + attr.tabs;
  attr.allowfullscreen = attr.allowfullscreen || 'allowfullscreen';
  attr.frameborder = attr.frameborder || '0';

  attr = obj.omit(attr, ['id', 'tabs', 'skin']);
  return '<iframe ' + utils.toAttributes(attr) + '></iframe>';
};

@@ -12,20 +12,18 @@
 ';%0A%0Avar 
-path
+fs
  = requi
@@ -30,20 +30,18 @@
 re('
-path
+fs
 ');%0Avar 
 conc
@@ -36,22 +36,20 @@
 ');%0Avar 
-conc
+p
 at
+h
  = requi
@@ -56,21 +56,12 @@
 re('
-helper-conc
+p
 at
+h
 ');%0A
@@ -690,72 +690,38 @@
 e = 
-concat.sync(fp);%0A%0A  if (ext === 'markdown' %7C%7C ext === 'md') %7B%0A  
+fs.readFileSync(fp, 'utf8');%0A%0A
   //
@@ -765,16 +765,60 @@
 ckticks%0A
+  if (ext === 'markdown' %7C%7C ext === 'md') %7B%0A
     code

use `fs.readFileSync`, much faster. no need for globs here