After converting .md files to .Rmd (using mmv), several cleanup steps are required to ensure compatibility with knitr and bookdown. These steps use mreplace to fix code blocks, quotes, and cross-references.
Fix Code Blocks and Quotes
- Convert
{.r} to {r}. - Replace AsciiDoc smart quote markers (
RSQUO, RDQUO, LDQUO) with standard single (') or double (") quotes.
Fix Figure, Recipe, and Chapter References
knitr does not support underscores in numbered references. You must rename identifiers from NAME_X_Y to NAME-X-Y and update the reference syntax.
- Figures: Rename
FIG_X_Y to FIG-X-Y and change references to Figure \@ref(fig:...). - Recipes: Rename
RECIPE_X_Y to RECIPE-X-Y and change references to Recipe \@ref(RECIPE...). - Chapters: Rename
CHAPTER_X_Y to CHAPTER-X-Y and change references to Chapter \@ref(CHAPTER...).
Note: For Figure, Recipe, and Chapter renames, run the command repeatedly until no more matches are found.
Remove Labels and Fix Math Delimiters
- Remove Problem/Solution/See Also/Discussion labels (e.g.,
{#_problem_1}). - Replace
$$_$$ patterns with a single underscore _.
cd ../rgcookbook2e
mmv -e \\.md .Rmd
# Fix code blocks
mreplace -e " \{.r\}" "{r}" *.Rmd
# Fix smart quote markers
mreplace -e "RSQUO" "'" *.Rmd
mreplace -e "RDQUO" '"' *.Rmd
mreplace -e "LDQUO" '"' *.Rmd
# Rename FIG_X_Y to FIG-X-Y (Run repeatedly)
mreplace -e "FIG([^ \n]*?)_" "FIG\\1-" *.Rmd
mreplace -e "\\[figure\\\\_title\\]\(#" "Figure \\@ref(fig:" *.Rmd
# Rename RECIPE_X_Y to RECIPE-X-Y (Run repeatedly)
mreplace -e "RECIPE([^ \n]*?)_" "RECIPE\\1-" *.Rmd
mreplace -e "\\[.*?\]\(#RECIPE" "Recipe \\@ref(RECIPE" *.Rmd
# Rename CHAPTER_X_Y to CHAPTER-X-Y (Run repeatedly)
mreplace -e "CHAPTER([^ \n]*?)_" "CHAPTER\\1-" *.Rmd
mreplace -e "\\[.*?\]\(#CHAPTER" "Chapter \\@ref(CHAPTER" *.Rmd
# Remove Problem/Solution/See Also/Discussion labels
mreplace " +{#_(problem|solution|see_also|discussion)_\\d+}" "" *.Rmd
# Replace "$$\u005F$$" with "_"
mreplace "(\w+)\\\$\\\$\_\\\$\\\\\$\\(\w+)" "\1_\2" *.Rmd