Browse Source

pandoc template issue solved: templates can be read from an URL :); octomode is ready to be used in multiple sessions at the same time now

pull/30/head
manetta 3 years ago
parent
commit
0742986e8f
  1. 67
      octomode.py

67
octomode.py

@ -67,20 +67,12 @@ def create_pad_on_first_run(name, ext):
api_call = 'createPad' api_call = 'createPad'
response = json.load(urlopen(f"{ APP.config['PAD_API_URL'] }{ api_call }", data=urlencode(arguments).encode())) response = json.load(urlopen(f"{ APP.config['PAD_API_URL'] }{ api_call }", data=urlencode(arguments).encode()))
def update_pad_contents(name): def update_pad_contents(name, ext):
# download the md pad + stylesheet + template pad # download the md pad + stylesheet + template pad
md_pad = f'{ name }.md' pad_name = f'{ name }{ ext }'
css_pad = f'{ name }.css' pad_content = get_pad_content(pad_name)
template_pad = f'{ name }.template'
md = get_pad_content(md_pad) return pad_content
css = get_pad_content(css_pad)
template = get_pad_content(template_pad)
# !!! this breaks the whole idea that this application can be shared by multiple projects at the same time
# !!! but py_pandoc needs to run with files........ hmmm
with open('templates/pandoc-template.html', 'w') as f:
f.write(template)
return md, css, template
# --- # ---
@ -102,25 +94,25 @@ def main(name):
# !!! Add a if/else here to check if the environment is "created" already # !!! Add a if/else here to check if the environment is "created" already
ext = '.md' ext = '.md'
create_pad_on_first_run(name, ext) create_pad_on_first_run(name, ext)
return flask.render_template('pad.html', name=name.strip(), ext=ext) return flask.render_template('pad.html', pad_url=APP.config['PAD_URL'], name=name.strip(), ext=ext)
@APP.route('/<name>/pad/') @APP.route('/<name>/pad/')
def pad(name): def pad(name):
ext = '.md' ext = '.md'
create_pad_on_first_run(name, ext) create_pad_on_first_run(name, ext)
return flask.render_template('pad.html', name=name.strip(), ext=ext) return flask.render_template('pad.html', pad_url=APP.config['PAD_URL'], name=name.strip(), ext=ext)
@APP.route('/<name>/stylesheet/', methods=['GET']) @APP.route('/<name>/stylesheet/', methods=['GET'])
def stylesheet(name): def stylesheet(name):
ext = '.css' ext = '.css'
create_pad_on_first_run(name, ext) create_pad_on_first_run(name, ext)
return flask.render_template('pad.html', name=name.strip(), ext=ext) return flask.render_template('pad.html', pad_url=APP.config['PAD_URL'], name=name.strip(), ext=ext)
@APP.route('/<name>/template/', methods=['GET']) @APP.route('/<name>/template/', methods=['GET'])
def template(name): def template(name):
ext = '.template' ext = '.template'
create_pad_on_first_run(name, ext) create_pad_on_first_run(name, ext)
return flask.render_template('pad.html', name=name.strip(), ext=ext) return flask.render_template('pad.html', pad_url=APP.config['PAD_URL'], name=name.strip(), ext=ext)
# @APP.route('/<name>/html/') # @APP.route('/<name>/html/')
# def html(name): # def html(name):
@ -144,6 +136,7 @@ def pdf(name):
exts = ['.md', '.css', '.template'] exts = ['.md', '.css', '.template']
for ext in exts: for ext in exts:
create_pad_on_first_run(name, ext) create_pad_on_first_run(name, ext)
return flask.render_template('pdf.html', name=name.strip()) return flask.render_template('pdf.html', name=name.strip())
# ////////////// # //////////////
@ -151,60 +144,34 @@ def pdf(name):
@APP.route('/<name>/print.css') @APP.route('/<name>/print.css')
def css(name): def css(name):
x, css, x = update_pad_contents(name) css = update_pad_contents(name, '.css')
return css, 200, {'Content-Type': 'text/css; charset=utf-8'} return css, 200, {'Content-Type': 'text/css; charset=utf-8'}
@APP.route('/<name>/pandoc-template.html') @APP.route('/<name>/pandoc-template.html')
def pandoc_template(name): def pandoc_template(name):
x, x, template = update_pad_contents(name) template = update_pad_contents(name, '.template')
return template, 200, {'Content-Type': 'text/html; charset=utf-8'} return template, 200, {'Content-Type': 'text/html; charset=utf-8'}
@APP.route('/<name>/pagedjs.html') @APP.route('/<name>/pagedjs.html')
def pagedjs(name): def pagedjs(name):
# update pad contents # update pad contents
md, css, template = update_pad_contents(name) md = update_pad_contents(name, '.md')
# generate html page with the pandoc template (with paged.js inserted in the header) # generate html page with the pandoc template (with paged.js inserted in the header)
# the pandoc template is loaded dynamically from /<name>/pandoc-template.html
# Needs pandoc >2.2.2 for this!
# https://github.com/jgm/pandoc/issues/5246
pandoc_args = [ pandoc_args = [
'--toc', '--toc',
'--toc-depth=1', '--toc-depth=1',
'--template=templates/pandoc-template.html', f'--template=http://localhost:5001/{ name }/pandoc-template.html',
'--standalone' '--standalone'
] ]
html = pypandoc.convert_text(md, 'html', format='md', extra_args=pandoc_args) html = pypandoc.convert_text(md, 'html', format='md', extra_args=pandoc_args)
return html, 200, {'Content-Type': 'text/html; charset=utf-8'} return html, 200, {'Content-Type': 'text/html; charset=utf-8'}
# //////////////
def flask_logger():
# creates logging information
# https://www.vantage-ai.com/en/blog/adding-on-screen-logging-in-a-few-steps-using-a-flask-application
from io import StringIO
import logging
from time import sleep
log_stream = StringIO()
handler = logging.StreamHandler(log_stream)
log = logging.getLogger('werkzeug')
log.setLevel(logging.DEBUG)
log.addHandler(handler)
while True:
yield log_stream.getvalue()
# "flush" the stream, move the seek/truncate points to 0
log_stream.seek(0)
log_stream.truncate(0)
sleep(0.1)
@APP.route('/log/', methods=['GET'])
def log():
# returns logging information
return flask.Response(flask_logger(), mimetype="text/plain", content_type="text/event-stream")
# ///////////// # /////////////

Loading…
Cancel
Save