diff --git a/README.md b/README.md index fee8867..418f533 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/setup.py b/setup.py index 5c9d71d..22fb118 100644 --- a/setup.py +++ b/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'] ) diff --git a/src/ciclostile/__init__.py b/src/ciclostile/__init__.py new file mode 100644 index 0000000..f428529 --- /dev/null +++ b/src/ciclostile/__init__.py @@ -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) diff --git a/src/ciclostile_render b/src/ciclostile_render index 16bd01e..fe9482c 100755 --- a/src/ciclostile_render +++ b/src/ciclostile_render @@ -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__': diff --git a/src/ciclostile_web b/src/ciclostile_web new file mode 100755 index 0000000..710a458 --- /dev/null +++ b/src/ciclostile_web @@ -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/') +def assets(path): + assets_path = os.path.join(target_path, 'assets') + return send_from_directory(assets_path, path) + + +@app.route('/') +@app.route('/.html') +def page(page_name): + return ciclostile.render(page_name, markdown_path, template_path) + + +@app.route('//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() diff --git a/src/templates/edit.html b/src/templates/edit.html new file mode 100644 index 0000000..64bdb06 --- /dev/null +++ b/src/templates/edit.html @@ -0,0 +1,20 @@ + + + + + {{ page_name }}: edit + + + +

{{ page_name }}: edit

+ + +
+ + + +
+ + + +