fork download
  1. #!/usr/bin/env python3
  2.  
  3. import sys
  4.  
  5. def escape_html(s):
  6. return ' db `' + s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\n", "\\n") + '`'
  7.  
  8. def generate_html_printer(s, i):
  9. return """
  10. pusha
  11. mov eax, 4
  12. mov ebx, 1
  13. mov ecx, text_{0}
  14. mov edx, {1}
  15. int 0x80
  16. popa
  17. section .data
  18. text_{2}:
  19. {3}
  20. section .code""".format(i, len(s), i, escape_html(s))
  21.  
  22. def generate_header():
  23. return """
  24. bits 32
  25. section .code
  26. global _start
  27. _start:"""
  28.  
  29. def generate_footer():
  30. return """
  31. mov eax, 1
  32. mov ebx, 0
  33. int 0x80"""
  34.  
  35. def preprocess_template(text):
  36. mode = "html"
  37. out = generate_header()
  38. msg_id = 1
  39. while True:
  40. if mode == "html":
  41. pos = text.find("<%")
  42. if pos == -1:
  43. out += generate_html_printer(text, msg_id)
  44. return out + generate_footer()
  45. out += generate_html_printer(text[:pos], msg_id)
  46. msg_id += 1
  47. text = text[pos+2:]
  48. mode = "asm"
  49. elif mode == "asm":
  50. pos = text.find("%>")
  51. if pos == -1:
  52. raise Exception("Unclosed <% tag")
  53. out += text[:pos]
  54. text = text[pos+2:]
  55. mode = "html"
  56.  
  57.  
  58. print(preprocess_template(sys.stdin.read()))
Success #stdin #stdout 0.03s 9984KB
stdin
<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
</head>
<body>
<table>
<%
    mov ecx, 10
rpt:
%>
<tr><td>Hui!</td></tr>
<%
    dec ecx
    jnz rpt
%>
</table>
</body>
</html>
stdout
    bits 32
    section .code
global _start
_start:
    pusha
    mov eax, 4
    mov ebx, 1
    mov ecx, text_1
    mov edx, 82
    int 0x80
    popa
    section .data
text_1:
    db `<!doctype html>\n<html>\n<head>\n    <meta charset=\"utf-8\" />\n</head>\n<body>\n<table>\n`
    section .code
    mov ecx, 10
rpt:

    pusha
    mov eax, 4
    mov ebx, 1
    mov ecx, text_2
    mov edx, 24
    int 0x80
    popa
    section .data
text_2:
    db `\n<tr><td>Hui!</td></tr>\n`
    section .code
    dec ecx
    jnz rpt

    pusha
    mov eax, 4
    mov ebx, 1
    mov ecx, text_3
    mov edx, 25
    int 0x80
    popa
    section .data
text_3:
    db `\n</table>\n</body>\n</html>`
    section .code
    mov eax, 1
    mov ebx, 0
    int 0x80