From ccda9af14a525112ba006c426a8d18dfe09b7cb8 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 27 Jun 2023 12:12:38 -0400 Subject: [PATCH] Fixes #1071 auto-numbering does not mess up static images The auto-numbering algorithm for Figures assumes that the caption is below the figure. In the qualification example, it was above. The fix renders a more robust way of including the bookmarks before the figure without needing a placeholder for the md figure inclusion line --- R/utilities-writing-report.R | 43 +++++++++++-------- .../utilities-report/testReportNumFigs.md | 11 ----- .../utilities-report/testReportNumSecs.md | 13 +----- .../data/utilities-report/testReportNumToc.md | 14 +----- 4 files changed, 26 insertions(+), 55 deletions(-) diff --git a/R/utilities-writing-report.R b/R/utilities-writing-report.R index 9f0f5818..5c27c230 100644 --- a/R/utilities-writing-report.R +++ b/R/utilities-writing-report.R @@ -667,17 +667,33 @@ updateArtifactNumbers <- function(fileContent, pattern, replacement, anchorId, c updatedFileContent <- NULL count <- 1 titleIndex <- 1 - artifactContent <- NULL for (lineIndex in seq_along(fileContent)) { # Counting is performed within sections # Need to reset count at lines of titles if (lineIndex %in% titleLines) { count <- 1 } + # Get section number of figure as last value lower than line index + # If no value found, section is empty and figure count is only global count + section <- tail(titleNumbers[titleLines < lineIndex], 1) + artifactNumber <- paste(c(section, count), collapse = "-") + # Create reference anchor with id matching figure number + anchorContent <- anchor(paste(anchorId, artifactNumber, sep = "-")) + # Assess if artifact from first line element firstElement <- getFirstLineElement(fileContent[lineIndex]) - # Place holder for artifact content, if caption is below - if(all(captionBelow, grepl(pattern = "\\!\\[", x = firstElement))){ - artifactContent <- fileContent[lineIndex] + # Artifact is Figure and update is called within Figure updates + # Otherwise, the function will also update figures for with Table reference + figureRequireUpdate <- all( + grepl(pattern = "\\!\\[", x = firstElement), + grepl(pattern = "Figure", x = pattern) + ) + if(figureRequireUpdate){ + updatedFileContent <- c( + updatedFileContent, + anchorContent, + "", + fileContent[lineIndex] + ) next } # If line is not related to an artifact, nothing to update @@ -685,15 +701,9 @@ updateArtifactNumbers <- function(fileContent, pattern, replacement, anchorId, c updatedFileContent <- c(updatedFileContent, fileContent[lineIndex]) next } - # Get section number of figure as last value lower than line index - # If no value found, section is empty and figure count is only global count - section <- tail(titleNumbers[titleLines < lineIndex], 1) - artifactNumber <- paste(c(section, count), collapse = "-") - - # Create reference anchor with id matching figure/table number - anchorContent <- anchor(paste(anchorId, artifactNumber, sep = "-")) - + # Else line starts by Figure or Table # Update caption with appropriate figure/table count + # TODO: should we prevent auto-numbering when users included their own numbering ? updatedArtifactContent <- gsub( pattern = pattern, replacement = paste0(replacement, " ", artifactNumber, ":"), @@ -703,17 +713,12 @@ updateArtifactNumbers <- function(fileContent, pattern, replacement, anchorId, c # Updated file content includes reference, figure/table and intra-section numbering updatedFileContent <- c( updatedFileContent, - "", - anchorContent, - "", - # if caption is not below, artifactContent is NULL - artifactContent, + # in case of caption below, the anchor was already included before artifact (figure) + anchorContent[!captionBelow], "", updatedArtifactContent ) - count <- count + 1 - artifactContent <- NULL } return(updatedFileContent) } diff --git a/tests/data/utilities-report/testReportNumFigs.md b/tests/data/utilities-report/testReportNumFigs.md index ff428580..497ea34b 100644 --- a/tests/data/utilities-report/testReportNumFigs.md +++ b/tests/data/utilities-report/testReportNumFigs.md @@ -7,17 +7,11 @@ ## Sub title 1 - - - Figure 1-1: this is figure 1 ## Sub title 2 - - - Figure 1-2: this is figure 2 @@ -28,18 +22,13 @@ Figure 1-2: this is figure 2 ## Sub title 1 - - Table 2-1: this is table 1 ## Sub title 2 - - - Figure 2-1: this is figure 3 diff --git a/tests/data/utilities-report/testReportNumSecs.md b/tests/data/utilities-report/testReportNumSecs.md index 38d32061..15585340 100644 --- a/tests/data/utilities-report/testReportNumSecs.md +++ b/tests/data/utilities-report/testReportNumSecs.md @@ -7,15 +7,9 @@ ## Sub title 1 - - - Figure 1-1: this is figure 1 -## 1.1 Sub title 2 - - - +## Sub title 2 Figure 1-2: this is figure 2 @@ -28,18 +22,13 @@ Figure 1-2: this is figure 2 ## 2.1 Sub title 1 - - Table 2-1: this is table 1 ## 2.2 Sub title 2 - - - Figure 2-1: this is figure 3 diff --git a/tests/data/utilities-report/testReportNumToc.md b/tests/data/utilities-report/testReportNumToc.md index b389ff1d..9e89e181 100644 --- a/tests/data/utilities-report/testReportNumToc.md +++ b/tests/data/utilities-report/testReportNumToc.md @@ -1,7 +1,6 @@ # Table of Contents * [1 Title 1](#title-1) - * [1.1 Sub title 2](#figure-1-1) * [2 Title 2](#title-2) * [2.1 Sub title 1](#subtitle-21) * [2.2 Sub title 2](#table-2-1) @@ -16,15 +15,9 @@ ## Sub title 1 - - - Figure 1-1: this is figure 1 -## 1.1 Sub title 2 - - - +## Sub title 2 Figure 1-2: this is figure 2 @@ -37,18 +30,13 @@ Figure 1-2: this is figure 2 ## 2.1 Sub title 1 - - Table 2-1: this is table 1 ## 2.2 Sub title 2 - - - Figure 2-1: this is figure 3