mkdocs-backlinks-section-plugin 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 six-two
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,71 @@
1
+ Metadata-Version: 2.1
2
+ Name: mkdocs-backlinks-section-plugin
3
+ Version: 0.0.1
4
+ Summary: Adds a backlinks section that lists every page linking to the current page.
5
+ Home-page: https://github.com/six-two/mkdocs-backlinks-section-plugin
6
+ Author: six-two
7
+ Author-email: pip@six-two.dev
8
+ License: MIT License
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: POSIX :: Linux
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.9
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Requires-Python: >=3.9
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE
20
+ Requires-Dist: mkdocs>=1.5.0
21
+
22
+ # MkDocs Backlinks Section Plugin
23
+
24
+ [![PyPI version](https://img.shields.io/pypi/v/mkdocs-backlinks-section-plugin)](https://pypi.org/project/mkdocs-backlinks-section-plugin/)
25
+ ![License](https://img.shields.io/pypi/l/mkdocs-backlinks-section-plugin)
26
+ ![Python versions](https://img.shields.io/pypi/pyversions/mkdocs-backlinks-section-plugin)
27
+
28
+ Adds a backlinks section that lists every page linking to the current page.
29
+
30
+ ## Installation
31
+
32
+ You can install it with `pip`:
33
+ ```bash
34
+ pip install mkdocs-backlinks-section-plugin
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ Add the plugin to your `mkdocs.yml`:
40
+ ```yaml
41
+ plugins:
42
+ - search
43
+ - backlinks_section
44
+ ```
45
+
46
+ ## Configuration
47
+
48
+ You can customize the text inserted by the plugin with the configuration values below:
49
+
50
+ Option | Type | Default value
51
+ --- | --- | ---
52
+ `title` | string | `Backlinks`
53
+ `description` | string | `The following pages link to this page:`
54
+ `description_no_links` | string | `No other pages link to this page.`
55
+
56
+
57
+ So for example if you would want the text to be in German, you could do this in your `mkdocs.yml`:
58
+ ```yaml
59
+ plugins:
60
+ - search
61
+ - backlinks_section:
62
+ title: Rückverweise
63
+ description: "Die folgenden Seiten referenzieren die aktuelle Seite:"
64
+ description_no_links: Es gibt keine Verweise auf diese Seite.
65
+ ```
66
+
67
+ ## Notable changes
68
+
69
+ ### Version 0.0.1
70
+
71
+ - Initial version
@@ -0,0 +1,50 @@
1
+ # MkDocs Backlinks Section Plugin
2
+
3
+ [![PyPI version](https://img.shields.io/pypi/v/mkdocs-backlinks-section-plugin)](https://pypi.org/project/mkdocs-backlinks-section-plugin/)
4
+ ![License](https://img.shields.io/pypi/l/mkdocs-backlinks-section-plugin)
5
+ ![Python versions](https://img.shields.io/pypi/pyversions/mkdocs-backlinks-section-plugin)
6
+
7
+ Adds a backlinks section that lists every page linking to the current page.
8
+
9
+ ## Installation
10
+
11
+ You can install it with `pip`:
12
+ ```bash
13
+ pip install mkdocs-backlinks-section-plugin
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ Add the plugin to your `mkdocs.yml`:
19
+ ```yaml
20
+ plugins:
21
+ - search
22
+ - backlinks_section
23
+ ```
24
+
25
+ ## Configuration
26
+
27
+ You can customize the text inserted by the plugin with the configuration values below:
28
+
29
+ Option | Type | Default value
30
+ --- | --- | ---
31
+ `title` | string | `Backlinks`
32
+ `description` | string | `The following pages link to this page:`
33
+ `description_no_links` | string | `No other pages link to this page.`
34
+
35
+
36
+ So for example if you would want the text to be in German, you could do this in your `mkdocs.yml`:
37
+ ```yaml
38
+ plugins:
39
+ - search
40
+ - backlinks_section:
41
+ title: Rückverweise
42
+ description: "Die folgenden Seiten referenzieren die aktuelle Seite:"
43
+ description_no_links: Es gibt keine Verweise auf diese Seite.
44
+ ```
45
+
46
+ ## Notable changes
47
+
48
+ ### Version 0.0.1
49
+
50
+ - Initial version
@@ -0,0 +1,3 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
@@ -0,0 +1,41 @@
1
+ [metadata]
2
+ name = mkdocs-backlinks-section-plugin
3
+ version = 0.0.1
4
+ author = six-two
5
+ author_email = pip@six-two.dev
6
+ description = Adds a backlinks section that lists every page linking to the current page.
7
+ long_description = file: README.md
8
+ long_description_content_type = text/markdown
9
+ url = https://github.com/six-two/mkdocs-backlinks-section-plugin
10
+ license = MIT License
11
+ classifiers =
12
+ License :: OSI Approved :: MIT License
13
+ Operating System :: POSIX :: Linux
14
+ Programming Language :: Python :: 3
15
+ Programming Language :: Python :: 3.9
16
+ Programming Language :: Python :: 3.10
17
+ Programming Language :: Python :: 3.11
18
+ Programming Language :: Python :: 3.12
19
+ Programming Language :: Python :: 3.13
20
+
21
+ [options]
22
+ include_package_data = True
23
+ package_dir =
24
+ = src
25
+ packages = find:
26
+ python_requires = >=3.9
27
+ scripts =
28
+ install_requires =
29
+ mkdocs>=1.5.0 # Needed for mkdocs.plugins.get_plugin_logger
30
+
31
+ [options.entry_points]
32
+ mkdocs.plugins =
33
+ backlinks_section = mkdocs_backlinks_section_plugin:BacklinksSectionPlugin
34
+
35
+ [options.packages.find]
36
+ where = src
37
+
38
+ [egg_info]
39
+ tag_build =
40
+ tag_date = 0
41
+
@@ -0,0 +1,128 @@
1
+ # builtin
2
+
3
+ # The idea is based roughly on https://github.com/danodic-dev/mkdocs-backlinks, but instead of dealing with a template, this plugin just injects the backlinks into the HTML at the bottom of the page
4
+ import os
5
+ import html
6
+ import random
7
+ import re
8
+ from typing import Optional
9
+ # pip
10
+ from mkdocs.config.base import Config
11
+ from mkdocs.config.config_options import Type
12
+ from mkdocs.config.defaults import MkDocsConfig
13
+ from mkdocs.plugins import BasePlugin, get_plugin_logger
14
+ from mkdocs.structure.files import Files
15
+ from mkdocs.structure.pages import Page
16
+
17
+ # Marks URLs that should be ignored
18
+ BAD_URL_STARTS = ["http://", "https://", "tel:", "#"]
19
+ # Regular expression for anchor tags
20
+ LINK_START_TAG_REGEX = re.compile(r"<a[^>]*>", re.MULTILINE)
21
+ # Logger
22
+ LOGGER = get_plugin_logger(__name__)
23
+
24
+ class BacklinksSectionConfig(Config):
25
+ title = Type(str, default="Backlinks")
26
+ description = Type(str, default="The following pages link to this page:")
27
+ description_no_links = Type(str, default="No other pages link to this page.")
28
+
29
+
30
+ class BacklinksSectionPlugin(BasePlugin[BacklinksSectionConfig]):
31
+ def __init__(self):
32
+ # Use a random placeholder to prevent accidential collisions with strings like BACKLINK_PLUGIN
33
+ self.backlink_placeholder = f"BACKLINK_PLUGIN_{random.randint(0,10000000)}_PLACEHOLDER"
34
+
35
+ def on_nav(self, nav, config: MkDocsConfig, files: Files):
36
+ # self.backlinks | normalized_url: str -> (page_url: str, page_title: str)
37
+ self.backlinks: dict[str,set[tuple[str,str]]] = {normalize_link(file.url): set() for file in files}
38
+ return nav
39
+
40
+ def on_page_markdown(self, markdown, page: Page, config: MkDocsConfig, files: Files) -> str:
41
+ # Add backlinks section placeholder
42
+ # We need to do this in the Markdown phase, so that the new section is added to the table of contents
43
+ return f"{markdown}\n\n## {self.config.title}\n\n{self.backlink_placeholder}"
44
+
45
+ def on_page_content(self, html, page: Page, config: MkDocsConfig, files: Files) -> str:
46
+ # Collect the backlinks
47
+ for url in parse_links_to_other_pages(html):
48
+ destination_link = normalize_link(url, page.url)
49
+
50
+ if destination_link in self.backlinks:
51
+ self.backlinks[destination_link].add((page.url, page.title))
52
+
53
+ return html
54
+
55
+ def on_post_page(self, output: str, page: Page, config: MkDocsConfig) -> str:
56
+ # Insert the backlink list
57
+ key = normalize_link(page.url, "")
58
+ links = self.backlinks[key]
59
+ if links:
60
+ backlink_html = f"<p>{html.escape(self.config.description)}</p>" if self.config.description else ""
61
+ backlink_html += "<ul>"
62
+ # sort by title
63
+ for link, title in sorted(links, key=lambda x: x[1]):
64
+ link_to_page = "/" + link # @TODO: only for testing, this will break later # @TODO: escape?
65
+ backlink_html += f'<li><a href="{link_to_page}">{html.escape(title)}</a></li>'
66
+ backlink_html += "</ul>"
67
+ output = output.replace(self.backlink_placeholder, backlink_html)
68
+ else:
69
+ output = output.replace(self.backlink_placeholder, f"<p>{html.escape(self.config.description_no_links)}</p>")
70
+ return output
71
+
72
+
73
+ def normalize_link(path: str, base_url: str = "") -> str:
74
+ path = path.split("#", 1)[0] # Remove anything after a hashtag (exact anchor on page)
75
+
76
+ # @TODO: isn't this dependent on how directory urls? A: probs not, does not need to be "right", just consistent(ly wrong)
77
+ if path.startswith("/"):
78
+ # Absolute URL
79
+ path = os.path.normpath(path)[1:]
80
+ else:
81
+ if base_url:
82
+ path = os.path.join(os.path.dirname(base_url), path)
83
+
84
+ path = os.path.normpath(path)
85
+
86
+ if path.endswith("/index.html") or path == "index.html":
87
+ path = path[:-len("index.html")]
88
+
89
+ return path
90
+
91
+ def is_valid_link(url: str) -> bool:
92
+ url = url.lower()
93
+ for bad_pattern in BAD_URL_STARTS:
94
+ if url.startswith(bad_pattern):
95
+ # Url is not a local path
96
+ return False
97
+ # Url is probably a local file
98
+ return True
99
+
100
+
101
+ def parse_href_from_anchor_tag(anchor_tag: str) -> Optional[str]:
102
+ for part in anchor_tag.split():
103
+ if part.endswith(">"):
104
+ part = part[:-1]
105
+ if part.startswith("href="):
106
+ link = part[5:]
107
+ if link[0] in ["\"", "'"]:
108
+ if link[0] == link[-1]:
109
+ return link[1:-1]
110
+ else:
111
+ LOGGER.warn(f"href does not have matching quotes: {link}")
112
+ return ""
113
+ else:
114
+ return link
115
+
116
+ LOGGER.warn(f"anchor tag has no href: {anchor_tag}")
117
+ return ""
118
+
119
+
120
+ def parse_links_to_other_pages(html: str) -> list[str]:
121
+ results = []
122
+ for link_start_tag in LINK_START_TAG_REGEX.findall(html):
123
+ link = parse_href_from_anchor_tag(link_start_tag)
124
+ if link and is_valid_link(link):
125
+ results.append(link)
126
+
127
+ return results
128
+
@@ -0,0 +1,71 @@
1
+ Metadata-Version: 2.1
2
+ Name: mkdocs-backlinks-section-plugin
3
+ Version: 0.0.1
4
+ Summary: Adds a backlinks section that lists every page linking to the current page.
5
+ Home-page: https://github.com/six-two/mkdocs-backlinks-section-plugin
6
+ Author: six-two
7
+ Author-email: pip@six-two.dev
8
+ License: MIT License
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: POSIX :: Linux
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.9
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Requires-Python: >=3.9
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE
20
+ Requires-Dist: mkdocs>=1.5.0
21
+
22
+ # MkDocs Backlinks Section Plugin
23
+
24
+ [![PyPI version](https://img.shields.io/pypi/v/mkdocs-backlinks-section-plugin)](https://pypi.org/project/mkdocs-backlinks-section-plugin/)
25
+ ![License](https://img.shields.io/pypi/l/mkdocs-backlinks-section-plugin)
26
+ ![Python versions](https://img.shields.io/pypi/pyversions/mkdocs-backlinks-section-plugin)
27
+
28
+ Adds a backlinks section that lists every page linking to the current page.
29
+
30
+ ## Installation
31
+
32
+ You can install it with `pip`:
33
+ ```bash
34
+ pip install mkdocs-backlinks-section-plugin
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ Add the plugin to your `mkdocs.yml`:
40
+ ```yaml
41
+ plugins:
42
+ - search
43
+ - backlinks_section
44
+ ```
45
+
46
+ ## Configuration
47
+
48
+ You can customize the text inserted by the plugin with the configuration values below:
49
+
50
+ Option | Type | Default value
51
+ --- | --- | ---
52
+ `title` | string | `Backlinks`
53
+ `description` | string | `The following pages link to this page:`
54
+ `description_no_links` | string | `No other pages link to this page.`
55
+
56
+
57
+ So for example if you would want the text to be in German, you could do this in your `mkdocs.yml`:
58
+ ```yaml
59
+ plugins:
60
+ - search
61
+ - backlinks_section:
62
+ title: Rückverweise
63
+ description: "Die folgenden Seiten referenzieren die aktuelle Seite:"
64
+ description_no_links: Es gibt keine Verweise auf diese Seite.
65
+ ```
66
+
67
+ ## Notable changes
68
+
69
+ ### Version 0.0.1
70
+
71
+ - Initial version
@@ -0,0 +1,11 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ setup.cfg
5
+ src/mkdocs_backlinks_section_plugin/__init__.py
6
+ src/mkdocs_backlinks_section_plugin.egg-info/PKG-INFO
7
+ src/mkdocs_backlinks_section_plugin.egg-info/SOURCES.txt
8
+ src/mkdocs_backlinks_section_plugin.egg-info/dependency_links.txt
9
+ src/mkdocs_backlinks_section_plugin.egg-info/entry_points.txt
10
+ src/mkdocs_backlinks_section_plugin.egg-info/requires.txt
11
+ src/mkdocs_backlinks_section_plugin.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [mkdocs.plugins]
2
+ backlinks_section = mkdocs_backlinks_section_plugin:BacklinksSectionPlugin