diff --git a/app/__init__.py b/app/__init__.py index 0319c5a..f577408 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -7,6 +7,7 @@ from .constants import ( PermissionLevel, permission_level_string, InfoboxKind, InfoboxIcons, InfoboxHTMLClass ) +from .lib.babycode import babycode_to_html, EMOJI from datetime import datetime import os import time @@ -103,6 +104,7 @@ def create_app(): "InfoboxHTMLClass": InfoboxHTMLClass, "InfoboxKind": InfoboxKind, "__commit": commit, + "__emoji": EMOJI, } @app.context_processor @@ -124,4 +126,18 @@ def create_app(): def permission_string(term): return permission_level_string(term) + @app.template_filter('babycode') + def babycode_filter(markup): + return babycode_to_html(markup) + + @app.template_filter('extract_h2') + def extract_h2(content): + import re + pattern = r']*>(.*?)<\/h2>' + matches = re.findall(pattern, content, re.IGNORECASE | re.DOTALL) + return [ + {'id': id_.strip(), 'text': text.strip()} + for id_, text in matches + ] + return app diff --git a/app/lib/babycode.py b/app/lib/babycode.py index 890aa9e..1aa02c6 100644 --- a/app/lib/babycode.py +++ b/app/lib/babycode.py @@ -5,7 +5,7 @@ import re def tag_code(children, attr): is_inline = children.find('\n') == -1 if is_inline: - return f"{children}" + return f"{children}" else: t = children.strip() button = f"" diff --git a/app/routes/app.py b/app/routes/app.py index 0892bc3..4937d08 100644 --- a/app/routes/app.py +++ b/app/routes/app.py @@ -1,4 +1,4 @@ -from flask import Blueprint, redirect, url_for +from flask import Blueprint, redirect, url_for, render_template bp = Blueprint("app", __name__, url_prefix = "/") @@ -9,4 +9,4 @@ def index(): @bp.route("/babycode") def babycode_guide(): - return "not yet" + return render_template('babycode.html') diff --git a/app/templates/babycode.html b/app/templates/babycode.html new file mode 100644 index 0000000..851c9f0 --- /dev/null +++ b/app/templates/babycode.html @@ -0,0 +1,114 @@ + +{% extends 'base.html' %} +{% block title %}babycode guide{% endblock %} +{% block content %} +
+

Babycode guide

+
+
+
+ {% set sections %} +
+

What is babycode?

+

You may be familiar with BBCode, a loosely related family of markup languages popular on forums. Babycode is another, simplified, dialect of those languages. It is a way of formatting text by enclosing parts of it in special tags.

+
+
+

Text formatting tags

+
    +
  • To make some text bold, enclose it in [b][/b]:
    + [b]Hello World[/b]
    + Will become
    + Hello World +
+
    +
  • To italicize text, enclose it in [i][/i]:
    + [i]Hello World[/i]
    + Will become
    + Hello World +
+
    +
  • To make some text strikethrough, enclose it in [s][/s]:
    + [s]Hello World[/s]
    + Will become
    + Hello World +
+
+
+

Emoji

+

There are a few emoji in the style of old forum emotes:

+ + + + + + {% for emoji in __emoji %} + + + + + {% endfor %} +
Short codeEmoji result
:{{ emoji }}:{{ __emoji[emoji] | safe }}
+

Special thanks to the Forumoji project and its contributors for these graphics.

+
+
+

Paragraph rules

+

Line breaks in babycode work like Markdown: to start a new paragraph, use two line breaks:

+ {{ '[code]paragraph 1\n\nparagraph 2[/code]' | babycode | safe }} + Will produce:
+ {{ 'paragraph 1\n\nparagraph 2' | babycode | safe }} +

To break a line without starting a new paragraph, end a line with two spaces:

+ {{ '[code]paragraph 1 \nstill paragraph 1[/code]' | babycode | safe }} + That will produce:
+ {{ 'paragraph 1 \nstill paragraph 1' | babycode | safe }} +
+
+ +

Loose links (starting with http:// or https://) will automatically get converted to clickable links. To add a label to a link, use
[url=https://example.com]Link label[/url]:
+ Link label

+
+
+

Attaching an image

+

To add an image to your post, use the [img] tag:
+ [img=https://forum.poto.cafe/avatars/default.webp]the Python logo with a cowboy hat[/img] + {{ '[img=/static/avatars/default.webp]the Python logo with a cowboy hat[/img]' | babycode | safe }} +

+

Text inside the tag becomes the alt text. The attribute is the image URL.

+

Images will always break up a paragraph and will get scaled down to a maximum of 400px. The text inside the tag will become the image's alt text.

+
+
+

Adding code blocks

+ {% set code = 'func _ready() -> void:\n\tprint("hello world!")' %} +

There are two kinds of code blocks recognized by babycode: inline and block. Inline code blocks do not break a paragraph. They can be added with [code]your code here[/code]. As long as there are no line breaks inside the code block, it is considered inline. If there are any, it will produce this:

+ {{ ('[code]%s[/code]' % code) | babycode | safe }} +
+

Inline code tags look like this: {{ '[code]Inline code[/code]' | babycode | safe }}

+

Babycodes are not parsed inside code blocks.

+
+
+

Quoting

+

Text enclosed within [quote][/quote] will look like a quote:

+
A man provided with paper, pencil, and rubber, and subject to strict discipline, is in effect a universal machine.
+
+
+

Lists

+ {% set list = '[ul]\nitem 1\n\nitem 2\n\nitem 3 \nstill item 3 (break line without inserting a new item by using two spaces at the end of a line)\n[/ul]' %} +

There are two kinds of lists, ordered (1, 2, 3, ...) and unordered (bullet points). Ordered lists are made with [ol][/ol] tags, and unordered with [ul][/ul]. Every new paragraph according to the usual paragraph rules will create a new list item. For example:

+ {{ ('[code]%s[/code]' % list) | babycode | safe }} + Will produce the following list: + {{ list | babycode | safe }} +
+ {% endset %} + {{ sections | safe }} +
+
+

Table of contents

+ {% set toc = sections | extract_h2 %} + +
+
+ +{% endblock %}