Performing text processing at the content level, including custom tag processing

I am using the flatpages application in my project to manage some html content. This content will contain images, so I created a model ContentImagethat allows the user to upload images using the admin panel. Then the user should be able to include these images in the content of flat pages. Of course, he can do this by typing the image URL into a tag <img>, but that’s not what I'm looking for.

To make the images more convenient, I am thinking of something like this:

  • The user edits an additional, say, pre_contentmodel field CustomFlatPage(I already use a custom model model)
  • instead of directly defining tags <img>, it uses its own tag, something like [img=...]where ...is the instance nameContentImage
  • now the hardest part: before saving, the CustomFlatPagefield is pre_contentchecked for all inputs [img=...], and they are processed as follows:
  • ContentImagea search is performed if the image instance with the given name, and if so, [img=...]is replaced by the corresponding tag <img>.
  • the actual one is contentfilled in by the processed one pre_content, and then the flat page is saved (it pre_contentdoes not change as edited by the user)

, , - . ? -, . ? , , Python, .

- - ?

+3
1

, , . , . :

import re
from django.utils.translation import ugettext as _

def process_image_tags(text, ImageModel):
    '''image tag usage:
        ... some text ... [img=image_name:image_class(optional)] ... some text ...
    '''
    t1 = re.split(r'(\[img=[a-z0-9\-_\:]+\])', text)
    t2 = []
    for i in t1:
        if i[:5] == '[img=':
            attrs = i[5:-1].split(':')
            name_attr = attrs[0] #name attribute
            error = None
            try:
                image = ImageModel.objects.get(name=name_attr)
            except ImageModel.DoesNotExist:
                error = '<span class="image_tag_error">%s</span>' % _('Image with given name not found')
            except ImageModel.MultipleObjectsReturned:
                error = '<span class="image_tag_error">%s</span>' % _('More than one image found')
            if not error:
                p = ['<img']
                p.append('src="%s"' % image.image.url) 
                if len(attrs) > 1:
                    p.append('class="%s"' % attrs[1]) #class attribute
                if image.description:
                    p.append('title="%s"' % image.description)
                p.append('alt="%s"' % image.name)
                p.append('/>')                   
                t2.append(' '.join(p))
            else:
                t2.append(error)
        else:
            t2.append(i)
    return ''.join(t2)

CustomFlatPage, :

def save(self, *args, **kwargs):           
    self.content = process_image_tags(self.pre_content, ContentImage)        
    super(CustomFlatPage, self).save(*args, **kwargs)

, , , , . , javascript , , , , URL-, .

+1

Source: https://habr.com/ru/post/1793311/


All Articles