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.

Djensen
  • 1,337
  • 1
  • 22
  • 32
KA01
  • 4,191
  • 3
  • 19
  • 26

1 Answers1

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.

Joseph
  • 691
  • 1
  • 4
  • 12
  • 1
    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 `<h2>here is some text </h2>` but it suppose to be rendered as

    here is some text

    (but here its formatted without the tags)
    – KA01 May 06 '16 at 01:43
  • 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. – Joseph May 06 '16 at 02:21
  • Are you looking at your result in a browser? You're not going to get formatted text in whatever shell you're using. – Joseph May 06 '16 at 02:24
  • Yes I'm using a browser and using the browsers dev tool – KA01 May 06 '16 at 02:34
  • ok... so, you're escaping <> with < and > but its converting them back so that the browser sees <h2> as

    and displays "here is some text" as a heading. Correct?

    – Joseph May 06 '16 at 07:25
  • check out [this question](http://stackoverflow.com/questions/2820453/display-html-code-in-html), it might solve your problem. You might also try wrapping your code samples in
     and  tags.
    – Joseph May 06 '16 at 07:29
  • In the html it looks like this `
    <h2>Some h2 text</h2>
    ` where `<h2>Some h2 text</h2>` should be `

    Some h2 text

    `. In the browser it shows `

    Some h2 text

    ` NOT as a H2 heading but in non-heading text.
    – KA01 May 09 '16 at 17:27