Skip to content

Commit

Permalink
Fix #9201: websupport: UndefinedError is raised: 'css_tag' is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed May 10, 2021
1 parent aa9fab5 commit fa8212a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand Down
31 changes: 21 additions & 10 deletions sphinx/builders/html/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1000,16 +1000,6 @@ def pathto(otheruri: str, resource: bool = False, baseuri: str = default_baseuri
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 '<link %s />' % ' '.join(attrs)
ctx['css_tag'] = css_tag

def hasdoc(name: str) -> bool:
if name in self.env.all_docs:
return True
Expand Down Expand Up @@ -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 '<link %s />' % ' '.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.
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit fa8212a

Please sign in to comment.