views:

203

answers:

3

I've got a django-powered site. Currently, all of the textfields are just plain old text inputs that take raw HTML. I have no fancy editor or anything. I would like to start using Markdown and WMD to make things easier for everyone.

Do I have to run some sort of script to go over every text field in the database to convert all the HTML to markdown? If I run the HTML through the markdown filter, will it come out the same on the other side? Will the WMD editor read the HTML from the server and convert it to Markdown for the user to edit?

What's the correct course of action here?

+2  A: 

WMD does not do Html-to-markdown, only viceversa; you can convert HTML to markdown with html2text. WMD by default only uses your first text area as a markdown editor, but you can override that as well as add previews -- just follow the instructions in the simple examples that come with WMD. The API is changing anyway (and, to his credit, the author makes no mystery of this) as the whole things moves to a more truly open-source concept, so it's not worth explaining it in detail right now, nor trying anything fancy!-)

Alex Martelli
+2  A: 

As I mentioned on your other question, a good pattern when using markdown is to store the text in markdown and html format in your database. That way, you don't need to convert to html every time the page is viewed, and it is easy to edit the original markdown in the admin.

For example, a blog app might have

class BlogPost(models.Model):

    ...

    body = models.TextField(help_text='Use Markdown syntax.')
    #field to store generated html
    body_html = models.Textfield(editable=False, blank=True)

    ...

    def save(self):
        body_html=markdown(self.body)
        super(BlogPost, self).save()

    ...

Then in the template, you can use {{body_html|safe}}.

I hope that gives you an idea of how you may want to alter your models. I'm afraid I don't have suggestions for how to handle the legacy html data.

Alasdair
A: 

Apparently converting the old HTML to markdown is completely unnecessary. I applied the django.contrib.markup.markdown filter to my templates, and the HTML in the legacy database records was passed straight through. Markdown in non-legacy records was also rendered correctly.

Granted, my web application does not allow users to be modifying these fields, so it is ok to let HTML pass straight through. If this was a user-editable field, like comments or a wiki, this solution would not suffice. You would have to pass the safe parameter to the markdown template filter, which would strip out all the HTML, and some HTML to markdown conversion would be necessary for legacy posts written in HTML.

Another solution, in that case, would be to write a new template filter that wrapped the markdown filter. It would allow old HTML posts to pass through, but apply the safe markdown filter to non-legacy posts.

Apreche