summaryrefslogtreecommitdiffstats
path: root/nikola/utils.py
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@unit193.net>2022-04-20 00:12:09 -0400
committerLibravatarUnit 193 <unit193@unit193.net>2022-04-20 00:12:09 -0400
commit942e313727d1ad886a1024c24fe4a9e8e2e0bb3e (patch)
tree1c4d5d826655cdb812c88563a25410f8b54e41d2 /nikola/utils.py
parent8eeed31eb2f86ac982fa4b26f93b15828289c56d (diff)
New upstream version 8.2.0.upstream/8.2.0
Diffstat (limited to 'nikola/utils.py')
-rw-r--r--nikola/utils.py20
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()