]> xn--ix-yja.es Git - alex.git/commitdiff
Add links
authoralex <alex@pdp7.net>
Sun, 8 Oct 2023 18:48:47 +0000 (20:48 +0200)
committeralex <alex@pdp7.net>
Sun, 8 Oct 2023 18:48:47 +0000 (20:48 +0200)
* Prettify using lxml instead of bs4, because bs4 changes whitespace :(

blog_experiment/blog/blog_pages.py
blog_experiment/blog/html.py
blog_experiment/blog/meta.py
blog_experiment/blog/pretty.py [new file with mode: 0644]
blog_experiment/poetry.lock
blog_experiment/pyproject.toml

index b6f2208728f06175ff9707527ddee5bda3fd00bb..065e4881072709f79ef89e8acc9378f2ac501229 100644 (file)
@@ -3,15 +3,13 @@ import itertools
 import pathlib
 import textwrap
 
-import bs4
-
 import bicephalus
 
 import htmlgenerator as h
 
 from feedgen import feed
 
-from blog import html, page, gemtext, meta
+from blog import html, page, gemtext, meta, pretty
 
 
 class Entry:
@@ -152,7 +150,7 @@ class Root(page.BasePage):
             fe.published(datetime.datetime.combine(entry.posted, datetime.datetime.min.time(), tzinfo=datetime.timezone.utc))
             fe.title(entry.title)
             html = h.render(h.BaseElement(*entry.html()), {})
-            html = bs4.BeautifulSoup(html, features="html.parser").prettify()
+            html = pretty.pretty_html(html)
             fe.content(html, type="html")
 
         return bicephalus.Response(
index 25a2e82e08bddf3d5fd8199254311b07a8fc3a34..c7112ea8668974efc181b556b505c33a50f38e5a 100644 (file)
@@ -1,25 +1,35 @@
-import bs4
+import itertools
+
 import htmlgenerator as h
 
-from blog import meta
+from blog import meta, pretty
 
 
 def html_template(*content, page_title=None):
-    title = meta.TITLE
+    title = [h.A(meta.TITLE, href=meta.BASE_URL)]
     if page_title:
         title += f" - {page_title}"
-    return bs4.BeautifulSoup(h.render(
+
+    title = h.BaseElement(*title)
+
+    links = list(itertools.chain(*[(h.A(text, href=href), ", ") for text, href in meta.LINKS]))
+
+    links += h.BaseElement(f" {meta.EMAIL_TEXT}")
+
+    return pretty.pretty_html(h.render(
         h.HTML(
             h.HEAD(
-                h.TITLE(title),
+                h.TITLE(meta.TITLE + (f" - {page_title}" if page_title else "")),
                 h.LINK(rel="alternate", type="application/rss+xml", title=meta.TITLE, href=f"{meta.BASE_URL}/feed/"),
             ),
             h.BODY(
                 h.H1(title),
                 h.H2(meta.SUBTITLE),
+                h.P(*links),
                 *content,
             ),
             doctype="html",
         ),
         {},
-    ), features="html.parser").prettify()
+    ))
+
index 81c74a9d690b426607a4c58c5d32808f6f32912a..8ab088488071186590cee655e8fa2d6a0ce56397 100644 (file)
@@ -1,3 +1,12 @@
 TITLE = "El blog es mío"
 SUBTITLE = "Hay otros como él, pero este es el mío"
 BASE_URL = "https://blog.pdp7.net"
+
+LINKS = (
+    ("GitHub", "https://github.com/alexpdp7/"),
+    ("LinkedIn", "https://es.linkedin.com/in/alexcorcoles"),
+    ("Project Euler", "https://projecteuler.net/profile/koalillo.png"),
+    ("Stack Exchange", "https://stackexchange.com/users/13361/alex"),
+)
+
+EMAIL_TEXT = "escríbeme cogiendo el dominio de esta web y cambiando el primer punto por una arroba"
diff --git a/blog_experiment/blog/pretty.py b/blog_experiment/blog/pretty.py
new file mode 100644 (file)
index 0000000..2ae916a
--- /dev/null
@@ -0,0 +1,5 @@
+from lxml import etree, html
+
+
+def pretty_html(s):
+    return etree.tostring(html.fromstring(s), pretty_print=True).decode("utf8")
index 508ed169af7f70041131ec7584ca6f0094f4a0f7..57ce657d2efc8800c5c6200ac53580d8dce72a4f 100644 (file)
@@ -1321,4 +1321,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p
 [metadata]
 lock-version = "2.0"
 python-versions = "^3.9"
-content-hash = "d84139eb7e19e366ef33bb9e36f3c74ca432b9c5931d9f19d6fb1b8693400654"
+content-hash = "97fc5ab9e66a2dd243b910c6408cbf77194f61b070c35ef9d2aca9ea1c329c56"
index 61878a932228e5b8e2d1bc6af81d18fa2e1c23d6..b236e8ea62902896c04e3967f8a4fecd8596b119 100644 (file)
@@ -12,6 +12,7 @@ bicephalus = { git = "https://github.com/alexpdp7/bicephalus.git" }
 htmlgenerator = "^1.2.28"
 beautifulsoup4 = "^4.12.2"
 feedgen = "^0.9.0"
+lxml = "^4.9.3"
 
 [tool.poetry.group.dev.dependencies]
 pytest = "^7.4.2"