From da950a93e23289a47421b83fa7cd7c24ed126d94 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Tue, 14 Mar 2023 00:58:31 +0100 Subject: [PATCH] Fix issues with links, includes and notes in Sphinx .rst preview styling --- sphinx/README.md | 5 +++++ sphinx/sphinx_to_html.py | 9 +++++---- templates/custom/header.tmpl | 27 +++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/sphinx/README.md b/sphinx/README.md index 296a807..5aecf40 100644 --- a/sphinx/README.md +++ b/sphinx/README.md @@ -18,6 +18,11 @@ Add to Gitea app.ini. RENDER_COMMAND = "timeout 30s ./custom/sphinx/sphinx_to_html.py --user sphinx --user-work-dir /path/to/dir" IS_INPUT_FILE = true + [markup.sanitizer.restructuredtext] + ELEMENT = div + ALLOW_ATTR = class + REGEXP = ^(restructuredtext)$ + [repository.editor] LINE_WRAP_EXTENSIONS = .txt,.md,.markdown,.mdown,.mkd,.rst PREVIEWABLE_FILE_MODES = markdown,restructuredtext diff --git a/sphinx/sphinx_to_html.py b/sphinx/sphinx_to_html.py index c8b86b7..1107b30 100755 --- a/sphinx/sphinx_to_html.py +++ b/sphinx/sphinx_to_html.py @@ -69,10 +69,10 @@ with tempfile.TemporaryDirectory(dir=args.user_work_dir) as tmp_dir: # Disable include directives and raw for security. They are already disabled # by docutils.py, this is just to be extra careful. def include_directive(matchobj): - return f"warning:: include directives disabled: {html.escape(matchobj.group(1))}" + return f"warning:: include not available in preview: {html.escape(matchobj.group(1))}" def raw_directive(matchobj): - return f"warning:: raw disabled: {html.escape(matchobj.group(1))}" - page_contents = re.sub("literalinclude::.*", include_directive, page_contents) + return f"warning:: raw not available in preview: {html.escape(matchobj.group(1))}" + page_contents = re.sub("literalinclude::(.*)", include_directive, page_contents) page_contents = re.sub("include::(.*)", include_directive, page_contents) page_contents = re.sub("raw::(.*)", raw_directive, page_contents) @@ -105,6 +105,7 @@ with tempfile.TemporaryDirectory(dir=args.user_work_dir) as tmp_dir: if result.returncode == 0 and out_filepath.is_file(): contents = out_filepath.read_text() body = contents.split("")[1].split("")[0] - body = body.replace(placeholder_url, "#") + body = body.replace(f'href="{placeholder_url}', 'href="#link-not-available-in-preview"') body = body.replace('href="http', 'target="_blank" href="http') + body = '
' + body + '
' print(body) diff --git a/templates/custom/header.tmpl b/templates/custom/header.tmpl index 122d1b4..1b8e259 100644 --- a/templates/custom/header.tmpl +++ b/templates/custom/header.tmpl @@ -228,4 +228,31 @@ footer a { color: var(--color-footer-links); } + + /* Manual preview tweaks. */ + .restructuredtext a { + color: var(--color-accent) !important; + } + .restructuredtext :is(section, dd) > div { + background-color: var(--color-markup-code-block); + padding-bottom: 8px; + margin-bottom: 16px; + padding: 0; + border-radius: 5px; + } + .restructuredtext section div > p:first-child { + font-weight: 700; + } + .restructuredtext section div * { + margin: 0 !important; + } + .restructuredtext section div > :is(p, dl) { + padding: 8px 12px; + } + .restructuredtext section div > ul { + padding-bottom: 12px; + } + .restructuredtext section dl dt { + font-style: normal; + }