From 0a2d50f290217a248ca5681152113dcb6cbefdd0 Mon Sep 17 00:00:00 2001 From: Tue Herlau <tuhe@dtu.dk> Date: Mon, 2 Jan 2023 16:37:58 +0100 Subject: [PATCH] tentative rst support --- src/snipper/fix_cite.py | 29 +++++++++++++++++++++++++---- src/snipper/load_citations.py | 3 ++- src/snipper/snip_dir.py | 18 ++++++++++++++++-- src/snipper/snipper_main.py | 8 +++++++- 4 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/snipper/fix_cite.py b/src/snipper/fix_cite.py index 0d46a3e..a76120a 100644 --- a/src/snipper/fix_cite.py +++ b/src/snipper/fix_cite.py @@ -1,13 +1,25 @@ 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) diff --git a/src/snipper/load_citations.py b/src/snipper/load_citations.py index f645b0a..18bedf7 100644 --- a/src/snipper/load_citations.py +++ b/src/snipper/load_citations.py @@ -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) diff --git a/src/snipper/snip_dir.py b/src/snipper/snip_dir.py index 431ab41..b538303 100644 --- a/src/snipper/snip_dir.py +++ b/src/snipper/snip_dir.py @@ -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: diff --git a/src/snipper/snipper_main.py b/src/snipper/snipper_main.py index 1fab5eb..ff5b299 100644 --- a/src/snipper/snipper_main.py +++ b/src/snipper/snipper_main.py @@ -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) -- GitLab