Skip to content

Commit

Permalink
Cleaner indenting using early returns
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke committed Sep 12, 2023
1 parent 30c665e commit 44100f5
Showing 1 changed file with 47 additions and 43 deletions.
90 changes: 47 additions & 43 deletions _extensions/quarto-ext/shinylive/shinylive.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,50 +96,54 @@ end
return {
{
CodeBlock = function(el)
if el.attr and (
el.attr.classes:includes("{shinylive-python}")
or el.attr.classes:includes("{shinylive-r}")
) then
local language = "python"
if el.attr.classes:includes("{shinylive-r}") then
language = "r"
end
ensureShinyliveSetup(language)

-- Convert code block to JSON string in the same format as app.json.
local parsedCodeblockJson = pandoc.pipe(
"quarto",
{ "run", codeblockScript },
el.text
)

-- This contains "files" and "quartoArgs" keys.
local parsedCodeblock = quarto.json.decode(parsedCodeblockJson)

-- Find Python package dependencies for the current app.
local appDepsJson = callShinylive(
language,
{ "package-deps" },
quarto.json.encode(parsedCodeblock["files"])
)

local appDeps = quarto.json.decode(appDepsJson)

for idx, dep in ipairs(appDeps) do
quarto.doc.attach_to_dependency("shinylive", dep)
end

if el.attr.classes:includes("{shinylive-python}") then
el.attributes.engine = "python"
el.attr.classes = pandoc.List()
el.attr.classes:insert("shinylive-python")
elseif el.attr.classes:includes("{shinylive-r}") then
el.attributes.engine = "r"
el.attr.classes = pandoc.List()
el.attr.classes:insert("shinylive-r")
end
return el
if not el.attr then
-- Not a shinylive codeblock, return
return
end

if el.attr.classes:includes("{shinylive-r}") then
language = "r"
elseif el.attr.classes:includes("{shinylive-python}") then
language = "python"
else
-- Not a shinylive codeblock, return
return
end
ensureShinyliveSetup(language)

-- Convert code block to JSON string in the same format as app.json.
local parsedCodeblockJson = pandoc.pipe(
"quarto",
{ "run", codeblockScript },
el.text
)

-- This contains "files" and "quartoArgs" keys.
local parsedCodeblock = quarto.json.decode(parsedCodeblockJson)

-- Find Python package dependencies for the current app.
local appDepsJson = callShinylive(
language,
{ "package-deps" },
quarto.json.encode(parsedCodeblock["files"])
)

local appDeps = quarto.json.decode(appDepsJson)

for idx, dep in ipairs(appDeps) do
quarto.doc.attach_to_dependency("shinylive", dep)
end

if el.attr.classes:includes("{shinylive-python}") then
el.attributes.engine = "python"
el.attr.classes = pandoc.List()
el.attr.classes:insert("shinylive-python")
elseif el.attr.classes:includes("{shinylive-r}") then
el.attributes.engine = "r"
el.attr.classes = pandoc.List()
el.attr.classes:insert("shinylive-r")
end
return el
end
}
}

0 comments on commit 44100f5

Please sign in to comment.