Debian Codegraph @

inst/rmarkdown/lua/codefolding.lua

raw symbols SCIP
1 --[[
2 codefolding - fold code blocks in HTML documents
3
4 MIT License
5
6 Copyright @ 2022-2022 Atsushi Yasumoto
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 --]]
27
28 local DEFAULT_STATE
29
30 function Meta(meta)
31 DEFAULT_STATE = meta["rmd_codefolding_lua"] == "show" and " open" or ""
32 end
33
34 function CodeBlock(elem)
35 local SUPPORTED_CLASSES = {
36 'r', 'python', 'bash', 'sql', 'cpp', 'stan', 'julia', 'foldable'
37 }
38
39 -- Do nothing if no supported classes are given
40 local folding = false
41 for _, v in pairs(SUPPORTED_CLASSES) do
42 if elem.classes:find(v) then
43 folding = true
44 break
45 end
46 end
47 if not folding then return elem end
48
49 -- Do nothin if has "fold-none" class
50 if elem.classes:find("fold-none") then return elem end
51
52 -- Do folding
53 local state =
54 (elem.classes:find("fold-show") and " open") or
55 (elem.classes:find("fold-hide") and "") or
56 DEFAULT_STATE
57
58 return {
59 pandoc.RawBlock(
60 "html",
61 "<details class=chunk-details" .. state .. ">"
62 ..
63 "<summary class=chunk-summary><span class=chunk-summary-text>Code</span></summary>"
64 ),
65 elem,
66 pandoc.RawBlock("html", "</details>")
67 }
68 end
69
70 return {
71 { Meta = Meta },
72 { CodeBlock = CodeBlock }
73 }