Rendering newlines in user-submitted content (Python web app)

前端 未结 4 2042
死守一世寂寞
死守一世寂寞 2020-12-11 23:20

I have a web.py app that takes input from a textarea and inputs it to a database. I can get the information from the database and post it to the page but the NEWLINES are g

4条回答
  •  情深已故
    2020-12-12 00:06

    Instead of using using the inserted text which is not html and therefore will not be displayed as html (\r\n is not a "p" tag etc), you could use a formatting language like markdown (as you mentioned).

    Otherwise you will need to replace/parse the entered text manually (which I think is not a good idea, because languages like markdown have been invented for this).

    There are some good python libraries to convert markdown (the data you store in database) to html, like python-markdown2.

    It's really easy to use, see the examples from the python-markdown link:

    >>> import markdown2
    >>> markdown2.markdown("*boo!*")  # or use `html = markdown_path(PATH)`
    u'

    boo!

    \n' >>> markdowner = Markdown() >>> markdowner.convert("*boo!*") u'

    boo!

    \n' >>> markdowner.convert("**boom!**") u'

    boom!

    \n'

    This forces you to enter the content using markdown syntax (or whatever format you use). To make this easier, you could use an wysiwyg-editor that creates the markdown for you (like the editor here on Stackoverflow). I think Stackoverflow uses wmd but there are a lot of other markdown wysiwyg editors.

    Example for wmd:

    
        
            WMD Example using jquery
            
    
            
            
        
        
            

    WMD Example using jquery