Skip to content

Commit 7582bfb

Browse files
committed
md->pdf and Reorganize code
1 parent a6eb7fd commit 7582bfb

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

problemtools/problem2html.py

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import string
66
import argparse
77
import subprocess
8-
from typing import Optional
98

109
from . import tex2html
1110
from . import md2html

problemtools/problem2pdf.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -79,39 +79,50 @@ def latex2pdf(options: argparse.Namespace) -> bool:
7979
problembase = os.path.splitext(os.path.basename(problem_root))[0]
8080
destfile = string.Template(options.destfile).safe_substitute(problem=problembase)
8181

82-
# We skip PDF check when verifying problems with markdown statements
83-
if os.path.isfile(os.path.join(problem, "problem_statement", "problem.%s.md" % options.language)) and ignore_markdown:
84-
return True
82+
if statement_common.find_statement_extension(problem_root, language=options.language) == "md":
83+
statement_path = statement_common.find_statement(problem_root, extension="md", language=options.language)
8584

8685
# Set up template if necessary
8786
with template.Template(problem_root, language=options.language) as templ:
8887
texfile = templ.get_file_name()
8988

90-
origcwd = os.getcwd()
89+
statement_md += samples_md
90+
with tempfile.NamedTemporaryFile(mode='w', suffix=".md") as temp_file:
91+
temp_file.write(statement_md)
92+
temp_file.flush()
93+
# Do .read so that the file isn't deleted until pandoc is done
94+
os.popen(f"pandoc {temp_file.name} -o {problembase}.pdf --resource-path={statement_dir}").read()
95+
96+
else:
97+
# Set up template if necessary
98+
with template.Template(problem_root, language=options.language) as templ:
99+
texfile = templ.get_file_name()
100+
101+
origcwd = os.getcwd()
91102

92-
os.chdir(os.path.dirname(texfile))
93-
params = ['pdflatex', '-interaction=nonstopmode']
94-
output = None
95-
if options.quiet:
96-
output = open(os.devnull, 'w')
97-
if options.nopdf:
98-
params.append('-draftmode')
103+
os.chdir(os.path.dirname(texfile))
104+
params = ['pdflatex', '-interaction=nonstopmode']
105+
output = None
106+
if options.quiet:
107+
output = open(os.devnull, 'w')
108+
if options.nopdf:
109+
params.append('-draftmode')
99110

100-
params.append(texfile)
111+
params.append(texfile)
101112

102-
status = subprocess.call(params, stdout=output)
103-
if status == 0:
104113
status = subprocess.call(params, stdout=output)
114+
if status == 0:
115+
status = subprocess.call(params, stdout=output)
105116

106-
if output is not None:
107-
output.close()
117+
if output is not None:
118+
output.close()
108119

109-
os.chdir(origcwd)
120+
os.chdir(origcwd)
110121

111-
if status == 0 and not options.nopdf:
112-
shutil.move(os.path.splitext(texfile)[0] + '.pdf', destfile)
122+
if status == 0 and not options.nopdf:
123+
shutil.move(os.path.splitext(texfile)[0] + '.pdf', destfile)
113124

114-
return status == 0
125+
return status == 0
115126

116127

117128
def get_parser() -> argparse.ArgumentParser:

problemtools/statement_common.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import os
2+
<<<<<<< HEAD
23
from typing import Optional, List
34
import html
45
import tempfile
56
import subprocess
67
import re
78
import json
89
from pathlib import Path
10+
=======
11+
from typing import Optional
12+
import html
13+
>>>>>>> 5e2aecf (md->pdf and Reorganize code)
914

1015
from . import verifyproblem
1116

@@ -29,7 +34,6 @@ def find_statement(problem_root: str, extension: str, language: Optional[str]) -
2934

3035
def find_statement_extension(problem_root: str, language: Optional[str]) -> str:
3136
"""Given a language, find whether the extension is tex or md
32-
3337
Args:
3438
problem_root: path to problem root
3539
"""

problemtools/verifyproblem.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from . import problem2html
3131
from . import statement_common
3232
from . import formatversion
33+
from . import statement_common
3334

3435
from . import config
3536
from . import languages
@@ -1039,7 +1040,7 @@ def __init__(self, problem: Problem):
10391040
self._problem = problem
10401041
self.languages = []
10411042
glob_path = os.path.join(problem.probdir, 'problem_statement', 'problem.')
1042-
for extension in problem2html.SUPPORTED_EXTENSIONS:
1043+
for extension in statement_common.SUPPORTED_EXTENSIONS:
10431044
if glob.glob(glob_path + extension):
10441045
self.languages.append('')
10451046
for f in glob.glob(glob_path + '[a-z][a-z].%s' % extension):
@@ -1065,7 +1066,7 @@ def check(self, context: Context) -> bool:
10651066
options.language = lang
10661067
options.nopdf = True
10671068
options.quiet = True
1068-
if not problem2pdf.convert(options, ignore_markdown=True):
1069+
if not problem2pdf.convert(options):
10691070
langparam = f' --language {lang}' if lang != '' else ''
10701071
self.error(f'Could not compile problem statement for language "{lang}". Run problem2pdf{langparam} on the problem to diagnose.')
10711072
except Exception as e:
@@ -1087,7 +1088,7 @@ def __str__(self) -> str:
10871088

10881089
def get_config(self) -> dict[str, dict[str, str]]:
10891090
ret: dict[str, dict[str, str]] = {}
1090-
for extension in problem2html.SUPPORTED_EXTENSIONS:
1091+
for extension in statement_common.SUPPORTED_EXTENSIONS:
10911092
for lang in self.languages:
10921093
filename = f'problem.{lang}.{extension}' if lang != '' else 'problem.{extension}'
10931094
if not os.path.isfile(filename):

0 commit comments

Comments
 (0)