Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

326 warn if problemtitle differs from problemsettingsnamelanguage #329

18 changes: 18 additions & 0 deletions bin/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,21 @@ def build_contest_pdfs(contest, problems, tmpdir, lang=None, solutions=False, we
return all(
build_contest_pdf(contest, problems, tmpdir, lang, solutions, web) for lang in languages
)

def get_argument_for_command(texfile, command):
""" Return the (whitespace-normalised) argument for the given command in the given texfile.
If texfile contains `\foo{bar baz }`, returns the string 'bar baz'.
The command is given without backslash.


Assumptions:
the command and its argument are on the same line,
and that the argument contains no closing curly brackets.
"""

for line in texfile:
regex = r"\\" + command + r"\{(.*)\}"
match = re.search(regex, line)
if match:
return ' '.join(match.group(1).split())
return None
31 changes: 26 additions & 5 deletions bin/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pathlib import Path

import config
import latex
import parallel
import program
import run
Expand Down Expand Up @@ -58,19 +59,39 @@ def _determine_statement_languages(self):
"""
if isinstance(self.settings.name, str):
self.settings.name = {'en': self.settings.name}
yamlnames = set(self.settings.name)
texfiles = set(
yamllangs = set(self.settings.name)
texlangs = set(
path.suffixes[0][1:] for path in glob(self.path, 'problem_statement/problem.*.tex')
)
for lang in texfiles - yamlnames:
for lang in texlangs - yamllangs:
error(
f"{self.name}: Found problem.{lang}.tex, but no corresponding name in problem.yaml."
)
for lang in yamlnames - texfiles:
for lang in yamllangs - texlangs:
error(
f"{self.name}: Found name for language {lang} in problem.yaml, but not problem.{lang}.tex."
)
return sorted(texfiles & yamlnames)
# Check that names in problem.yaml and \problemname{} in problem.*.tex agree:
for lang in texlangs & yamllangs:
thorehusfeldt marked this conversation as resolved.
Show resolved Hide resolved
unnormalised_yamlname = self.settings.name[lang]
yamlname = ' '.join(unnormalised_yamlname.split())
with open(self.path / 'problem_statement' / f'problem.{lang}.tex') as texfile:
match texname := latex.get_argument_for_command(texfile, 'problemname'):
case None:
error(rf"No \problemname found in problem.{lang}.tex")
continue
case '\problemname':
continue
case s if '\\' in s or '_' in s or '^' in s:
# texname contains markup, like "CO_2" or "\emph{Hello}":
# Assume authors know what they're doing
continue
case s if s != yamlname:
warn(f'Problem titles in problem.{lang}.tex ({texname})' +
f' and problem.yaml ({yamlname}) differ;' +
r' consider using \problemname{\problemyamlname}.'
)
return sorted(texlangs & yamllangs)

def _read_settings(self):
# some defaults
Expand Down
1 change: 1 addition & 0 deletions test/problems/boolfind/problem_statement/problem.en.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\problemname{\problemyamlname}
3 changes: 3 additions & 0 deletions test/problems/different/problem.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# problem.yaml


name:
en: A Different Problem
## At least one of author, source, or rights_owner must be provided.
##
## Author of the problem (default: null)
Expand Down
1 change: 1 addition & 0 deletions test/problems/fltcmp/problem_statement/problem.en.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\problemname{\problemyamlname}
2 changes: 2 additions & 0 deletions test/problems/guess/problem.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
name:
en: Guess the Number
source: Kattis
license: cc by-sa

Expand Down
1 change: 1 addition & 0 deletions test/problems/hello/problem_statement/problem.en.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\problemname{\problemyamlname}
1 change: 1 addition & 0 deletions test/problems/hellounix/problem_statement/problem.en.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\problemname{\problemyamlname}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\problemname{\problemyamlname}
Loading