Fix article description
This commit is contained in:
parent
bb3083b9d5
commit
cbef85ffbd
4 changed files with 5 additions and 27 deletions
14
poetry.lock
generated
14
poetry.lock
generated
|
@ -296,14 +296,6 @@ gevent = ["gevent (>=1.4.0)"]
|
||||||
setproctitle = ["setproctitle"]
|
setproctitle = ["setproctitle"]
|
||||||
tornado = ["tornado (>=0.2)"]
|
tornado = ["tornado (>=0.2)"]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "html2text"
|
|
||||||
version = "2020.1.16"
|
|
||||||
description = "Turn HTML into equivalent Markdown-structured text."
|
|
||||||
category = "main"
|
|
||||||
optional = false
|
|
||||||
python-versions = ">=3.5"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "identify"
|
name = "identify"
|
||||||
version = "2.4.0"
|
version = "2.4.0"
|
||||||
|
@ -844,7 +836,7 @@ multidict = ">=4.0"
|
||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "1.1"
|
lock-version = "1.1"
|
||||||
python-versions = "^3.10"
|
python-versions = "^3.10"
|
||||||
content-hash = "30053d4662f7e86ae956249a199beeaed34b338fe420d09ad2db0ae4f9d2d8bd"
|
content-hash = "de3fbd11622b6f872fc18cd21b78878fe8e0b3e8faa54527400fe98f0ecb6e34"
|
||||||
|
|
||||||
[metadata.files]
|
[metadata.files]
|
||||||
asgiref = [
|
asgiref = [
|
||||||
|
@ -1015,10 +1007,6 @@ gunicorn = [
|
||||||
{file = "gunicorn-20.1.0-py3-none-any.whl", hash = "sha256:9dcc4547dbb1cb284accfb15ab5667a0e5d1881cc443e0677b4882a4067a807e"},
|
{file = "gunicorn-20.1.0-py3-none-any.whl", hash = "sha256:9dcc4547dbb1cb284accfb15ab5667a0e5d1881cc443e0677b4882a4067a807e"},
|
||||||
{file = "gunicorn-20.1.0.tar.gz", hash = "sha256:e0a968b5ba15f8a328fdfd7ab1fcb5af4470c28aaf7e55df02a99bc13138e6e8"},
|
{file = "gunicorn-20.1.0.tar.gz", hash = "sha256:e0a968b5ba15f8a328fdfd7ab1fcb5af4470c28aaf7e55df02a99bc13138e6e8"},
|
||||||
]
|
]
|
||||||
html2text = [
|
|
||||||
{file = "html2text-2020.1.16-py3-none-any.whl", hash = "sha256:c7c629882da0cf377d66f073329ccf34a12ed2adf0169b9285ae4e63ef54c82b"},
|
|
||||||
{file = "html2text-2020.1.16.tar.gz", hash = "sha256:e296318e16b059ddb97f7a8a1d6a5c1d7af4544049a01e261731d2d5cc277bbb"},
|
|
||||||
]
|
|
||||||
identify = [
|
identify = [
|
||||||
{file = "identify-2.4.0-py2.py3-none-any.whl", hash = "sha256:eba31ca80258de6bb51453084bff4a923187cd2193b9c13710f2516ab30732cc"},
|
{file = "identify-2.4.0-py2.py3-none-any.whl", hash = "sha256:eba31ca80258de6bb51453084bff4a923187cd2193b9c13710f2516ab30732cc"},
|
||||||
{file = "identify-2.4.0.tar.gz", hash = "sha256:a33ae873287e81651c7800ca309dc1f84679b763c9c8b30680e16fbfa82f0107"},
|
{file = "identify-2.4.0.tar.gz", hash = "sha256:a33ae873287e81651c7800ca309dc1f84679b763c9c8b30680e16fbfa82f0107"},
|
||||||
|
|
|
@ -15,7 +15,6 @@ django-anymail = {version = "^8.4", extras = ["mailgun"]}
|
||||||
pillow = "^8.4"
|
pillow = "^8.4"
|
||||||
django-cleanup = "^5.0"
|
django-cleanup = "^5.0"
|
||||||
requests = "^2.24"
|
requests = "^2.24"
|
||||||
html2text = "^2020.1.16"
|
|
||||||
readtime = "^1.1.1"
|
readtime = "^1.1.1"
|
||||||
pylibmc = "^1.6.1"
|
pylibmc = "^1.6.1"
|
||||||
django-debug-toolbar = "^3.2"
|
django-debug-toolbar = "^3.2"
|
||||||
|
@ -23,6 +22,7 @@ whitenoise = {extras = ["brotli"], version = "^5.2.0"}
|
||||||
rcssmin = "^1.0.6"
|
rcssmin = "^1.0.6"
|
||||||
django-csp = "^3.7"
|
django-csp = "^3.7"
|
||||||
django-two-factor-auth = {extras = ["phonenumberslite"], git = "https://github.com/Bouke/django-two-factor-auth.git", rev = "ffe4422e"}
|
django-two-factor-auth = {extras = ["phonenumberslite"], git = "https://github.com/Bouke/django-two-factor-auth.git", rev = "ffe4422e"}
|
||||||
|
beautifulsoup4 = "^4.10.0"
|
||||||
|
|
||||||
[tool.poetry.dev-dependencies]
|
[tool.poetry.dev-dependencies]
|
||||||
pre-commit = "^2.7"
|
pre-commit = "^2.7"
|
||||||
|
|
|
@ -4,6 +4,7 @@ from functools import cached_property
|
||||||
|
|
||||||
import rcssmin
|
import rcssmin
|
||||||
import readtime
|
import readtime
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.auth.models import AbstractUser
|
from django.contrib.auth.models import AbstractUser
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
|
@ -16,7 +17,6 @@ from django.utils import timezone
|
||||||
from articles.utils import (
|
from articles.utils import (
|
||||||
build_full_absolute_url,
|
build_full_absolute_url,
|
||||||
format_article_content,
|
format_article_content,
|
||||||
get_html_to_text_converter,
|
|
||||||
truncate_words_after_char_count,
|
truncate_words_after_char_count,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -88,8 +88,8 @@ class Article(models.Model):
|
||||||
@cached_property
|
@cached_property
|
||||||
def get_description(self):
|
def get_description(self):
|
||||||
html = self.get_formatted_content
|
html = self.get_formatted_content
|
||||||
converter = get_html_to_text_converter()
|
bs = BeautifulSoup(html, "html.parser")
|
||||||
text = converter.handle(html)
|
text = bs.find("p", recursive=False).text
|
||||||
return truncate_words_after_char_count(text, 160)
|
return truncate_words_after_char_count(text, 160)
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import re
|
import re
|
||||||
|
|
||||||
import html2text
|
|
||||||
import markdown
|
import markdown
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from markdown.extensions.codehilite import CodeHiliteExtension
|
from markdown.extensions.codehilite import CodeHiliteExtension
|
||||||
|
@ -37,12 +36,3 @@ def truncate_words_after_char_count(text, char_count):
|
||||||
text_result.append(word)
|
text_result.append(word)
|
||||||
total_length += len(word) + 1
|
total_length += len(word) + 1
|
||||||
return " ".join(text_result) + "..."
|
return " ".join(text_result) + "..."
|
||||||
|
|
||||||
|
|
||||||
def get_html_to_text_converter():
|
|
||||||
converter = html2text.HTML2Text()
|
|
||||||
converter.ignore_images = True
|
|
||||||
converter.ignore_links = True
|
|
||||||
converter.ignore_tables = True
|
|
||||||
converter.ignore_emphasis = True
|
|
||||||
return converter
|
|
||||||
|
|
Reference in a new issue