[+] Generator script

This commit is contained in:
Hykilpikonna
2021-12-25 17:40:59 -05:00
parent 76ff289e24
commit 622332ab1c
3 changed files with 86 additions and 0 deletions
+37
View File
@@ -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]')
+46
View File
@@ -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 <UserName@users.noreply.github.com>
# - user_info -> Your Display Name <your-actual@email.com>
# - github_actions -> github-actions <email associated with the github logo>
# 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'