I'm trying to recreate a blog CRUD application in which a user can create posts by inputting text into a textarea
of a <form>
. From here the input is stored in Postgresql as the type text
for later use. I'd like to parse that string of text into HTML.
The issue I am having:
User inputs a string of what would be formatted into HTML. The program would take this string and format it into raw HTML.
What input is expected:
<h2>Title here</h2>
<p>some text here</p>
What is should be formatted to:
Title here
some text here
How it is displayed:
<h2>Title here </h2>
<p>some text here </p>
What I have done:
I've read about using a WYSIWYG text editors like CKEditor and TinyMCE. I even used CKEditor but when I save the input into the database it's rendered as a string not the HTML I want, which makes sense. I've also used modules such as HTML5lib, html and BeautifulSoup but it's still rendering as '<h2>Title here</h2><p>some text here</p>'
with the single quotations. So it's still a string. I want to escape the first and last quotes but not every single one between.
Basically I am trying to reproduce this very text editor on Stackoverflow. I am storing the input as a Postgresql text
data type. I figured it's optimal to format the string into HTML when it is retrieved from the database, and not before it is stored (wrong?). I have even implemented a class to do so, like:
class Post():
body = db.Column(db.Text)
def to_html(self, body):
'''format string of chars to HTML. Return HTML'''
# ...
Then in the html template I could (I'm using Jinja to do this):
{{ Post.to_html(body) }}
I believe I am confused about encoding/decoding text and html.