Skip to content

Commit 8bcdfa5

Browse files
authored
tool: definition list converter (#59)
1 parent f13b227 commit 8bcdfa5

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Definition List Converter
2+
3+
This tool converts Pandoc's native DefinitionList AST nodes to the div-based definition list syntax used by quarto-markdown.
4+
5+
## Background
6+
7+
Pandoc supports definition lists using the following syntax:
8+
9+
```markdown
10+
Term 1
11+
: Definition 1
12+
13+
Term 2
14+
: Definition 2a
15+
: Definition 2b
16+
```
17+
18+
However, quarto-markdown uses a div-based syntax with explicit structure:
19+
20+
```markdown
21+
::: {.definition-list}
22+
* Term 1
23+
- Definition 1
24+
* Term 2
25+
- Definition 2a
26+
- Definition 2b
27+
:::
28+
```
29+
30+
This Lua filter converts from Pandoc's native definition list syntax to the quarto-markdown div syntax.
31+
32+
## Usage
33+
34+
To convert a document containing Pandoc-style definition lists:
35+
36+
```bash
37+
pandoc -f markdown -t markdown \
38+
--lua-filter=definition-list-to-div.lua \
39+
input.md -o output.md
40+
```
41+
42+
### Example
43+
44+
Given this input (`test-input.md`):
45+
46+
```markdown
47+
Term 1
48+
: Definition 1
49+
50+
Term 2
51+
: Definition 2a
52+
: Definition 2b
53+
```
54+
55+
Running the filter:
56+
57+
```bash
58+
pandoc -f markdown -t markdown \
59+
--lua-filter=definition-list-to-div.lua \
60+
test-input.md
61+
```
62+
63+
Produces:
64+
65+
```markdown
66+
::: definition-list
67+
- Term 1
68+
69+
- Definition 1
70+
71+
- Term 2
72+
73+
- Definition 2a
74+
- Definition 2b
75+
:::
76+
```
77+
78+
## Features
79+
80+
- Converts all DefinitionList AST nodes to div-based syntax
81+
- Preserves term formatting (bold, emphasis, code, etc.)
82+
- Handles multiple definitions per term
83+
- Output can be parsed by quarto-markdown back into DefinitionList nodes
84+
85+
## Requirements
86+
87+
- Pandoc 2.11 or later
88+
89+
## See Also
90+
91+
- [Definition Lists Documentation](../../../../docs/syntax/definition-lists.qmd)
92+
- Similar tool: [Grid Table Fixer](../grid-table-fixer/)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
-- Lua filter to convert Pandoc DefinitionList AST nodes to div-based definition lists
2+
-- This produces output in the definition-list div syntax used by quarto-markdown
3+
4+
if PANDOC_VERSION and PANDOC_VERSION.must_be_at_least then
5+
PANDOC_VERSION:must_be_at_least("2.11")
6+
else
7+
error("pandoc version >=2.11 is required")
8+
end
9+
10+
-- Convert a DefinitionList to a div with .definition-list class
11+
local function definition_list_to_div(def_list)
12+
-- Build div attributes with .definition-list class
13+
local div_attr = pandoc.Attr('', {'definition-list'}, {})
14+
15+
-- Build the outer bullet list containing all term-definition pairs
16+
local outer_items = {}
17+
18+
-- Each item in the definition list is a tuple: (term, definitions)
19+
-- term: list of inline elements
20+
-- definitions: list of definition blocks (each definition is a list of blocks)
21+
for _, item in ipairs(def_list.content) do
22+
local term = item[1] -- List of inline elements
23+
local definitions = item[2] -- List of definition blocks
24+
25+
-- Create the inner bullet list containing the definitions
26+
local def_items = {}
27+
for _, def_blocks in ipairs(definitions) do
28+
-- Each definition is a list of blocks
29+
-- Clone the blocks to avoid modifying the original
30+
local blocks = pandoc.Blocks({})
31+
for _, block in ipairs(def_blocks) do
32+
table.insert(blocks, block:clone())
33+
end
34+
35+
-- Ensure we have at least one block
36+
if #blocks == 0 then
37+
blocks = pandoc.Blocks({pandoc.Para({})})
38+
end
39+
40+
table.insert(def_items, blocks)
41+
end
42+
43+
-- Create a bullet list for the definitions
44+
local def_list_elem = pandoc.BulletList(def_items)
45+
46+
-- Create the outer list item containing:
47+
-- 1. The term as a paragraph
48+
-- 2. The nested bullet list of definitions
49+
local term_para = pandoc.Para(term)
50+
table.insert(outer_items, {term_para, def_list_elem})
51+
end
52+
53+
-- Create the outer bullet list (list of term-definition pairs)
54+
local outer_list = pandoc.BulletList(outer_items)
55+
56+
-- Create the div containing the outer list
57+
return pandoc.Div({outer_list}, div_attr)
58+
end
59+
60+
return {{DefinitionList = definition_list_to_div}}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Term 1
2+
: Definition 1
3+
4+
Term 2
5+
: Definition 2a
6+
: Definition 2b
7+
8+
**Bold Term**
9+
: A definition with a bold term
10+
11+
`code term`
12+
: A definition with a code term
13+
14+
Complex Term with *emphasis*
15+
: First definition
16+
: Second definition
17+
: Third definition

0 commit comments

Comments
 (0)