Skip to content

Commit 0cd67f5

Browse files
committed
Re-organise functions
1 parent 9567171 commit 0cd67f5

File tree

5 files changed

+61
-61
lines changed

5 files changed

+61
-61
lines changed

sphinxext/opengraph/__init__.py

+21-21
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,23 @@
5353
}
5454

5555

56-
def make_tag(property: str, content: str, type_: str = 'property') -> str:
57-
# Parse quotation, so they won't break html tags if smart quotes are disabled
58-
content = content.replace('"', '"')
59-
return f'<meta {type_}="{property}" content="{content}" />'
56+
def html_page_context(
57+
app: Sphinx,
58+
pagename: str,
59+
templatename: str,
60+
context: dict[str, Any],
61+
doctree: nodes.document,
62+
) -> None:
63+
if doctree:
64+
context['metatags'] += get_tags(
65+
context,
66+
doctree,
67+
srcdir=app.srcdir,
68+
outdir=app.outdir,
69+
config=app.config,
70+
builder=app.builder,
71+
env=app.env,
72+
)
6073

6174

6275
def get_tags(
@@ -302,23 +315,10 @@ def social_card_for_page(
302315
return posixpath.join(ogp_site_url, image_path.as_posix())
303316

304317

305-
def html_page_context(
306-
app: Sphinx,
307-
pagename: str,
308-
templatename: str,
309-
context: dict[str, Any],
310-
doctree: nodes.document,
311-
) -> None:
312-
if doctree:
313-
context['metatags'] += get_tags(
314-
context,
315-
doctree,
316-
srcdir=app.srcdir,
317-
outdir=app.outdir,
318-
config=app.config,
319-
builder=app.builder,
320-
env=app.env,
321-
)
318+
def make_tag(property: str, content: str, type_: str = 'property') -> str:
319+
# Parse quotation, so they won't break html tags if smart quotes are disabled
320+
content = content.replace('"', '&quot;')
321+
return f'<meta {type_}="{property}" content="{content}" />'
322322

323323

324324
def setup(app: Sphinx) -> ExtensionMetadata:

sphinxext/opengraph/_description_parser.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99
from collections.abc import Set
1010

1111

12+
def get_description(
13+
doctree: nodes.document,
14+
description_length: int,
15+
known_titles: Set[str] = frozenset(),
16+
) -> str:
17+
mcv = DescriptionParser(
18+
doctree, desc_len=description_length, known_titles=known_titles
19+
)
20+
doctree.walkabout(mcv)
21+
return mcv.description
22+
23+
1224
class DescriptionParser(nodes.NodeVisitor):
1325
"""Finds the title and creates a description from a doctree."""
1426

@@ -95,15 +107,3 @@ def dispatch_departure(self, node: nodes.Element) -> None:
95107
self.description = self.description[:-3] + '...'
96108

97109
self.stop = True
98-
99-
100-
def get_description(
101-
doctree: nodes.document,
102-
description_length: int,
103-
known_titles: Set[str] = frozenset(),
104-
) -> str:
105-
mcv = DescriptionParser(
106-
doctree, desc_len=description_length, known_titles=known_titles
107-
)
108-
doctree.walkabout(mcv)
109-
return mcv.description

sphinxext/opengraph/_meta_parser.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
from html.parser import HTMLParser
44

55

6+
def get_meta_description(meta_tags: str) -> bool:
7+
htp = HTMLTextParser()
8+
htp.feed(meta_tags)
9+
htp.close()
10+
11+
return htp.meta_description
12+
13+
614
class HTMLTextParser(HTMLParser):
715
"""Parse HTML into text."""
816

@@ -19,11 +27,3 @@ def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None
1927
if name == 'content':
2028
self.meta_description = value
2129
break
22-
23-
24-
def get_meta_description(meta_tags: str) -> bool:
25-
htp = HTMLTextParser()
26-
htp.feed(meta_tags)
27-
htp.close()
28-
29-
return htp.meta_description

sphinxext/opengraph/_social_cards.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,6 @@
4545
}
4646

4747

48-
# These functions are used when creating social card objects to set MPL values.
49-
# They must be defined here otherwise Sphinx errors when trying to pickle them.
50-
# They are dependent on the `multiple` variable defined when the figure is created.
51-
# Because they are depending on the figure size and renderer used to generate them.
52-
def _set_page_title_line_width() -> int:
53-
return 825
54-
55-
56-
def _set_description_line_width() -> int:
57-
return 1000
58-
59-
6048
def create_social_card(
6149
config_social: dict[str, bool | str],
6250
site_name: str,
@@ -318,3 +306,15 @@ def create_social_card_objects(
318306
for ax in fig.axes:
319307
ax.set_axis_off()
320308
return fig, txt_site, txt_page, txt_description, txt_url
309+
310+
311+
# These functions are used when creating social card objects to set MPL values.
312+
# They must be defined here otherwise Sphinx errors when trying to pickle them.
313+
# They are dependent on the `multiple` variable defined when the figure is created.
314+
# Because they are depending on the figure size and renderer used to generate them.
315+
def _set_page_title_line_width() -> int:
316+
return 825
317+
318+
319+
def _set_description_line_width() -> int:
320+
return 1000

sphinxext/opengraph/_title_parser.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
from html.parser import HTMLParser
44

55

6+
def get_title(title: str) -> tuple[str, str]:
7+
htp = HTMLTextParser()
8+
htp.feed(title)
9+
htp.close()
10+
11+
return htp.text, htp.text_outside_tags
12+
13+
614
class HTMLTextParser(HTMLParser):
715
"""Parse HTML into text."""
816

@@ -24,11 +32,3 @@ def handle_data(self, data: str) -> None:
2432
self.text += data
2533
if self.level == 0:
2634
self.text_outside_tags += data
27-
28-
29-
def get_title(title: str) -> tuple[str, str]:
30-
htp = HTMLTextParser()
31-
htp.feed(title)
32-
htp.close()
33-
34-
return htp.text, htp.text_outside_tags

0 commit comments

Comments
 (0)