Skip to content

Commit

Permalink
Move CVE and CWE base URLs to class constants
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Oct 11, 2024
1 parent 58263e2 commit ca99447
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
4 changes: 0 additions & 4 deletions sphinx/environment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@
'image_loading': 'link',
'embed_stylesheet': False,
'cloak_email_addresses': True,
'cve_base_url': 'https://www.cve.org/CVERecord?id=CVE-',
'cve_references': None,
'cwe_base_url': 'https://cwe.mitre.org/data/definitions/',
'cwe_references': None,
'pep_base_url': 'https://peps.python.org/',
'pep_references': None,
'rfc_base_url': 'https://datatracker.ietf.org/doc/html/',
Expand Down
15 changes: 9 additions & 6 deletions sphinx/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

if TYPE_CHECKING:
from collections.abc import Sequence
from typing import Final

from docutils.nodes import Element, Node, TextElement, system_message

Expand Down Expand Up @@ -197,6 +198,8 @@ def process_link(


class CVE(ReferenceRole):
_BASE_URL: Final = 'https://www.cve.org/CVERecord?id=CVE-'

def run(self) -> tuple[list[Node], list[system_message]]:
target_id = f'index-{self.env.new_serialno("index")}'
entries = [
Expand Down Expand Up @@ -233,14 +236,15 @@ def run(self) -> tuple[list[Node], list[system_message]]:
return [index, target, reference], []

def build_uri(self) -> str:
base_url = self.inliner.document.settings.cve_base_url
ret = self.target.split('#', 1)
if len(ret) == 2:
return f'{base_url}{ret[0]}#{ret[1]}'
return f'{base_url}{ret[0]}'
return f'{CVE._BASE_URL}{ret[0]}#{ret[1]}'
return f'{CVE._BASE_URL}{ret[0]}'


class CWE(ReferenceRole):
_BASE_URL: Final = 'https://cwe.mitre.org/data/definitions/'

def run(self) -> tuple[list[Node], list[system_message]]:
target_id = f'index-{self.env.new_serialno("index")}'
entries = [
Expand Down Expand Up @@ -277,11 +281,10 @@ def run(self) -> tuple[list[Node], list[system_message]]:
return [index, target, reference], []

def build_uri(self) -> str:
base_url = self.inliner.document.settings.cwe_base_url
ret = self.target.split('#', 1)
if len(ret) == 2:
return f'{base_url}{int(ret[0])}.html#{ret[1]}'
return f'{base_url}{int(ret[0])}.html'
return f'{CWE._BASE_URL}{int(ret[0])}.html#{ret[1]}'
return f'{CWE._BASE_URL}{int(ret[0])}.html'


class PEP(ReferenceRole):
Expand Down

0 comments on commit ca99447

Please sign in to comment.