mirror of
https://git.cmoser.eu/tinytools/django-tinywiki.git
synced 2026-02-04 06:06:33 +01:00
2025.10.06-03:08:05
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import bbcode
|
||||
from . import formatters
|
||||
PARSER = bbcode.Parser(newline="\n",escape_html=True)
|
||||
PARSER = bbcode.Parser(newline="\n",escape_html=True,replace_links=False)
|
||||
|
||||
def _():
|
||||
for i in formatters.SIMPLE_FORMATTERS:
|
||||
|
||||
@@ -10,26 +10,36 @@ from .text_formatters import (
|
||||
render_wiki_image,
|
||||
render_wiki_link,
|
||||
render_wiki_url,
|
||||
)
|
||||
from .simple_formatters import (
|
||||
SIMPLE_HEADER_FORMATTERS,
|
||||
render_table,
|
||||
render_table_header,
|
||||
render_table_data,
|
||||
render_table_row,
|
||||
render_youtube_video,
|
||||
)
|
||||
|
||||
from .simple_formatters import SIMPLE_FORMATTERS
|
||||
# a list of tuples containig an tuple args and a dict of kwargs
|
||||
SIMPLE_FORMATTERS=[
|
||||
*SIMPLE_HEADER_FORMATTERS,
|
||||
]
|
||||
|
||||
#a list of tuples containing an tuple of args and a dict of kwargs
|
||||
FORMATTERS=[
|
||||
(('url',render_url),{'strip':True,'swallow_trailing_newline':True,'same_tag_closes':True}),
|
||||
(('wiki-url',render_wiki_url),{'strip':True,'swallow_trailing_newline':True,'same_tag_closes':True}),
|
||||
(('wiki',render_wiki_link),{'strip':True,'swallow_tailin_newline':True,'standalone':True}),
|
||||
(('codeblock',render_codeblock),{'strip':False,'swallow_trailing_newline':False,'same_tag_closes':True}),
|
||||
(('codeblock',render_codeblock),{'strip':True,'swallow_trailing_newline':False,'same_tag_closes':False,"render_embedded":False}),
|
||||
(('ol',render_ordered_list),{}),
|
||||
(('ul',render_unordered_list),{}),
|
||||
(('li',render_list_item),{}),
|
||||
(('p',render_paragraph),{}),
|
||||
(('image',render_image),{}),
|
||||
(('wiki-image',render_wiki_image),{'standalone':True})
|
||||
(('p',render_paragraph),{'same_tag_closes':False}),
|
||||
(('image',render_image),{'same_tag_closes':True}),
|
||||
(('img',render_image),{'same_tag_closes':True}),
|
||||
(('wiki-image',render_wiki_image),{'standalone':True}),
|
||||
(('wimg',render_wiki_image),{'standalone':True}),
|
||||
(('table',render_table),{}),
|
||||
(('table-row',render_table_row),{}),
|
||||
(('tr',render_table_row),{}),
|
||||
(('table-header',render_table_header),{}),
|
||||
(('th',render_table_header),{}),
|
||||
(('table-data',render_table_data),{}),
|
||||
(('td',render_table_data),{}),
|
||||
(('youtube',render_youtube_video),{'same_tag_closes':True}),
|
||||
]
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
|
||||
SIMPLE_HEADER_FORMATTERS = [
|
||||
SIMPLE_FORMATTERS = [
|
||||
(('h1',"<h1>%(value)s</h1>"),{}),
|
||||
(('h2',"<h2>%(value)s</h2>"),{}),
|
||||
(('h3',"<h3>%(value)s</h3>"),{}),
|
||||
(('h4',"<h4>%(value)s</h4>"),{}),
|
||||
(('h5',"<h5>%(value)s</h5>"),{}),
|
||||
(('h6',"<h6>%(value)s</h6>"),{}),
|
||||
(('mark',"<mark>%(value)s</mark>"),{}),
|
||||
(("strong","<strong>%(value)s</strong>"),{}),
|
||||
(("em","<em>%(value)s</em>"),{}),
|
||||
(("br","<br>"),{"standalone":True}),
|
||||
(('copy',"©"),{'standalone':True}),
|
||||
(('reg',"®"),{'standalone':True}),
|
||||
(('trade',"™"),{'standalone':True}),
|
||||
|
||||
@@ -2,9 +2,10 @@ from django_project.settings import STATIC_URL
|
||||
from django.urls import reverse
|
||||
from django.template.loader import render_to_string
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
from ... import settings
|
||||
from ... import models
|
||||
|
||||
import bbcode
|
||||
|
||||
def render_url(tag_name:str,value,options,parent,context):
|
||||
try:
|
||||
@@ -12,17 +13,19 @@ def render_url(tag_name:str,value,options,parent,context):
|
||||
except KeyError:
|
||||
url = value
|
||||
|
||||
if '://' not in url:
|
||||
if not url.startswith('/') and '://' not in url:
|
||||
url = "http://" + url
|
||||
|
||||
if settings.USE_BOOTSTRAP:
|
||||
return f"<a href=\"{url}\" class=\"icon-link icon-link-hover\" referrer-policy=\"no-referrer\" rel=\"noreferrer noopener\">{value}{render_to_string('tinywiki/icons/box-arrow-up-right.svg')}</a>"
|
||||
if ['noicon in options']:
|
||||
return f"<a href=\"{url}\" referrer-policy=\"no-referrer\" rel=\"noreferrer noopener\">{value}</a>"
|
||||
return f"<a href=\"{url}\" class=\"icon-link icon-link-hover\" referrer-policy=\"no-referrer\" rel=\"noreferrer noopener\">{value}<svg class=\"bi\"><use xlink:href=\"{settings.settings.STATIC_URL + "tinywiki/icons/bootstrap-icons.svg"}#box-arrow-up-right\"></use></svg></a>"
|
||||
return f"<a href=\"{url}\" referrer-policy=\"no-referrer\" rel=\"noreferrer noopener\">{value}</a>"
|
||||
|
||||
def render_wiki_url(tag_name,value,options,parent,context):
|
||||
if tag_name in options:
|
||||
url = reverse("tinywiki:page",kwargs={'slug':options[tag_name]})
|
||||
slug=options['tag_name']
|
||||
slug=options[tag_name]
|
||||
try:
|
||||
page = models.Page.objects.get(slug=slug)
|
||||
except models.Page.DoesNotExist:
|
||||
@@ -33,47 +36,49 @@ def render_wiki_url(tag_name,value,options,parent,context):
|
||||
slug=None
|
||||
|
||||
if settings.USE_BOOTSTRAP:
|
||||
href = settings.settings.STATIC_URL+"tinywiki/icons/bootstrap-icons.svg"
|
||||
if page:
|
||||
if page.slug.startswith('tw-'):
|
||||
svg=render_to_string('tinywiki/icons/journal.svg')
|
||||
svg = "journal"
|
||||
elif page.slug:
|
||||
svg=render_to_string('tinywiki/icons/book.svg')
|
||||
svg = "book"
|
||||
else:
|
||||
svg=render_to_string('tinywiki/icons/file-earmark-x')
|
||||
|
||||
return f"<a href=\"{url}\" class=\"icon-link icon-link-hover\">{value}{svg}</a>"
|
||||
svg=href + "file-earmark-x"
|
||||
return f"<a href=\"{url}\" class=\"icon-link icon-link-hover\">{value}<svg class=\"bi\"><use xlink:href=\"{href}#{svg}\"></use></svg></a>"
|
||||
return f"<a href=\"{url}\">{value}</a>"
|
||||
|
||||
|
||||
def render_wiki_link(tag_name,value,options,parent,context):
|
||||
if tag_name in options:
|
||||
slug = options['tag_name']
|
||||
slug = options[tag_name]
|
||||
print("slug",slug)
|
||||
try:
|
||||
page = models.Page.objects.get(slug=slug)
|
||||
title = page.title
|
||||
if slug.starts_with('tw-'):
|
||||
svg = "tinywiki/icons/journal.svg"
|
||||
if slug.startswith('tw-'):
|
||||
svg = "journal"
|
||||
else:
|
||||
svg = "tinywiki/icons/book.svg"
|
||||
except:
|
||||
svg = "book"
|
||||
except models.Page.DoesNotExist:
|
||||
page = None
|
||||
title = _("Page not found")
|
||||
svg_template = "tinywiki/icons/file-earmark-x.svg"
|
||||
svg = "file-earmark-x"
|
||||
url = reverse("tinywiki:page",kwargs={'slug':slug})
|
||||
else:
|
||||
slug = None
|
||||
title = _("Home")
|
||||
url = reverse("tinywiki:home")
|
||||
svg_template = "tinywiki/icons/house.svg"
|
||||
svg = "house"
|
||||
|
||||
if settings.USE_BOOTSTRAP:
|
||||
return f"<a href=\"{url}\" class=\"icon-link icon-link-hover\">{value}{render_to_string(svg_template)}</a>"
|
||||
href = settings.settings.STATIC_URL + "tinywiki/icons/bootstrap-icons.svg"
|
||||
return f"<a href=\"{url}\" class=\"icon-link icon-link-hover\">{title}<svg class=\"bi\"><use xlink:href=\"{href}#{svg}\"></use></svg></a>"
|
||||
return f"<a href=\"{url}\">{value}</a>"
|
||||
|
||||
def render_codeblock(tag_name:str,value,options,parent,context)->str:
|
||||
if 'codeblock' in options:
|
||||
return f"<pre><code class=\"language-{options['codeblock']}\">{value}</pre></code>"
|
||||
return f"<pre><code>{value}</pre></code>"
|
||||
if tag_name in options:
|
||||
return f"<pre style=\"overflow-x:auto;\"><code class=\"language-{options[tag_name]}\">{value}</pre></code>"
|
||||
return f"<pre style=\"overflow-x:auto;\"><code>{value}</pre></code>"
|
||||
|
||||
def render_ordered_list(tag_name:str,value,options,parent,context)->str:
|
||||
return f"<ol>{value}</ol>"
|
||||
@@ -112,7 +117,7 @@ def render_image(tag_name:str,value,options,parent,context):
|
||||
|
||||
if 'width' in options:
|
||||
_w = options['width']
|
||||
if _w.endswith('px'):
|
||||
if _w.endswith('px') or _w.endswith('em') or _w.endswith('rem'):
|
||||
fig_styles.append(f"width:{_w};")
|
||||
else:
|
||||
if _w.endswith('%'):
|
||||
@@ -129,16 +134,41 @@ def render_image(tag_name:str,value,options,parent,context):
|
||||
fig_classes.append(f'w-{width}')
|
||||
else:
|
||||
fig_styles.append(f"width:{_w}%;")
|
||||
|
||||
if 'height' in options:
|
||||
_h = options['width']
|
||||
if _h.endswith('px') or _h.endswith('em') or _h.endswith('rem'):
|
||||
fig_styles.append(f"height:{_h};")
|
||||
else:
|
||||
if _h.endswith('%'):
|
||||
_h= _h[:-1]
|
||||
if _h.isdigit():
|
||||
_h=int(_w)
|
||||
if _h > 100:
|
||||
_h = 100
|
||||
if settings.USE_BOOTSTRAP:
|
||||
if 1 < int(_h) <= 25:
|
||||
height = 25
|
||||
else:
|
||||
height = ((_h // 25) * 25)
|
||||
if height > 100:
|
||||
height = 100
|
||||
|
||||
fig_classes.append(f'h-{height}')
|
||||
else:
|
||||
fig_styles.append(f"height:{_h}%;")
|
||||
|
||||
if "position" in options:
|
||||
pos = options['position']
|
||||
if settings.USE_BOOTSTRAP:
|
||||
if pos == "left" or pos=="start":
|
||||
fig_classes += ["float-start","me-2"]
|
||||
classes += ["float-start","me-2"]
|
||||
elif pos == "right" or pos == "end":
|
||||
fig_classes += ["float-end","ms-2"]
|
||||
classes += ["float-end","ms-2"]
|
||||
elif pos == "center":
|
||||
fig_classes += ["mx-auto","d-block"]
|
||||
classes += ["mx-auto","d-block"]
|
||||
|
||||
if styles:
|
||||
style=f"style=\"{"".join(styles)}\""
|
||||
@@ -150,7 +180,7 @@ def render_image(tag_name:str,value,options,parent,context):
|
||||
else:
|
||||
fig_style=""
|
||||
if settings.USE_BOOTSTRAP:
|
||||
return f'<figure class="{" ".join(fig_classes)} {fig_style}"><img src="{options[tag_name]}" class="{' '.join(classes)}" alt="{alt}" {style}><figcaption class="figure-caption text-end">{value}</figcaption></figure>'
|
||||
return f'<figure class="{" ".join(fig_classes)} {fig_style}"><img src="{options[tag_name]}" class="{' '.join(classes)}" alt="{alt}" {style}><figcaption class="figure-caption text-end">{ value }</figcaption></figure>'
|
||||
else:
|
||||
return f'<figure {fig_style}><img src="{options[tag_name]}" {style}><figcaption>{value}</figcaption></figure>'
|
||||
|
||||
@@ -176,7 +206,7 @@ def render_wiki_image(tag_name:str,value,options,parent,context):
|
||||
|
||||
if 'width' in options:
|
||||
_w = options['width']
|
||||
if _w.endswith('px'):
|
||||
if _w.endswith('px') or _w.endswith('em') or _w.endswith('rem'):
|
||||
fig_styles.append(f"width:{_w};")
|
||||
else:
|
||||
if _w.endswith('%'):
|
||||
@@ -193,7 +223,29 @@ def render_wiki_image(tag_name:str,value,options,parent,context):
|
||||
fig_classes.append(f'w-{width}')
|
||||
else:
|
||||
fig_styles.append(f"width:{_w}%;")
|
||||
|
||||
if 'height' in options:
|
||||
_h = options['width']
|
||||
if _h.endswith('px') or _h.endswith('em') or _h.endswith('rem'):
|
||||
fig_styles.append(f"height:{_h};")
|
||||
else:
|
||||
if _h.endswith('%'):
|
||||
_h= _h[:-1]
|
||||
if _h.isdigit():
|
||||
_h=int(_w)
|
||||
if _h > 100:
|
||||
_h = 100
|
||||
if settings.USE_BOOTSTRAP:
|
||||
if 1 < int(_h) <= 25:
|
||||
height = 25
|
||||
else:
|
||||
height = ((_h // 25) * 25)
|
||||
if height > 100:
|
||||
height = 100
|
||||
|
||||
fig_classes.append(f'h-{height}')
|
||||
else:
|
||||
fig_styles.append(f"height:{_h}%;")
|
||||
|
||||
if "position" in options:
|
||||
pos = options['position']
|
||||
if settings.USE_BOOTSTRAP:
|
||||
@@ -215,7 +267,155 @@ def render_wiki_image(tag_name:str,value,options,parent,context):
|
||||
fig_style=""
|
||||
|
||||
if settings.USE_BOOTSTRAP:
|
||||
return f'<figure class="{" ".join(fig_classes)}" {fig_style}><img src="{image.image.url}" alt="{image.alt}" class="{' '.join(classes)}" {style}><figcaption class="figure-caption text-end">{image.description}</figcaption></figure>'
|
||||
return f'<figure class="{" ".join(fig_classes)}" {fig_style}><img src="{image.image.url}" alt="{image.alt}" class="{' '.join(classes)}" {style}><figcaption class="figure-caption text-end">{image.description_html}</figcaption></figure>'
|
||||
else:
|
||||
return f'<figure {fig_style}><img src="{image.image.url}" alt="{image.alt}" {style}><figcaption>{image.description}</figcaption></figure>'
|
||||
|
||||
def render_table(tag_name:str,value,options,parent,context):
|
||||
if settings.USE_BOOTSTRAP:
|
||||
classes=["table"]
|
||||
if "bordered" in options:
|
||||
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"):
|
||||
classes.append(f"border-{options['bordered']}")
|
||||
|
||||
if tag_name in options:
|
||||
if options[tag_name] in ("primary","secondary","info","warning","danger","success","light","dark"):
|
||||
classes.append(f"table-{options[tag_name]}")
|
||||
|
||||
return f"<table class=\"{" ".join(classes)}\">{value}</table>"
|
||||
|
||||
return f"<table>{value}</table>"
|
||||
|
||||
def render_table_row(tag_name:str,value,options,parent,context):
|
||||
classes=[]
|
||||
if settings.USE_BOOTSTRAP:
|
||||
if tag_name in options:
|
||||
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"<tr {class_attr}>{value}</tr>"
|
||||
|
||||
def render_table_header(tag_name:str,value,options,parent,context):
|
||||
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"):
|
||||
classes.append(f"table-{options[tag_name]}")
|
||||
class_attr=f"class=\"{" ".join(classes)}\"" if classes else ""
|
||||
return f"<th {class_attr} {" ".join(extra_attributes)}>{value}</th>"
|
||||
|
||||
def render_table_data(tag_name:str,value,options,parent,context):
|
||||
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"):
|
||||
classes.append(f"table-{options[tag_name]}")
|
||||
class_attr=f"class=\"{" ".join(classes)}\"" if classes else ""
|
||||
return f"<td {class_attr} {" ".join(extra_attributes)}>{value}</td>"
|
||||
|
||||
def render_youtube_video(tag_name:str,value,options,parent,context):
|
||||
if tag_name not in options:
|
||||
return ""
|
||||
|
||||
if 'alt' in options:
|
||||
alt=options['alt']
|
||||
else:
|
||||
alt=""
|
||||
|
||||
if settings.USE_BOOTSTRAP:
|
||||
styles=[]
|
||||
classes=["w-100"]
|
||||
div_classes=["my-1"]
|
||||
div_styles=[]
|
||||
else:
|
||||
styles=["max-width:100%;"]
|
||||
classes=[]
|
||||
div_classes=[]
|
||||
div_styles=[]
|
||||
|
||||
|
||||
if 'width' in options:
|
||||
_w = options['width']
|
||||
if _w.endswith('px') or _w.endswith('em') or _w.endswith('rem'):
|
||||
if settings.USE_BOOTSTRAP:
|
||||
div_styles.append(f"width:{_w};")
|
||||
styles.append(f"width:{_w};")
|
||||
else:
|
||||
if _w.endswith('%'):
|
||||
_w = _w[:-1]
|
||||
if _w.isdigit():
|
||||
_w=int(_w)
|
||||
if _w > 100:
|
||||
_w = 100
|
||||
if settings.USE_BOOTSTRAP:
|
||||
if 1 < int(_w) <= 25:
|
||||
width = 25
|
||||
else:
|
||||
width = ((_w // 25) * 25)
|
||||
div_classes.append(f'w-{width}')
|
||||
else:
|
||||
styles.append(f"width:{_w}%;")
|
||||
if 'height' in options:
|
||||
_h = options['width']
|
||||
if _h.endswith('px') or _h.endswith('em') or _h.endswith('rem'):
|
||||
if settings.USE_BOOTSTRAP:
|
||||
div_styles.append(f"height:{_h};")
|
||||
else:
|
||||
styles.append(f"height:{_h};")
|
||||
else:
|
||||
if _h.endswith('%'):
|
||||
_h= _h[:-1]
|
||||
if _h.isdigit():
|
||||
_h=int(_w)
|
||||
if _h > 100:
|
||||
_h = 100
|
||||
if settings.USE_BOOTSTRAP:
|
||||
if 1 < int(_h) <= 25:
|
||||
height = 25
|
||||
else:
|
||||
height = ((_h // 25) * 25)
|
||||
if height > 100:
|
||||
height = 100
|
||||
|
||||
div_classes.append(f'h-{height}')
|
||||
else:
|
||||
styles.append(f"height:{_h}%;")
|
||||
|
||||
if "position" in options:
|
||||
pos = options['position']
|
||||
if settings.USE_BOOTSTRAP:
|
||||
if pos == "left" or pos=="start":
|
||||
div_classes += ["float-start","me-2"]
|
||||
#classes += ["float-start","me-2"]
|
||||
elif pos == "right" or pos == "end":
|
||||
div_classes += ["float-end","ms-2"]
|
||||
#classes += ["float-end","ms-2"]
|
||||
elif pos == "center":
|
||||
div_classes += ["mx-auto","d-block"]
|
||||
#classes += ["mx-auto","d-block"]
|
||||
|
||||
if styles:
|
||||
style=f"style=\"{"".join(styles)}\""
|
||||
else:
|
||||
style=""
|
||||
|
||||
if div_styles:
|
||||
div_style=f'style="{"".join(div_styles)}"'
|
||||
else:
|
||||
div_style=""
|
||||
if settings.USE_BOOTSTRAP:
|
||||
return f'<div class=""{' '.join(div_classes)}" {div_style}><iframe class={" ".join(classes)}" src="https://www.youtube.com/embed/{options[tag_name]}" allowfullscreen></iframe></div>'
|
||||
else:
|
||||
return f'<div {div_style}><iframe src="https://www.youtube.com/embed/{options[tag_name]}?rel=0" allowfullscreen></iframe></div>'
|
||||
Reference in New Issue
Block a user