On Nov 29, 11:35 am, Alex Jonsson <[EMAIL PROTECTED]> wrote:
> Hey everyone,
>
> I could use some help with my application. When I present my blog
> posts in my template, I would like to make the three first words of
> every paragraph bold. How would be the easiest way to do this?
>
> I've already declared a template tag, but I'm clueless about the code.
> The post is passed in HTML format, so the most logical thing would be
> to split the paragraphs by </p><p> perhaps, and then ... yeah then
> what :)?
>
> Thanks,
> Alex
You probably want to use BeautifulSoup to get the paragraphs, then
normal Python string handling to get the first three words of each
one. Something like (untested - and using a filter rather than a tag):
@register.filter
def boldify(text):
parsed_text = BeautifulSoup.BeautifulSoup(text)
for p in parsed_text.findChildren('p'):
text = p.string.split(' ')
replacetext = '<p><b> + ' '.join(text[:3]) + '</b>' + ' '.join
(text[3:]) + '</p>'
p.replaceWith(replacetext)
return parsed_text.renderContents()
--
DR.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---