mirror of
https://codeberg.org/rimu/pyfedi
synced 2025-02-03 00:31:25 -08:00
generate titles from microblog posts using the first sentence of the first paragraph
(limited to 150 chars, as before)
This commit is contained in:
parent
76d228f5fa
commit
4d7acb9396
1 changed files with 14 additions and 20 deletions
34
app/utils.py
34
app/utils.py
|
@ -247,28 +247,22 @@ def markdown_to_text(markdown_text) -> str:
|
||||||
def microblog_content_to_title(html: str) -> str:
|
def microblog_content_to_title(html: str) -> str:
|
||||||
soup = BeautifulSoup(html, 'html.parser')
|
soup = BeautifulSoup(html, 'html.parser')
|
||||||
|
|
||||||
title_found = False
|
title = ''
|
||||||
for tag in soup.find_all():
|
for tag in soup.find_all('p'):
|
||||||
if tag.name == 'p':
|
title = tag.get_text()
|
||||||
if not title_found:
|
break
|
||||||
title_found = True
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
tag = tag.extract()
|
|
||||||
else:
|
|
||||||
tag = tag.extract()
|
|
||||||
|
|
||||||
if title_found:
|
period_index = title.find('.')
|
||||||
result = soup.text
|
if period_index != -1:
|
||||||
if len(result) > 150:
|
title = title[:period_index]
|
||||||
for i in range(149, -1, -1):
|
|
||||||
if result[i] == ' ':
|
|
||||||
break;
|
|
||||||
result = result[:i] + ' ...' if i > 0 else ''
|
|
||||||
else:
|
|
||||||
result = ''
|
|
||||||
|
|
||||||
return result
|
if len(title) > 150:
|
||||||
|
for i in range(149, -1, -1):
|
||||||
|
if title[i] == ' ':
|
||||||
|
break
|
||||||
|
title = title[:i] + ' ...' if i > 0 else ''
|
||||||
|
|
||||||
|
return title
|
||||||
|
|
||||||
|
|
||||||
def community_link_to_href(link: str) -> str:
|
def community_link_to_href(link: str) -> str:
|
||||||
|
|
Loading…
Add table
Reference in a new issue