If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

it is easy to display markdown text rendered as html in a view

{{=gluon.contrib.markdown.WIKI(row.body)}}

But currently there is no What You See Is What You Get Editor for Markdown. To get around this you can use a WYSIWYG Editor form HTML and convert the HTML to Markdown. However the problem with most WYSIWYG Editors is that they replace the texarea that you want to edit in a way that you cannot submit the textarea via Ajax (plug-in/LOAD with ajax=True)

To solve the second problem I chose http://code.google.com/p/jwysiwyg/ as an editor and created my view:

<script type="text/javascript" src="{{=URL(request.application,'static/jwysiwyg','jquery.wysiwyg.js')}}"></script>
<link rel="stylesheet" media="screen,projection" type="text/css" href="{{=URL(request.application,'static/jwysiwyg','jquery.wysiwyg.css')}}" />
<script type="text/javascript"> $('#no_table_body').wysiwyg(); </script>
<h1>Comments</h1>
{{import gluon.contrib.markdown}}
{{for row in rows:}}
    <h2>{{=row.title}}</h2>
    {{=gluon.contrib.markdown.WIKI(row.body)}}
{{pass}}
<h1>Post a comment</h1>
{{=form}}

To convert the HTML to Markdown I used http://www.aaronsw.com/2002/html2text/ and created my controller:

import applications.movie.modules.html2text as html2text
from gluon.sqlhtml import form_factory
form=form_factory( Field('title',requires = IS_NOT_EMPTY()), Field('body','text',requires = IS_NOT_EMPTY()) )
if form.accepts(request.vars, session):
    form.vars.body = html2text.html2text(form.vars.body)
    db.comment.insert(title= form.vars.title, body = form.vars.body)
rows = db(db.comment.id>0).select()
return dict(form=form,rows=rows)

Related slices

Comments (0)


Hosting graciously provided by:
Python Anywhere