Skip to content

Commit e5801b5

Browse files
committed
feat: basic extension start
1 parent b8c1275 commit e5801b5

File tree

8 files changed

+207
-1
lines changed

8 files changed

+207
-1
lines changed

interlinks/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.html
2+
*.pdf
3+
*_files/

interlinks/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Interlinks Extension For Quarto
2+
3+
_TODO_: Add a short description of your extension.
4+
5+
## Installing
6+
7+
_TODO_: Replace the `<github-organization>` with your GitHub organization.
8+
9+
```bash
10+
quarto add <github-organization>/interlinks
11+
```
12+
13+
This will install the extension under the `_extensions` subdirectory.
14+
If you're using version control, you will want to check in this directory.
15+
16+
## Using
17+
18+
_TODO_: Describe how to use your extension.
19+
20+
## Example
21+
22+
Here is the source code for a minimal example: [example.qmd](example.qmd).
23+
24+
## Developing
25+
26+
To see the AST the filter operates on, use this command:
27+
28+
```
29+
quarto render example.qmd --to native --output -
30+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
title: Interlinks
2+
author: Michael Chow
3+
version: 1.0.0
4+
quarto-required: ">=1.2.0"
5+
contributes:
6+
filters:
7+
- interlinks.lua
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
inventory = {
2+
mutate = 'https://siuba.org/api/verbs-mutate-transmute/',
3+
['plotnine.positions.position.position'] = 'someurl'
4+
}
5+
6+
-- from https://stackoverflow.com/a/7615129/1144523
7+
function str_split (inputstr, sep)
8+
if sep == nil then
9+
sep = "%s"
10+
end
11+
local t={}
12+
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
13+
table.insert(t, str)
14+
end
15+
return t
16+
end
17+
18+
19+
-- Reformat all heading text
20+
function Header(el)
21+
el.content = pandoc.Emph(el.content)
22+
return el
23+
end
24+
25+
26+
function Code(el)
27+
if el.attr.classes:find("ref") == nil then
28+
return el
29+
end
30+
31+
if el.text:find("^~") then
32+
prefix = "~"
33+
ref_name = el.text:sub(2)
34+
splits = str_split(el.text, "%.")
35+
out_name = splits[#splits]
36+
else
37+
prefix = ""
38+
ref_name = el.text
39+
out_name = el.text
40+
end
41+
42+
dst = inventory[ref_name]
43+
if dst ~= nil then
44+
return pandoc.Link(out_name, dst)
45+
end
46+
47+
return el
48+
end
49+
50+
function Pandoc(doc)
51+
-- write your setup code here
52+
end
53+
54+
function map(tbl, fn)
55+
local result = {}
56+
for k, v in pairs(tbl) do
57+
result[k] = fn(v)
58+
end
59+
return result
60+
end
61+
62+
local inventory = {}
63+
function Meta(meta)
64+
for k, v in pairs(meta.interlinks.sources) do
65+
print(k)
66+
print(pandoc.utils.stringify(v.fallback))
67+
local json = quarto.json.decode(read_file_contents(v.fallback))
68+
for inv_k, inv_v in pairs(json.items) do
69+
inventory[inv_v.name] = inv_v
70+
end
71+
end
72+
quarto.utils.dump(inventory)
73+
74+
end
75+
76+
function read_file_contents(file)
77+
local f = io.open(pandoc.utils.stringify(file), "r")
78+
if f == nil then
79+
error("Error resolving " .. target .. "- unable to open file " .. file)
80+
os.exit(1)
81+
end
82+
local contents = f:read("*all")
83+
f:close()
84+
return contents
85+
end

interlinks/example.qmd

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: "Interlinks Example"
3+
filters:
4+
- interlinks
5+
interlinks:
6+
sources:
7+
siuba:
8+
url: https://siuba.org
9+
inv: null
10+
fallback: objects_siuba.json
11+
---
12+
13+
## Heading {#sec-hello}
14+
15+
## Testing
16+
17+
Tilde means don't display module:
18+
19+
* `plotnine.positions.position.position`{.ref}
20+
* `~plotnine.positions.position.position`{.ref}
21+
* `some explanation <mutate>`{.ref}
22+
* `some explanation <siuba:siuba.dply.verbs.mutate>`{.ref}
23+
* [some explanation](siuba:siuba.dply.verbs.mutate)

interlinks/objects_siuba.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"project": "Siuba", "version": "0.4.2", "count": 2,
3+
"items": [
4+
{
5+
"name": "siuba.dply.verbs.mutate",
6+
"domain": "py",
7+
"role": "function",
8+
"priority": 0,
9+
"uri": "api/verbs-mutate-transmute/",
10+
"dispname": "-"
11+
},
12+
{
13+
"name": "siuba.dply.verbs.summarize",
14+
"domain": "py",
15+
"role": "function",
16+
"priority": 0,
17+
"uri": "api/verb-summarize/",
18+
"dispname": "-"
19+
}
20+
]
21+
}

interlinks/objects_vetiver.inv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Sphinx inventory version 2
2+
# Project: vetiver
3+
# Version: 0.1.8
4+
# The remainder of this file is compressed using zlib.
5+
xڥWMo� ��+Fj�ި���ڨ�)U՛��k� ���/6v� ��)���fǏ���Y��Ï����{%y+4t�ۺ���O�œ�B�������~�K�˲��-˞�(�A��ʚ��5�*<��\-������T���P���������f�A��ȦO�N��(�*;e,�u�*I���}��H���3�0�헝����W����d�{�;{��{�~����~!kl��z�9�Z;���Q�6=��IֶQ2ʷ���\t�����JJ�/e�.)�k6;���49� �ԍ碒�D�sq�E~���Ĩ-h�1:�#R/�Ǿ[�}[P[,��%[U�{��;�6�pte]i�e��!M�Hr�/�q"-\��tc�<c�0�Y��y���������Z�#T���Y<.$0B�����1 3�t�;7�^�h��Ea�JHީFZ2�� M9�y��]�>`Q*ћ��UdH�UבI_W��t�, W�����*~���L�7��Xu,Ǚ ��[��� 7���!�85(���?�H1��A����$�˷�{!��˚w������ ��@���\�FغK1%<���#�"�YvqI�W��²`���x�Q�AF,n% �g�1�� � �F,.���Y�UD�^P�1�C���� b�`& �#b�sn�H��A<Dz�\)�ĀD����X���,C�)��v��fYqꍵ�0�P��o/D�j+����` ����������h���L)[{� K:� ��v�&�h�H�e�,o�i�+3�,#�Y�^�C(-�c����B2�U���  �!f�Kl�}3�M��7��Շ���1������f���3�

quartodoc.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import click
2+
13
from griffe.loader import GriffeLoader
24
from griffe.docstrings.parsers import Parser, parse
35
from griffe.docstrings import dataclasses as ds
@@ -11,6 +13,32 @@
1113
from typing import Tuple, Union
1214

1315

16+
@click.command()
17+
@click.option("--in-name", help="Name of input inventory file")
18+
@click.option("--out-name", help="Name of result (defaults to <input_name>.json)")
19+
def convert_inventory(in_name, out_name=None):
20+
"""Convert a sphinx inventory file to json."""
21+
import json
22+
import sphobjinv as soi
23+
24+
from pathlib import Path
25+
26+
if out_name is None:
27+
out_name = Path(in_name).with_suffix(".json")
28+
29+
inv = soi.Inventory(in_name)
30+
31+
obj = inv.json_dict()
32+
33+
long = list(obj.items())
34+
meta, entries = long[:3], [v for k, v in long[3:]]
35+
36+
out = dict(meta)
37+
out["entries"] = entries
38+
39+
json.dump(out, open(out_name, "w"))
40+
41+
1442
def parse_function(module: str, func_name: str):
1543
griffe = GriffeLoader()
1644
mod = griffe.load_module(module)
@@ -183,7 +211,7 @@ def to_md(self, el: ds.DocstringParameter) -> Tuple[str]:
183211
if isinstance(el.annotation, str):
184212
annotation = el.annotation
185213
else:
186-
annotation = el.annotation.full if el.annotation else None
214+
annotation = el.annotation.full if el.annotation else None
187215
return (escape(el.name), annotation, el.description, default)
188216

189217
# examples ----
@@ -231,3 +259,7 @@ def to_md(self, el: ExampleText):
231259
)
232260
def to_md(self, el):
233261
raise NotImplementedError(f"{type(el)}")
262+
263+
264+
if __name__ == "__main__":
265+
convert_inventory()

0 commit comments

Comments
 (0)