Skip to content

Commit b92ff7c

Browse files
authored
Merge pull request #98 from trojsten/io-block-tweaks
fix: better io block rendering on mobile
2 parents f2ba2ce + 60f6670 commit b92ff7c

File tree

2 files changed

+81
-34
lines changed

2 files changed

+81
-34
lines changed

css/tailwind.css

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,19 @@
1111
color utility to any element that depends on these defaults.
1212
*/
1313
@layer base {
14-
*,
15-
::after,
16-
::before,
17-
::backdrop,
18-
::file-selector-button {
19-
border-color: var(--color-gray-200, currentcolor);
20-
}
14+
15+
*,
16+
::after,
17+
::before,
18+
::backdrop,
19+
::file-selector-button {
20+
border-color: var(--color-gray-200, currentcolor);
21+
}
2122
}
2223

23-
[x-cloak] { display: none; }
24+
[x-cloak] {
25+
display: none;
26+
}
2427

2528
.codehilite {
2629
background: none !important;
@@ -36,13 +39,15 @@
3639

3740
/** I/O table in problem statements **/
3841
.io {
39-
@apply grid grid-cols-1 sm:grid-cols-2 gap-x-4;
42+
@apply grid grid-cols-1 sm:grid-cols-2 gap-4 my-5;
4043
}
44+
4145
.io h3 {
4246
@apply mt-0;
4347
}
48+
4449
.io pre {
45-
@apply mt-0;
50+
@apply m-0;
4651
}
4752

4853
.input {

school/pages/markdown_extensions.py

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
from xml.etree.ElementTree import ElementTree, SubElement
2+
from xml.etree.ElementTree import Element, SubElement
33

44
from django.conf import settings
55
from markdown import Extension, Markdown
@@ -9,7 +9,7 @@
99

1010

1111
class SchoolImageTreeprocessor(Treeprocessor):
12-
def run(self, root: ElementTree):
12+
def run(self, root: Element):
1313
for elem in root.iter("img"):
1414
image_url = elem.get("src")
1515
if image_url is None:
@@ -35,7 +35,7 @@ def run(self, root: ElementTree):
3535

3636

3737
class CodeBlocksProcessor(Preprocessor):
38-
def run(self, lines) -> list[str]:
38+
def run(self, lines: list[str]) -> list[str]:
3939
new_lines = []
4040
inside = 0
4141
for line in lines:
@@ -45,29 +45,37 @@ def run(self, lines) -> list[str]:
4545

4646
if line == "```vstup":
4747
new_lines.append("<io>")
48-
# new_lines.append("### Vstup")
48+
new_lines.append("<io:input>")
4949
new_lines.append("```")
5050
inside = 1
5151
elif line == "```vystup":
52-
# new_lines.append("### Výstup")
52+
new_lines.append("<io:output>")
5353
new_lines.append("```")
54-
inside = 2
55-
elif line == "```" and inside == 2:
54+
inside = 3
55+
elif line == "```":
5656
new_lines.append("```")
57-
new_lines.append("</io>")
58-
inside = 0
57+
if inside == 3:
58+
new_lines.append("</io:output>\n")
59+
new_lines.append("</io>")
60+
inside = 0
61+
elif inside == 1:
62+
new_lines.append("</io:input>\n")
63+
inside = 2
5964
else:
6065
new_lines.append(line)
6166
return new_lines
6267

6368

64-
class BoxBlockProcessor(BlockProcessor):
65-
RE_FENCE_START = r"<io>"
66-
RE_FENCE_END = r"</io>"
69+
class FencedBlockProcessor(BlockProcessor):
70+
RE_FENCE_START: str
71+
RE_FENCE_END: str
6772

68-
def test(self, parent, block) -> bool:
73+
def test(self, parent: Element, block: str) -> bool:
6974
return re.match(self.RE_FENCE_START, block) is not None
7075

76+
def fence_logic(self, parent: Element, blocks: list[str]) -> None:
77+
raise NotImplementedError()
78+
7179
def run(self, parent, blocks):
7280
original_block = blocks[0]
7381
blocks[0] = re.sub(self.RE_FENCE_START, "", blocks[0])
@@ -77,26 +85,60 @@ def run(self, parent, blocks):
7785
if re.search(self.RE_FENCE_END, block):
7886
# remove fence
7987
blocks[block_num] = re.sub(self.RE_FENCE_END, "", block)
80-
# render fenced area inside a new div
81-
e = SubElement(parent, "div")
82-
e.set("class", "io")
83-
inp = SubElement(e, "h3")
84-
inp.text = "Vstup"
85-
out = SubElement(e, "h3")
86-
out.text = "Výstup"
87-
88-
self.parser.parseBlocks(e, blocks[0 : block_num + 1])
88+
89+
self.fence_logic(parent, blocks[0 : block_num + 1])
90+
8991
# remove used blocks
90-
for i in range(0, block_num + 1):
92+
for _ in range(0, block_num + 1):
9193
blocks.pop(0)
9294
return True # or could have had no return statement
9395
# No closing marker! Restore and do nothing
9496
blocks[0] = original_block
9597
return False # equivalent to our test() routine returning False
9698

9799

100+
class BoxBlockProcessor(FencedBlockProcessor):
101+
RE_FENCE_START = r"<io>"
102+
RE_FENCE_END = r"</io>"
103+
104+
def fence_logic(self, parent, blocks):
105+
d = SubElement(parent, "div")
106+
d.set("class", "io")
107+
self.parser.parseBlocks(d, blocks)
108+
109+
110+
class IOInputBlockProcessor(FencedBlockProcessor):
111+
RE_FENCE_START = r"<io:input>"
112+
RE_FENCE_END = r"</io:input>"
113+
114+
def fence_logic(self, parent, blocks):
115+
d = SubElement(parent, "div")
116+
inp = SubElement(d, "h3")
117+
inp.text = "Vstup"
118+
self.parser.parseBlocks(d, blocks)
119+
120+
121+
class IOOutputBlockProcessor(FencedBlockProcessor):
122+
RE_FENCE_START = r"<io:output>"
123+
RE_FENCE_END = r"</io:output>"
124+
125+
def fence_logic(self, parent, blocks):
126+
d = SubElement(parent, "div")
127+
inp = SubElement(d, "h3")
128+
inp.text = "Výstup"
129+
self.parser.parseBlocks(d, blocks)
130+
131+
98132
class KspSchoolExtension(Extension):
99133
def extendMarkdown(self, md: Markdown) -> None: # noqa: N802
100134
md.treeprocessors.register(SchoolImageTreeprocessor(md), "ksp_school_images", 0)
101135
md.preprocessors.register(CodeBlocksProcessor(md), "IO_code_blocks", 100000)
102-
md.parser.blockprocessors.register(BoxBlockProcessor(md.parser), "box", 100000)
136+
md.parser.blockprocessors.register(
137+
BoxBlockProcessor(md.parser), "IO_box", 100000
138+
)
139+
md.parser.blockprocessors.register(
140+
IOInputBlockProcessor(md.parser), "IO_input", 100000
141+
)
142+
md.parser.blockprocessors.register(
143+
IOOutputBlockProcessor(md.parser), "IO_output", 100000
144+
)

0 commit comments

Comments
 (0)