Created
April 19, 2012 04:55
-
-
Save btbytes/2418666 to your computer and use it in GitHub Desktop.
flask, flask-flatpages, hamlish_jinja, pyparsing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| title: Random | |
| %p#somepara | |
| Paragraph with a link to [[http://www.google.com.au|Google]]. | |
| %p | |
| Paragraph with link to [[{{ url_for('index') }}|index]]. | |
| %pre.python | |
| from flask import Flask | |
| app = Flask(__name__) | |
| @app.route("/") | |
| def hello(): | |
| return "Hello World!" | |
| if __name__ == "__main__": | |
| app.run() | |
| %p.someclass | |
| Another paragraph. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <title>test</title> | |
| </head> | |
| <body> | |
| <h1>{{ page.title }}</h1> | |
| <div id="something"> | |
| {{ page }} | |
| </div> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from flask import Flask, render_template | |
| from flaskext.flatpages import FlatPages | |
| from hamlish_jinja import HamlishTagExtension | |
| from pygments import highlight | |
| from pygments.formatters import HtmlFormatter | |
| from pygments.lexers import get_lexer_by_name | |
| from pyparsing import * | |
| app = Flask(__name__) | |
| pages = FlatPages(app) | |
| def code(s, l, t): | |
| return highlight(t[1], get_lexer_by_name(t[0]), HtmlFormatter()) | |
| def link(s, l, t): | |
| a, title = t[0].split('|') | |
| return '<a href="%s">%s</a>' % (a, title) | |
| def markup(text): | |
| env = app.jinja_env.overlay() | |
| env.add_extension(HamlishTagExtension) | |
| # debug required otherwise blank lines removed within <pre> | |
| env.hamlish_mode = 'debug' | |
| text = '{% haml %}\n' + text + '{% endhaml %}' | |
| html = env.from_string(text).render() | |
| # parse html ignoring pre blocks | |
| LINK = QuotedString('[[', endQuoteChar=']]').setParseAction(link) | |
| PRE = Literal('<pre') + SkipTo('</pre>') + Literal('</pre>') | |
| markup = LINK | |
| markup.ignore(PRE) | |
| html = markup.transformString(html) | |
| # pass pre blocks with a class to pygments | |
| o = Literal('<pre class=').suppress() | |
| c = Literal('</pre>').suppress() | |
| PRE = o + QuotedString('"') + Literal('>').suppress() + SkipTo(c) + c | |
| PRE.setParseAction(code) | |
| markup = PRE | |
| return markup.transformString(html) | |
| app.config['FLATPAGES_EXTENSION'] = '.haml' | |
| app.config['FLATPAGES_HTML_RENDERER'] = markup | |
| @app.route('/') | |
| def index(): | |
| p = pages.get('index') | |
| return render_template('index.html', page=p) | |
| if __name__ == "__main__": | |
| app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment