Skip to content

Commit d1cd854

Browse files
Add script to update state config type and README default config from init.lua
# Details Currently any time an argument is added to the configuration, it needs to replicated in 3 places: - `init.lua` with the user config classes and default configuration - `state.lua` with non optional main config - `README.md` with the updated default config This is mildly annoying and error prone. Instead use `init.lua` as our source of truth and replicate this information to `state.lua` and `README.md`. Logic is contained in `scripts/update.py` and can be executed with `just update`. To make this easier 2 other changes were made: - Rather than storing the config classes like `render.md.Config` in `state.lua` move this to a new module `types.lua`. By doing this we avoid needing special handling for the other information contained in `state.lua` and instead can fully overwrite `types.lua`. - Add comments on default config currently only in `README.md` to the config in `init.lua`, that way when the configuration is pulled the comments get included as well with no special logic.
1 parent c1d9edc commit d1cd854

File tree

6 files changed

+189
-56
lines changed

6 files changed

+189
-56
lines changed

justfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ demo zoom=default_zoom:
1717
--last-frame-duration 1
1818
rm demo.cast
1919

20+
update:
21+
python -Wignore scripts/update.py
22+
2023
gen-doc:
2124
# https://github.com/kdheepak/panvimdoc
2225
# https://pandoc.org/

lua/render-markdown/init.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,12 @@ local M = {}
6464
function M.setup(opts)
6565
---@type render.md.Config
6666
local default_config = {
67+
-- Configure whether Markdown should be rendered by default or not
6768
start_enabled = true,
69+
-- Maximum file size (in MB) that this plugin will attempt to render
70+
-- Any file larger than this will effectively be ignored
6871
max_file_size = 1.5,
72+
-- Capture groups that get pulled from markdown
6973
markdown_query = [[
7074
(atx_heading [
7175
(atx_h1_marker)
@@ -97,37 +101,60 @@ function M.setup(opts)
97101
(pipe_table_delimiter_row) @table_delim
98102
(pipe_table_row) @table_row
99103
]],
104+
-- Capture groups that get pulled from inline markdown
100105
inline_query = [[
101106
(code_span) @code
102107
103108
(shortcut_link) @callout
104109
]],
110+
-- The level of logs to write to file: vim.fn.stdpath('state') .. '/render-markdown.log'
111+
-- Only intended to be used for plugin development / debugging
105112
log_level = 'error',
113+
-- Filetypes this plugin will run on
106114
file_types = { 'markdown' },
115+
-- Vim modes that will show a rendered view of the markdown file
116+
-- All other modes will be uneffected by this plugin
107117
render_modes = { 'n', 'c' },
118+
-- Characters that will replace the # at the start of headings
108119
headings = { '󰲡 ', '󰲣 ', '󰲥 ', '󰲧 ', '󰲩 ', '󰲫 ' },
120+
-- Character to use for the horizontal break
109121
dash = '',
122+
-- Character to use for the bullet points in lists
110123
bullets = { '', '', '', '' },
111124
checkbox = {
125+
-- Character that will replace the [ ] in unchecked checkboxes
112126
unchecked = '󰄱 ',
127+
-- Character that will replace the [x] in checked checkboxes
113128
checked = '',
114129
},
130+
-- Character that will replace the > at the start of block quotes
115131
quote = '',
132+
-- Symbol / text to use for different callouts
116133
callout = {
117134
note = ' Note',
118135
tip = ' Tip',
119136
important = '󰅾 Important',
120137
warning = ' Warning',
121138
caution = '󰳦 Caution',
122139
},
140+
-- See :h 'conceallevel' for more information about meaning of values
123141
conceal = {
142+
-- conceallevel used for buffer when not being rendered, get user setting
124143
default = vim.opt.conceallevel:get(),
144+
-- conceallevel used for buffer when being rendered
125145
rendered = 3,
126146
},
147+
-- Determines how tables are rendered
148+
-- full: adds a line above and below tables + normal behavior
149+
-- normal: renders the rows of tables
150+
-- none: disables rendering, use this if you prefer having cell highlights
127151
table_style = 'full',
152+
-- Define the highlight groups to use when rendering various components
128153
highlights = {
129154
heading = {
155+
-- Background of heading line
130156
backgrounds = { 'DiffAdd', 'DiffChange', 'DiffDelete' },
157+
-- Foreground of heading character only
131158
foregrounds = {
132159
'markdownH1',
133160
'markdownH2',
@@ -137,19 +164,29 @@ function M.setup(opts)
137164
'markdownH6',
138165
},
139166
},
167+
-- Horizontal break
140168
dash = 'LineNr',
169+
-- Code blocks
141170
code = 'ColorColumn',
171+
-- Bullet points in list
142172
bullet = 'Normal',
143173
checkbox = {
174+
-- Unchecked checkboxes
144175
unchecked = '@markup.list.unchecked',
176+
-- Checked checkboxes
145177
checked = '@markup.heading',
146178
},
147179
table = {
180+
-- Header of a markdown table
148181
head = '@markup.heading',
182+
-- Non header rows in a markdown table
149183
row = 'Normal',
150184
},
185+
-- LaTeX blocks
151186
latex = '@markup.math',
187+
-- Quote character in a block quote
152188
quote = '@markup.quote',
189+
-- Highlights to use for different callouts
153190
callout = {
154191
note = 'DiagnosticInfo',
155192
tip = 'DiagnosticOk',

lua/render-markdown/state.lua

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,3 @@
1-
---@class render.md.Callout
2-
---@field public note string
3-
---@field public tip string
4-
---@field public important string
5-
---@field public warning string
6-
---@field public caution string
7-
8-
---@class render.md.TableHighlights
9-
---@field public head string
10-
---@field public row string
11-
12-
---@class render.md.CheckboxHighlights
13-
---@field public unchecked string
14-
---@field public checked string
15-
16-
---@class render.md.HeadingHighlights
17-
---@field public backgrounds string[]
18-
---@field public foregrounds string[]
19-
20-
---@class render.md.Highlights
21-
---@field public heading render.md.HeadingHighlights
22-
---@field public dash string
23-
---@field public code string
24-
---@field public bullet string
25-
---@field public checkbox render.md.CheckboxHighlights
26-
---@field public table render.md.TableHighlights
27-
---@field public latex string
28-
---@field public quote string
29-
---@field public callout render.md.Callout
30-
31-
---@class render.md.Conceal
32-
---@field public default integer
33-
---@field public rendered integer
34-
35-
---@class render.md.Checkbox
36-
---@field public unchecked string
37-
---@field public checked string
38-
39-
---@class render.md.Config
40-
---@field public start_enabled boolean
41-
---@field public max_file_size number
42-
---@field public markdown_query string
43-
---@field public inline_query string
44-
---@field public log_level 'debug'|'error'
45-
---@field public file_types string[]
46-
---@field public render_modes string[]
47-
---@field public headings string[]
48-
---@field public dash string
49-
---@field public bullets string[]
50-
---@field public checkbox render.md.Checkbox
51-
---@field public quote string
52-
---@field public callout render.md.Callout
53-
---@field public conceal render.md.Conceal
54-
---@field public table_style 'full'|'normal'|'none'
55-
---@field public highlights render.md.Highlights
56-
571
---@class render.md.State
582
---@field config render.md.Config
593
---@field enabled boolean

lua/render-markdown/types.lua

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---@class render.md.Callout
2+
---@field public note string
3+
---@field public tip string
4+
---@field public important string
5+
---@field public warning string
6+
---@field public caution string
7+
8+
---@class render.md.TableHighlights
9+
---@field public head string
10+
---@field public row string
11+
12+
---@class render.md.CheckboxHighlights
13+
---@field public unchecked string
14+
---@field public checked string
15+
16+
---@class render.md.HeadingHighlights
17+
---@field public backgrounds string[]
18+
---@field public foregrounds string[]
19+
20+
---@class render.md.Highlights
21+
---@field public heading render.md.HeadingHighlights
22+
---@field public dash string
23+
---@field public code string
24+
---@field public bullet string
25+
---@field public checkbox render.md.CheckboxHighlights
26+
---@field public table render.md.TableHighlights
27+
---@field public latex string
28+
---@field public quote string
29+
---@field public callout render.md.Callout
30+
31+
---@class render.md.Conceal
32+
---@field public default integer
33+
---@field public rendered integer
34+
35+
---@class render.md.Checkbox
36+
---@field public unchecked string
37+
---@field public checked string
38+
39+
---@class render.md.Config
40+
---@field public start_enabled boolean
41+
---@field public max_file_size number
42+
---@field public markdown_query string
43+
---@field public inline_query string
44+
---@field public log_level 'debug'|'error'
45+
---@field public file_types string[]
46+
---@field public render_modes string[]
47+
---@field public headings string[]
48+
---@field public dash string
49+
---@field public bullets string[]
50+
---@field public checkbox render.md.Checkbox
51+
---@field public quote string
52+
---@field public callout render.md.Callout
53+
---@field public conceal render.md.Conceal
54+
---@field public table_style 'full'|'normal'|'none'
55+
---@field public highlights render.md.Highlights

scripts/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tree_sitter==0.21.3
2+
tree_sitter_languages==1.10.2

scripts/update.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from pathlib import Path
2+
from textwrap import dedent
3+
4+
from tree_sitter_languages import get_language, get_parser
5+
6+
7+
def main() -> None:
8+
init_file: Path = Path("lua/render-markdown/init.lua")
9+
update_types(init_file)
10+
update_readme(init_file)
11+
12+
13+
def update_types(init_file: Path) -> None:
14+
lines: list[str] = []
15+
for comment in get_comments(init_file):
16+
comment_type: str = comment.split()[0].split("@")[-1]
17+
if comment_type in ["class", "field"]:
18+
comment = comment.replace("User", "")
19+
if comment_type == "field":
20+
assert "?" in comment, f"All fields must be optional: {comment}"
21+
comment = comment.replace("?", "")
22+
if comment_type == "class" and len(lines) > 0:
23+
lines.append("")
24+
lines.append(comment)
25+
lines.append("")
26+
27+
types_file: Path = Path("lua/render-markdown/types.lua")
28+
types_file.write_text("\n".join(lines))
29+
30+
31+
def update_readme(init_file: Path) -> None:
32+
default_config = get_default_config(init_file)
33+
new_config = " require('render-markdown').setup(" + default_config + ")"
34+
new_config = dedent(new_config)
35+
36+
readme_file = Path("README.md")
37+
current_config = get_readme_config(readme_file)
38+
39+
text = readme_file.read_text()
40+
text = text.replace(current_config, new_config)
41+
readme_file.write_text(text)
42+
43+
44+
def get_comments(file: Path) -> list[str]:
45+
query = "(comment) @comment"
46+
return ts_query(file, query, "comment")
47+
48+
49+
def get_default_config(file: Path) -> str:
50+
query = """
51+
(local_variable_declaration(
52+
(variable_list(
53+
variable name: (identifier) @name
54+
(#eq? @name "default_config")
55+
))
56+
(expression_list value: (table)) @value
57+
))
58+
"""
59+
default_configs = ts_query(file, query, "value")
60+
assert len(default_configs) == 1
61+
return default_configs[0]
62+
63+
64+
def get_readme_config(file: Path) -> str:
65+
query = "(code_fence_content) @content"
66+
code_blocks = ts_query(file, query, "content")
67+
query_code_blocks = [code for code in code_blocks if "query" in code]
68+
assert len(query_code_blocks) == 1
69+
return query_code_blocks[0]
70+
71+
72+
def ts_query(file: Path, query_string: str, target: str) -> list[str]:
73+
ts_language: str = {
74+
".lua": "lua",
75+
".md": "markdown",
76+
}[file.suffix]
77+
parser = get_parser(ts_language)
78+
tree = parser.parse(file.read_text().encode())
79+
80+
language = get_language(ts_language)
81+
query = language.query(query_string)
82+
captures = query.captures(tree.root_node)
83+
84+
values: list[str] = []
85+
for node, capture in captures:
86+
if capture == target:
87+
values.append(node.text.decode())
88+
return values
89+
90+
91+
if __name__ == "__main__":
92+
main()

0 commit comments

Comments
 (0)