Skip to content

Commit ebd88f3

Browse files
committed
Convert b & i tags into span tags
1 parent ddfe5a4 commit ebd88f3

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

tbx/core/models.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
from modelcluster.fields import ParentalKey
66
from tbx.core.utils.fields import StreamField
7+
from tbx.core.utils.formatting import (
8+
convert_bold_links_to_pink,
9+
convert_italic_links_to_purple,
10+
)
711
from tbx.core.utils.models import (
812
ColourThemeMixin,
913
NavigationFields,
@@ -162,8 +166,9 @@ def partner_logos(self):
162166
"hero_introduction",
163167
heading="Introduction",
164168
help_text=mark_safe(
165-
'Use bold to mark text as <span style="color:#EE5276">pink</span>,'
166-
" and use italics to mark text as"
169+
"Use bold to mark links as"
170+
' <span style="color:#EE5276">pink</span>,'
171+
" and use italics to mark links as"
167172
' <span style="color:#6F60D0">purple</span>.'
168173
),
169174
),
@@ -193,6 +198,9 @@ def partner_logos(self):
193198
def get_context(self, request):
194199
context = super().get_context(request)
195200
context["is_home_page"] = True
201+
context["hero_introduction"] = convert_bold_links_to_pink(
202+
convert_italic_links_to_purple(self.hero_introduction)
203+
)
196204
return context
197205

198206

tbx/core/utils/formatting.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from bs4 import BeautifulSoup
2+
3+
4+
def convert_bold_links_to_pink(html_text):
5+
"""Convert <b> tags inside links into <span class="text-coral">."""
6+
soup = BeautifulSoup(html_text, "html.parser")
7+
for anchor_tag in soup.find_all("a"):
8+
for bold_tag in anchor_tag.find_all("b"):
9+
tag_content = "".join([str(c) for c in bold_tag.contents])
10+
html_text = html_text.replace(
11+
str(bold_tag), f'<span class="text-coral">{tag_content}</span>'
12+
)
13+
return html_text
14+
15+
16+
def convert_italic_links_to_purple(html_text):
17+
"""Convert <i> tags inside links into <span class="text-nebuline">."""
18+
soup = BeautifulSoup(html_text, "html.parser")
19+
for anchor_tag in soup.find_all("a"):
20+
for italic_tag in anchor_tag.find_all("i"):
21+
tag_content = "".join([str(c) for c in italic_tag.contents])
22+
html_text = html_text.replace(
23+
str(italic_tag), f'<span class="text-nebuline">{tag_content}</span>'
24+
)
25+
return html_text

tbx/project_styleguide/templates/patterns/pages/home/home_page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
{% include "patterns/molecules/home-page-hero/home-page-hero.html" %}
77

88
<div class="grid__intro heading heading--six">
9-
{{ page.hero_introduction|richtext }}
9+
{{ hero_introduction|richtext }}
1010
</div>
1111

1212
<div class="grid__partners">

0 commit comments

Comments
 (0)