Skip to content
Snippets Groups Projects
Commit 0a2d50f2 authored by tuhe's avatar tuhe
Browse files

tentative rst support

parent 18bd4a9f
Branches
No related tags found
No related merge requests found
Pipeline #11628 passed
from snipper.load_citations import find_tex_cite
from snipper.legacy import COMMENT
def fix_citations(lines, references, strict=True, file=None):
"""
Tue 2023: This appears to be the main entry point of the reference-file. I.e. it is the only one that seems directly called by snip_dir.
def fix_citations(lines, references, strict=True):
I also pass the input file along (although this should not be edited) to allow context-dependent actions.
I.e. rst files should get rst references.
"""
if str(file).endswith(".rst"):
print(file)
lines = fix_aux(lines, aux=references.get('aux', {}) )
for cm in references.get('commands', []):
# Probably just go with this one.
lines = fix_aux_special(lines, aux=cm['aux'], command=cm['command'], output=cm['output'])
lines = fix_bibtex(lines, bibtex=references.get('bibtex', {}))
# rst_mode = str(file).endswith(".rst")
lines = fix_bibtex(lines, bibtex=references.get('bibtex', {}), rst_mode=str(file).endswith(".rst"))
return lines
......@@ -18,11 +30,16 @@ def fix_aux_special(lines, aux, command='\\nref', output='\cite[%s]{my_bibtex_en
return l2
def fix_aux(lines, aux, strict=True):
print("fix_cite.py/fix_aux() deprecated...")
l2 = fix_single_reference(lines, aux=aux, cmd="\\ref", strict=True)
# print("\n".join(l2))
return l2
def fix_bibtex(lines, bibtex):
def fix_bibtex(lines, bibtex, rst_mode=False):
if rst_mode:
a = 234
pass
s = "\n".join(lines)
i = 0
all_refs = []
......@@ -36,10 +53,14 @@ def fix_bibtex(lines, bibtex):
raise IndexError("no such reference: " + reference)
ref = bibtex[reference]
label = ref['label']
rtxt = f"({label}" + (", "+txt if txt is not None else "") + ")"
pageno = (", " + txt if txt is not None else "")
rtxt = f"({label}" + pageno + ")"
r = ref['plain']
if r not in all_refs:
all_refs.append(r)
if rst_mode:
rtxt = f"(:cite:t:`{reference}`{pageno})"
s = s[:i] + rtxt + s[j+1:]
i = i + len(rtxt)
......
......@@ -10,7 +10,8 @@ def get_aux(auxfile):
# paths = get_paths()
# auxfile = os.path.join(paths['02450public'], auxfile)
auxfile = os.path.normpath(auxfile) # Unsure if this is required. But had problems finding the auxfile on git.
auxfile = auxfile.replace(os.sep, '/')
# auxfile = auxfile.replace(os.sep, '/')
auxfile = auxfile.replace("\\", '/') # Fucking windows.
if not os.path.exists(auxfile):
# print(auxfile)
......
......@@ -68,9 +68,23 @@ def snip_dir(source_dir, # Sources
n = 0
cutouts = {}
for f, accept in acceptable:
if os.path.isdir(f) or not str(f).endswith(".py") or str(f).endswith("_grade.py"):
if os.path.isdir(f) or str(f).endswith("_grade.py"):
continue
if accept:
# if os.path.isdir(f) or not str(f).endswith(".py") or str(f).endswith("_grade.py"):
# continue
# if accept and str(f).endswith(".rst"):
# nrem, cut = censor_file(f, run_files=run_files, run_out_dirs=output_dir, cut_files=cut_files,
# # solution_list=solution_list,
# base_path=dest_dir,
# references=references,
# license_head=license_head,
# censor_files=censor_files,
# **kwargs)
# print("rst file")
if accept and (str(f).endswith(".py") or str(f).endswith(".rst")):
# if f.endswith("rst"):
# pass
solution_list = []
kwargs = {}
if verbose:
......
......@@ -34,7 +34,10 @@ def censor_file(file, run_files=True, run_out_dirs=None, cut_files=True,
strict=True,
references=None,
license_head=None):
if str(file).endswith("rst"):
assert not run_files and not cut_files and not censor_files
print(file)
if references == None:
references = {}
......@@ -50,7 +53,10 @@ def censor_file(file, run_files=True, run_out_dirs=None, cut_files=True,
lines[k] = l.replace("# !", "#!")
try:
lines = fix_citations(lines, references, strict=strict)
# if str(file).endswith("rst"):
# print(file)
lines = fix_citations(lines, references, strict=strict, file=file)
except IndexError as e:
print(e)
print("Error in file, cite/reference tag not found!>", file)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment