Skip to content

Commit

Permalink
add scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
TianyiShi2001 committed Apr 15, 2020
1 parent 0303af3 commit acfb4cf
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ node_modules
*.vsix
*/**/__pycache__/*
dist
scripts
1 change: 1 addition & 0 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ vsc-extension-quickstart.md
**/.eslintrc.json
**/*.map
**/*.ts
dist/**
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions scripts/_dir.py
Original file line number Diff line number Diff line change
@@ -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)
56 changes: 56 additions & 0 deletions scripts/commands.py
Original file line number Diff line number Diff line change
@@ -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()
60 changes: 60 additions & 0 deletions scripts/convert.py
Original file line number Diff line number Diff line change
@@ -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()
36 changes: 36 additions & 0 deletions scripts/make_index.py
Original file line number Diff line number Diff line change
@@ -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()
25 changes: 25 additions & 0 deletions syntaxes/addition.json
Original file line number Diff line number Diff line change
@@ -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<quote>[\"']) ([^)]+) (\\g<quote>) (\\))",
"name": "meta.image.inline.markdown",
"extends": "image-inline"
}
}

0 comments on commit acfb4cf

Please sign in to comment.