You're looking at it

Homepage: https://roundabout-host.com

By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 syntax_colouring.py

View raw Download
text/plain • 3.04 kiB
Python script, ASCII text executable
        
            
1
"""
2
This module is under development and not yet in use in Roundabout. It is
3
an experiment to try syntax highlighting in the application's web interface.
4
5
Roundabout - git hosting for everyone <https://roundabout-host.com>
6
Copyright (C) 2023-2025 Roundabout developers <root@roundabout-host.com>
7
8
This program is free software: you can redistribute it and/or modify
9
it under the terms of the GNU Affero General Public License as published by
10
the Free Software Foundation, either version 3 of the License, or
11
(at your option) any later version.
12
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU Affero General Public License for more details.
17
18
You should have received a copy of the GNU Affero General Public License
19
along with this program. If not, see <http://www.gnu.org/licenses/>.
20
"""
21
22
from pygments import highlight
23
from pygments.style import Style
24
from pygments import token
25
from pygments.lexers import PythonLexer
26
from pygments.formatters import HtmlFormatter
27
from bs4 import BeautifulSoup
28
29
30
class CustomStyle(Style):
31
default_style = ""
32
styles = {
33
token.Text: "",
34
token.Keyword: "bold #ff9800",
35
token.Name: "",
36
token.Operator: "",
37
38
token.Name.Class: "#ffeb3b",
39
token.Name.Function: "#be9ddb",
40
token.Name.Builtin: "#ab47bc",
41
token.Name.Attribute: "#4db6ac",
42
token.Name.Constant: "italic #ef5350",
43
token.Name.Decorator: "#bcaaa4",
44
token.Name.Function.Magic: "#e040fb",
45
token.Name.Label: "#ff4081",
46
47
token.String: "#8bc34a",
48
token.Number: "#42a5f5",
49
token.Punctuation: "#ff9800",
50
token.Comment: "italic #b0bec5",
51
token.Generic: "",
52
token.Escape: "#ff5722",
53
token.String.Escape: "#ff5722",
54
token.String.Regex: "#ffffff",
55
token.Comment.Preproc: "#536dfe",
56
token.Comment.Hashbang: "#536dfe",
57
token.Error: "#f44336",
58
token.Other: "",
59
token.Whitespace: "",
60
}
61
62
63
code = """
64
___all___ = ["hello_world"]
65
66
def interesting_function(func: Callable) -> Callable:
67
\"\"\"Print the function's result when called.
68
69
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam nec nulla
70
nec nulla laoreet fermentum. Integer id lacus ut odio interdum ultricies.
71
\"\"\"
72
def wrapper(*args, **kwargs):
73
result = func(*args, **kwargs) # avoid double evaluation
74
print(result)
75
return result
76
return wrapper
77
78
@interesting_function
79
def hello_world():
80
return "Hello, world!\\a" # a nice bell
81
82
if __name__ == "__main__":
83
hello_world()
84
"""
85
86
lexer = PythonLexer()
87
formatter = HtmlFormatter(style="colorful")
88
html_code = highlight(code, lexer, formatter)
89
90
soup = BeautifulSoup(html_code, "html.parser")
91
pre = soup.find("pre")
92
lines = pre.decode_contents().splitlines()
93
94
for line in lines:
95
line_soup = BeautifulSoup(line, "html.parser")
96
clean_line = line_soup.prettify()
97
print(clean_line)
98