Skip to content

Commit

Permalink
Don't remove other comments on line with code annotation in LaTeX
Browse files Browse the repository at this point in the history
Adapt regex for code annotation replacement in LaTeX to keep other comments on the same line.
  • Loading branch information
cderv committed Nov 14, 2023
1 parent 80b3705 commit 8cd9484
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions news/changelog-1.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
- ([#6990](https://github.com/quarto-dev/quarto-cli/issues/6990)): Fix an issue where underscore in `filename` code cell attribute were not escaped.
- ([#7175](https://github.com/quarto-dev/quarto-cli/issues/7175)): Fix an issue with code annotations when more than one digit is used for annotation number.
- ([#7267](https://github.com/quarto-dev/quarto-cli/issues/7267)): Fix issue with longtable environments interfering with the `table` counter.
- ([#7568](https://github.com/quarto-dev/quarto-cli/issues/7568)): Code annotation now works in LaTeX document when having other comments on same line.

## Docusaurus Format

Expand Down
9 changes: 7 additions & 2 deletions src/format/pdf/format-pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ const placePandocBibliographyEntries = (
};

const kCodeAnnotationRegex =
/(.*)\\CommentTok\{.* \\textless\{\}(\d+)\\textgreater\{\}.*\}$/gm;
/(.*)\\CommentTok\{(.*?)[^\s]+? \\textless\{\}(\d+)\\textgreater\{\}.*\}$/gm;
const kCodePlainAnnotationRegex = /(.*)% \((\d+)\)$/g;
const codeAnnotationPostProcessor = () => {
let lastAnnotation: string | undefined;
Expand All @@ -971,9 +971,14 @@ const codeAnnotationPostProcessor = () => {
// Replace colorized code
line = line.replaceAll(
kCodeAnnotationRegex,
(_match, prefix: string, annotationNumber: string) => {
(_match, prefix: string, comment: string, annotationNumber: string) => {
if (annotationNumber !== lastAnnotation) {
lastAnnotation = annotationNumber;
if (comment.length > 0) {
// There is something else inside the comment line so
// We need to recreate the comment line without the annotation
prefix = `${prefix}\\CommentTok\{${comment}\}`;
}
return `${prefix}\\hspace*{\\fill}\\NormalTok{\\circled{${annotationNumber}}}`;
} else {
return `${prefix}`;
Expand Down
2 changes: 1 addition & 1 deletion src/resources/filters/quarto-pre/code-annotation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ function processLaTeXAnnotation(line, annoteNumber, annotationProvider)
if hasHighlighting then
-- highlighting is enabled, allow the comment through
local placeholderComment = annotationProvider.createComment("<" .. tostring(annoteNumber) .. ">")
local replaced = annotationProvider.replaceAnnotation(line, annoteNumber, percentEscape(placeholderComment))
local replaced = annotationProvider.replaceAnnotation(line, annoteNumber, percentEscape(" " .. placeholderComment))
return replaced
else
-- no highlighting enabled, ensure we use a standard comment character
Expand Down
40 changes: 40 additions & 0 deletions tests/docs/smoke-all/2023/11/14/7568.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
format: latex
_quarto:
tests:
pdf: null
latex:
ensureFileRegexMatches:
-
- '\\#mutate\(speed \= speed \* 2\)'
- '\\CommentTok\{\\# with a comment \}'
- '\\CommentTok\{// return \}'
- '\\CommentTok\{// let greeting \= name \+ hello; \}'
- '\\circled\{1\}'
- '\\circled\{2\}'
- '\\circled\{3\}'
- []
---

```r
library(dplyr)
cars %>% # <1>
#mutate(speed = speed * 2) %>% # <2>
mutate(speed = speed * 4) # with a comment # <3>
```

1. test1
2. test2
3. test3

```js
let name = "Bingo"; // <1>
name;
let hello = " dit bonjour&nbsp;!";
hello; // return // <2>
// let greeting = name + hello; // <3>
```

1. test1
2. test2
3. test3

0 comments on commit 8cd9484

Please sign in to comment.