Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 107 lines (88 sloc) 3.871 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
#[coffeekup](http://github.com/mauricemach/coffeekup) rewrite

cs = require 'coffee-script'
fs = require 'fs'

doctypes =
  '1.1': '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">'
  '5': '<!DOCTYPE html>'
  'basic': '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.1//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd">'
  'frameset': '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'
  'mobile': '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.2//EN" "http://www.openmobilealliance.org/tech/DTD/xhtml-mobile12.dtd">'
  'strict': '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
  'transitional': '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
  'xml': '<?xml version="1.0" encoding="utf-8" ?>'

tagsNormal = 'a abbr acronym address applet article aside audio b bdo big blockquote body button canvas caption center cite code colgroup command datalist dd del details dfn dir div dl dt em embed fieldset figcaption figure font footer form frameset h1 h2 h3 h4 h5 h6 head header hgroup html i iframe ins keygen kbd label legend li map mark menu meter nav noframes noscript object ol optgroup option output p pre progress q rp rt ruby s samp script section select small source span strike strong style sub summary sup table tbody td textarea tfoot th thead time title tr tt u ul var video wbr xmp'.split ' '
tagsSelfClosing = 'area base basefont br col frame hr img input link meta param'.split ' '

html = null
indent = null
newline = null

options = {}

nest = (arg) ->
  if typeof arg is 'function'
    indent += ' ' if options.format
    arg = arg.call options.context
    indent = indent.slice 0, -1 if options.format

    # https://github.com/jashkenas/coffee-script/issues/issue/1081
    # `coffee -e 'fn key: val, foo.bar'` throws an error, so we have to
    # wrap up property accessing in a function.
    if arg is undefined
      html += "#{newline}#{indent}"
  if typeof arg is 'string'
    html += if options.autoescape then scope.esc arg else arg

compileTag = (tag, selfClosing) ->
  scope[tag] = (args...) ->
    html += "#{newline}#{indent}<#{tag}"

    if typeof args[0] is 'object'
      for key, val of args.shift()
        if typeof val is 'boolean'
          html += " #{key}" if val is true
        else
          html += " #{key}=\"#{val}\""

    html += ">"

    return if selfClosing

    nest arg for arg in args

    html += "</#{tag}>"

    return

scope =
  coffeescript: (fn) ->
    @script fn.toString().replace 'function () ', ''
    return
  comment: (str) ->
    html += "#{newline}#{indent}<!--#{str}-->"
    return
  doctype: (key=5) ->
    html += "#{indent}#{doctypes[key]}"
    return
  esc: (str) ->
    str.replace /[&<>"']/g, (c) ->
      switch c
        when '&' then '&amp;'
        when '<' then '&lt;'
        when '>' then '&gt;'
        when '"' then '&quot;'
        when "'" then '&#39;'
  ie: (expr, arg) ->
    html += "#{newline}#{indent}<!--[if #{expr}]>"
    nest arg
    html += "<![endif]-->"
    return

for tag in tagsNormal
  compileTag tag, false # don't self close
for tag in tagsSelfClosing
  compileTag tag, true # self close

@compileFile = (path) ->
  code = fs.readFileSync path, 'utf8'
  @compile code

@compile = (code) ->
  code =
    if typeof code is 'function'
      code.toString().replace 'function () ', ''
    else
      cs.compile code, bare: true
  fn = Function 'scope', "with (scope) { #{code} }"
  (_options) ->
    options = _options
    html = ''
    indent = ''
    newline = if options.format then '\n' else ''
    fn.call options.context, scope
    html
Something went wrong with that request. Please try again.