|
| 1 | +-- return contents of named file |
| 2 | +function read_file(name) |
| 3 | + local file = io.open(name, "r") |
| 4 | + if file == nil then |
| 5 | + return "" |
| 6 | + end |
| 7 | + local contents = file:read("a") |
| 8 | + file:close() |
| 9 | + return contents |
| 10 | +end |
| 11 | + |
| 12 | +-- write content to named file path |
| 13 | +function write_file(name, content) |
| 14 | + local file = io.open(name, "w") |
| 15 | + if file == nil then |
| 16 | + return "" |
| 17 | + end |
| 18 | + file:write(content) |
| 19 | + file:close() |
| 20 | + return content |
| 21 | +end |
| 22 | + |
| 23 | +function append_to_file(name, content) |
| 24 | + local file = io.open(name, "a") |
| 25 | + if file == nil then |
| 26 | + return "" |
| 27 | + end |
| 28 | + file:write(content) |
| 29 | + file:close() |
| 30 | + return content |
| 31 | +end |
| 32 | + |
| 33 | +-- use mkdir (windows) or mkdir -p (*nix) to create directories |
| 34 | +-- from https://stackoverflow.com/a/14425862/3246758 |
| 35 | +function get_path_sep() |
| 36 | + return package.config:sub(1, 1) |
| 37 | +end |
| 38 | + |
| 39 | +-- create a folder recursively (mkdir on windows, mkdir -p on *nix) |
| 40 | +function create_dir_recursively(path) |
| 41 | + local path_separator = get_path_sep() |
| 42 | + if path_separator == "\\" or path_separator == "\"" then |
| 43 | + os.execute("mkdir " .. path) |
| 44 | + else |
| 45 | + -- macos/linux |
| 46 | + os.execute("mkdir -p " .. path) |
| 47 | + end |
| 48 | +end |
| 49 | + |
| 50 | +-- path_dir: extract the folder path from a file path |
| 51 | +-- from https://stackoverflow.com/a/9102300/3246758 |
| 52 | +function path_dir(path) |
| 53 | + return path:match("(.*".. get_path_sep() ..")") or "" |
| 54 | +end |
| 55 | + |
| 56 | +-- TODO - |
| 57 | + |
| 58 | +local preprocess_qmd_filter = { |
| 59 | + |
| 60 | + -- search for `import_svelte("X.svelte")` refs in codeblocks and switch them |
| 61 | + -- to `import("X.js")` |
| 62 | + CodeBlock = function(block) |
| 63 | + if block.classes:includes("{ojs}") then |
| 64 | + |
| 65 | + local svelte_import_syntax = |
| 66 | + "import%_svelte%(\"([%w;,/%?:@&=%+%$%-_%.!~%*'%(%)#]+)%.svelte\"%)" |
| 67 | + |
| 68 | + local block_text = block.text |
| 69 | + |
| 70 | + -- get the qmd_path from disk |
| 71 | + local current_qmd_path = read_file(".sverto/.sverto-current-qmd-folder") |
| 72 | + |
| 73 | + -- first, extract .svelte paths in import_svelte() statements |
| 74 | + for svelte_path in block_text:gmatch(svelte_import_syntax) do |
| 75 | + append_to_file(".sverto/.sverto-imports", |
| 76 | + current_qmd_path .. svelte_path .. ".svelte\n") |
| 77 | + end |
| 78 | + |
| 79 | + -- now change `import_svelte("X.svelte")` refs to `import("X.js")` |
| 80 | + -- TODO - neaten up relative paths instead of assuming we're going from |
| 81 | + -- /site_libs/quarto-ojs |
| 82 | + block.text = block_text:gsub( |
| 83 | + svelte_import_syntax, |
| 84 | + "import(\"./../../" .. current_qmd_path .. "%1.js\")") |
| 85 | + |
| 86 | + end |
| 87 | + return block |
| 88 | + end, |
| 89 | + |
| 90 | + -- return the doc as a a whole unchanged... |
| 91 | + -- except without the first block (the include statement) |
| 92 | + Pandoc = function(doc) |
| 93 | + local new_blocks = pandoc.Blocks({}) |
| 94 | + for i, v in ipairs(doc.blocks) do |
| 95 | + if i ~= 1 then |
| 96 | + new_blocks:insert(v) |
| 97 | + end |
| 98 | + end |
| 99 | + doc.blocks = new_blocks |
| 100 | + return doc |
| 101 | + |
| 102 | + end |
| 103 | +} |
| 104 | + |
| 105 | +create_dir_recursively(".sverto/") |
| 106 | + |
| 107 | +-- collect the input qmd paths |
| 108 | +in_file_string = os.getenv("QUARTO_PROJECT_INPUT_FILES") |
| 109 | +in_files = {} |
| 110 | +for in_file in string.gmatch(in_file_string, "([^\n]+)") do |
| 111 | + table.insert(in_files, in_file) |
| 112 | +end |
| 113 | + |
| 114 | +-- transform each input qmd, saving the transformation in .sverto/[path] |
| 115 | +-- (write the identified .svelte files out to a file too!) |
| 116 | +for key, qmd_path in ipairs(in_files) do |
| 117 | + |
| 118 | + local doc = pandoc.read(read_file(qmd_path)) |
| 119 | + |
| 120 | + -- store the current qmd_path on disk so the filter can access it |
| 121 | + write_file(".sverto/.sverto-current-qmd-folder", path_dir(qmd_path)) |
| 122 | + |
| 123 | + -- pre-process the qmd, populating `svelte_files` in the process |
| 124 | + -- local svelte_files = {} |
| 125 | + local transformed_doc = doc:walk(preprocess_qmd_filter) |
| 126 | + create_dir_recursively(".sverto/" .. path_dir(qmd_path)) |
| 127 | + write_file(".sverto/" .. qmd_path, pandoc.write(transformed_doc, "markdown")) |
| 128 | + |
| 129 | +end |
| 130 | + |
| 131 | +-- write the output dir path temporarily (so rollup can use it) |
| 132 | +write_file(".sverto/.sverto-outdir", os.getenv("QUARTO_PROJECT_OUTPUT_DIR")) |
| 133 | + |
| 134 | +-- TODO - if there's no {{< import .sverto/file.qmd >}} block, add it? |
0 commit comments