Skip to content

Commit 94e7fa7

Browse files
authored
Merge pull request #15 from pythonhealthdatascience/dev
Dev
2 parents 467323d + 3578d5c commit 94e7fa7

File tree

31 files changed

+1164
-107
lines changed

31 files changed

+1164
-107
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
title: Downloadthis
2+
author: Shafayet Khan Shafee
3+
version: 1.1.0
4+
quarto-required: ">=1.2.0"
5+
contributes:
6+
shortcodes:
7+
- downloadthis.lua
8+
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
--[[
2+
MIT License
3+
4+
Copyright (c) 2023 Shafayet Khan Shafee
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
]]--
24+
25+
26+
27+
local str = pandoc.utils.stringify
28+
--local p = quarto.log.output
29+
30+
local function ensureHtmlDeps()
31+
quarto.doc.add_html_dependency({
32+
name = "downloadthis",
33+
version = "1.9.1",
34+
stylesheets = {"resources/css/downloadthis.css"}
35+
})
36+
end
37+
38+
local function optional(arg, default)
39+
if arg == nil or arg == ""
40+
then
41+
return default
42+
else
43+
return arg
44+
end
45+
end
46+
47+
function import(script)
48+
local path = PANDOC_SCRIPT_FILE:match("(.*[/\\])")
49+
package.path = path .. script .. ";" .. package.path
50+
return require(script)
51+
end
52+
53+
local puremagic = import("puremagic.lua")
54+
55+
return {
56+
['downloadthis'] = function(args, kwargs, meta)
57+
58+
-- args and kwargs
59+
local file_path = str(args[1])
60+
local extension = "." .. file_path:match("[^.]+$")
61+
local dname = optional(str(kwargs["dname"]), "file")
62+
local dfilename = dname .. extension
63+
local btn_label = " " .. optional(str(kwargs["label"]), "Download") .. " "
64+
local btn_type = optional(str(kwargs["type"]), "default")
65+
local icon = optional(str(kwargs["icon"]), "download")
66+
local class = " " .. optional(str(kwargs["class"]), "")
67+
local rand = "dnldts" .. str(math.random(1, 65000))
68+
local id = optional(str(kwargs["id"]), rand)
69+
-- reading files
70+
local fh = io.open(file_path, "rb")
71+
if not fh then
72+
io.stderr:write("Cannot open file " ..
73+
file_path ..
74+
" | Skipping adding buttons\n")
75+
return pandoc.Null()
76+
else
77+
local contents = fh:read("*all")
78+
fh:close()
79+
80+
-- creating dataURI object
81+
local b64_encoded = quarto.base64.encode(contents)
82+
local mimetype = puremagic.via_path(file_path)
83+
local data_uri = 'data:' .. mimetype .. ";base64," .. b64_encoded
84+
85+
-- js code taken from
86+
-- https://github.com/fmmattioni/downloadthis/blob/master/R/utils.R#L59
87+
local js = [[fetch('%s').then(res => res.blob()).then(blob => {
88+
const downloadURL = window.URL.createObjectURL(blob);
89+
const a = document.createElement('a');
90+
document.body.appendChild(a);
91+
a.href = downloadURL;
92+
a.download = '%s'; a.click();
93+
window.URL.revokeObjectURL(downloadURL);
94+
document.body.removeChild(a);
95+
});]]
96+
97+
local clicked = js:format(data_uri, dfilename)
98+
99+
-- creating button
100+
local button =
101+
"<button class=\"btn btn-" .. btn_type .. " downloadthis " ..
102+
class .. "\"" ..
103+
" id=\"" .. id .. "\"" ..
104+
"><i class=\"bi bi-" .. icon .. "\"" .. "></i>" ..
105+
btn_label ..
106+
"</button>"
107+
if quarto.doc.is_format("html:js") and quarto.doc.has_bootstrap()
108+
then
109+
ensureHtmlDeps()
110+
return pandoc.RawInline('html',
111+
"<a href=\"#" .. id .. "\"" ..
112+
" onclick=\"" .. clicked .. "\">" .. button .. "</a>"
113+
)
114+
else
115+
return pandoc.Null()
116+
end
117+
end
118+
end
119+
}
120+
121+

0 commit comments

Comments
 (0)