Skip to content

Commit 92f957b

Browse files
committed
Turn rest api page into a template
1 parent bd55ddd commit 92f957b

File tree

4 files changed

+29
-51
lines changed

4 files changed

+29
-51
lines changed

custom/rest_api.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{% extends "base.html" %}
2+
3+
{% block styles %}
4+
{{ super() }}
5+
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
6+
<style>
7+
article.md-content__inner {margin: 0; padding: 0;}
8+
article.md-content__inner::before {height: 0;}
9+
div.md-main__inner {margin: 0;}
10+
div.md-grid {max-width: none;}
11+
</style>
12+
{% endblock %}
13+
14+
{% block site_nav %}
15+
{% endblock %}
16+
17+
{% block content %}
18+
<redoc spec-url="../api.json"></redoc>
19+
{% endblock %}
20+
21+
{% block scripts %}
22+
{{ super() }}
23+
<script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"></script>
24+
{% endblock %}
25+
26+
{% block footer %}
27+
{% endblock %}

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edit_uri_template: "https://github.com/pulp/pulp-docs/edit/main/pulp_docs/{path}
77
docs_dir: "pulp_docs"
88
theme:
99
name: material
10+
custom_dir: "custom"
1011
logo: assets/pulp_logo_icon.svg
1112
favicon: assets/favicon.ico
1213
features:

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ dependencies = [
1414
"mkdocs-macros-plugin~=1.3.7",
1515
"mkdocs-site-urls~=0.2.0",
1616
"mkdocs-literate-nav~=0.6.1",
17-
"bs4",
1817
"httpx",
1918
"rich",
2019
]

src/pulp_docs/plugin.py

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import typing as t
22
from pathlib import Path
33
import json
4-
import re
54
import tomllib
65
import yaml
76

87
import httpx
98
from git import Repo
10-
from bs4 import BeautifulSoup as bs
119
from mkdocs.config import Config, config_options
1210
from mkdocs.config.defaults import MkDocsConfig
1311
from mkdocs.plugins import event_priority, get_plugin_logger, BasePlugin
@@ -18,32 +16,12 @@
1816

1917
log = get_plugin_logger(__name__)
2018

21-
SECTION_PATTERN = re.compile(r"([a-z-_]+)/()|(/docs/dev)index.md")
22-
2319
REST_API_MD = """\
2420
---
25-
restapi_json_file: "../api.json"
21+
template: "rest_api.html"
2622
---
2723
"""
2824

29-
REDOC_HEADER = """
30-
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
31-
<style>
32-
body {
33-
margin: 0;
34-
padding: 0;
35-
}
36-
</style>
37-
"""
38-
39-
REDOC_TAG_TEMPLATE = """
40-
<redoc spec-url='%s'></redoc>
41-
"""
42-
43-
REDOC_SCRIPT = """
44-
<script src="https://cdn.jsdelivr.net/npm/redoc@next/bundles/redoc.standalone.js"> </script>
45-
"""
46-
4725

4826
@config_options.SubConfig
4927
class RepositoryOption(Config):
@@ -383,30 +361,3 @@ def on_pre_page(
383361
if edit_url := pulp_meta.get("edit_url"):
384362
page.edit_url = edit_url
385363
return page
386-
387-
def on_post_page(
388-
self,
389-
output: str,
390-
page: Page,
391-
config: MkDocsConfig,
392-
) -> str | None:
393-
# TODO reimplement this as a template
394-
if basepath := page.meta.get("restapi_json_file"):
395-
redoc_tag = REDOC_TAG_TEMPLATE % basepath
396-
bs_page = bs(output, "html.parser")
397-
398-
# Append <head>scripts
399-
bs_page.html.head.append(bs(REDOC_HEADER, "html.parser"))
400-
401-
# Replace main content-container with <redoc> tag
402-
main_container = bs_page.find_all("div", class_="md-main__inner")[0]
403-
main_container.replace_with(bs(redoc_tag, "html.parser"))
404-
405-
# Append <script> tag at the end of body
406-
bs_page.html.body.append(bs(REDOC_SCRIPT, "html.parser"))
407-
408-
# Remove footer (looks weird)
409-
footer = bs_page.find_all(class_="md-footer")[0]
410-
footer.decompose()
411-
output = str(bs_page)
412-
return output

0 commit comments

Comments
 (0)