我想为我的主页创建内容片段。示例帖子看起来像
<p>Your favorite Harry Potter characters enter the Game of Thrones
universe, and you'll never guess what happens!</p>
<readmore/>
<p>...they all die</p>
在首页我只想要之前的东西<readmore/>
现身。我想我可以在 Jinja 过滤器中使用 Beautiful Soup 来删除阅读更多内容以及其后的所有内容。如果没有,它应该在第一个换行符或段落结尾处剪辑<readmore/>
存在。
我怎样才能做到这一点?
没有必要使用美丽汤。只需检查是否<readmore/>
或者文本中存在其他一些子字符串,并在其上拆分,或者如果不存在则在换行符上拆分。
from markupsafe import Markup
@app.template_filter()
def snippet(value):
for sep in ('<readmore/>', '<br/>', '<br>', '</p>'):
if sep in value:
break
else:
sep = '\n'
return Markup(value.split(sep, 1)[0])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)