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