Skip to content

Commit

Permalink
Fixes Open-Systems-Pharmacology#1071 auto-numbering does not mess up …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
pchelle committed Jun 27, 2023
1 parent fd5ecd8 commit ccda9af
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 55 deletions.
43 changes: 24 additions & 19 deletions R/utilities-writing-report.R
Original file line number Diff line number Diff line change
Expand Up @@ -667,33 +667,43 @@ 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
if (!grepl(pattern = pattern, x = firstElement)) {
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, ":"),
Expand All @@ -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)
}
Expand Down
11 changes: 0 additions & 11 deletions tests/data/utilities-report/testReportNumFigs.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@
## Sub title 1


<a id="figure-1-1"></a>


Figure 1-1: this is figure 1

## Sub title 2


<a id="figure-1-2"></a>


Figure 1-2: this is figure 2

<a id="title-2"></a>
Expand All @@ -28,18 +22,13 @@ Figure 1-2: this is figure 2

## Sub title 1


<a id="table-2-1"></a>


Table 2-1: this is table 1

## Sub title 2


<a id="figure-2-1"></a>


Figure 2-1: this is figure 3

<a id="subtitle-23"></a>
Expand Down
13 changes: 1 addition & 12 deletions tests/data/utilities-report/testReportNumSecs.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,9 @@
## Sub title 1


<a id="figure-1-1"></a>


Figure 1-1: this is figure 1

## 1.1 Sub title 2


<a id="figure-1-2"></a>
## Sub title 2


Figure 1-2: this is figure 2
Expand All @@ -28,18 +22,13 @@ Figure 1-2: this is figure 2

## 2.1 Sub title 1


<a id="table-2-1"></a>


Table 2-1: this is table 1

## 2.2 Sub title 2


<a id="figure-2-1"></a>


Figure 2-1: this is figure 3

<a id="subtitle-23"></a>
Expand Down
14 changes: 1 addition & 13 deletions tests/data/utilities-report/testReportNumToc.md
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -16,15 +15,9 @@
## Sub title 1


<a id="figure-1-1"></a>


Figure 1-1: this is figure 1

## 1.1 Sub title 2


<a id="figure-1-2"></a>
## Sub title 2


Figure 1-2: this is figure 2
Expand All @@ -37,18 +30,13 @@ Figure 1-2: this is figure 2

## 2.1 Sub title 1


<a id="table-2-1"></a>


Table 2-1: this is table 1

## 2.2 Sub title 2


<a id="figure-2-1"></a>


Figure 2-1: this is figure 3

<a id="subtitle-23"></a>
Expand Down

0 comments on commit ccda9af

Please sign in to comment.