1
+ -- Function to process code blocks
2
+ local function clean_code_block (el , language )
3
+
4
+ -- Check if the code block contains R code
5
+ if el .text :match (" ^```{{" .. language ) then
6
+ -- Remove the ```{{<language>}} and ``` lines
7
+ local cleaned_text = el .text :gsub (" ```{{" .. language .. " }}\n " , " " ):gsub (" \n ```" , " " )
8
+
9
+ -- Remove lines starting with #| (options)
10
+ cleaned_text = cleaned_text :gsub (" #|.-\n " , " " )
11
+
12
+ -- Add 'language' to the class list if not already present
13
+ if not el .attr .classes :includes (language ) then
14
+ table.insert (el .attr .classes , 1 , language )
15
+ end
16
+
17
+ -- Return the modified code block
18
+ return pandoc .CodeBlock (cleaned_text , el .attr )
19
+ end
20
+
21
+ -- If not an R code block, return unchanged
22
+ return el
23
+ end
24
+
25
+ -- Helper function to clone and update code block attributes
26
+ local function clone_and_update_code_block (code_block , new_classes )
27
+ local new_attr = code_block .attr :clone ()
28
+ new_attr .classes = pandoc .List (new_classes )
29
+ return pandoc .CodeBlock (code_block .text , new_attr )
30
+ end
31
+
32
+ function Div (div )
33
+ local to_webr = div .classes :includes (" to-webr" )
34
+ local to_pyodide = div .classes :includes (" to-pyodide" )
35
+
36
+ -- Check if the `div` has the class "to-source"/"to-webr"/"to-pyodide"
37
+ if not (div .classes :includes (" to-source" ) or to_webr or to_pyodide ) then
38
+ return
39
+ end
40
+
41
+ -- Initialize local variables for code block, cell output, and language
42
+ local code_block = nil
43
+ local cell_output = nil
44
+ local language = nil
45
+
46
+ -- Walk through the content of the `div` to find `CodeBlock` and `Div` elements
47
+ div :walk ({
48
+ CodeBlock = function (code )
49
+ -- If a `CodeBlock` with the class "cell-code" is found, assign it to `code_block`
50
+ if code .classes :includes (" cell-code" ) then
51
+ code_block = code
52
+ -- Determine the language of the code block
53
+ if code .classes :includes (" r" ) or code .text :match (" ^```{{r" ) then
54
+ language = " r"
55
+ elseif code .classes :includes (" python" ) or code .text :match (" ^```{{python" ) then
56
+ language = " python"
57
+ else
58
+ quarto .log .error (" Please only specify either R or Python code cells inside of the `to-panel` div." )
59
+ end
60
+ end
61
+ end ,
62
+ Div = function (div )
63
+ -- If a `Div` with the class "cell-output" is found, assign it to `cell_output`
64
+ if div .classes :includes (" cell-output" ) then
65
+ cell_output = div
66
+ end
67
+ end
68
+ })
69
+
70
+ local cleaned_code_cell = clean_code_block (code_block , language )
71
+
72
+ -- Determine the type of Tab to use
73
+ local tabs = nil
74
+
75
+ -- Check if the language matches the required condition
76
+ if to_webr or to_pyodide then
77
+ -- Create a tab for the Result
78
+ local result_tab = quarto .Tab ({ title = " Result" , content = pandoc .List ({code_block , cell_output }) })
79
+
80
+ -- Pick attribute classes
81
+ local code_block_attr_classes = to_webr and {" {webr-r}" , " cell-code" } or {" {pyodide-python}" , " cell-code" }
82
+
83
+ -- Create a tab for the Source
84
+ local interactive_tab = quarto .Tab ({ title = " Interactive" , content = clone_and_update_code_block (code_block , code_block_attr_classes ) })
85
+
86
+ -- Combine the tabs into a list
87
+ tabs = pandoc .List ({ result_tab , interactive_tab })
88
+ else
89
+ -- Create a tab for the Rendered
90
+ local rendered_tab = quarto .Tab ({ title = " Result" , content = pandoc .List ({cleaned_code_cell , cell_output }) })
91
+
92
+ -- Create a tab for the Source
93
+ local source_tab = quarto .Tab ({ title = " Source" , content = clone_and_update_code_block (code_block , {" md" , " cell-code" }) })
94
+
95
+ -- Combine the tabs into a list
96
+ tabs = pandoc .List ({ rendered_tab , source_tab })
97
+ end
98
+
99
+ -- Return a `quarto.Tabset` with the created tabs and specific attributes
100
+ return quarto .Tabset ({
101
+ level = 3 ,
102
+ tabs = tabs ,
103
+ attr = pandoc .Attr (" " , {" panel-tabset" }) -- This attribute assignment shouldn't be necessary but addresses a known issue. Remove when using Quarto 1.5 or greater as required version.
104
+ })
105
+ end
0 commit comments