From fa8212ac34eff272287688ed5245292400f98c8b Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Tue, 11 May 2021 02:13:12 +0900 Subject: [PATCH] Fix #9201: websupport: UndefinedError is raised: 'css_tag' is undefined --- CHANGES | 1 + sphinx/builders/html/__init__.py | 31 +++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/CHANGES b/CHANGES index 2dfc594f7..4000500f0 100644 --- a/CHANGES +++ b/CHANGES @@ -24,6 +24,7 @@ Bugs fixed * #9198: i18n: Babel emits errors when running compile_catalog * #9205: py domain: The :canonical: option causes "more than one target for cross-reference" warning +* #9201: websupport: UndefinedError is raised: 'css_tag' is undefined Testing -------- diff --git a/sphinx/builders/html/__init__.py b/sphinx/builders/html/__init__.py index 348914b4d..85669474e 100644 --- a/sphinx/builders/html/__init__.py +++ b/sphinx/builders/html/__init__.py @@ -1000,16 +1000,6 @@ class StandaloneHTMLBuilder(Builder): return uri ctx['pathto'] = pathto - def css_tag(css: Stylesheet) -> str: - attrs = [] - for key in sorted(css.attributes): - value = css.attributes[key] - if value is not None: - attrs.append('%s="%s"' % (key, html.escape(value, True))) - attrs.append('href="%s"' % pathto(css.filename, resource=True)) - return '' % ' '.join(attrs) - ctx['css_tag'] = css_tag - def hasdoc(name: str) -> bool: if name in self.env.all_docs: return True @@ -1140,6 +1130,26 @@ def convert_html_js_files(app: Sphinx, config: Config) -> None: config.html_js_files = html_js_files # type: ignore +def setup_css_tag_helper(app: Sphinx, pagename: str, templatename: str, + context: Dict, doctree: Node) -> None: + """Set up css_tag() template helper. + + .. note:: This set up function is added to keep compatibility with webhelper. + """ + pathto = context.get('pathto') + + def css_tag(css: Stylesheet) -> str: + attrs = [] + for key in sorted(css.attributes): + value = css.attributes[key] + if value is not None: + attrs.append('%s="%s"' % (key, html.escape(value, True))) + attrs.append('href="%s"' % pathto(css.filename, resource=True)) + return '' % ' '.join(attrs) + + context['css_tag'] = css_tag + + def setup_js_tag_helper(app: Sphinx, pagename: str, templatename: str, context: Dict, doctree: Node) -> None: """Set up js_tag() template helper. @@ -1347,6 +1357,7 @@ def setup(app: Sphinx) -> Dict[str, Any]: app.connect('config-inited', validate_html_logo, priority=800) app.connect('config-inited', validate_html_favicon, priority=800) app.connect('builder-inited', validate_math_renderer) + app.connect('html-page-context', setup_css_tag_helper) app.connect('html-page-context', setup_js_tag_helper) app.connect('html-page-context', setup_resource_paths)