Commit a0b5ee30 authored by moto's avatar moto Committed by Facebook GitHub Bot
Browse files

Tweak badge link URL generation (#2677)

Summary:
Currently, the way feature badges are generated assumes that both documentations and the supported features page are on the same level from the root.

This does not work when we introduce `:autosummary:` which generates individual documentation pages one level below.

This commit changes it so that links to the supported features page are properly relative from the documentation level.

There is no appearance change from this commit.

Pull Request resolved: https://github.com/pytorch/audio/pull/2677

Reviewed By: carolineechen

Differential Revision: D39507451

Pulled By: mthrok

fbshipit-source-id: f18da4201f0eb747586be21c8bd9a958217aebc2
parent 9f2bbf6c
import hashlib import hashlib
import os
from pathlib import Path from pathlib import Path
from typing import List from typing import List
from urllib.parse import quote, urlencode from urllib.parse import quote, urlencode
...@@ -6,6 +7,7 @@ from urllib.parse import quote, urlencode ...@@ -6,6 +7,7 @@ from urllib.parse import quote, urlencode
import requests import requests
from docutils import nodes from docutils import nodes
from docutils.parsers.rst.directives.images import Image from docutils.parsers.rst.directives.images import Image
from sphinx.util.docutils import SphinxDirective
_THIS_DIR = Path(__file__).parent _THIS_DIR = Path(__file__).parent
...@@ -38,10 +40,18 @@ def _fetch_image(url): ...@@ -38,10 +40,18 @@ def _fetch_image(url):
path = _get_cache_path(url.encode("utf-8"), ext=".svg") path = _get_cache_path(url.encode("utf-8"), ext=".svg")
if not path.exists(): if not path.exists():
_download(url, path) _download(url, path)
return str(path.relative_to(_THIS_DIR)) return os.sep + str(path.relative_to(_THIS_DIR))
class BaseShield(Image): def _get_relpath(target, base):
target = os.sep + target
base = os.sep + base
target_path, filename = os.path.split(target)
rel_path = os.path.relpath(target_path, os.path.dirname(base))
return os.path.normpath(os.path.join(rel_path, filename))
class BaseShield(Image, SphinxDirective):
def run(self, params, alt, section) -> List[nodes.Node]: def run(self, params, alt, section) -> List[nodes.Node]:
url = f"https://img.shields.io/static/v1?{urlencode(params, quote_via=quote)}" url = f"https://img.shields.io/static/v1?{urlencode(params, quote_via=quote)}"
path = _fetch_image(url) path = _fetch_image(url)
...@@ -50,7 +60,8 @@ class BaseShield(Image): ...@@ -50,7 +60,8 @@ class BaseShield(Image):
if "class" not in self.options: if "class" not in self.options:
self.options["class"] = [] self.options["class"] = []
self.options["class"].append("shield-badge") self.options["class"].append("shield-badge")
self.options["target"] = f"supported_features.html#{section}" target = _get_relpath("supported_features.html", self.env.docname)
self.options["target"] = f"{target}#{section}"
return super().run() return super().run()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment