From acfb4cf731987c686d9994607563acf618784e21 Mon Sep 17 00:00:00 2001 From: Tianyi Shi Date: Wed, 15 Apr 2020 11:15:32 +0800 Subject: [PATCH] add scripts --- .gitignore | 1 - .vscodeignore | 1 + README.md | 2 +- scripts/_dir.py | 9 +++++++ scripts/commands.py | 56 +++++++++++++++++++++++++++++++++++++++ scripts/convert.py | 60 ++++++++++++++++++++++++++++++++++++++++++ scripts/make_index.py | 36 +++++++++++++++++++++++++ syntaxes/addition.json | 25 ++++++++++++++++++ 8 files changed, 188 insertions(+), 2 deletions(-) create mode 100644 scripts/_dir.py create mode 100644 scripts/commands.py create mode 100644 scripts/convert.py create mode 100644 scripts/make_index.py create mode 100644 syntaxes/addition.json diff --git a/.gitignore b/.gitignore index eda4478..e3ffc41 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,3 @@ node_modules *.vsix */**/__pycache__/* dist -scripts diff --git a/.vscodeignore b/.vscodeignore index c06f4a7..ad798b8 100644 --- a/.vscodeignore +++ b/.vscodeignore @@ -8,3 +8,4 @@ vsc-extension-quickstart.md **/.eslintrc.json **/*.map **/*.ts +dist/** diff --git a/README.md b/README.md index 14d6f92..71bae6f 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ Before you can use blogdown-specific features, you need to first open the direct 1. In the command palette (`Ctrl/Cmd+Shift+P`), search for `new post` 2. Execute `Blogdown: New Post`, then fill out basic information (title, author, category, archetype) - - You can set the default author in the settings `Ctrl/Cmd + ,` + - You can set the default author in the settings `Ctrl/Cmd + ,` 3. You are redirected to your new post! ![blogdown new post demo](images/demo/blogdown/newpost.gif) diff --git a/scripts/_dir.py b/scripts/_dir.py new file mode 100644 index 0000000..f738882 --- /dev/null +++ b/scripts/_dir.py @@ -0,0 +1,9 @@ +import os +from pathlib import Path + +project_dir = os.path.split(os.path.dirname(__file__))[0] +project_dir_path = Path(project_dir) +src_dir = os.path.join(project_dir, "src") +src_dir_path = Path(src_dir) + +ch_src_dir = lambda: os.chdir(src_dir) diff --git a/scripts/commands.py b/scripts/commands.py new file mode 100644 index 0000000..902bf53 --- /dev/null +++ b/scripts/commands.py @@ -0,0 +1,56 @@ +from _dir import src_dir_path, project_dir_path +import os +from pathlib import Path +import re +import json + +SUBPACKAGES = ("rmarkdown-core", "bookdown", "blogdown") +TITLE_PATTERN = re.compile(r"""__title__ = (["'`])([^\1]+?)\1""") +PACKAGE_NAME = "rmarkdown_vscode" + + +def main(): + auto_ts = src_dir_path / "auto.ts" + package_json_file = project_dir_path / "package.json" + package_json_content = json.loads(package_json_file.read_text()) + commands = package_json_content["contributes"]["commands"] + commands_in_package_json = [c["command"] for c in commands] + import_statements = "import * as vscode from 'vscode';" + load_function = "export function loadCommands(context: vscode.ExtensionContext) {" + for subpackage in SUBPACKAGES: + subpackage_path = src_dir_path / subpackage + commands_ts = subpackage_path.glob("commands/*.ts") + for command_ts in commands_ts: + command_name = command_ts.name[:-3] + command_name_full = ".".join([PACKAGE_NAME, subpackage, command_name]) + content = command_ts.read_text() + TITLE = TITLE_PATTERN.findall(content)[0][1] + + if command_name_full not in commands_in_package_json: + commands.append({"command": command_name_full, "title": TITLE}) + + x = next(c for c in commands if c["command"] == command_name_full) + if x["title"] != TITLE: + commands[commands.index(x)] = {"command": command_name_full,"title": TITLE} + + import_statements += f"import {{ {command_name} }} from './{command_ts.with_suffix('').relative_to(src_dir_path)}';" + load_function += make_subscription_push(command_name_full, command_name) + load_function += "}" + + package_json_file.write_text(json.dumps(package_json_content)) + + auto_ts.write_text(import_statements + load_function) + + +def make_subscription_push(command_name_full, command_name): + return ( + 'context.subscriptions.push(vscode.commands.registerCommand("' + + command_name_full + + '", () => { new ' + + command_name + + "().run();}));" + ) + + +if __name__ == "__main__": + main() diff --git a/scripts/convert.py b/scripts/convert.py new file mode 100644 index 0000000..59e354c --- /dev/null +++ b/scripts/convert.py @@ -0,0 +1,60 @@ +"""convert markdown syntax to rmarkdown syntax +""" +import re +import json +import os + +from toolz.functoolz import pipe + +from _dir import project_dir, project_dir_path + + +def main(): + syntaxes_dir_path = project_dir_path / "syntaxes" + src_json = syntaxes_dir_path / "markdown.tmLanguage.json" + dst_json = syntaxes_dir_path / "rmarkdown.tmLanguage.json" + addition_json = syntaxes_dir_path / "addition.json" + + content = json.loads(src_json.read_text()) + additions = json.loads(addition_json.read_text()) + + # content = conv_chunkparser(content) + # add_new_with_context = lambda content: add_new(content, additions) + content = pipe(content, conv_chunkparser) # , add_new_with_context) + + dst_json.write_text(json.dumps(content)) + + +def conv_chunkparser(content): + langAliases = {"js": "js|javascript|node"} + for field, attrs in content["repository"].items(): + if m := re.match(r"fenced_code_block_(\w+)", field): + lang = m[1] + if lang == "unknown": + next + engine = langAliases[lang] if langAliases.get(lang) else lang + attrs["begin"] = r"(^|\G)(\s*)(```)\{(" + engine + r").*\}\s*$" + attrs["end"] = r"(^|\G)(\2)(```)\s*$" + if attrs["beginCaptures"].get("5"): + del attrs["beginCaptures"]["5"] + return content + + +def add_new(content, additions: dict): + entries: dict = content["repository"] + for k, v in additions.items(): + if k not in entries.keys(): + entries.update({k: v}) + extends = v["extends"] + for e in entries.values(): + if parent_includes := e.get("patterns"): + includes = [ + i["include"][1:] for i in parent_includes if i.get("include") + ] + if (extends in includes) and (k not in includes): + parent_includes.append({"include": f"#{k}"}) + return content + + +if __name__ == "__main__": + main() diff --git a/scripts/make_index.py b/scripts/make_index.py new file mode 100644 index 0000000..d482fcd --- /dev/null +++ b/scripts/make_index.py @@ -0,0 +1,36 @@ +"""make indexes e.g. from common/*.ts to common.ts +""" +import re +import json +import os +from pathlib import Path + +TARGETS = ["common", "utils"] +EXPORT_PATTERN = re.compile( + r"^export.+(?:function|const|let|class|type|interface) (\w+)", re.MULTILINE +) + +from _dir import src_dir + + +def main(): + for target in TARGETS: + target_dir = Path(src_dir) / target + target_ts = Path(src_dir) / f"{target}.ts" + export_statement = "" + for ts in target_dir.glob("*.ts"): + content = ts.read_text() + if exports := EXPORT_PATTERN.findall(content): + export_statement += f"export {{ {', '.join(exports)} }} from './{target}/{ts.name[:-3]}';\n" + target_ts.write_text(export_statement) + + # for ts in os.listdir(target): + # if (not ts.endswith("ts")) or (ts[0] in [".", "_"]): + # continue + # with open(ts) as f: + # content = f.read() + # print(content) + + +if __name__ == "__main__": + main() diff --git a/syntaxes/addition.json b/syntaxes/addition.json new file mode 100644 index 0000000..8774963 --- /dev/null +++ b/syntaxes/addition.json @@ -0,0 +1,25 @@ +{ + "include-graphics": { + "captures": { + "1": { + "name": "punctuation.definition.link.markdown" + }, + "2": { + "name": "punctuation.definition.link.markdown" + }, + "3": { + "name": "punctuation.definition.link.markdown" + }, + "4": { + "name": "markup.underline.link.image.markdown" + }, + "5": { + "name": "punctuation.definition.link.markdown" + }, + "6": { "name": "punctuation.definition.link.markdown" } + }, + "match": "(?x) (include_graphics)(\\() (?P[\"']) ([^)]+) (\\g) (\\))", + "name": "meta.image.inline.markdown", + "extends": "image-inline" + } +}