2

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:

&lt;h2&gt;Title here &lt;/h2&gt;
&lt;p&gt;some text here &lt;/p&gt;

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.

1
  • 2
    i think you are confused cus you're confusing me.... you realize that HTML is a string... Commented May 5, 2016 at 19:45

1 Answer 1

2

You need to understand a bit about html... What you have is raw HTML:

<h2>Title here</h2><p>some text here</p>

Save that in a blank file with a .htm extension, then open it with a browser. The browser interprets the raw HTML and gives you the formatted output you want.

I suggest you read this tutorial before continuing with your project.

Sign up to request clarification or add additional context in comments.

7 Comments

the text is stored into Postgresql as the type text under the table Post in the row Post.body. It isn't stored as a file. When I extract the data in Post.body and place it into an html template it comes out, for example, like &lt;h2&gt;here is some text &lt;/h2&gt; but it suppose to be rendered as <h2>here is some text</h2> (but here its formatted without the tags)
Storing html as text/string is fine... That's all html is. You don't need to worry about how it looks until you display it to a user.
Are you looking at your result in a browser? You're not going to get formatted text in whatever shell you're using.
Yes I'm using a browser and using the browsers dev tool
ok... so, you're escaping <> with &lt; and &gt; but its converting them back so that the browser sees &lt;h2&gt; as <h2> and displays "here is some text" as a heading. Correct?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.