From 64f84d58bc9f2cd6264b299bb2589af54fbaa9d5 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Mon, 2 Oct 2023 21:37:00 +0200 Subject: [PATCH] path markdown html; table mod --- docker/main/requirements.txt | 1 + gawa/blog/data/articles/de-bash-arrays.md | 5 +++++ gawa/blog/data/articles/en-bash-arrays.md | 5 +++++ gawa/blog/models.py | 21 +++++++++++++++++++-- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/docker/main/requirements.txt b/docker/main/requirements.txt index 99f0877..bad9a1b 100644 --- a/docker/main/requirements.txt +++ b/docker/main/requirements.txt @@ -9,3 +9,4 @@ favicon>=0.7.0 markdown>=3.4.4 Pygments>=2.16.1 toml>=0.10 +beautifulsoup4>=4.12.2 diff --git a/gawa/blog/data/articles/de-bash-arrays.md b/gawa/blog/data/articles/de-bash-arrays.md index 1b84e4c..37c5b49 100644 --- a/gawa/blog/data/articles/de-bash-arrays.md +++ b/gawa/blog/data/articles/de-bash-arrays.md @@ -9,6 +9,11 @@ markdown rendering. [TOC] +| very | important | table | +|--------|-----------|-----------| +| v | i | t | +| yes | super | important | +| really | really | really | ## Wait, but why? diff --git a/gawa/blog/data/articles/en-bash-arrays.md b/gawa/blog/data/articles/en-bash-arrays.md index 1fa7e64..536665b 100644 --- a/gawa/blog/data/articles/en-bash-arrays.md +++ b/gawa/blog/data/articles/en-bash-arrays.md @@ -8,6 +8,11 @@ markdown rendering. [TOC] +| very | important | table | +|--------|-----------|-----------| +| v | i | t | +| yes | super | important | +| really | really | really | ## Wait, but why? diff --git a/gawa/blog/models.py b/gawa/blog/models.py index 89bc886..9f218c6 100644 --- a/gawa/blog/models.py +++ b/gawa/blog/models.py @@ -4,6 +4,7 @@ import re import os import pathlib import markdown +from bs4 import BeautifulSoup from django.db import models from django.utils.translation import gettext as _ from start.models import Keyword, Searchable @@ -111,6 +112,22 @@ class BlogPost(Searchable): # redundand vvvv self.save() + @staticmethod + def __patch_html(html: str) -> str: + soup = BeautifulSoup(html) + + # add bootstrap classes to regular tables + tables = soup.select("table") + for table in tables: + if 'class' in table.attrs and "codehilitetable" in table.attrs['class']: + # table has at least one class already AND + # this table is a codehighlighting table, + # not a regular one + continue + # set the bootstrap classes for tables + table.attrs['class'] = 'table table-striped border' + return soup.prettify() + def sync_file(self): """ generate an article fromm it's original markdown file @@ -155,12 +172,12 @@ class BlogPost(Searchable): self.title_en = data['lang'][lang]["title"] self.subtitle_en = data['lang'][lang]["subtitle"] self.desc_en = data['lang'][lang]["desc"] - self.body_en = MD.convert(body) + self.body_en = BlogPost.__patch_html(MD.convert(body)) case "de": self.title_de = data['lang'][lang]["title"] self.subtitle_de = data['lang'][lang]["subtitle"] self.desc_de = data['lang'][lang]["desc"] - self.body_de = MD.convert(body) + self.body_de = BlogPost.__patch_html(MD.convert(body)) case _: logger.error( f"unknown language '{lang}' in meta file for '{self}'") -- 2.40.1