Skip to content

Commit 5accf4e

Browse files
committed
lua - fix lift-code-blocks to allow code cell merging
1 parent 2570e28 commit 5accf4e

File tree

3 files changed

+295
-14
lines changed

3 files changed

+295
-14
lines changed

src/resources/filters/quarto-post/foldcode.lua

+19-14
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,23 @@ function render_folded_block(block)
3232
div.content:insert(pandoc.RawBlock("html", "</details>"))
3333
return div
3434
end
35-
local make_code_cell_scaffold = function(div)
36-
return pandoc.Div({ block }, pandoc.Attr("", { "quarto-scaffold" }))
37-
end
38-
if not block.attr.classes:includes("cell-code") then
39-
return nil
40-
end
41-
if not (_quarto.format.isHtmlOutput() or _quarto.format.isMarkdownWithHtmlOutput()) then
42-
return make_code_cell_scaffold(block)
35+
if (not block.attr.classes:includes("cell-code") or
36+
(not (_quarto.format.isHtmlOutput() or
37+
_quarto.format.isMarkdownWithHtmlOutput()))) then
38+
return block, false
4339
end
4440
local fold = foldAttribute(block)
4541
local summary = summaryAttribute(block)
4642
if fold ~= nil or summary ~= nil then
4743
block.attr.attributes["code-fold"] = nil
4844
block.attr.attributes["code-summary"] = nil
4945
if fold ~= "none" then
50-
return make_code_fold_html(fold, summary)
46+
return make_code_fold_html(fold, summary), true
5147
else
52-
return block
48+
return block, false
5349
end
5450
else
55-
return block
51+
return block, false
5652
end
5753
end
5854

@@ -77,15 +73,23 @@ function fold_code_and_lift_codeblocks()
7773
return nil, false
7874
end,
7975
CodeBlock = function(block)
80-
local folded_block = make_scaffold(pandoc.Div, { render_folded_block(block) })
76+
local folded_block, did_fold = render_folded_block(block)
77+
local need_to_lift = did_fold or block.classes:includes("code-annotation-code")
78+
if need_to_lift then
79+
folded_block = make_scaffold(pandoc.Div, { folded_block } )
80+
end
8181
if block.classes:includes("code-annotation-code") then
8282
prev_annotated_code_block_scaffold = folded_block
8383
prev_annotated_code_block = block
8484
else
8585
prev_annotated_code_block_scaffold = nil
8686
end
87-
blocks:insert(folded_block)
88-
return {}
87+
if need_to_lift then
88+
blocks:insert(folded_block)
89+
return {}
90+
else
91+
return nil
92+
end
8993
end,
9094
Div = function(div)
9195
if not div.classes:includes("cell-annotation") then
@@ -102,6 +106,7 @@ function fold_code_and_lift_codeblocks()
102106
})
103107
if need_to_move_dl then
104108
assert(prev_annotated_code_block_scaffold)
109+
print(prev_annotated_code_block_scaffold)
105110
prev_annotated_code_block_scaffold.content:insert(div)
106111
return {}
107112
end
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: "Untitled"
3+
format: latex
4+
execute:
5+
warning: false
6+
_quarto:
7+
tests:
8+
latex:
9+
ensureSnapshotMatches: true
10+
---
11+
12+
```{r}
13+
#| label: tbl-tables
14+
#| tbl-cap: "Tables"
15+
#| tbl-subcap:
16+
#| - cars
17+
#| - pressure
18+
#| layout-ncol: 2
19+
20+
library(knitr)
21+
kable(head(cars))
22+
kable(head(pressure))
23+
```
24+
25+
See @tbl-tables for examples. In particular, @tbl-tables-2.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
% Options for packages loaded elsewhere
2+
\PassOptionsToPackage{unicode}{hyperref}
3+
\PassOptionsToPackage{hyphens}{url}
4+
\PassOptionsToPackage{dvipsnames,svgnames,x11names}{xcolor}
5+
%
6+
\documentclass[
7+
letterpaper,
8+
DIV=11,
9+
numbers=noendperiod]{scrartcl}
10+
11+
\usepackage{amsmath,amssymb}
12+
\usepackage{iftex}
13+
\ifPDFTeX
14+
\usepackage[T1]{fontenc}
15+
\usepackage[utf8]{inputenc}
16+
\usepackage{textcomp} % provide euro and other symbols
17+
\else % if luatex or xetex
18+
\usepackage{unicode-math}
19+
\defaultfontfeatures{Scale=MatchLowercase}
20+
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
21+
\fi
22+
\usepackage{lmodern}
23+
\ifPDFTeX\else
24+
% xetex/luatex font selection
25+
\fi
26+
% Use upquote if available, for straight quotes in verbatim environments
27+
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
28+
\IfFileExists{microtype.sty}{% use microtype if available
29+
\usepackage[]{microtype}
30+
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
31+
}{}
32+
\makeatletter
33+
\@ifundefined{KOMAClassName}{% if non-KOMA class
34+
\IfFileExists{parskip.sty}{%
35+
\usepackage{parskip}
36+
}{% else
37+
\setlength{\parindent}{0pt}
38+
\setlength{\parskip}{6pt plus 2pt minus 1pt}}
39+
}{% if KOMA class
40+
\KOMAoptions{parskip=half}}
41+
\makeatother
42+
\usepackage{xcolor}
43+
\setlength{\emergencystretch}{3em} % prevent overfull lines
44+
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
45+
% Make \paragraph and \subparagraph free-standing
46+
\ifx\paragraph\undefined\else
47+
\let\oldparagraph\paragraph
48+
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
49+
\fi
50+
\ifx\subparagraph\undefined\else
51+
\let\oldsubparagraph\subparagraph
52+
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
53+
\fi
54+
55+
\usepackage{color}
56+
\usepackage{fancyvrb}
57+
\newcommand{\VerbBar}{|}
58+
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
59+
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
60+
% Add ',fontsize=\small' for more characters per line
61+
\usepackage{framed}
62+
\definecolor{shadecolor}{RGB}{241,243,245}
63+
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
64+
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
65+
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
66+
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.40,0.45,0.13}{#1}}
67+
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
68+
\newcommand{\BuiltInTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
69+
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
70+
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
71+
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
72+
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
73+
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
74+
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
75+
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
76+
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
77+
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
78+
\newcommand{\ExtensionTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
79+
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
80+
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.28,0.35,0.67}{#1}}
81+
\newcommand{\ImportTok}[1]{\textcolor[rgb]{0.00,0.46,0.62}{#1}}
82+
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
83+
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
84+
\newcommand{\NormalTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
85+
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
86+
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
87+
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.68,0.00,0.00}{#1}}
88+
\newcommand{\RegionMarkerTok}[1]{\textcolor[rgb]{0.00,0.23,0.31}{#1}}
89+
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{#1}}
90+
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
91+
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
92+
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.07,0.07,0.07}{#1}}
93+
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.13,0.47,0.30}{#1}}
94+
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.37,0.37,0.37}{\textit{#1}}}
95+
96+
\providecommand{\tightlist}{%
97+
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}\usepackage{longtable,booktabs,array}
98+
\usepackage{calc} % for calculating minipage widths
99+
% Correct order of tables after \paragraph or \subparagraph
100+
\usepackage{etoolbox}
101+
\makeatletter
102+
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
103+
\makeatother
104+
% Allow footnotes in longtable head/foot
105+
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
106+
\makesavenoteenv{longtable}
107+
\usepackage{graphicx}
108+
\makeatletter
109+
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
110+
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
111+
\makeatother
112+
% Scale images if necessary, so that they will not overflow the page
113+
% margins by default, and it is still possible to overwrite the defaults
114+
% using explicit options in \includegraphics[width, height, ...]{}
115+
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
116+
% Set default figure placement to htbp
117+
\makeatletter
118+
\def\fps@figure{htbp}
119+
\makeatother
120+
121+
\KOMAoption{captions}{tableheading}
122+
\makeatletter
123+
\@ifpackageloaded{caption}{}{\usepackage{caption}}
124+
\AtBeginDocument{%
125+
\ifdefined\contentsname
126+
\renewcommand*\contentsname{Table of contents}
127+
\else
128+
\newcommand\contentsname{Table of contents}
129+
\fi
130+
\ifdefined\listfigurename
131+
\renewcommand*\listfigurename{List of Figures}
132+
\else
133+
\newcommand\listfigurename{List of Figures}
134+
\fi
135+
\ifdefined\listtablename
136+
\renewcommand*\listtablename{List of Tables}
137+
\else
138+
\newcommand\listtablename{List of Tables}
139+
\fi
140+
\ifdefined\figurename
141+
\renewcommand*\figurename{Figure}
142+
\else
143+
\newcommand\figurename{Figure}
144+
\fi
145+
\ifdefined\tablename
146+
\renewcommand*\tablename{Table}
147+
\else
148+
\newcommand\tablename{Table}
149+
\fi
150+
}
151+
\@ifpackageloaded{float}{}{\usepackage{float}}
152+
\floatstyle{ruled}
153+
\@ifundefined{c@chapter}{\newfloat{codelisting}{h}{lop}}{\newfloat{codelisting}{h}{lop}[chapter]}
154+
\floatname{codelisting}{Listing}
155+
\newcommand*\listoflistings{\listof{codelisting}{List of Listings}}
156+
\makeatother
157+
\makeatletter
158+
\makeatother
159+
\makeatletter
160+
\@ifpackageloaded{caption}{}{\usepackage{caption}}
161+
\@ifpackageloaded{subcaption}{}{\usepackage{subcaption}}
162+
\makeatother
163+
\ifLuaTeX
164+
\usepackage{selnolig} % disable illegal ligatures
165+
\fi
166+
\usepackage{bookmark}
167+
168+
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
169+
\urlstyle{same} % disable monospaced font for URLs
170+
\hypersetup{
171+
pdftitle={Untitled},
172+
colorlinks=true,
173+
linkcolor={blue},
174+
filecolor={Maroon},
175+
citecolor={Blue},
176+
urlcolor={Blue},
177+
pdfcreator={LaTeX via pandoc}}
178+
179+
\title{Untitled}
180+
\author{}
181+
\date{}
182+
183+
\begin{document}
184+
\maketitle
185+
186+
\begin{Shaded}
187+
\begin{Highlighting}[]
188+
\FunctionTok{library}\NormalTok{(knitr)}
189+
\FunctionTok{kable}\NormalTok{(}\FunctionTok{head}\NormalTok{(cars))}
190+
\FunctionTok{kable}\NormalTok{(}\FunctionTok{head}\NormalTok{(pressure))}
191+
\end{Highlighting}
192+
\end{Shaded}
193+
194+
\begin{table}
195+
196+
\caption{\label{tbl-tables}Tables}
197+
198+
\begin{minipage}{0.50\linewidth}
199+
200+
\subcaption{\label{tbl-tables-1}cars}
201+
202+
\centering{
203+
204+
\begin{tabular}{rr}
205+
\toprule
206+
speed & dist\\
207+
\midrule
208+
4 & 2\\
209+
4 & 10\\
210+
7 & 4\\
211+
7 & 22\\
212+
8 & 16\\
213+
9 & 10\\
214+
\bottomrule
215+
\end{tabular}
216+
217+
}
218+
219+
\end{minipage}%
220+
%
221+
\begin{minipage}{0.50\linewidth}
222+
223+
\subcaption{\label{tbl-tables-2}pressure}
224+
225+
\centering{
226+
227+
\begin{tabular}{rr}
228+
\toprule
229+
temperature & pressure\\
230+
\midrule
231+
0 & 0.0002\\
232+
20 & 0.0012\\
233+
40 & 0.0060\\
234+
60 & 0.0300\\
235+
80 & 0.0900\\
236+
100 & 0.2700\\
237+
\bottomrule
238+
\end{tabular}
239+
240+
}
241+
242+
\end{minipage}%
243+
244+
\end{table}%
245+
246+
See Table~\ref{tbl-tables} for examples. In particular,
247+
Table~\ref{tbl-tables-2}.
248+
249+
250+
251+
\end{document}

0 commit comments

Comments
 (0)