improve javaScript

This commit is contained in:
qvalentin 2021-08-29 18:28:34 +02:00
parent c52922507d
commit 29722b7266
1 changed files with 5 additions and 6 deletions

View File

@ -17,10 +17,11 @@ from pygments.formatters.html import HtmlFormatter
from pygments.lexers import guess_lexer_for_filename
def render_gist_js(code):
template = Template("document.write( '{{ code|tojson }}' );")
def render_gist_js(code,path):
template = Template("""script = document.querySelector('script[src$="{{ path }}"]')
script.insertAdjacentHTML( 'afterend','{{ code|tojson }}' );""")
return template.render(code=code)
return template.render(code=code,path=path)
class HTTPRequestHandler(BaseHTTPRequestHandler):
@ -60,15 +61,13 @@ class HTTPRequestHandler(BaseHTTPRequestHandler):
result = highlighted_code + f"<style> {css}</style>"
js_rendered = render_gist_js(result)
js_rendered = render_gist_js(result,self.path)
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.wfile.write(js_rendered.encode('utf8'))
else:
self.send_response(404)