How to modify a long paragraph string and insert html tags into it based on a list of modifiers?

361 views Asked by At

I am working with a Python backend and the prismic.io CMS to create a blog.

When I call the Prismic API I get a JSON response with the content.

For paragraph type content Prismic responds the following way:

enter image description here

The paragraph modifiers are located inside the spans with start, end, type, and data attributes.

I am now trying to parse this so that I can render the html in the backend. For this I need to take every item in the list of spans, and modify the original paragraph string.

I started using BeautifulSoup() for this, however I am a little lost. I am trying to answer the following question:

How can I insert HTML tags for each span element based on the starting position and end positions into the original paragraph text string while taking into account that there might be overlapping elements?

Below is the code which I started writing, but I don't think I am approaching the problem correctly. Is there some smarter string replacement or tag insert logic I can use?

    def parse(self):
        soup = BeautifulSoup()
        
        for item in self.prismic_data:
            if item['type'] == 'paragraph':
                text = item['text']
                new_tag = Tag(name="p", attrs = {"class": "paragraph"})
                if len(item['spans'] > 0):
                    for span in item['spans']:
                        if span['type'] == 'hyperlink':
                            url = span['data']['url']
                            modified_text = text[span['start']:[span['end']]]
                            a_tag = f'<a href="{url}">{modified_text}</a>'
                            # Unfinished logic
                
                new_tag.string = text
                soup.append(new_tag)
        
        return str(soup)   
1

There are 1 answers

1
Nounou On