Skip to content

Commit 00b2b5e

Browse files
committed
Align to Carlos's approach (WIP - stack limit)
1 parent 53d1b9e commit 00b2b5e

File tree

5 files changed

+122
-89
lines changed

5 files changed

+122
-89
lines changed

_extensions/sverto/_extension.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ contributes:
1414
formats:
1515
html:
1616
filters:
17-
- lift-imports.lua
17+
- cleanup-transform.lua
1818
revealjs:
1919
filters:
20-
- lift-imports.lua
20+
- cleanup-transform.lua
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- replaces the doc body with just its first block (assumed to be the
2+
-- version of the doc with transformed .svelte -> .js refs)
3+
Pandoc = function(doc)
4+
doc.blocks = doc.blocks[1].content
5+
return doc
6+
end
Lines changed: 100 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,45 @@
1-
-- ref: https://pandoc.org/lua-filters.html#pandoc-module
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
232

333
-- use mkdir (windows) or mkdir -p (*nix) to create directories
434
-- from https://stackoverflow.com/a/14425862/3246758
535
function get_path_sep()
636
return package.config:sub(1, 1)
737
end
838

39+
-- create a folder recursively (mkdir on windows, mkdir -p on *nix)
940
function create_dir_recursively(path)
10-
print("Creating " .. path)
1141
local path_separator = get_path_sep()
1242
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
1643
os.execute("mkdir " .. path)
1744
else
1845
-- macos/linux
@@ -26,67 +53,86 @@ function path_dir(path)
2653
return path:match("(.*".. get_path_sep() ..")") or ""
2754
end
2855

29-
-- create .sverto
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+
print(">>> OJS block found...")
71+
print(block_text)
72+
73+
-- first, extract .svelte paths in import_svelte() statements
74+
for svelte_path in block_text:gmatch(svelte_import_syntax) do
75+
print("Svelte file found...")
76+
print(svelte_path)
77+
-- table.insert(svelte_files, svelte_path .. ".svelte")
78+
append_to_file(".sverto/.sverto-imports", svelte_path .. ".svelte\n")
79+
end
80+
81+
-- now change `import_svelte("X.svelte")` refs to `import("X.js")`
82+
block.text = block_text:gsub(
83+
svelte_import_syntax,
84+
"import(\"%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+
30105
create_dir_recursively(".sverto/")
31106

32-
-- get the input files as a table
107+
-- collect the input qmd paths
33108
in_file_string = os.getenv("QUARTO_PROJECT_INPUT_FILES")
34109
in_files = {}
35110
for in_file in string.gmatch(in_file_string, "([^\n]+)") do
36111
table.insert(in_files, in_file)
37112
end
38113

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
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
46117

47-
local svelte_key = doc_content.meta["svelte"]
118+
-- print(">>> CREATING IMPORT FOR " .. qmd_path)
119+
local doc = pandoc.read(read_file(qmd_path))
48120

49-
if (svelte_key) then
50-
local import_block = "```{ojs}\n"
51-
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
71-
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)
78-
end
79-
end
121+
-- pre-process the qmd, populating `svelte_files` in the process
122+
-- local svelte_files = {}
123+
local transformed_doc = doc:walk(preprocess_qmd_filter)
124+
create_dir_recursively(".sverto/" .. path_dir(qmd_path))
125+
write_file(".sverto/" .. qmd_path, pandoc.write(transformed_doc, "markdown"))
80126

81-
-- create the imports for each quarto doc
82-
for key, value in ipairs(in_files) do
83-
create_imports(value)
127+
-- write the svelte_files out to .sverto-imports
128+
-- svelte_file_text = table.concat(svelte_files, "\n")
129+
-- print("Saving Svelte imports:")
130+
-- print(svelte_file_text)
131+
-- write_file(".sverto/.sverto-imports", svelte_file_text)
132+
84133
end
85134

86135
-- write the output dir path temporarily (so rollup can use it)
87-
outdir_file = io.open(".sverto/.sverto-outdir", "w")
88-
io.output(outdir_file):write(
89-
os.getenv("QUARTO_PROJECT_OUTPUT_DIR"))
90-
io.close(outdir_file)
136+
write_file(".sverto/.sverto-outdir", os.getenv("QUARTO_PROJECT_OUTPUT_DIR"))
91137

92-
-- TODO - if there's no {{< import .sverto/file.qmd >}} block, add it
138+
-- TODO - if there's no {{< import .sverto/file.qmd >}} block, add it?

_extensions/sverto/lift-imports.lua

Lines changed: 0 additions & 11 deletions
This file was deleted.

example.qmd

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,23 @@
22
title: Svelte example
33
author: James Goldie, 360info
44
date: last-modified
5-
svelte:
6-
- Circles.svelte
75
---
86

9-
Here's an example of how to use the included example Svelte component, `Circles.svelte`. There are six steps:
10-
11-
### 1. Import the component
12-
13-
The first thing you should note is in the frontmatter: a `svelte` key with a list of Svelte components. For example:
7+
:::{.svelteimport}
8+
{{< include /.sverto/example.qmd >}}
9+
:::
1410

15-
```yaml
16-
---
17-
title: Svelte example
18-
svelte:
19-
- Circles.svelte
20-
---
21-
```
11+
Here's an example of how to use the included example Svelte component, `Circles.svelte`. There are six steps:
2212

23-
### 2. Add a magic block to your document
13+
### 2. Add a magic block to the top of your document
2414

2515
Add a magic block anywhere in your document that looks like this:
2616

2717
````markdown
28-
:::{.svelteimport}
29-
{{< import /.sverto/example.qmd >}}
3018
:::
31-
````
32-
33-
When this document renders, it'll turn into an OJS block with a series of statements like `X = import("X.js")`:
34-
35-
:::{.svelteimport}
3619
{{< include /.sverto/example.qmd >}}
3720
:::
21+
````
3822

3923
Replace `example.qmd` with the path to your Quarto doc within the project. For example:
4024

@@ -45,6 +29,14 @@ Replace `example.qmd` with the path to your Quarto doc within the project. For e
4529

4630
> If you ever move or rename your Quarto document, you'll need to update this block with the new location and filename!
4731
32+
### 2. Import the component
33+
34+
Import your Svelte file in an OJS block. Here we call the result `Circles`:
35+
36+
```{ojs}
37+
Circles = import_svelte("Circles.svelte")
38+
```
39+
4840
### 3. Create a place for your visual to live
4941

5042
We need to create an instance of our imported component, but---unlike a typical OJS block---Svelte components put the visual itself somewhere else in the document.

0 commit comments

Comments
 (0)