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 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const selfClosingTags = require("./util/selfClosingTags");
  2. module.exports = class Compiler {
  3. constructor(tree, context) {
  4. this.tree = tree;
  5. this.context = context;
  6. this.pos = 0;
  7. this.result = "";
  8. }
  9. compile() {
  10. this.tree.forEach(node => {
  11. switch (node.constructor.name) {
  12. case "Application":
  13. this.result += this.application(node);
  14. break;
  15. case "Number":
  16. case "String":
  17. this.result += node.value;
  18. break;
  19. }
  20. });
  21. return this.result;
  22. }
  23. application(node) {
  24. let result = `<${node.functionName.name}`;
  25. node.args
  26. .filter(arg => arg.constructor.name === "Attribute")
  27. .forEach(arg => {
  28. result += ` ${arg.name}`;
  29. let compiler = new Compiler([arg.value], this.context);
  30. let attrValue = compiler.compile();
  31. if (attrValue) {
  32. result += `="${attrValue}"`;
  33. }
  34. });
  35. result += ">";
  36. node.args
  37. .filter(arg => arg.constructor.name !== "Attribute")
  38. .forEach(arg => {
  39. let compiler = new Compiler([arg], this.context);
  40. result += compiler.compile();
  41. });
  42. if (!selfClosingTags.includes(node.functionName.name)) {
  43. result += `</${node.functionName.name}>`;
  44. }
  45. return result;
  46. }
  47. };