Created
October 2, 2024 21:56
-
-
Save webbedfeet/b885f11351ec0029d1da446457ca39e4 to your computer and use it in GitHub Desktop.
Convert RMarkdown chunk labels to Quarto chunk labels, adding labels if they're not present
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re, os | |
regex = "```\{r (.+)\}" | |
def name_chunks(f): | |
""" | |
This function takes a quarto (.qmd) file, checks for rmarkdown-style | |
labels, and converts to quarto-style labels. If a quarto-style label | |
exists, it preserves it (it checks the first line of the chunk metadata). | |
If no label exists, it creates one based on the file name, in sequential order | |
in the format "<file name>-i" where the ".*" is removed from the file name | |
INPUT: A file name as a string | |
OUTPUT: A list of strings that can be used in writelines() | |
""" | |
if re.search("qmd$", f) is None: | |
raise NameError("Input file must be a Quarto source file") | |
with open(f, 'r') as file: | |
txt = file.readlines() | |
chunk_prefix = os.path.basename(f).replace('.qmd','') | |
indx = 0 | |
for i, l in enumerate(txt): | |
if re.search('```\{r', l) is not None: # Start of a chunk | |
if re.search(regex, l) is not None: # rmd label | |
lbl = re.search(regex, l).group(1) | |
else: # no rmd label | |
indx +=1 | |
lbl = chunk_prefix + '-' + str(indx) | |
if re.search('label', txt[i+1]) is None: # Check that label doesn't exist | |
txt[i] = "```{r}\n#| label: "+ lbl + "\n" | |
else: | |
txt[i] = "```{r}\n" | |
return txt | |
# if __name__ =="__main__": | |
# qfiles = glob(str(pyhere.here('chapters/*.qmd'))) | |
# for q in qfiles: | |
# txt = name_chunks(q) | |
# with open(q, 'w') as f: | |
# f.writelines(txt) # Over-write file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment