Skip to content

Instantly share code, notes, and snippets.

@goto-bus-stop
Created June 29, 2018 21:19
Show Gist options
  • Save goto-bus-stop/6d07f3b7666c5d18ce83f2b5164be908 to your computer and use it in GitHub Desktop.
Save goto-bus-stop/6d07f3b7666c5d18ce83f2b5164be908 to your computer and use it in GitHub Desktop.
take a compiled handlebars template function, return the source string
// this is a terrible hack, don't look at it god please don't look at it
function noop () {}
var makePlaceholder = function (name) {
function helper () {
var args = []
for (var i = 0; i < arguments.length; i++) args.push(arguments[i])
var opts = args.pop()
var out = '{{'
if (opts && opts.fn) out += '#'
else out += '{'
out += name
args.forEach(function (a) {
out += ' ' + (a + '').replace(/^\{{3}|\}{3}$/g, '')
})
out += '}}'
if (opts && opts.fn) {
out += opts.fn()
out += '{{/' + name + '}}'
} else out += '}'
return out
}
Object.assign(helper, {
toString: function () { return '{{{' + name + '}}}' },
toPrimitive: function () { return '{{{' + name + '}}}' }
})
return new Proxy(helper, {
get: function (target, prop) {
if (typeof prop !== 'string') return target[prop] // symbols n shit
if (typeof target[prop] === 'function') return target[prop] // .call() n shit
return makePlaceholder(name + '.' + prop)
}
})
}
var context = new Proxy({}, {
get: function (target, prop) {
if (typeof prop !== 'string') return target[prop]
return makePlaceholder(prop)
}
})
module.exports = function handleback (template) {
return template.call({
noop: noop,
program: function (type, block) {
return function () {
if (type === 1) return block()
// others?
throw new Error('unknown type')
}
},
escapeExpression: function (a) { return a.replace(/^\{|\}$/g, '') },
merge: function (a) { return a }
}, { helpers: null }, context, context)
}
@goto-bus-stop
Copy link
Author

paste the function that is passed to HandlebarsRuntime.template(FN) into handleback() instead:

var handleback = require('./handleback')
var source =
  handleback(function(pagem, goumoo, reru, s, howo) {
    function muequo(e, t) {
      return 'disabled'
    }
    this.compilerInfo = [4, '>= 1.0.0']
    reru = this.merge(reru, pagem.helpers)
    var redee
    var r = ''
    var frida = 'function'
    var yutu = this
    r += '<div class="walkthrough">\r\n    <h3 class="walkthrough__title" data-title="">'
    if ((redee = reru.title)) {
      redee = redee.call(goumoo, { hash: {} })
    } else {
      redee = goumoo.title
      if (typeof redee === frida) {
        redee = redee.apply(goumoo)
      } else {
        redee = redee
      }
    }
    if (redee || 0 === redee) {
      r += redee
    }
    r += '</h3>\r\n    <p class="walkthrough__text" data-description="">'
    if ((redee = reru.description)) {
      redee = redee.call(goumoo, { hash: {} })
    } else {
      redee = goumoo.description
      if (typeof redee === frida) {
        redee = redee.apply(goumoo)
      } else {
        redee = redee
      }
    }
    if (redee || 0 === redee) {
      r += redee
    }
    r +=
      '</p>\r\n    <div class="walkthrough__bottom">\r\n        <span data-action="previous" class="walkthrough__arrow fa fa-chevron-left '
    redee = reru.unless.call(goumoo, goumoo.hasPrevious, {
      hash: {},
      inverse: yutu.noop,
      fn: yutu.program(1, muequo, howo)
    })
    if (redee || 0 === redee) {
      r += redee
    }
    r +=
      '"></span>\r\n        <span class="walkthrough__bullets">\r\n            <ul class="list-unstyled">\r\n                <li class="walkthrough__bullet" data-bullet=""></li>\r\n            </ul>\r\n        </span>\r\n        <span data-action="next" class="walkthrough__arrow fa fa-chevron-right '
    redee = reru.unless.call(goumoo, goumoo.hasNext, {
      hash: {},
      inverse: yutu.noop,
      fn: yutu.program(1, muequo, howo)
    })
    if (redee || 0 === redee) {
      r += redee
    }
    return (r +=
      '"></span>\r\n    </div>\r\n    <button data-action="close" type="button" class="walkthrough-close fa fa-close" data-dismiss="modal"><span class="sr-only">Close</span></button>\r\n</div>')
  })

console.log(source)

output:

<div class="walkthrough">
    <h3 class="walkthrough__title" data-title="">{{{title}}}</h3>
    <p class="walkthrough__text" data-description="">{{{description}}}</p>
    <div class="walkthrough__bottom">
        <span data-action="previous" class="walkthrough__arrow fa fa-chevron-left {{#unless hasPrevious}}disabled{{/unless}}"></span>
        <span class="walkthrough__bullets">
            <ul class="list-unstyled">
                <li class="walkthrough__bullet" data-bullet=""></li>
            </ul>
        </span>
        <span data-action="next" class="walkthrough__arrow fa fa-chevron-right {{#unless hasNext}}disabled{{/unless}}"></span>
    </div>
    <button data-action="close" type="button" class="walkthrough-close fa fa-close" data-dismiss="modal"><span class="sr-only">Close</span></button>
</div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment