diff options
| author | 2022-04-20 00:12:09 -0400 | |
|---|---|---|
| committer | 2022-04-20 00:12:09 -0400 | |
| commit | 942e313727d1ad886a1024c24fe4a9e8e2e0bb3e (patch) | |
| tree | 1c4d5d826655cdb812c88563a25410f8b54e41d2 /nikola/utils.py | |
| parent | 8eeed31eb2f86ac982fa4b26f93b15828289c56d (diff) | |
New upstream version 8.2.0.upstream/8.2.0
Diffstat (limited to 'nikola/utils.py')
| -rw-r--r-- | nikola/utils.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/nikola/utils.py b/nikola/utils.py index 54cd36f..363ab40 100644 --- a/nikola/utils.py +++ b/nikola/utils.py @@ -30,6 +30,7 @@ import configparser import datetime import hashlib import io +import lxml.html import operator import os import re @@ -656,6 +657,25 @@ def get_theme_chain(theme, themes_dirs): return themes +def html_tostring_fragment(document): + """Convert a HTML snippet to a fragment, ready for insertion elsewhere.""" + try: + doc = lxml.html.tostring(document.body, encoding='unicode').strip() + except Exception: + doc = lxml.html.tostring(document, encoding='unicode').strip() + start_fragments = ["<html>", "<body>"] + end_fragments = ["</body>", "</html>"] + for start in start_fragments: + if doc.startswith(start): + doc = doc[len(start):].strip() + print(repr(doc)) + for end in end_fragments: + if doc.endswith(end): + doc = doc[:-len(end)].strip() + print(repr(doc)) + return doc + + INCOMPLETE_LANGUAGES_WARNED = set() |
