|
1 | 1 | -- ref: https://pandoc.org/lua-filters.html#pandoc-module |
2 | 2 |
|
3 | | --- create .sverto |
4 | | -os.execute("mkdir .sverto/") |
| 3 | +-- use mkdir (windows) or mkdir -p (*nix) to create directories |
| 4 | +-- from https://stackoverflow.com/a/14425862/3246758 |
| 5 | +function get_path_sep() |
| 6 | + return package.config:sub(1, 1) |
| 7 | +end |
| 8 | + |
| 9 | +function create_dir_recursively(path) |
| 10 | + print("Creating " .. path) |
| 11 | + local path_separator = get_path_sep() |
| 12 | + if path_separator == "\\" or path_separator == "\"" then |
| 13 | + -- windows |
| 14 | + -- NOTE - there is one edge case where folders might not be made |
| 15 | + -- recursively, https://www.robvanderwoude.com/cmdextmsg.php |
| 16 | + os.execute("mkdir " .. path) |
| 17 | + else |
| 18 | + -- macos/linux |
| 19 | + os.execute("mkdir -p " .. path) |
| 20 | + end |
| 21 | +end |
5 | 22 |
|
6 | | --- get the input files |
| 23 | +-- path_dir: extract the folder path from a file path |
| 24 | +-- from https://stackoverflow.com/a/9102300/3246758 |
| 25 | +function path_dir(path) |
| 26 | + return path:match("(.*".. get_path_sep() ..")") or "" |
| 27 | +end |
7 | 28 |
|
| 29 | +-- create .sverto |
| 30 | +create_dir_recursively(".sverto/") |
| 31 | + |
| 32 | +-- get the input files as a table |
8 | 33 | in_file_string = os.getenv("QUARTO_PROJECT_INPUT_FILES") |
9 | 34 | in_files = {} |
10 | 35 | for in_file in string.gmatch(in_file_string, "([^\n]+)") do |
11 | 36 | table.insert(in_files, in_file) |
12 | 37 | end |
13 | 38 |
|
14 | | --- test case: single file (we want to do this for all files!) |
15 | | - |
16 | | -first_file_name = in_files[1] |
17 | | -first_file = io.open(first_file_name, "r") |
18 | | -first_file_contents = pandoc.read(io.output(first_file):read("a")) |
19 | | - |
20 | | --- check if there's svelte in the frontmatter |
21 | | - |
22 | | -svelte_key = first_file_contents.meta["svelte"] |
| 39 | +-- create_imports: given a quarto doc path, writes the svelte import |
| 40 | +-- declarations to .svelte/[path] |
| 41 | +function create_imports(quarto_doc_name) |
| 42 | + local doc_handle = io.open(quarto_doc_name, "r") |
| 43 | + local doc_content = pandoc.read(io.output(doc_handle):read("a")) |
| 44 | + |
| 45 | + -- check if there's svelte in the frontmatter |
| 46 | + |
| 47 | + local svelte_key = doc_content.meta["svelte"] |
| 48 | + |
| 49 | + if (svelte_key) then |
| 50 | + local import_block = "```{ojs}\n" |
23 | 51 |
|
24 | | -if (svelte_key) then |
25 | | - import_block = "```{ojs}\n" |
26 | | - for i, item in ipairs(svelte_key) do |
27 | | - -- TODO - assert item is a pandoc string |
| 52 | + -- write each svelte file in this doc's frontmatter out to |
| 53 | + -- (a) the require block of this doc, and |
| 54 | + -- (b) the global imports file going to the svelte compiler |
| 55 | + for i, item in ipairs(svelte_key) do |
| 56 | + -- TODO - assert item is a pandoc string |
| 57 | + |
| 58 | + local item_string = pandoc.utils.stringify(item) |
| 59 | + local item_js = string.sub(item_string, 1, string.len(item_string) - 7) |
| 60 | + |
| 61 | + -- write the item out to .sverto/.svelte-imports |
| 62 | + local import_list_file = io.open(".sverto/.sverto-imports", "a") |
| 63 | + io.output(import_list_file):write(item_string .. "\n") |
| 64 | + io.close(import_list_file) |
| 65 | + |
| 66 | + -- add the item to the require block |
| 67 | + import_block = import_block .. |
| 68 | + item_js .. " = import(\"/" .. item_js ..".js\")\n" |
| 69 | + |
| 70 | + end |
28 | 71 |
|
29 | | - item_string = pandoc.utils.stringify(item) |
30 | | - item_js = string.sub(item_string, 1, string.len(item_string) - 7) |
31 | | - |
32 | | - -- write the item out to .sverto/.svelte-imports |
33 | | - import_list_file = io.open(".sverto/.sverto-imports", "a") |
34 | | - io.output(import_list_file):write(item_string .. "\n") |
35 | | - io.close(import_list_file) |
36 | | - |
37 | | - -- add the item to the require block |
38 | | - import_block = import_block .. |
39 | | - item_js .. " = import(\"/" .. item_js ..".js\")\n" |
40 | | - |
| 72 | + -- write the import block (inc. the final backticks) out to .sverto/[path] |
| 73 | + create_dir_recursively(".sverto/" .. path_dir(quarto_doc_name)) |
| 74 | + local import_block = import_block .. "```" |
| 75 | + local import_file = io.open(".sverto/" .. quarto_doc_name, "w") |
| 76 | + io.output(import_file):write(import_block .. "\n") |
| 77 | + io.close(import_file) |
41 | 78 | end |
42 | | - |
43 | | - -- write the import block out to .sverto/[path] |
44 | | - import_block = import_block .. "```" |
45 | | - import_file = io.open(".sverto/" .. first_file_name, "w") |
46 | | - io.output(import_file):write(import_block .. "\n") |
47 | | - io.close(import_file) |
48 | 79 | end |
49 | 80 |
|
50 | | --- write the output dir temporarily |
| 81 | +-- create the imports for each quarto doc |
| 82 | +for key, value in ipairs(in_files) do |
| 83 | + create_imports(value) |
| 84 | +end |
| 85 | + |
| 86 | +-- write the output dir path temporarily (so rollup can use it) |
51 | 87 | outdir_file = io.open(".sverto/.sverto-outdir", "w") |
52 | 88 | io.output(outdir_file):write( |
53 | 89 | os.getenv("QUARTO_PROJECT_OUTPUT_DIR")) |
|
0 commit comments