Skip to content

Commit 5df0bba

Browse files
Merge pull request #31 from ezhevita/feat/formatted-localized-strings
feat: parse interpolated localized strings
2 parents 0cd7545 + 917c116 commit 5df0bba

6 files changed

Lines changed: 65 additions & 17 deletions

File tree

.gitignore

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
.venv
2-
__pycache__
3-
decompiled-*
4-
out
1+
.venv/
2+
__pycache__/
3+
decompiled-*/
4+
out/
5+
.DS_Store

.vscode/launch.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Build the Deltarune site",
6+
"type": "debugpy",
7+
"request": "launch",
8+
"program": "build.py",
9+
"console": "integratedTerminal",
10+
"args": "deltarune"
11+
}
12+
]
13+
}

script.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22
import argparse
3+
import html
34
import os
45
import re
56
import sys
@@ -18,7 +19,7 @@
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: {'&': '&amp;', '<': '&lt;'}[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
),

static/main.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
--close-text-color: #fff;
2424
--face-background-color: #252;
2525
--face-text-color: #8c8;
26+
--arg-background-color: #707;
27+
--arg-text-color: #fff;
28+
--arg-collapsed-text-color: #aaa;
2629
--flag-not-found-text-color: #ec6;
2730
--flag-found-text-color: #fd8;
2831
--flag-found-background-color: black;

static/script.css

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,27 @@ pre:not(.funcCode), code, .hljs {
111111
user-select: none;
112112
}
113113

114+
.cc-arg {
115+
background: var(--arg-background-color);
116+
color: var(--arg-text-color);
117+
user-select: none;
118+
}
119+
120+
.cc-arg > span {
121+
position: absolute;
122+
right: 0;
123+
bottom: 0.1em;
124+
display: inline-block;
125+
font-size: 70%;
126+
color: var(--arg-collapsed-text-color);
127+
text-indent: 0;
128+
font-style: italic;
129+
}
130+
131+
.cc-arg:hover > span {
132+
display: none;
133+
}
134+
114135
.cc-delay:before {
115136
content: '\231b';
116137
margin-right: 0.2em;
@@ -124,6 +145,10 @@ pre:not(.funcCode), code, .hljs {
124145
content: '\239a';
125146
}
126147

148+
.cc-arg:before {
149+
content: '\2386';
150+
}
151+
127152
.cc-color {
128153
color: white;
129154
}

templates/highlight/text.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div class="langtext"><span class="skip-highlight">{{ parsed_text | safe }}</span><div class="langvar">{{ before_var }}{{ variable | safe }}{{ after_var }}</div></div>
1+
<div class="langtext"><span class="skip-highlight">{{ parsed_text | safe }}</span><div class="langvar">{{ before_var | safe }}{{ variable | safe }}{{ after_var | safe }}</div></div>

0 commit comments

Comments
 (0)