feat(mods): Add external RST renderer

This commit is contained in:
jeanluc
2022-05-11 09:36:25 +02:00
parent 93849b8001
commit a2ddc1c1a1
37 changed files with 762 additions and 0 deletions

14
gitea/mods/rst/clean.sls Normal file
View File

@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*-
# vim: ft=sls
{%- set tplroot = tpldir.split('/')[0] %}
{%- set sls_config_file = tplroot ~ '.config.file' %}
{%- from tplroot ~ "/map.jinja" import mapdata as gitea with context %}
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
Gitea RST external renderer is absent:
file.absent:
- names:
- {{ gitea.lookup.mod_rst.chroma_build_path }}
- {{ gitea.lookup.mod_rst.chroma_bin }}
- {{ gitea.lookup.mod_rst.path }}

View File

@@ -0,0 +1,128 @@
#!/usr/bin/python3
import subprocess
import sys
from docutils import nodes, core, io
from docutils.parsers.rst import directives, Directive
class Chroma(Directive):
"""Source code syntax highlighting."""
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = True
option_spec = {}
has_content = True
def run(self):
self.assert_has_content()
data = "\n".join(self.content)
try:
parsed = subprocess.run(
["{{ gitea.lookup.mod_rst.chroma_bin }}", "--html", "--html-only", "-l", self.arguments[0]],
text=True,
input=data,
capture_output=True,
check=True,
)
except subprocess.CalledProcessError:
parsed = subprocess.run(
["{{ gitea.lookup.mod_rst.chroma_bin }}", "--html", "--html-only"],
text=True,
input=data,
capture_output=True,
check=True,
)
return [nodes.raw("", parsed.stdout, format="html")]
def html_parts(
input_string,
source_path=None,
destination_path=None,
input_encoding="utf8",
doctitle=True,
initial_header_level=1,
):
"""
Given an input string, returns a dictionary of HTML document parts.
Dictionary keys are the names of parts, and values are Unicode strings;
encoding is up to the client.
Parameters:
- `input_string`: A multi-line text string; required.
- `source_path`: Path to the source file or object. Optional, but useful
for diagnostic output (system messages).
- `destination_path`: Path to the file or object which will receive the
output; optional. Used for determining relative paths (stylesheets,
source links, etc.).
- `input_encoding`: The encoding of `input_string`. If it is an encoded
8-bit string, provide the correct encoding. If it is a Unicode string,
use "unicode", the default.
- `doctitle`: Disable the promotion of a lone top-level section title to
document title (and subsequent section title to document subtitle
promotion); enabled by default.
- `initial_header_level`: The initial level for header elements (e.g. 1
for "<h1>").
"""
overrides = {
"input_encoding": input_encoding,
"doctitle_xform": doctitle,
"initial_header_level": initial_header_level,
"report_level": 4,
}
parts = core.publish_parts(
source=input_string,
source_path=source_path,
destination_path=destination_path,
writer_name="html",
settings_overrides=overrides,
)
return parts
def html_body(
input_string,
source_path=None,
destination_path=None,
input_encoding="utf8",
output_encoding="unicode",
doctitle=True,
initial_header_level=1,
):
"""
Given an input string, returns an HTML fragment as a string.
The return value is the contents of the <body> element.
Parameters (see `html_parts()` for the remainder):
- `output_encoding`: The desired encoding of the output. If a Unicode
string is desired, use the default value of "unicode" .
"""
parts = html_parts(
input_string=input_string,
source_path=source_path,
destination_path=destination_path,
input_encoding=input_encoding,
doctitle=doctitle,
initial_header_level=initial_header_level,
)
fragment = parts["html_body"]
if output_encoding != "unicode":
fragment = fragment.encode(output_encoding)
return fragment
if __name__ == "__main__":
directives.register_directive("code-block", Chroma)
with open(sys.argv[1], "r") as f:
source = "".join(f.readlines())
print(html_body(source.encode()))

67
gitea/mods/rst/init.sls Normal file
View File

@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
# vim: ft=sls
{%- set tplroot = tpldir.split('/')[0] %}
{%- set sls_config_file = tplroot ~ '.config.file' %}
{%- set sls_go_install = tplroot ~ '.go' %}
{%- from tplroot ~ "/map.jinja" import mapdata as gitea with context %}
{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %}
include:
- {{ sls_go_install }}
RST external renderer pkg dependencies are installed:
pkg.installed:
- pkgs: {{ gitea.lookup.mod_rst.pkg_dependencies | json }}
RST external renderer pip dependencies are installed:
pip.installed:
- names: {{ gitea.lookup.mod_rst.pip_dependencies | json }}
- require:
- RST external renderer pkg dependencies are installed
Chroma is cloned:
file.directory:
- name: {{ gitea.lookup.mod_rst.chroma_build_path }}
- makedirs: true
- user: {{ gitea.lookup.build_user }}
- mode: '0755'
git.latest:
- name: {{ gitea.lookup.mod_rst.chroma_repo }}
- target: {{ gitea.lookup.mod_rst.chroma_build_path }}
- rev: {{ gitea.lookup.mod_rst.chroma_rev }}
- user: {{ gitea.lookup.build_user }}
- force_reset: true
- require:
- file: {{ gitea.lookup.mod_rst.chroma_build_path }}
Chroma is installed:
file.directory:
- name: {{ salt["file.dirname"](gitea.lookup.mod_rst.chroma_bin) }}
cmd.run:
- name: {{ gitea.lookup.build_user_home | path_join("go", "bin", "go") }} build -o '{{ gitea.lookup.mod_rst.chroma_bin }}'
- cwd: {{ gitea.lookup.mod_rst.chroma_build_path | path_join("cmd", "chroma") }}
- runas: {{ gitea.lookup.build_user }}
- require:
- RST external renderer pkg dependencies are installed
- file: {{ salt["file.dirname"](gitea.lookup.mod_rst.chroma_bin) }}
- sls: {{ sls_go_install }}
- onchanges:
- Chroma is cloned
RST external renderer is installed:
file.managed:
- name: {{ gitea.lookup.mod_rst.path }}
- source: {{ files_switch(['grst'],
use_subpath=True,
lookup='RST external renderer is installed'
)
}}
- mode: '0755'
- user: root
- group: {{ gitea.lookup.rootgroup }}
- template: jinja
- context:
gitea: {{ gitea | json }}
- require:
- RST external renderer pip dependencies are installed