Skip to content

Commit

Permalink
Add pre-commit hook / linter (#159)
Browse files Browse the repository at this point in the history
* Add pre-commit hook / linter

* Run pre-commit
  • Loading branch information
jarrodmillman authored Mar 21, 2022
1 parent e79b8ce commit 92568e5
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 44 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: style

on: [push, pull_request]

jobs:
format:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.9]

steps:
- uses: actions/checkout@v2

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install packages
run: |
pip install --upgrade pip
pip install -r requirements.txt
- name: Lint
run: pre-commit run --all-files --show-diff-on-failure --color always
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Install pre-commit hooks via
# pre-commit install

repos:
- repo: https://github.com/psf/black
rev: 22.1.0
hooks:
- id: black

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.6.0
hooks:
- id: prettier
files: \.(md|yml|yaml)
args: [--prose-wrap=preserve]
4 changes: 2 additions & 2 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
params:
fadeFrontPage: false
images:
- /images/logo.svg
- /images/logo.svg
navColor: blue
font:
name: "Lato"
sizes: [400,900]
sizes: [400, 900]
plausible:
dataDomain: null
javaScript: "https://views.scientific-python.org/js/script.js"
1 change: 0 additions & 1 deletion doc/content/about/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ The **Scientific Python Hugo Theme** is a theme for the
[Hugo](https://gohugo.io) static site generator built on the
[Fresh](https://github.com/StefMa/hugo-fresh) theme.


{{< include-html "static/teams/theme-team.html" >}}
2 changes: 1 addition & 1 deletion doc/content/example-markdown.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
This is some *example markdown* with **bold**!
This is some _example markdown_ with **bold**!
18 changes: 9 additions & 9 deletions doc/content/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ title: Features
The following partials are meant to be overridden:

- `post_meta.html`: Render meta-data under a post title.
We have not yet defined this template, but you can use it to add author information, date, etc.
- `footer_actions`: This appears in the right-hand side of the footer. E.g., numpy.org uses it for mailing list subscriptions.
We have not yet defined this template, but you can use it to add author information, date, etc.
- `footer_actions`: This appears in the right-hand side of the footer. E.g., numpy.org uses it for mailing list subscriptions.

## Shortcut list

The depths of the shortcut list on the left of each post can be
controlled by setting the `shortcutDepth` parameter in the post
preamble. It defaults to 2.
preamble. It defaults to 2.

## Page information

Expand Down Expand Up @@ -67,23 +67,23 @@ def foo(x):
## News

The first post from `/content/en/news` will be highlighted on the
front page. If you don't want that, remove the `/content/en/news`
front page. If you don't want that, remove the `/content/en/news`
folder.

By default, news items link to the `/news` category page (which lists
all news items). You can override that by setting `newsLink` in the
all news items). You can override that by setting `newsLink` in the
preamble of any news post.

### Single page news

If you prefer to list all your news items on one page, you may do so
in `/news.md`, instead of adding posts to `/news`. Set the
in `/news.md`, instead of adding posts to `/news`. Set the
`newsHeader` parameter in the preamble of that document to populate
the banner on the front page.

## Team gallery

The `tools/team_query.py` file gets a list of team members from GitHub. To
The `tools/team_query.py` file gets a list of team members from GitHub. To
use it, you will need to set the GH_TOKEN environment variable
to a personal access token.

Expand Down Expand Up @@ -111,15 +111,15 @@ params:
```

By default, `javaScript` points to the server at
`https://views.scientific-python.org`. Contact the Scientific
`https://views.scientific-python.org`. Contact the Scientific
Python team to have your analytics hosted there.

## Icons

You can add custom icons (for use in, e.g., the footer) by downloading Material-UI SVGs from [Google Fonts](https://fonts.google.com/icons) to the `/assets/icons` directory.

In the footer, the icons can then be used like the ones built-into the theme.
To use them elsewhere, e.g. in Hugo templates, we provide an `svg-icon` partial. For example, `/assets/icons/my-icon.svg` is displayed using:
To use them elsewhere, e.g. in Hugo templates, we provide an `svg-icon` partial. For example, `/assets/icons/my-icon.svg` is displayed using:

```
{{ partial "svg-icon" "my-icon" }}
Expand Down
2 changes: 1 addition & 1 deletion doc/content/getstarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Browse to `http://localhost:1313`, and hopefully you will see your new site!

## Build HTML

Run `make html`. Output appears in `./public`.
Run `make html`. Output appears in `./public`.

## Customizing the site

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pre-commit>=2.17
29 changes: 17 additions & 12 deletions tools/render_shortcode_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,30 @@ def shortcode_doc(fn):
"""
Return description, example, code
"""
with open(fn, 'r') as f:
with open(fn, "r") as f:
data = f.read()

match = re.match('^{{/\*.*doc: ([^\n]*)(.*?)^\*/}}$', data, re.MULTILINE | re.DOTALL)
match = re.match(
"^{{/\*.*doc: ([^\n]*)(.*?)^\*/}}$", data, re.MULTILINE | re.DOTALL
)

if not match:
return None, None, None

description, code = match.group(1), match.group(2).strip()

example = (code
.replace('{{< ', '{{</* ')
.replace(' >}}', ' */>}}')
.replace('{{% ', '{{%/* ')
.replace(' %}}', ' */%}}'))
example = (
code.replace("{{< ", "{{</* ")
.replace(" >}}", " */>}}")
.replace("{{% ", "{{%/* ")
.replace(" %}}", " */%}}")
)

return description, example, code


print('''\
print(
"""\
---
title: Shortcodes
shortcutDepth: 1
Expand All @@ -50,10 +54,11 @@ def shortcode_doc(fn):
that Hugo renders using a predefined template.
Here are some shortcodes used by this theme.
''')
"""
)

for shortcode_fn in shortcodes:
title = os.path.basename(shortcode_fn).replace('.html', '')
title = os.path.basename(shortcode_fn).replace(".html", "")
description, example, code = shortcode_doc(shortcode_fn)
if description is None:
continue
Expand All @@ -62,6 +67,6 @@ def shortcode_doc(fn):
print(description)
print(f"```\n{example}\n```")
print("This example renders as:")
print('___')
print("___")
print(code)
print('___')
print("___")
45 changes: 27 additions & 18 deletions tools/team_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import requests


team_query = string.Template("""
team_query = string.Template(
"""
query {
organization(login: "$org") {
team(slug: "$team") {
Expand All @@ -22,50 +23,57 @@
}
}
}
""")
"""
)


def api(query):
request = requests.post(
'https://api.github.com/graphql',
json={'query': query},
headers={'Authorization': f'bearer {token}'}
"https://api.github.com/graphql",
json={"query": query},
headers={"Authorization": f"bearer {token}"},
)
if request.status_code == 200:
return request.json()
else:
raise RuntimeError("Request received HTTP {request.status_code}: {query}")


parser = argparse.ArgumentParser(description='Generate team gallery from GitHub')
parser.add_argument('--org', required=True, help='GitHub organization name')
parser.add_argument('--team', required=True, help='Team name in the organization')
parser = argparse.ArgumentParser(description="Generate team gallery from GitHub")
parser.add_argument("--org", required=True, help="GitHub organization name")
parser.add_argument("--team", required=True, help="Team name in the organization")
args = parser.parse_args()

org = args.org
team = args.team

token = os.environ.get('GH_TOKEN', None)
token = os.environ.get("GH_TOKEN", None)
if token is None:
print("No token found. Please export a GH_TOKEN with permissions "
"to read team members.", file=sys.stderr)
print(
"No token found. Please export a GH_TOKEN with permissions "
"to read team members.",
file=sys.stderr,
)
sys.exit(-1)


resp = api(team_query.substitute(org=org, team=team))
members = resp['data']['organization']['team']['members']['nodes']
team_name = resp['data']['organization']['team']['name']
members = resp["data"]["organization"]["team"]["members"]["nodes"]
team_name = resp["data"]["organization"]["team"]["name"]

team_template = string.Template('''
team_template = string.Template(
"""
<div class="team">
<h6 class="name title">${team_name}</h6>
<div class="members">
${members}
</div>
</div>
''')
"""
)

member_template = string.Template('''
member_template = string.Template(
"""
<div class="member">
<a href="${url}" class="name">
<div class="photo">
Expand All @@ -78,14 +86,15 @@ def api(query):
${name}
</a>
</div>
''')
"""
)

members_list = []
for m in members:
m["name"] = m["name"] or m["login"]
members_list.append(member_template.substitute(**m))

members_str = ''.join(members_list)
members_str = "".join(members_list)
team_str = team_template.substitute(members=members_str, team_name=team_name)

print(team_str)

0 comments on commit 92568e5

Please sign in to comment.