A templating language that looks like Lisp and compiles to HTML
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

compiler.js 882B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const AST = require('./ast')
  2. module.exports = class Compiler {
  3. compile(tree) {
  4. let output = ''
  5. tree.forEach(node => {
  6. output += this.compileNode(node)
  7. })
  8. return output
  9. }
  10. compileNode(node) {
  11. switch (node.constructor) {
  12. case AST.Number:
  13. case AST.String:
  14. return node.value
  15. case AST.Lambda:
  16. return '<lambda>'
  17. case AST.HTMLElement:
  18. let el = `<${node.name}`
  19. if (node.attributes.length > 0) {
  20. el += ' '
  21. el += node.attributes.map(attr => `${attr.name}="${this.compileNode(attr.value)}"`).join(' ')
  22. }
  23. el += '>'
  24. if (node.contents.length > 0) {
  25. el += node.contents.map(content => this.compileNode(content)).join('')
  26. }
  27. if (node.selfClosing === false) {
  28. el += `</${node.name}>`
  29. }
  30. return el
  31. }
  32. }
  33. }