From 3d578129d1ac5092f5e67edc49b395564fb14ae4 Mon Sep 17 00:00:00 2001 From: Christian Moser Date: Thu, 25 Dec 2025 15:47:21 +0100 Subject: [PATCH] 2025.12.25 15:47:21 (cachyos.cmoser.eu) --- templates/base.html | 2 +- ...ntext_processors => context_processors.py} | 4 +- ...004_sidebar.py => 0004_default_sidebar.py} | 1 + tinywiki/parser/bbcode/formatters.py | 2 + tinywiki/parser/bbcode/simple_formatters.py | 26 +-- tinywiki/parser/bbcode/text_formatters.py | 162 +++++++++++------- 6 files changed, 123 insertions(+), 74 deletions(-) rename tinywiki/{context_processors => context_processors.py} (80%) rename tinywiki/migrations/{0004_sidebar.py => 0004_default_sidebar.py} (98%) diff --git a/templates/base.html b/templates/base.html index 96f11d6..f3009dd 100644 --- a/templates/base.html +++ b/templates/base.html @@ -68,7 +68,7 @@
{% block left_sidebar %} {{ tinywiki_sidebar }} - {% enblock left_sidebar %} + {% endblock left_sidebar %}
diff --git a/tinywiki/context_processors b/tinywiki/context_processors.py similarity index 80% rename from tinywiki/context_processors rename to tinywiki/context_processors.py index 6878c17..6154842 100644 --- a/tinywiki/context_processors +++ b/tinywiki/context_processors.py @@ -2,9 +2,9 @@ from django.http import HttpRequest from django.utils.safestring import mark_safe from .models import SidebarSection -def sidebar(request: HttpRequest): +def sidebar(request: HttpRequest = None): sections = [ section.widget - for section in SidebarSection.objects.filter(is_visible=True).order_by('-priority'): + for section in SidebarSection.objects.filter(is_visible=True).order_by('-priority') ] return {'tinywiki_sidebar': mark_safe("\n".join(sections))} diff --git a/tinywiki/migrations/0004_sidebar.py b/tinywiki/migrations/0004_default_sidebar.py similarity index 98% rename from tinywiki/migrations/0004_sidebar.py rename to tinywiki/migrations/0004_default_sidebar.py index 694efb4..3b1ccc7 100644 --- a/tinywiki/migrations/0004_sidebar.py +++ b/tinywiki/migrations/0004_default_sidebar.py @@ -89,4 +89,5 @@ class Migration(migrations.Migration): ] operations = [ + migrations.RunPython(create_tinywiki_sections), ] diff --git a/tinywiki/parser/bbcode/formatters.py b/tinywiki/parser/bbcode/formatters.py index d540a1f..d88c206 100644 --- a/tinywiki/parser/bbcode/formatters.py +++ b/tinywiki/parser/bbcode/formatters.py @@ -21,6 +21,8 @@ from .simple_formatters import SIMPLE_FORMATTERS # noqa: F401 # a list of tuples containig an tuple args and a dict of kwargs # a list of tuples containing an tuple of args and a dict of kwargs + + FORMATTERS=[ ( ('url', render_url), diff --git a/tinywiki/parser/bbcode/simple_formatters.py b/tinywiki/parser/bbcode/simple_formatters.py index 2d723f3..63b5da1 100644 --- a/tinywiki/parser/bbcode/simple_formatters.py +++ b/tinywiki/parser/bbcode/simple_formatters.py @@ -1,16 +1,16 @@ SIMPLE_FORMATTERS = [ - (('h1',"

%(value)s

"),{}), - (('h2',"

%(value)s

"),{}), - (('h3',"

%(value)s

"),{}), - (('h4',"

%(value)s

"),{}), - (('h5',"
%(value)s
"),{}), - (('h6',"
%(value)s
"),{}), - (('mark',"%(value)s"),{}), - (("strong","%(value)s"),{}), - (("em","%(value)s"),{}), - (("br","
"),{"standalone":True}), - (('copy',"©"),{'standalone':True}), - (('reg',"®"),{'standalone':True}), - (('trade',"™"),{'standalone':True}), + (('h1', "

%(value)s

"), {}), + (('h2', "

%(value)s

"), {}), + (('h3', "

%(value)s

"), {}), + (('h4', "

%(value)s

"), {}), + (('h5', "
%(value)s
"), {}), + (('h6', "
%(value)s
"), {}), + (('mark', "%(value)s"), {}), + (("strong","%(value)s"), {}), + (("em", "%(value)s"), {}), + (("br", "
"), {"standalone":True}), + (('copy', "©"), {'standalone':True}), + (('reg', "®"), {'standalone':True}), + (('trade', "™"), {'standalone':True}), ] \ No newline at end of file diff --git a/tinywiki/parser/bbcode/text_formatters.py b/tinywiki/parser/bbcode/text_formatters.py index 8758f09..7580c7e 100644 --- a/tinywiki/parser/bbcode/text_formatters.py +++ b/tinywiki/parser/bbcode/text_formatters.py @@ -5,10 +5,10 @@ from django.utils.translation import gettext as _ from ... import settings from ... import models -import bbcode +#import bbcode -def render_url(tag_name:str,value,options,parent,context): +def render_url(tag_name: str, value, options, parent, context) -> str: try: url = options['url'] except KeyError: @@ -19,15 +19,15 @@ def render_url(tag_name:str,value,options,parent,context): if settings.USE_BOOTSTRAP: if ['noicon in options']: - return f"{value}" - return f"{value}" - return f"{value}" + return f"{value}" # noqa: E501 + return f"{value}" # noqa: E501 + return f"{value}" # noqa: E501 -def render_wiki_url(tag_name,value,options,parent,context): +def render_wiki_url(tag_name: str, value, options, parent, context) -> str: if tag_name in options: - url = reverse("tinywiki:page",kwargs={'slug':options[tag_name]}) - slug=options[tag_name] + url = reverse("tinywiki:page", kwargs={'slug': options[tag_name]}) + slug = options[tag_name] try: page = models.Page.objects.get(slug=slug) except models.Page.DoesNotExist: @@ -35,7 +35,7 @@ def render_wiki_url(tag_name,value,options,parent,context): else: url = reverse('tinywiki:home') - slug=None + slug = None if settings.USE_BOOTSTRAP: href = settings.settings.STATIC_URL+"tinywiki/icons/bootstrap-icons.svg" @@ -45,15 +45,15 @@ def render_wiki_url(tag_name,value,options,parent,context): elif page.slug: svg = "book" else: - svg=href + "file-earmark-x" - return f"{value}" + svg = href + "file-earmark-x" + return f"{value}" # noqa: E501 return f"{value}" -def render_wiki_link(tag_name,value,options,parent,context): +def render_wiki_link(tag_name: str, value, options, parent, context): if tag_name in options: slug = options[tag_name] - print("slug",slug) + print("slug", slug) try: page = models.Page.objects.get(slug=slug) title = page.title @@ -65,7 +65,7 @@ def render_wiki_link(tag_name,value,options,parent,context): page = None title = _("Page not found") svg = "file-earmark-x" - url = reverse("tinywiki:page",kwargs={'slug':slug}) + url = reverse("tinywiki:page", kwargs={'slug': slug}) else: slug = None title = _("Home") @@ -74,35 +74,35 @@ def render_wiki_link(tag_name,value,options,parent,context): if settings.USE_BOOTSTRAP: href = settings.settings.STATIC_URL + "tinywiki/icons/bootstrap-icons.svg" - return f"{title}" + return f"{title}" # noqa: E501 return f"{value}" -def render_codeblock(tag_name:str,value,options,parent,context)->str: +def render_codeblock(tag_name: str, value, options, parent, context) -> str: if tag_name in options: - return f"
{value}
" - return f"
{value}
" + return f"
{value}
" # noqa: E501 + return f"
{value}
" -def render_ordered_list(tag_name:str,value,options,parent,context)->str: +def render_ordered_list(tag_name: str, value, options, parent, context) -> str: return f"
    {value}
" -def render_unordered_list(tag_name:str,value,options,parent,context)->str: +def render_unordered_list(tag_name: str, value, options, parent, context) -> str: # noqa: E501 return f"
    {value}
" -def render_list_item(tag_name:str,value,options,parent,context)->str: +def render_list_item(tag_name: str, value, options, parent, context) -> str: return f"
  • {value}
  • " -def render_paragraph(tag_name: str, value, options, parent, context): +def render_paragraph(tag_name: str, value, options, parent, context) -> str: if settings.USE_BOOTSTRAP: return f"

    {value}

    " return f"

    {value}

    " -def render_image(tag_name: str, value, options, parent, context): +def render_image(tag_name: str, value, options, parent, context) -> str: if tag_name not in options: return "" @@ -187,9 +187,9 @@ def render_image(tag_name: str, value, options, parent, context): else: fig_style = "" if settings.USE_BOOTSTRAP: - return f'
    {alt}
    { value }
    ' + return f'
    {alt}
    {value}
    ' # noqa: E501 else: - return f'
    {value}
    ' + return f'
    {value}
    ' # noqa: E501 def render_wiki_image(tag_name: str, value, options, parent, context): @@ -202,8 +202,8 @@ def render_wiki_image(tag_name: str, value, options, parent, context): return "" if settings.USE_BOOTSTRAP: - classes = ["img-fluid","figure-img","rounded"] - fig_classes = ["figure","my-1"] + classes = ["img-fluid", "figure-img", "rounded"] + fig_classes = ["figure", "my-1"] styles = [] fig_styles = [] else: @@ -237,9 +237,9 @@ def render_wiki_image(tag_name: str, value, options, parent, context): fig_styles.append(f"height:{_h};") else: if _h.endswith('%'): - _h= _h[:-1] + _h = _h[:-1] if _h.isdigit(): - _h=int(_w) + _h = int(_w) if _h > 100: _h = 100 if settings.USE_BOOTSTRAP: @@ -257,79 +257,125 @@ def render_wiki_image(tag_name: str, value, options, parent, context): if "position" in options: pos = options['position'] if settings.USE_BOOTSTRAP: - if pos == "left" or pos=="start": - fig_classes += ["float-start","me-2"] + if pos == "left" or pos == "start": + fig_classes += ["float-start", "me-2"] elif pos == "right" or pos == "end": - fig_classes += ["float-end","ms-2"] + fig_classes += ["float-end", "ms-2"] elif pos == "center": - fig_classes += ["mx-auto","d-block"] + fig_classes += ["mx-auto", "d-block"] if styles: - style=f"style=\"{"".join(styles)}\"" + style = f"style=\"{"".join(styles)}\"" else: - style="" + style = "" if fig_styles: - fig_style=f'style="{"".join(fig_styles)}"' + fig_style = f'style="{"".join(fig_styles)}"' else: - fig_style="" + fig_style = "" if settings.USE_BOOTSTRAP: - return f'
    {image.alt}
    {image.description_html}
    ' + return f'
    {image.alt}
    {image.description_html}
    ' # noqa: E501 else: - return f'
    {image.alt}
    {image.description}
    ' + return f'
    {image.alt}
    {image.description}
    ' # noqa: E501 -def render_table(tag_name:str,value,options,parent,context): +def render_table(tag_name: str, value, options, parent, context) -> str: if settings.USE_BOOTSTRAP: - classes=["table"] + classes = ["table"] if "bordered" in options: - if options["bordered"] not in ("0","n","no","false","off"): + if options["bordered"] not in ("0", "n", "no", "false", "off"): classes.append("table-bordered") - if options["bordered"] in ("primary","secondary","info","warning","danger","success","light","dark"): + if options["bordered"] in ( + "primary", + "secondary", + "info", + "warning", + "danger", + "success", + "light", + "dark" + ): classes.append(f"border-{options['bordered']}") if tag_name in options: - if options[tag_name] in ("primary","secondary","info","warning","danger","success","light","dark"): + if options[tag_name] in ( + "primary", + "secondary", + "info", + "warning", + "danger", + "success", + "light", + "dark" + ): classes.append(f"table-{options[tag_name]}") return f"{value}
    " return f"{value}
    " -def render_table_row(tag_name:str,value,options,parent,context): +def render_table_row(tag_name: str, value, options, parent, context) -> str: classes=[] if settings.USE_BOOTSTRAP: if tag_name in options: - if options[tag_name] in ("primary","secondary","info","warning","danger","success","light","dark"): + if options[tag_name] in ( + "primary", + "secondary", + "info", + "warning", + "danger", + "success", + "light", + "dark" + ): classes.append(f"table-{options[tag_name]}") - class_attr=f"class=\"{" ".join(classes)}\"" if classes else "" + class_attr = f"class=\"{" ".join(classes)}\"" if classes else "" return f"{value}" -def render_table_header(tag_name:str,value,options,parent,context): - extra_attributes=[] - classes=[] + +def render_table_header(tag_name: str, value, options, parent, context) -> str: + extra_attributes = [] + classes = [] if "colspan" in options: extra_attributes.append(f"colspan=\"{options['colspan']}\"") if "rowspan" in options: extra_attributes.append(f"rowspan=\"{options['rowspan']}\"") if settings.USE_BOOTSTRAP: if tag_name in options: - if options[tag_name] in ("primary","secondary","info","warning","danger","success","light","dark"): + if options[tag_name] in ( + "primary", + "secondary", + "info", + "warning", + "danger", + "success", + "light", + "dark" + ): classes.append(f"table-{options[tag_name]}") - class_attr=f"class=\"{" ".join(classes)}\"" if classes else "" + class_attr = f"class=\"{" ".join(classes)}\"" if classes else "" return f"{value}" -def render_table_data(tag_name:str,value,options,parent,context): - extra_attributes=[] - classes=[] +def render_table_data(tag_name: str, value, options, parent, context) -> str: + extra_attributes = [] + classes = [] if "colspan" in options: extra_attributes.append(f"colspan=\"{options['colspan']}\"") if "rowspan" in options: extra_attributes.append(f"rowspan=\"{options['rowspan']}\"") if settings.USE_BOOTSTRAP: if tag_name in options: - if options[tag_name] in ("primary","secondary","info","warning","danger","success","light","dark"): + if options[tag_name] in ( + "primary", + "secondary", + "info", + "warning", + "danger", + "success", + "light", + "dark" + ): classes.append(f"table-{options[tag_name]}") class_attr = f"class=\"{" ".join(classes)}\"" if classes else "" return f"{value}" @@ -369,7 +415,7 @@ def render_youtube_video(tag_name: str, value, options, parent, context): if _w.endswith('%'): _w = _w[:-1] if _w.isdigit(): - _w=int(_w) + _w = int(_w) if _w > 100: _w = 100 if settings.USE_BOOTSTRAP: @@ -430,7 +476,7 @@ def render_youtube_video(tag_name: str, value, options, parent, context): div_style = "" if settings.USE_BOOTSTRAP: return f"""
    - +
    """ else: return f'
    '