chore: auto-formatter removes tabs/whitespace
This commit is contained in:
parent
d9800c5223
commit
8b4d0905e2
214
octomode.py
214
octomode.py
@ -32,123 +32,123 @@ if APP.config.get('PAD_API_KEY', '') == '':
|
|||||||
# ---
|
# ---
|
||||||
|
|
||||||
def get_pad_content(pad_name, ext=""):
|
def get_pad_content(pad_name, ext=""):
|
||||||
if ext:
|
if ext:
|
||||||
pad_name = f'{ pad_name }{ ext }'
|
pad_name = f'{ pad_name }{ ext }'
|
||||||
|
|
||||||
print(pad_name)
|
print(pad_name)
|
||||||
|
|
||||||
arguments = {
|
arguments = {
|
||||||
'padID' : pad_name,
|
'padID' : pad_name,
|
||||||
'apikey' : APP.config['PAD_API_KEY']
|
'apikey' : APP.config['PAD_API_KEY']
|
||||||
}
|
}
|
||||||
api_call = 'getText'
|
api_call = 'getText'
|
||||||
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()))
|
||||||
|
|
||||||
# create pad in case it does not yet exist
|
|
||||||
if response['code'] == 1 and 'padID does not exist' == response['message']:
|
|
||||||
api_call = 'createPad'
|
|
||||||
urlopen(f"{ APP.config['PAD_API_URL'] }{ api_call }", data=urlencode(arguments).encode())
|
|
||||||
api_call = 'getText'
|
|
||||||
response = json.load(urlopen(f"{ APP.config['PAD_API_URL'] }{ api_call }", data=urlencode(arguments).encode()))
|
|
||||||
|
|
||||||
content = response['data']['text']
|
# create pad in case it does not yet exist
|
||||||
return content
|
if response['code'] == 1 and 'padID does not exist' == response['message']:
|
||||||
|
api_call = 'createPad'
|
||||||
|
urlopen(f"{ APP.config['PAD_API_URL'] }{ api_call }", data=urlencode(arguments).encode())
|
||||||
|
api_call = 'getText'
|
||||||
|
response = json.load(urlopen(f"{ APP.config['PAD_API_URL'] }{ api_call }", data=urlencode(arguments).encode()))
|
||||||
|
|
||||||
|
content = response['data']['text']
|
||||||
|
return content
|
||||||
|
|
||||||
def all_pads():
|
def all_pads():
|
||||||
arguments = {
|
arguments = {
|
||||||
'apikey' : APP.config['PAD_API_KEY'],
|
'apikey' : APP.config['PAD_API_KEY'],
|
||||||
}
|
}
|
||||||
api_call = 'listAllPads'
|
api_call = 'listAllPads'
|
||||||
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()))
|
||||||
|
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def create_pad_on_first_run(name, ext):
|
def create_pad_on_first_run(name, ext):
|
||||||
pads = all_pads()
|
pads = all_pads()
|
||||||
pad = name+ext
|
pad = name+ext
|
||||||
|
|
||||||
if pad not in pads['data']['padIDs']:
|
if pad not in pads['data']['padIDs']:
|
||||||
|
|
||||||
# Select default template
|
# Select default template
|
||||||
if 'md' in ext:
|
if 'md' in ext:
|
||||||
default_template = 'templates/default.md'
|
default_template = 'templates/default.md'
|
||||||
elif 'css' in ext:
|
elif 'css' in ext:
|
||||||
default_template = 'templates/default.css'
|
default_template = 'templates/default.css'
|
||||||
default_template = open(default_template).read()
|
default_template = open(default_template).read()
|
||||||
|
|
||||||
# Create pad and add the default template
|
# Create pad and add the default template
|
||||||
arguments = {
|
arguments = {
|
||||||
'padID' : pad,
|
'padID' : pad,
|
||||||
'apikey' : APP.config['PAD_API_KEY'],
|
'apikey' : APP.config['PAD_API_KEY'],
|
||||||
'text' : default_template
|
'text' : default_template
|
||||||
}
|
}
|
||||||
api_call = 'createPad'
|
api_call = 'createPad'
|
||||||
json.load(urlopen(f"{ APP.config['PAD_API_URL'] }{ api_call }", data=urlencode(arguments).encode()))
|
json.load(urlopen(f"{ APP.config['PAD_API_URL'] }{ api_call }", data=urlencode(arguments).encode()))
|
||||||
|
|
||||||
def md_to_html(md_pad_content):
|
def md_to_html(md_pad_content):
|
||||||
# Convert Markdown to HTML
|
# Convert Markdown to HTML
|
||||||
# html = markdown.markdown(md_pad_content, extensions=['meta', 'attr_list']) # attr_list does not work
|
# html = markdown.markdown(md_pad_content, extensions=['meta', 'attr_list']) # attr_list does not work
|
||||||
html = pypandoc.convert_text(md_pad_content, 'html', format='md')
|
html = pypandoc.convert_text(md_pad_content, 'html', format='md')
|
||||||
|
|
||||||
# Sanitize the Markdown
|
# Sanitize the Markdown
|
||||||
# html = bleach.clean(html)
|
# html = bleach.clean(html)
|
||||||
|
|
||||||
# Another built-in Flask way to sanitize
|
# Another built-in Flask way to sanitize
|
||||||
# html = escape(html)
|
# html = escape(html)
|
||||||
html = Markup(html)
|
html = Markup(html)
|
||||||
|
|
||||||
return html
|
return html
|
||||||
|
|
||||||
def get_md_metadata(md_pad_content):
|
def get_md_metadata(md_pad_content):
|
||||||
# Read the metadata from the Markdown
|
# Read the metadata from the Markdown
|
||||||
md = markdown.Markdown(extensions=['meta'])
|
md = markdown.Markdown(extensions=['meta'])
|
||||||
md.convert(md_pad_content)
|
md.convert(md_pad_content)
|
||||||
metadata = md.Meta
|
metadata = md.Meta
|
||||||
|
|
||||||
return metadata
|
return metadata
|
||||||
|
|
||||||
# ---
|
# ---
|
||||||
|
|
||||||
@APP.route('/', methods=['GET', 'POST'])
|
@APP.route('/', methods=['GET', 'POST'])
|
||||||
def index():
|
def index():
|
||||||
name = False
|
name = False
|
||||||
if request.values.get('name'):
|
if request.values.get('name'):
|
||||||
name = escape(request.values.get('name')) # Returns a Markup() object, which is "None" when False
|
name = escape(request.values.get('name')) # Returns a Markup() object, which is "None" when False
|
||||||
if name:
|
if name:
|
||||||
# This is when the environment is "created"
|
# This is when the environment is "created"
|
||||||
# The pads are filled with the default templates (pad, stylesheet, template)
|
# The pads are filled with the default templates (pad, stylesheet, template)
|
||||||
exts = ['.md', '.css']
|
exts = ['.md', '.css']
|
||||||
for ext in exts:
|
for ext in exts:
|
||||||
create_pad_on_first_run(name, ext)
|
create_pad_on_first_run(name, ext)
|
||||||
return redirect(f'{ APP.config["APPLICATION_ROOT"] }{ name }/pad/')
|
return redirect(f'{ APP.config["APPLICATION_ROOT"] }{ name }/pad/')
|
||||||
else:
|
else:
|
||||||
return render_template('start.html', application_root=APP.config["APPLICATION_ROOT"])
|
return render_template('start.html', application_root=APP.config["APPLICATION_ROOT"])
|
||||||
|
|
||||||
@APP.route('/<name>/')
|
@APP.route('/<name>/')
|
||||||
def main(name):
|
def main(name):
|
||||||
return redirect(f'{ APP.config["APPLICATION_ROOT"] }{ name }/pad/')
|
return redirect(f'{ APP.config["APPLICATION_ROOT"] }{ name }/pad/')
|
||||||
|
|
||||||
@APP.route('/<name>/pad/')
|
@APP.route('/<name>/pad/')
|
||||||
def pad(name):
|
def pad(name):
|
||||||
pad_name = f'{ name }.md'
|
pad_name = f'{ name }.md'
|
||||||
url = os.path.join(APP.config['PAD_URL'], pad_name)
|
url = os.path.join(APP.config['PAD_URL'], pad_name)
|
||||||
return render_template('iframe.html', url=url, name=name.strip(), application_root=APP.config["APPLICATION_ROOT"])
|
return render_template('iframe.html', url=url, name=name.strip(), application_root=APP.config["APPLICATION_ROOT"])
|
||||||
|
|
||||||
@APP.route('/<name>/stylesheet/')
|
@APP.route('/<name>/stylesheet/')
|
||||||
def stylesheet(name):
|
def stylesheet(name):
|
||||||
pad_name = f'{ name }.css'
|
pad_name = f'{ name }.css'
|
||||||
url = os.path.join(APP.config['PAD_URL'], pad_name)
|
url = os.path.join(APP.config['PAD_URL'], pad_name)
|
||||||
return render_template('iframe.html', url=url, name=name.strip(), application_root=APP.config["APPLICATION_ROOT"])
|
return render_template('iframe.html', url=url, name=name.strip(), application_root=APP.config["APPLICATION_ROOT"])
|
||||||
|
|
||||||
@APP.route('/<name>/html/')
|
@APP.route('/<name>/html/')
|
||||||
def html(name):
|
def html(name):
|
||||||
url = os.path.join(APP.config["APPLICATION_ROOT"], name, 'preview.html')
|
url = os.path.join(APP.config["APPLICATION_ROOT"], name, 'preview.html')
|
||||||
return render_template('iframe.html', url=url, name=name.strip(), application_root=APP.config["APPLICATION_ROOT"])
|
return render_template('iframe.html', url=url, name=name.strip(), application_root=APP.config["APPLICATION_ROOT"])
|
||||||
|
|
||||||
@APP.route('/<name>/pdf/')
|
@APP.route('/<name>/pdf/')
|
||||||
def pdf(name):
|
def pdf(name):
|
||||||
url = os.path.join(APP.config["APPLICATION_ROOT"], name, 'pagedjs.html')
|
url = os.path.join(APP.config["APPLICATION_ROOT"], name, 'pagedjs.html')
|
||||||
return render_template('pdf.html', url=url, name=name.strip(), application_root=APP.config["APPLICATION_ROOT"])
|
return render_template('pdf.html', url=url, name=name.strip(), application_root=APP.config["APPLICATION_ROOT"])
|
||||||
|
|
||||||
# //////////////////
|
# //////////////////
|
||||||
# RENDERED RESOURCES
|
# RENDERED RESOURCES
|
||||||
@ -157,39 +157,39 @@ def pdf(name):
|
|||||||
|
|
||||||
@APP.route('/<name>/stylesheet.css')
|
@APP.route('/<name>/stylesheet.css')
|
||||||
def css(name):
|
def css(name):
|
||||||
css = get_pad_content(name, '.css')
|
css = get_pad_content(name, '.css')
|
||||||
# Insert CSS sanitizer here.
|
# Insert CSS sanitizer here.
|
||||||
|
|
||||||
return css, 200, {'Content-Type': 'text/css; charset=utf-8'}
|
return css, 200, {'Content-Type': 'text/css; charset=utf-8'}
|
||||||
|
|
||||||
@APP.route('/<name>/preview.html')
|
@APP.route('/<name>/preview.html')
|
||||||
def preview(name):
|
def preview(name):
|
||||||
# TO GENERATE THE PREVIEW WEBPAGE
|
# TO GENERATE THE PREVIEW WEBPAGE
|
||||||
md_pad_content = get_pad_content(name, ext='.md')
|
md_pad_content = get_pad_content(name, ext='.md')
|
||||||
html = md_to_html(md_pad_content)
|
html = md_to_html(md_pad_content)
|
||||||
metadata = get_md_metadata(md_pad_content)
|
metadata = get_md_metadata(md_pad_content)
|
||||||
if metadata:
|
if metadata:
|
||||||
lang = metadata['language'][0]
|
lang = metadata['language'][0]
|
||||||
title = metadata['title'][0]
|
title = metadata['title'][0]
|
||||||
else:
|
else:
|
||||||
lang = "en"
|
lang = "en"
|
||||||
title = "No title"
|
title = "No title"
|
||||||
|
|
||||||
return render_template('preview.html', name=name.strip(), pad_content=html, lang=lang, title=title, application_root=APP.config["APPLICATION_ROOT"])
|
return render_template('preview.html', name=name.strip(), pad_content=html, lang=lang, title=title, application_root=APP.config["APPLICATION_ROOT"])
|
||||||
|
|
||||||
@APP.route('/<name>/pagedjs.html')
|
@APP.route('/<name>/pagedjs.html')
|
||||||
def pagedjs(name):
|
def pagedjs(name):
|
||||||
# TO GENERATE THE PAGED.JS WEBPAGE
|
# TO GENERATE THE PAGED.JS WEBPAGE
|
||||||
md_pad_content = get_pad_content(name, ext='.md')
|
md_pad_content = get_pad_content(name, ext='.md')
|
||||||
html = md_to_html(md_pad_content)
|
html = md_to_html(md_pad_content)
|
||||||
metadata = get_md_metadata(md_pad_content)
|
metadata = get_md_metadata(md_pad_content)
|
||||||
lang = metadata['language'][0]
|
lang = metadata['language'][0]
|
||||||
title = metadata['title'][0]
|
title = metadata['title'][0]
|
||||||
|
|
||||||
return render_template('pagedjs.html', name=name.strip(), pad_content=html, lang=lang, title=title, application_root=APP.config["APPLICATION_ROOT"])
|
return render_template('pagedjs.html', name=name.strip(), pad_content=html, lang=lang, title=title, application_root=APP.config["APPLICATION_ROOT"])
|
||||||
|
|
||||||
# //////////////////
|
# //////////////////
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
APP.debug=True
|
APP.debug=True
|
||||||
APP.run(host="0.0.0.0", port=f'{ APP.config["PORTNUMBER"] }', threaded=True)
|
APP.run(host="0.0.0.0", port=f'{ APP.config["PORTNUMBER"] }', threaded=True)
|
||||||
|
Loading…
Reference in New Issue
Block a user