Preview and save

This commit is contained in:
uid 2018-02-22 16:43:12 +01:00
parent b1105c091e
commit 7d11a91627
6 changed files with 134 additions and 55 deletions

View File

@ -18,6 +18,6 @@ To build the example run:
cd example cd example
mkdir target mkdir target
ciclostile ciclostile_render
A `test.html` file will appear inside the `target` folder. A `test.html` file will appear inside the `target` folder.

View File

@ -14,9 +14,9 @@ setup(
author='Unit', author='Unit',
author_email='unit at paranoici dot org', author_email='unit at paranoici dot org',
# package_dir={'': 'src'}, package_dir={'': 'src'},
# packages=['ciclostile'], packages=['ciclostile'],
scripts=['src/ciclostile_render'], scripts=['src/ciclostile_render', 'src/ciclostile_web'],
install_requires=['markdown', 'jinja2'] install_requires=['markdown', 'jinja2']
) )

View File

@ -0,0 +1,47 @@
import os
import markdown
import jinja2
def read_markdown(page_name, markdown_path):
md_file = os.path.join(markdown_path, page_name + '.md')
with open(md_file) as f:
md_text = f.read()
return md_text
def render_from_text(md_text, template_path):
md = markdown.Markdown(extensions=['meta'], output_format='html5')
html = md.convert(md_text)
# get markdown metadata
data = {key: md.Meta[key][0] for key in md.Meta.keys()}
data.update({
'content': html,
# 'page_name': page_name, # Needed?
})
template_name = data.get('template', 'default')
template_file = os.path.join(template_path, template_name + '.html')
with open(template_file) as f:
template = f.read()
page = jinja2.Template(template).render(**data)
return page
def render(page_name, markdown_path, template_path):
md_text = read_markdown(page_name, markdown_path)
page = render_from_text(md_text, template_path)
return page
def save(md_text, page_name, markdown_path):
md_file = os.path.join(markdown_path, page_name + '.md')
with open(md_file, 'w') as f:
f.write(md_text)

View File

@ -1,67 +1,30 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import os import os
import markdown import ciclostile
import jinja2
template_path = 'templates'
md_path = './markdown'
html_path = './target'
def write_page(page, out_name): working_dir = os.getcwd()
out_path = os.path.join(html_path, out_name)
with open(out_path, 'w') as f:
f.write(page)
target_path, template_path, markdown_path = [
def render_page(data): os.path.join(working_dir, folder_name)
template_name = data.get('template', 'default') for folder_name in ['target', 'templates', 'markdown']
template_file = os.path.join(template_path, template_name + '.html') ]
with open(template_file) as f:
template = f.read()
page = jinja2.Template(template).render(**data)
write_page(page, data['file_name'])
def read_md(md_file):
full_name = os.path.join(md_path, md_file)
with open(full_name) as f:
md_text = f.read()
return md_text
def make_page(md_file):
md_text = read_md(md_file)
md = markdown.Markdown(extensions=['meta'], output_format='html5')
html = md.convert(md_text)
# get markdown metadata
data = {key: md.Meta[key][0]
for key in md.Meta.keys()}
out_name = os.path.splitext(md_file)[0] + '.html'
data.update({
'content': html,
'file_name': out_name,
})
return data
def valid_md(md_file): def valid_md(md_file):
full_name = os.path.join(md_path, md_file) full_name = os.path.join(markdown_path, md_file)
return os.path.isfile(full_name) and full_name.endswith('.md') return os.path.isfile(full_name) and full_name.endswith('.md')
def main(): def main():
for md_file in os.listdir(md_path): for md_file in os.listdir(markdown_path):
if valid_md(md_file): if valid_md(md_file):
page = make_page(md_file) page_name = os.path.splitext(md_file)[0]
render_page(page) page = ciclostile.render(page_name, markdown_path, template_path)
out_path = os.path.join(target_path, page_name + '.html')
with open(out_path, 'w') as f:
f.write(page)
if __name__ == '__main__': if __name__ == '__main__':

49
src/ciclostile_web Executable file
View File

@ -0,0 +1,49 @@
#!/usr/bin/env python3
import os
from flask import Flask, send_from_directory, render_template, request,\
redirect
import ciclostile
working_dir = os.getcwd()
target_path, template_path, markdown_path = [
os.path.join(working_dir, folder_name)
for folder_name in ['target', 'templates', 'markdown']
]
app = Flask(__name__)
@app.route('/assets/<path:path>')
def assets(path):
assets_path = os.path.join(target_path, 'assets')
return send_from_directory(assets_path, path)
@app.route('/<string:page_name>')
@app.route('/<string:page_name>.html')
def page(page_name):
return ciclostile.render(page_name, markdown_path, template_path)
@app.route('/<string:page_name>/edit')
def edit(page_name):
md_text = ciclostile.read_markdown(page_name, markdown_path)
return render_template('edit.html', **locals())
@app.route('/edit', methods=['POST'])
def edit_actions():
page_name = request.form['page_name']
md_text = request.form['md_text']
if request.form['action'] == 'preview':
return ciclostile.render_from_text(md_text, template_path)
if request.form['action'] == 'save':
ciclostile.save(md_text, page_name, markdown_path)
return page(page_name)
if __name__ == '__main__':
app.run()

20
src/templates/edit.html Normal file
View File

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{ page_name }}: edit</title>
</head>
<body>
<h1>{{ page_name }}: edit</h1>
<textarea name="md_text" form="mdform">{{ md_text }}</textarea>
<form action="/edit" id="mdform" method="POST" target="preview_iframe">
<input type="hidden" name="page_name" value="{{ page_name }}">
<input type="submit" name="action" value="preview">
<input type="submit" name="action" value="save">
</form>
<iframe name="preview_iframe" src="/{{ page_name }}"></iframe>
</body>
</html>