Skip to content

Commit 608fe13

Browse files
committed
Use {{nextsample}} instead of \nextsample
1 parent cc5f26e commit 608fe13

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

problemtools/md2html.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,9 @@ def convert(problem: str, options: argparse.Namespace) -> bool:
3535
statement_common.foreach_image(statement_path,
3636
lambda img_name: copy_image(problem, img_name))
3737

38-
with open (statement_path, "r") as source:
39-
statement_md = source.read()
40-
# Replace \nextsample with \\nextsample to avoid pandoc interpreting it as a newline
41-
statement_md = statement_md.replace('\\nextsample', '\\\\nextsample')
42-
statement_md = statement_md.replace('\\remainingsamples', '\\\\remainingsamples')
43-
with tempfile.NamedTemporaryFile(mode='w', suffix=".md") as temp_file:
44-
temp_file.write(statement_md)
45-
temp_file.flush()
46-
47-
command = ["pandoc", temp_file.name, "-t" , "html", "-f", "markdown-raw_html", "--mathjax"]
48-
statement_html = subprocess.run(command, capture_output=True, text=True,
49-
shell=False, check=True).stdout
38+
command = ["pandoc", statement_path, "-t" , "html", "-f", "markdown-raw_html", "--mathjax"]
39+
statement_html = subprocess.run(command, capture_output=True, text=True,
40+
shell=False, check=True).stdout
5041

5142

5243
templatepaths = [os.path.join(os.path.dirname(__file__), 'templates/markdown_html'),
@@ -69,7 +60,7 @@ def convert(problem: str, options: argparse.Namespace) -> bool:
6960

7061
samples = statement_common.format_samples(problem, to_pdf=False)
7162

72-
# Insert samples at \nextsample and \remainingsamples
63+
# Insert samples at {{nextsample}} and {{remainingsamples}}
7364
html_template, remaining_samples = statement_common.inject_samples(html_template, samples, "")
7465

7566
# Insert the remaining samples at the bottom

problemtools/statement_common.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -106,27 +106,27 @@ def get_problem_name(problem: str, language: Optional[str]) -> Optional[str]:
106106

107107

108108
def inject_samples(html, samples, sample_separator):
109-
"""Injects samples at occurences of \nextsample and \remainingsamples
109+
"""Injects samples at occurences of {{nextsample}} and {{remainingsamples}}
110110
Non-destructive, returns the new html and all left-over samples
111111
112112
Returns:
113113
"""
114114

115115
while True:
116-
match = re.search(r'\\(nextsample|remainingsamples)', html)
116+
match = re.search(r'\{\{(nextsample|remainingsamples)\}\}', html)
117117
if not match:
118118
break
119119
matched_text = match.group(1)
120120
if matched_text == "nextsample" and len(samples) == 0:
121-
raise Exception("Error: called \\nextsample without any samples left")
121+
raise Exception("Error: called {{nextsample}} without any samples left")
122122

123123
num_inject = 1 if matched_text == "nextsample" else len(samples)
124124
to_inject = sample_separator.join(samples[:num_inject])
125125
samples = samples[num_inject:]
126126

127127
# Always inject, even if to_inject is empty
128-
# This will remove all occurences of \nextsample and \remainingsamples
129-
# (And also properly throw an error if \nextsample is called with no samples left)
128+
# This will remove all occurences of {{nextsample}} and {{remainingsamples}}
129+
# (And also properly throw an error if {{nextsample}} is called with no samples left)
130130
html = html[:match.start()] + to_inject + html[match.end():]
131131

132132
#print(html)

0 commit comments

Comments
 (0)