Skip to content

Commit

Permalink
Add style-doc checks (Makefile & Action) #8
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipMay committed Jul 5, 2021
1 parent 37b946e commit db352ed
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/static_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ jobs:

- name: Check with mdformat
run: mdformat --check *.md

- name: Check with style-doc
run: style-doc --max_len 119 --check_only --py_only ${{ env.src }}
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ src := style_doc setup.py

# format the code
format:
style-doc --max_len 119 --py_only $(src)
black $(src)
isort $(src)
mdformat *.md

# check the code
check:
style-doc --max_len 119 --check_only --py_only $(src)
black $(src) --check --diff
flake8 $(src)
isort $(src) --check --diff
Expand Down
10 changes: 7 additions & 3 deletions style_doc/style_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# Special blocks where the inside should be formatted.
TEXTUAL_BLOCKS = ["note", "warning"]
# List of acceptable characters for titles and sections underline.
TITLE_SPECIAL_CHARS = """= - ` : ' " ~ ^ _ * + # < >""".split(" ")
TITLE_SPECIAL_CHARS = ["=", "-", "`", ":", "'", '"', "~", "^", "_", "*", "+", "#", "<", ">"]
# Special words for docstrings (s? means the s is optional)
DOC_SPECIAL_WORD = [
"Args?",
Expand Down Expand Up @@ -492,12 +492,16 @@ def style_file_docstrings(code_file, max_len=119, check_only=False):
"""Style all docstrings in `code_file` to `max_len`."""
with open(code_file, "r", encoding="utf-8", newline="\n") as f:
code = f.read()
splits = code.split('"""')
# fmt: off
splits = code.split("\"\"\"")
# fmt: on
splits = [
(s if i % 2 == 0 or _re_doc_ignore.search(splits[i - 1]) is not None else style_docstring(s, max_len=max_len))
for i, s in enumerate(splits)
]
clean_code = '"""'.join(splits)
# fmt: off
clean_code = "\"\"\"".join(splits)
# fmt: on

diff = clean_code != code
if not check_only and diff:
Expand Down

0 comments on commit db352ed

Please sign in to comment.