All files for deployment ready and tested - further development in this repo.
This commit is contained in:
0
abschnitte/__init__.py
Normal file
0
abschnitte/__init__.py
Normal file
3
abschnitte/admin.py
Normal file
3
abschnitte/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
abschnitte/apps.py
Normal file
6
abschnitte/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AbschnitteConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'abschnitte'
|
||||
23
abschnitte/migrations/0001_initial.py
Normal file
23
abschnitte/migrations/0001_initial.py
Normal file
@@ -0,0 +1,23 @@
|
||||
# Generated by Django 5.2.5 on 2025-08-26 09:34
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='AbschnittTyp',
|
||||
fields=[
|
||||
('abschnitttyp', models.CharField(max_length=100, primary_key=True, serialize=False)),
|
||||
],
|
||||
options={
|
||||
'verbose_name_plural': 'Abschnitttypen',
|
||||
},
|
||||
),
|
||||
]
|
||||
0
abschnitte/migrations/__init__.py
Normal file
0
abschnitte/migrations/__init__.py
Normal file
23
abschnitte/models.py
Normal file
23
abschnitte/models.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from django.db import models
|
||||
|
||||
class AbschnittTyp(models.Model):
|
||||
abschnitttyp = models.CharField(max_length=100, primary_key=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.abschnitttyp
|
||||
|
||||
class Meta:
|
||||
verbose_name_plural = "Abschnitttypen"
|
||||
|
||||
|
||||
class Textabschnitt(models.Model):
|
||||
abschnitttyp = models.ForeignKey(
|
||||
AbschnittTyp, on_delete=models.PROTECT, blank=True, null=True
|
||||
)
|
||||
inhalt = models.TextField(blank=True, null=True)
|
||||
order=models.PositiveIntegerField(default=0)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
verbose_name = "Abschnitt"
|
||||
verbose_name_plural = "Abschnitte"
|
||||
3
abschnitte/tests.py
Normal file
3
abschnitte/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
79
abschnitte/utils.py
Normal file
79
abschnitte/utils.py
Normal file
@@ -0,0 +1,79 @@
|
||||
from markdown import markdown
|
||||
import base64
|
||||
import zlib
|
||||
import re
|
||||
from textwrap import dedent
|
||||
|
||||
DIAGRAMMSERVER="http://10.128.128.144:8000/diagramm"
|
||||
|
||||
def render_textabschnitte(queryset):
|
||||
"""
|
||||
Converts a queryset of Textabschnitt-like models into a list of (typ, html) tuples.
|
||||
Applies special formatting for 'liste' and 'tabelle' types.
|
||||
"""
|
||||
output = []
|
||||
|
||||
for abschnitt in queryset:
|
||||
typ = abschnitt.abschnitttyp.abschnitttyp if abschnitt.abschnitttyp else ''
|
||||
inhalt = abschnitt.inhalt or ''
|
||||
if typ == "liste ungeordnet":
|
||||
inhalt = "\n".join(["- " + line for line in inhalt.splitlines()])
|
||||
html = markdown(inhalt, extensions=['tables', 'attr_list'])
|
||||
elif typ == "liste geordnet":
|
||||
inhalt = "\n".join(["1. " + line for line in inhalt.splitlines()])
|
||||
html = markdown(inhalt, extensions=['tables', 'attr_list'])
|
||||
elif typ == "tabelle":
|
||||
html = md_table_to_html(inhalt)
|
||||
elif typ == "diagramm":
|
||||
temp=inhalt.splitlines()
|
||||
diagramtype=temp.pop(0)
|
||||
diagramoptions='width="100%"'
|
||||
if temp[0][0:6].lower() == "option":
|
||||
diagramoptions=temp.pop(0).split(":",1)[1]
|
||||
rest="\n".join(temp)
|
||||
html = '<p><img '+diagramoptions+' src="'+DIAGRAMMSERVER+"/"+diagramtype+"/svg/"
|
||||
html += base64.urlsafe_b64encode(zlib.compress(rest.encode("utf-8"),9)).decode()
|
||||
html += '"></p>'
|
||||
elif typ == "code":
|
||||
html = "<pre><code>"
|
||||
html += markdown(inhalt, extensions=['tables', 'attr_list'])
|
||||
html += "</code></pre>"
|
||||
else:
|
||||
html = markdown(inhalt, extensions=['tables', 'attr_list','footnotes'])
|
||||
output.append((typ, html))
|
||||
return output
|
||||
|
||||
def md_table_to_html(md: str) -> str:
|
||||
# 1. Split into lines and drop empties
|
||||
lines = [ln.strip() for ln in md.splitlines() if ln.strip()]
|
||||
|
||||
# 2. Remove the separator line (|---|----|)
|
||||
if len(lines) < 2:
|
||||
raise ValueError("Need at least header + separator line")
|
||||
header_line = lines[0]
|
||||
body_lines = lines[2:] # skip separator
|
||||
|
||||
# 3. Parse cells ----------------------------------------------------
|
||||
def split_row(line: str):
|
||||
# Trim possible leading/trailing pipes, then split
|
||||
return [cell.strip() for cell in line.strip('|').split('|')]
|
||||
|
||||
headers = split_row(header_line)
|
||||
rows = [split_row(ln) for ln in body_lines]
|
||||
|
||||
# 4. Build HTML -----------------------------------------------------
|
||||
def wrap(tag, inner):
|
||||
return f"<{tag}>{inner}</{tag}>\n"
|
||||
|
||||
thead = wrap("thead",
|
||||
wrap("tr",
|
||||
"".join(wrap("th", h) for h in headers)))
|
||||
|
||||
tbody_rows = []
|
||||
for r in rows:
|
||||
cells = "".join(wrap("td", c) for c in r)
|
||||
tbody_rows.append(wrap("tr", cells))
|
||||
tbody = wrap("tbody", "".join(tbody_rows))
|
||||
|
||||
html = f'<table class="table table-bordered table-hover">\n{thead}{tbody}</table>'
|
||||
return html
|
||||
3
abschnitte/views.py
Normal file
3
abschnitte/views.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.shortcuts import render
|
||||
|
||||
# Create your views here.
|
||||
Reference in New Issue
Block a user