From 622332ab1c7c6c3bb4142f3f9629a506cb188767 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Sat, 25 Dec 2021 17:40:59 -0500 Subject: [PATCH] [+] Generator script --- .github/workflows/generator.py | 37 ++++++++++++++++++++++++++ .github/workflows/generator.yml | 46 +++++++++++++++++++++++++++++++++ content/generated/metas.json | 3 +++ 3 files changed, 86 insertions(+) create mode 100644 .github/workflows/generator.py create mode 100644 .github/workflows/generator.yml create mode 100644 content/generated/metas.json diff --git a/.github/workflows/generator.py b/.github/workflows/generator.py new file mode 100644 index 0000000..a7486e4 --- /dev/null +++ b/.github/workflows/generator.py @@ -0,0 +1,37 @@ +import json +import os +from datetime import datetime, date +from pathlib import Path + +import yaml + + +class EnhancedJSONEncoder(json.JSONEncoder): + def default(self, o: object) -> object: + # Support encoding datetime + if isinstance(o, (datetime, date)): + return o.isoformat() + + return super().default(o) + + +if __name__ == '__main__': + metas = [] + for b in os.listdir('content/posts'): + if not b.endswith('.md'): + continue + + file = 'content/posts/' + b + with open(file, 'r', encoding='utf-8') as f: + md = f.read().strip() + start = md.index('---\n') + stop = md.index('---\n', start + 1) + yml = md[start + 4:stop] + meta = {**yaml.safe_load(yml), 'file': file} + metas.append(meta) + + metas_path = Path('content/generated/metas.json') + metas_path.parent.mkdir(exist_ok=True, parents=True) + with open(metas_path, 'w', encoding='utf-8') as f: + f.write('[\n ' + ',\n '.join( + json.dumps(m, cls=EnhancedJSONEncoder, ensure_ascii=False) for m in metas) + '\n]') diff --git a/.github/workflows/generator.yml b/.github/workflows/generator.yml new file mode 100644 index 0000000..0e3592d --- /dev/null +++ b/.github/workflows/generator.yml @@ -0,0 +1,46 @@ +# This is a basic workflow to help you get started with Actions +name: Generator + +# Controls when the workflow will run +on: + # Triggers the workflow on push or pull request events but only for the main branch + push: + branches: [ main ] + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + with: + python-version: "3.9" + + - name: "Install dependencies" + run: | + set -xe + python -VV + python -m pip install json5 + + - name: "Run generator" + run: "python .github/workflows/generator.py" + + - uses: EndBug/add-and-commit@v7 + with: + # Determines the way the action fills missing author name and email. Three options are available: + # - github_actor -> UserName + # - user_info -> Your Display Name + # - github_actions -> github-actions + # Default: github_actor + default_author: github_actions + + # The message for the commit. + # Default: 'Commit from GitHub Actions (name of the workflow)' + message: '[U] Generate json' \ No newline at end of file diff --git a/content/generated/metas.json b/content/generated/metas.json new file mode 100644 index 0000000..fce0987 --- /dev/null +++ b/content/generated/metas.json @@ -0,0 +1,3 @@ +[ + {"title": "一个测试文稿", "subtitle": "你好,世界", "tags": ["测试", "测试2"], "date": "2021-12-25", "file": "content/posts/2021-12-25 Test Post.md"} +] \ No newline at end of file