Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8916ccb
commit d73d6be
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: meta header check | ||
|
||
on: [push, pull_request, workflow_dispatch] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Set up Python | ||
uses: actions/setup-python@v1 | ||
with: | ||
python-version: '3.x' | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install requests | ||
- uses: actions/checkout@v2 | ||
- name: check | ||
run: python3 .github/workflows/script/meta_header_check.py |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import glob | ||
import os | ||
import sys | ||
import re | ||
|
||
def find_header_or_module(line: str) -> (re.Match, str): | ||
m = re.fullmatch(r'[\*-] (.*?)\[meta header\]', line, re.MULTILINE) | ||
if m: | ||
return m, "header" | ||
|
||
m = re.fullmatch(r'[\*-] (.*?)\[meta module\]', line, re.MULTILINE) | ||
if m: | ||
return m, "module" | ||
return None, "" | ||
|
||
def check_header(text: str, filename: str) -> bool: | ||
found_error: bool = False | ||
|
||
for line in text.split("\n"): | ||
m, header = find_header_or_module(line) | ||
if m: | ||
dirs = filename.split("/") | ||
meta_header = m.group(1) | ||
if len(dirs) < 2: | ||
found_error = True | ||
print("[{}] invalid directory: {}".format(filename, meta_header)) | ||
elif len(dirs) == 2 and dirs[0] in ("reference", "module"): | ||
own_filename = os.path.splitext(os.path.basename(filename))[0] | ||
if meta_header != own_filename: | ||
found_error = True | ||
print("[{0}] invalid meta {1} \"{2}\". You should specify \"{3}\" as meta {1}".format( | ||
filename, header, meta_header, own_filename)) | ||
elif len(dirs) > 2 and dirs[0] in ("reference", "module"): | ||
own_header = dirs[1] | ||
if meta_header != own_header: | ||
found_error = True | ||
print("[{0}] invalid meta {1} \"{2}\". You should specify \"{3}\" as meta {1}".format( | ||
filename, header, meta_header, own_header)) | ||
|
||
break | ||
|
||
return not found_error | ||
|
||
if __name__ == '__main__': | ||
found_error = False | ||
for p in sorted(list(glob.glob("**/*.md", recursive=True))): | ||
with open(p) as f: | ||
text = f.read() | ||
|
||
if not check_header(text, p): | ||
found_error = True | ||
|
||
if found_error: | ||
sys.exit(1) |