Created
September 12, 2023 05:55
-
-
Save AutMaple/70cf89b76afe50f7657d950d8b9569f8 to your computer and use it in GitHub Desktop.
[vim snippets] 自动转换自定义的 snippets #vim #snippets
This file contains hidden or 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
| { | |
| "store": { | |
| "prefix": "store", | |
| "body": [ | |
| "const state = {", | |
| "", | |
| "}", | |
| "", | |
| "const actions = {", | |
| "", | |
| "}", | |
| "", | |
| "const mutations = {", | |
| "", | |
| "}", | |
| "", | |
| "const getters = {", | |
| "", | |
| "}", | |
| "", | |
| "export default {", | |
| " state,", | |
| " actions,", | |
| " mutations,", | |
| " getters", | |
| "}" | |
| ], | |
| "description": "vuex modules template" | |
| } | |
| } |
This file contains hidden or 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
| { | |
| "name": "nvim-snippets", | |
| "author": "AutMaple", | |
| "engines": { | |
| "vscode": "^1.11.0" | |
| }, | |
| "contributes": { | |
| "snippets": [ | |
| { | |
| "language": ["javascript"], | |
| "path": "./javascript.json" | |
| } | |
| ] | |
| } | |
| } |
This file contains hidden or 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
| name = log | |
| prefix = log | |
| description = log content | |
| --- | |
| console.log($1) |
This file contains hidden or 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
| #!/bin/python3 | |
| import sys | |
| import json | |
| import argparse | |
| import os | |
| parser = argparse.ArgumentParser(description="Reference Document") | |
| parser.add_argument("-s", "--snippets-file", help="snippets json file") | |
| parser.add_argument("-t", "--template-file", help="the content of template file will be converted to json format then appended to snippets file") | |
| args = parser.parse_args() | |
| def read_file(file_path): | |
| with open(file_path, 'r') as file: | |
| return file.read() | |
| # Convert template_file to Json object then append to snippet_file | |
| def main(): | |
| snippets_file = args.snippets_file | |
| template_file = args.template_file | |
| snippets = read_snippets(snippets_file) | |
| snippet_name, metadata = template_to_json(template_file) | |
| snippets[snippet_name] = metadata | |
| with open(snippets_file, "w") as file: | |
| data = json.dumps(snippets, indent=2) | |
| file.write(data) | |
| def read_snippets(snippet_file): | |
| snippets = {} | |
| if os.path.exists(snippet_file): | |
| data = read_file(snippet_file) | |
| if data.strip() != "": | |
| snippets = json.loads(read_file(snippet_file)) | |
| return snippets | |
| def template_to_json(template_file): | |
| data = read_file(template_file) | |
| sections = [line.rstrip("\n") for line in data.split("---")] | |
| snippet_name, metadata = parse_meta_data(sections[0]) | |
| template = parse_template(sections[1]) | |
| metadata["body"] = template | |
| return snippet_name, metadata | |
| def parse_meta_data(data): | |
| snippet_name = None | |
| data = clean(data) | |
| metadata = {} | |
| for line in data: | |
| key, value = line.split("=") | |
| key = key.strip() | |
| value = value.strip() | |
| if key == "name": | |
| snippet_name = value | |
| else: | |
| metadata[key]=value | |
| return snippet_name, metadata | |
| def parse_template(data): | |
| return clean(data) | |
| # remove head blank element | |
| def clean(data): | |
| lines = data.split("\n") | |
| for i, val in enumerate(lines): | |
| if len(val.strip()) != 0: | |
| return lines[i:] | |
| return [] | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment