11#!/usr/bin/env python
22import argparse
3+ import html
34import os
45import re
56import sys
1819)
1920
2021
21- def parse_text (text : str ) -> str :
22+ def parse_text (text : str , is_format : bool ) -> str :
2223 # Parse Undertale codes the same as Deltarune
2324 text = re .sub (r'\\\\' , r'\\' , text )
2425 # General text codes
@@ -42,6 +43,13 @@ def parse_text(text: str) -> str:
4243 '<span class="cc cc-close">Close Message</span>' ,
4344 text ,
4445 )
46+ if is_format :
47+ # '~[x]': interpolated argument
48+ text = re .sub (
49+ r'~([1-9])' ,
50+ r'<span class="cc cc-arg">Argument №\1<span>\1</span></span>' ,
51+ text ,
52+ )
4553 # '\E[x]': emotion
4654 # '\M[x]': special, typically emotion on overworld sprite
4755 text = re .sub (
@@ -97,7 +105,7 @@ def highlight_text(matches: re.Match[str]) -> str:
97105 before_var = '"' ,
98106 variable = matches [2 ],
99107 after_var = matches [3 ],
100- parsed_text = parse_text (matches [2 ]),
108+ parsed_text = parse_text (matches [2 ], 'subloc' in matches [ 1 ] ),
101109 )
102110
103111
@@ -106,7 +114,9 @@ def highlight_text_ch1(matches: re.Match[str], data: Data) -> str:
106114 before_var = matches [1 ],
107115 variable = matches [2 ],
108116 after_var = matches [3 ],
109- parsed_text = parse_text (data .get_localized_string_ch1 (matches [2 ])),
117+ parsed_text = parse_text (
118+ data .get_localized_string_ch1 (matches [2 ]),
119+ False ),
110120 )
111121
112122
@@ -232,21 +242,17 @@ def process_line(
232242) -> str :
233243 # Escape dangerous HTML characters.
234244 # This preserves strings like "THE LEGEND OF THIS WORLD.#<DELTARUNE.>"
235- line = re .sub (
236- r'(&|<)' ,
237- lambda matches : {'&' : '&' , '<' : '<' }[matches [1 ]],
238- line ,
239- )
245+ line = html .escape (line , quote = False )
240246
241247 # Highlight localized strings
242248 line = re .sub (
243- r'([A-Za- z0-9_]+loc\((?:\d+, )?)"((?:[^"\\]|\\.)+)(", "[a-z0-9_-]+")\)' , # noqa: E501
249+ r'([a- z0-9_]+loc\((?:\d+, )?)"((?:[^"\\]|\\.)+)(", (?:.+, )? "[a-z0-9_-]+")\)' , # noqa: E501
244250 lambda matches : matches [1 ] + highlight_text (matches ) + ')' ,
245251 line ,
246252 flags = re .IGNORECASE ,
247253 )
248254 line = re .sub (
249- r'(scr_(?:84_get_lang_string(?:_ch1)?|gettext)\(")([a-zA-Z0 -9_-]+)("\))' , # noqa: E501
255+ r'(scr_(?:84_get_lang_string(?:_ch1)?|gettext)\(")([a-z0 -9_-]+)("\))' , # noqa: E501
250256 lambda matches : highlight_text_ch1 (matches , data ),
251257 line ,
252258 flags = re .IGNORECASE ,
@@ -259,7 +265,7 @@ def process_line(
259265 flags = re .IGNORECASE ,
260266 )
261267 line = re .sub (
262- r'(room_goto\()([A-Za -z0-9_]+)' ,
268+ r'(room_goto\()([a -z0-9_]+)' ,
263269 lambda matches : highlight_room (matches , data ),
264270 line ,
265271 flags = re .IGNORECASE ,
@@ -272,7 +278,7 @@ def process_line(
272278 )
273279 # Link to functions and alarms
274280 line = re .sub (
275- r'(\b)(s?cr?_[a-zA-Z0 -9_]+)\(' ,
281+ r'(\b)(s?cr?_[a-z0 -9_]+)\(' ,
276282 lambda matches : highlight_function (
277283 matches , script_name , text , data , resolve_references
278284 ),
0 commit comments