Performing text processing on flatpage content to include handling of custom tag

Posted by Dzejkob on Stack Overflow See other posts from Stack Overflow or by Dzejkob
Published on 2011-02-19T23:21:51Z Indexed on 2011/02/19 23:25 UTC
Read the original article Hit count: 356

Hi. I'm using flatpages app in my project to manage some html content. That content will include images, so I've made a ContentImage model allowing user to upload images using admin panel. The user should then be able to include those images in content of the flatpages. He can of course do that by manually typing image url into <img> tag, but that's not what I'm looking for.

To make including images more convenient, I'm thinking about something like this:

  • User edits an additional, let's say pre_content field of CustomFlatPage model (I'm using custom flatpage model already)
  • instead of defining <img> tags directly, he uses a custom tag, something like [img=...] where ... is name of the ContentImage instance
  • now the hardest part: before CustomFlatPage is saved, pre_content field is checked for all [img=...] occurences and they are processed like this:
  • ContentImage model is searched if there's image instance with given name and if so, [img=...] is replaced with proper <img> tag.
  • flatpage actual content is filled with processed pre_content and then flatpage is saved (pre_content is leaved unchanged, as edited by user)

The part that I can't cope with is text processing. Should I use regular expressions? Apparently they can be slow for large strings. And how to organize logic? I assume it's rather algorithmic question, but I'm not familliar with text processing in Python enough, to do it myself.

Can somebody give me any clues?

© Stack Overflow or respective owner

Related posts about python

Related posts about django