mirror of
https://github.com/tiyn/beaker-blog.git
synced 2025-04-03 16:07:45 +02:00
make preview use the first paragraph
This commit is contained in:
parent
070be5b0e2
commit
a862ac0966
@ -3,6 +3,7 @@ import os
|
|||||||
import pathlib
|
import pathlib
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from os import path
|
from os import path
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
import markdown
|
import markdown
|
||||||
|
|
||||||
@ -233,20 +234,12 @@ def create_preview(path, is_markdown):
|
|||||||
string: html-formated preview
|
string: html-formated preview
|
||||||
"""
|
"""
|
||||||
file = open(path, "r", encoding="utf-8")
|
file = open(path, "r", encoding="utf-8")
|
||||||
first_lines = file.readlines()
|
lines = file.read()
|
||||||
preview = ""
|
|
||||||
preview_length = 3
|
|
||||||
for i, line in enumerate(first_lines):
|
|
||||||
if i == 0:
|
|
||||||
continue
|
|
||||||
if i > preview_length:
|
|
||||||
break
|
|
||||||
if not line.isspace():
|
|
||||||
if is_markdown:
|
if is_markdown:
|
||||||
preview += markdown.markdown(line)
|
lines += markdown.markdown(lines)
|
||||||
else:
|
preview = ""
|
||||||
preview += line
|
first_p = BeautifulSoup(lines).find('p')
|
||||||
else:
|
if first_p is not None:
|
||||||
preview_length += 1
|
preview = "\n<p>" + first_p.text + "</p>\n"
|
||||||
preview += "<br>...<br>"
|
preview += "...<br>"
|
||||||
return preview
|
return preview
|
||||||
|
@ -4,3 +4,4 @@ Whoosh
|
|||||||
WTForms
|
WTForms
|
||||||
Flask_WTF
|
Flask_WTF
|
||||||
Font-Awesome-Flask
|
Font-Awesome-Flask
|
||||||
|
BeautifulSoup4
|
||||||
|
Loading…
x
Reference in New Issue
Block a user