This repository has been archived on 2023-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
python-blog/src/articles/utils.py

51 lines
1.4 KiB
Python
Raw Normal View History

2021-01-03 21:51:46 +01:00
import re
import markdown
from bs4 import BeautifulSoup
from django.conf import settings
2021-01-03 21:51:46 +01:00
from markdown.extensions.codehilite import CodeHiliteExtension
2021-12-28 12:40:42 +01:00
from markdown.extensions.toc import TocExtension
2021-01-03 21:51:46 +01:00
from articles.markdown import LazyLoadingImageExtension
2021-12-28 22:31:53 +01:00
def build_full_absolute_url(request, url: str) -> str:
if request:
return request.build_absolute_uri(url)
else:
return (settings.BLOG["base_url"] + url)[::-1].replace("//", "/", 1)[::-1]
2021-01-03 21:51:46 +01:00
2021-12-28 22:31:53 +01:00
def format_article_content(content: str) -> str:
2021-01-03 21:51:46 +01:00
md = markdown.Markdown(
extensions=[
"extra",
"admonition",
2021-12-28 12:40:42 +01:00
TocExtension(anchorlink=True),
CodeHiliteExtension(linenums=False, guess_lang=False),
2021-01-03 21:51:46 +01:00
LazyLoadingImageExtension(),
]
)
content = re.sub(r"(\s)#(\w+)", r"\1\#\2", content)
return md.convert(content)
2021-12-28 22:31:53 +01:00
def truncate_words_after_char_count(text: str, char_count: int) -> str:
2021-01-03 21:51:46 +01:00
total_length = 0
text_result = []
for word in text.split():
if len(word) + 1 + total_length > char_count:
break
text_result.append(word)
total_length += len(word) + 1
return " ".join(text_result) + "..."
2021-12-28 22:31:53 +01:00
def find_first_paragraph_with_text(html: str) -> str:
bs = BeautifulSoup(html, "html.parser")
2021-12-28 22:31:53 +01:00
paragraphs = bs.find_all("p", recursive=False)
for paragraph in paragraphs:
if paragraph.text.strip():
return paragraph.text
return ""