Skip to content

Commit 6deec4b

Browse files
authored
Add black formatter (#17)
* Add black formatter * add badge for black to readme
1 parent 9272707 commit 6deec4b

File tree

6 files changed

+78
-41
lines changed

6 files changed

+78
-41
lines changed

.github/workflows/workflow.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ on:
1010
tags:
1111
- '*'
1212
jobs:
13+
check:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v2
17+
- uses: actions/setup-python@v2
18+
with:
19+
python-version: 3.8
20+
- name: Black
21+
run: |
22+
pip install black
23+
black --check --diff .
24+
1325
test-extension:
1426
runs-on: ubuntu-latest
1527
strategy:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# sphinxext-opengraph
22
![Build](https://github.com/wpilibsuite/sphinxext-opengraph/workflows/Test%20and%20Deploy/badge.svg)
3+
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
34

45
Sphinx extension to generate OpenGraph metadata (https://ogp.me/)
56

setup.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
# This will fail if something happens or if not in a git repository.
55
# This is intentional.
66
try:
7-
ret = subprocess.run("git describe --tags --abbrev=0", stdout=subprocess.PIPE,
8-
stderr=subprocess.PIPE, check=True, shell=True)
7+
ret = subprocess.run(
8+
"git describe --tags --abbrev=0",
9+
stdout=subprocess.PIPE,
10+
stderr=subprocess.PIPE,
11+
check=True,
12+
shell=True,
13+
)
914
version = ret.stdout.decode("utf-8").strip()
1015
except:
1116
version = "master"
1217

13-
with open("README.md", 'r', encoding="utf-8") as readme:
18+
with open("README.md", "r", encoding="utf-8") as readme:
1419
long_description = readme.read()
1520

1621
setuptools.setup(
@@ -22,26 +27,26 @@
2227
long_description=long_description,
2328
long_description_content_type="text/markdown",
2429
url="https://github.com/wpilibsuite/sphinxext-opengraph",
25-
install_requires=['sphinx>=2.0'],
26-
packages=['sphinxext'],
30+
install_requires=["sphinx>=2.0"],
31+
packages=["sphinxext"],
2732
classifiers=[
28-
'Development Status :: 5 - Production/Stable',
29-
'Environment :: Plugins',
30-
'Environment :: Web Environment',
31-
'Framework :: Sphinx :: Extension',
32-
'Intended Audience :: Developers',
33-
'License :: OSI Approved :: MIT License',
34-
'Natural Language :: English',
35-
'Operating System :: OS Independent',
36-
'Programming Language :: Python :: 3.6',
37-
'Programming Language :: Python :: 3.7',
38-
'Programming Language :: Python :: 3.8',
39-
'Programming Language :: Python',
40-
'Topic :: Documentation :: Sphinx',
41-
'Topic :: Documentation',
42-
'Topic :: Software Development :: Documentation',
43-
'Topic :: Text Processing',
44-
'Topic :: Utilities',
33+
"Development Status :: 5 - Production/Stable",
34+
"Environment :: Plugins",
35+
"Environment :: Web Environment",
36+
"Framework :: Sphinx :: Extension",
37+
"Intended Audience :: Developers",
38+
"License :: OSI Approved :: MIT License",
39+
"Natural Language :: English",
40+
"Operating System :: OS Independent",
41+
"Programming Language :: Python :: 3.6",
42+
"Programming Language :: Python :: 3.7",
43+
"Programming Language :: Python :: 3.8",
44+
"Programming Language :: Python",
45+
"Topic :: Documentation :: Sphinx",
46+
"Topic :: Documentation",
47+
"Topic :: Software Development :: Documentation",
48+
"Topic :: Text Processing",
49+
"Topic :: Utilities",
4550
],
46-
python_requires='>=3.6'
51+
python_requires=">=3.6",
4752
)

sphinxext/opengraph.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
DEFAULT_DESCRIPTION_LENGTH = 200
1010

11+
1112
class HTMLTextParser(HTMLParser):
1213
"""
1314
Parse HTML into text
1415
"""
16+
1517
def __init__(self):
1618
super().__init__()
1719
# All text found
@@ -22,7 +24,7 @@ def __init__(self):
2224

2325
def handle_starttag(self, tag, attrs) -> None:
2426
self.level += 1
25-
27+
2628
def handle_endtag(self, tag) -> None:
2729
self.level -= 1
2830

@@ -31,16 +33,23 @@ def handle_data(self, data) -> None:
3133
if self.level == 0:
3234
self.text_outside_tags += data
3335

36+
3437
class OGMetadataCreatorVisitor(nodes.NodeVisitor):
3538
"""
3639
Finds the title and creates a description from a doctree
3740
"""
3841

39-
def __init__(self, desc_len: int, known_titles: Iterable[str] = None, document: nodes.document = None):
42+
def __init__(
43+
self,
44+
desc_len: int,
45+
known_titles: Iterable[str] = None,
46+
document: nodes.document = None,
47+
):
4048

4149
# Hack to prevent requirement for the doctree to be passed in.
42-
# It's only used by doctree.walk(...) to print debug messages.
50+
# It's only used by doctree.walk(...) to print debug messages.
4351
if document == None:
52+
4453
class document_cls:
4554
class reporter:
4655
@staticmethod
@@ -137,7 +146,9 @@ def make_tag(property: str, content: str) -> str:
137146
return f'<meta property="{property}" content="{content}" />\n '
138147

139148

140-
def get_tags(context: Dict[str, Any], doctree: nodes.document, config: Dict[str, Any]) -> str:
149+
def get_tags(
150+
context: Dict[str, Any], doctree: nodes.document, config: Dict[str, Any]
151+
) -> str:
141152

142153
# Set length of description
143154
try:
@@ -165,8 +176,7 @@ def get_tags(context: Dict[str, Any], doctree: nodes.document, config: Dict[str,
165176
# url tag
166177
# Get the URL of the specific page
167178
page_url = urljoin(
168-
config["ogp_site_url"],
169-
context["pagename"] + context["file_suffix"]
179+
config["ogp_site_url"], context["pagename"] + context["file_suffix"]
170180
)
171181
tags += make_tag("og:url", page_url)
172182

@@ -194,14 +204,20 @@ def get_tags(context: Dict[str, Any], doctree: nodes.document, config: Dict[str,
194204
tags += make_tag("og:image:alt", htp.text)
195205

196206
# custom tags
197-
tags += '\n'.join(config['ogp_custom_meta_tags'])
207+
tags += "\n".join(config["ogp_custom_meta_tags"])
198208

199209
return tags
200210

201211

202-
def html_page_context(app: Sphinx, pagename: str, templatename: str, context: Dict[str, Any], doctree: nodes.document) -> None:
212+
def html_page_context(
213+
app: Sphinx,
214+
pagename: str,
215+
templatename: str,
216+
context: Dict[str, Any],
217+
doctree: nodes.document,
218+
) -> None:
203219
if doctree:
204-
context['metatags'] += get_tags(context, doctree, app.config)
220+
context["metatags"] += get_tags(context, doctree, app.config)
205221

206222

207223
def setup(app: Sphinx) -> Dict[str, Any]:
@@ -213,10 +229,9 @@ def setup(app: Sphinx) -> Dict[str, Any]:
213229
app.add_config_value("ogp_site_name", None, "html")
214230
app.add_config_value("ogp_custom_meta_tags", [], "html")
215231

216-
app.connect('html-page-context', html_page_context)
232+
app.connect("html-page-context", html_page_context)
217233

218234
return {
219235
"parallel_read_safe": True,
220236
"parallel_write_safe": True,
221237
}
222-

tests/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def meta_tags(content):
2828

2929
@pytest.fixture()
3030
def og_meta_tags(content):
31-
return [tag for tag in _meta_tags(content) if tag.get("property", "").startswith("og:")]
31+
return [
32+
tag for tag in _meta_tags(content) if tag.get("property", "").startswith("og:")
33+
]
3234

3335

3436
def pytest_configure(config):
35-
config.addinivalue_line(
36-
"markers", "sphinx"
37-
)
37+
config.addinivalue_line("markers", "sphinx")

tests/test_options.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ def test_simple(og_meta_tags):
1717
assert len(og_meta_tags) > 0
1818
assert get_tag_content(og_meta_tags, "type") == "website"
1919
assert len(description) == 200
20-
assert description == "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at lorem ornare, fringilla massa nec, venenatis mi. Donec erat sapien, tincidunt nec rhoncus nec, scelerisque id diam. Orci vari..."
20+
assert (
21+
description
22+
== "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at lorem ornare, fringilla massa nec, venenatis mi. Donec erat sapien, tincidunt nec rhoncus nec, scelerisque id diam. Orci vari..."
23+
)
2124

2225

2326
@pytest.mark.sphinx("html", testroot="simple")
@@ -84,7 +87,9 @@ def test_list_punctuation(og_meta_tags):
8487
@pytest.mark.sphinx("html", testroot="nested-lists")
8588
def test_nested_list_punctuation(og_meta_tags):
8689
description = get_tag_content(og_meta_tags, "description")
87-
assert description == "Item 1, Item 2- Nested Item 1, Nested Item 2., Item 3, Item 4."
90+
assert (
91+
description == "Item 1, Item 2- Nested Item 1, Nested Item 2., Item 3, Item 4."
92+
)
8893

8994

9095
@pytest.mark.sphinx("html", testroot="skip-comments")
@@ -95,4 +100,3 @@ def test_skip_comments(og_meta_tags):
95100
@pytest.mark.sphinx("html", testroot="custom-tags")
96101
def test_custom_tags(og_meta_tags):
97102
assert get_tag_content(og_meta_tags, "ignore_canonical") == "true"
98-

0 commit comments

Comments
 (0)