Preview and save
This commit is contained in:
parent
b1105c091e
commit
7d11a91627
|
@ -18,6 +18,6 @@ To build the example run:
|
|||
|
||||
cd example
|
||||
mkdir target
|
||||
ciclostile
|
||||
ciclostile_render
|
||||
|
||||
A `test.html` file will appear inside the `target` folder.
|
||||
|
|
6
setup.py
6
setup.py
|
@ -14,9 +14,9 @@ setup(
|
|||
author='Unit',
|
||||
author_email='unit at paranoici dot org',
|
||||
|
||||
# package_dir={'': 'src'},
|
||||
# packages=['ciclostile'],
|
||||
scripts=['src/ciclostile_render'],
|
||||
package_dir={'': 'src'},
|
||||
packages=['ciclostile'],
|
||||
scripts=['src/ciclostile_render', 'src/ciclostile_web'],
|
||||
|
||||
install_requires=['markdown', 'jinja2']
|
||||
)
|
||||
|
|
47
src/ciclostile/__init__.py
Normal file
47
src/ciclostile/__init__.py
Normal 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)
|
|
@ -1,67 +1,30 @@
|
|||
#!/usr/bin/env python3
|
||||
import os
|
||||
import markdown
|
||||
import jinja2
|
||||
|
||||
template_path = 'templates'
|
||||
md_path = './markdown'
|
||||
html_path = './target'
|
||||
import ciclostile
|
||||
|
||||
|
||||
def write_page(page, out_name):
|
||||
out_path = os.path.join(html_path, out_name)
|
||||
with open(out_path, 'w') as f:
|
||||
f.write(page)
|
||||
working_dir = os.getcwd()
|
||||
|
||||
|
||||
def render_page(data):
|
||||
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)
|
||||
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
|
||||
target_path, template_path, markdown_path = [
|
||||
os.path.join(working_dir, folder_name)
|
||||
for folder_name in ['target', 'templates', 'markdown']
|
||||
]
|
||||
|
||||
|
||||
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')
|
||||
|
||||
|
||||
def main():
|
||||
for md_file in os.listdir(md_path):
|
||||
for md_file in os.listdir(markdown_path):
|
||||
if valid_md(md_file):
|
||||
page = make_page(md_file)
|
||||
render_page(page)
|
||||
page_name = os.path.splitext(md_file)[0]
|
||||
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__':
|
||||
|
|
49
src/ciclostile_web
Executable file
49
src/ciclostile_web
Executable 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
20
src/templates/edit.html
Normal 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>
|
Loading…
Reference in New Issue
Block a user