forked from MeanderingProgrammer/render-markdown.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
c1d9edc
commit d1cd854
Showing
6 changed files
with
189 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
---@class render.md.Callout | ||
---@field public note string | ||
---@field public tip string | ||
---@field public important string | ||
---@field public warning string | ||
---@field public caution string | ||
|
||
---@class render.md.TableHighlights | ||
---@field public head string | ||
---@field public row string | ||
|
||
---@class render.md.CheckboxHighlights | ||
---@field public unchecked string | ||
---@field public checked string | ||
|
||
---@class render.md.HeadingHighlights | ||
---@field public backgrounds string[] | ||
---@field public foregrounds string[] | ||
|
||
---@class render.md.Highlights | ||
---@field public heading render.md.HeadingHighlights | ||
---@field public dash string | ||
---@field public code string | ||
---@field public bullet string | ||
---@field public checkbox render.md.CheckboxHighlights | ||
---@field public table render.md.TableHighlights | ||
---@field public latex string | ||
---@field public quote string | ||
---@field public callout render.md.Callout | ||
|
||
---@class render.md.Conceal | ||
---@field public default integer | ||
---@field public rendered integer | ||
|
||
---@class render.md.Checkbox | ||
---@field public unchecked string | ||
---@field public checked string | ||
|
||
---@class render.md.Config | ||
---@field public start_enabled boolean | ||
---@field public max_file_size number | ||
---@field public markdown_query string | ||
---@field public inline_query string | ||
---@field public log_level 'debug'|'error' | ||
---@field public file_types string[] | ||
---@field public render_modes string[] | ||
---@field public headings string[] | ||
---@field public dash string | ||
---@field public bullets string[] | ||
---@field public checkbox render.md.Checkbox | ||
---@field public quote string | ||
---@field public callout render.md.Callout | ||
---@field public conceal render.md.Conceal | ||
---@field public table_style 'full'|'normal'|'none' | ||
---@field public highlights render.md.Highlights |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
tree_sitter==0.21.3 | ||
tree_sitter_languages==1.10.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from pathlib import Path | ||
from textwrap import dedent | ||
|
||
from tree_sitter_languages import get_language, get_parser | ||
|
||
|
||
def main() -> None: | ||
init_file: Path = Path("lua/render-markdown/init.lua") | ||
update_types(init_file) | ||
update_readme(init_file) | ||
|
||
|
||
def update_types(init_file: Path) -> None: | ||
lines: list[str] = [] | ||
for comment in get_comments(init_file): | ||
comment_type: str = comment.split()[0].split("@")[-1] | ||
if comment_type in ["class", "field"]: | ||
comment = comment.replace("User", "") | ||
if comment_type == "field": | ||
assert "?" in comment, f"All fields must be optional: {comment}" | ||
comment = comment.replace("?", "") | ||
if comment_type == "class" and len(lines) > 0: | ||
lines.append("") | ||
lines.append(comment) | ||
lines.append("") | ||
|
||
types_file: Path = Path("lua/render-markdown/types.lua") | ||
types_file.write_text("\n".join(lines)) | ||
|
||
|
||
def update_readme(init_file: Path) -> None: | ||
default_config = get_default_config(init_file) | ||
new_config = " require('render-markdown').setup(" + default_config + ")" | ||
new_config = dedent(new_config) | ||
|
||
readme_file = Path("README.md") | ||
current_config = get_readme_config(readme_file) | ||
|
||
text = readme_file.read_text() | ||
text = text.replace(current_config, new_config) | ||
readme_file.write_text(text) | ||
|
||
|
||
def get_comments(file: Path) -> list[str]: | ||
query = "(comment) @comment" | ||
return ts_query(file, query, "comment") | ||
|
||
|
||
def get_default_config(file: Path) -> str: | ||
query = """ | ||
(local_variable_declaration( | ||
(variable_list( | ||
variable name: (identifier) @name | ||
(#eq? @name "default_config") | ||
)) | ||
(expression_list value: (table)) @value | ||
)) | ||
""" | ||
default_configs = ts_query(file, query, "value") | ||
assert len(default_configs) == 1 | ||
return default_configs[0] | ||
|
||
|
||
def get_readme_config(file: Path) -> str: | ||
query = "(code_fence_content) @content" | ||
code_blocks = ts_query(file, query, "content") | ||
query_code_blocks = [code for code in code_blocks if "query" in code] | ||
assert len(query_code_blocks) == 1 | ||
return query_code_blocks[0] | ||
|
||
|
||
def ts_query(file: Path, query_string: str, target: str) -> list[str]: | ||
ts_language: str = { | ||
".lua": "lua", | ||
".md": "markdown", | ||
}[file.suffix] | ||
parser = get_parser(ts_language) | ||
tree = parser.parse(file.read_text().encode()) | ||
|
||
language = get_language(ts_language) | ||
query = language.query(query_string) | ||
captures = query.captures(tree.root_node) | ||
|
||
values: list[str] = [] | ||
for node, capture in captures: | ||
if capture == target: | ||
values.append(node.text.decode()) | ||
return values | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |