Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding custom admonitions classes #183

Merged
merged 2 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions docs/demo/demo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -428,15 +428,30 @@ Admonitions

You can make up your own admonition too.

.. admonition:: If you add a name flag, it will be styled
:name: warning
.. admonition:: If you add a class flag, it will be styled
:class: warning
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is nice - we don't need to use our hacky extra code to achieve this effect now - this is functionality that "already works" in Sphinx


For example, this admonition block uses the following code:

.. code-block::

.. admonition:: If you add a name flag, it will be styled
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. admonition:: If you add a name flag, it will be styled
.. admonition:: If you add a class flag, it will be styled

:name: warning
:class: warning

Here are a list of classes you can try:

.. code-block::

note
important
tip
attention
caution
warning
danger
error
hint


Topics, Sidebars, and Rubrics
-----------------------------
Expand Down Expand Up @@ -499,7 +514,7 @@ HTML
The HTML below shouldn't display, but it uses RequireJS to make sure that all
works as expected. If the widgets don't show up, RequireJS may be broken.

.. jupyter-execute::
.. jupyter-execute::

import plotly.io as pio
import plotly.express as px
Expand Down
59 changes: 0 additions & 59 deletions pydata_sphinx_theme/bootstrap_html_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,12 @@
"""
from docutils import nodes

from sphinx.locale import admonitionlabels
from sphinx.writers.html5 import HTML5Translator
from sphinx.util import logging

logger = logging.getLogger(__name__)


# Mapping of admonition classes to Bootstrap contextual classes
alert_classes = {
"attention": "primary",
"caution": "warning",
"danger": "danger",
"error": "danger",
"hint": "info",
"important": "primary",
"note": "info",
"seealso": "info",
"tip": "primary",
"warning": "warning",
"todo": "info",
"example": "info",
}


class BootstrapHTML5Translator(HTML5Translator):
"""Custom HTML Translator for a Bootstrap-ified Sphinx layout
This is a specialization of the HTML5 Translator of sphinx.
Expand All @@ -37,47 +19,6 @@ def __init__(self, *args, **kwds):
super().__init__(*args, **kwds)
self.settings.table_style = "table"

def visit_admonition(self, node, name=""):
# type: (nodes.Element, str) -> None
# copy of sphinx source to add alert classes
classes = ["alert"]

# If we have a generic admonition block, style it as info
if (
any("admonition-" in iclass for iclass in node.attributes["classes"])
and name == ""
):
name = "admonition"
if node.attributes.get("names"):
# If `name` is specified, try to look it up in the list of alerts
alert_name = node.attributes.get("names")[0]
else:
# If no `name` is specified, style it as `note`
alert_name = "note"
if alert_name not in alert_classes:
logger.warning(
f"Unsupported admonition name: `{alert_name}`. Using style `note`.",
location=(self.docnames[0], node.children[0].line),
)
alert_name = "note"

# Add the "admonition" class to alert_classes so that it can be referenced
alert_classes[name] = alert_classes[alert_name]

# Remove the title from this admonition and add it to the admonitionlabels
# Because this is how Sphinx inserts titles into admonitions
title = node.children.pop(0)
admonitionlabels[name] = title.astext()

if name:
classes.append("alert-{0}".format(alert_classes[name]))

# This mimics what Sphinx does in its own `visit_admonition`
# but wraps in `alert`
self.body.append(self.starttag(node, "div", CLASS=" ".join(classes)))
if name:
node.insert(0, nodes.title(name, admonitionlabels[name]))

def visit_table(self, node):
# type: (nodes.Element) -> None
# copy of sphinx source to *not* add 'docutils' and 'align-default' classes
Expand Down
2 changes: 1 addition & 1 deletion pydata_sphinx_theme/static/css/index.css

Large diffs are not rendered by default.

157 changes: 157 additions & 0 deletions src/scss/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,160 @@ pre {
margin: 1.5em 0 1.5em 0;
box-shadow: 1px 1px 1px #d8d8d8;
}

// Admonitions CSS inspired by https://squidfunk.github.io/mkdocs-material/getting-started/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe directly put this in a separate file _admonitions.scss ?

$icon-info-circle: "\f05a";
$icon-exclamation-circle: "\f06a";
$icon-lightbulb: "\f0eb";
$icon-question: "\f128";
$icon-exclamation-triangle: "\f071";
$icon-times-circle: "\f057";

.admonition {
margin: 1.5625em 0 !important;
padding: 0 .6rem !important;
overflow: hidden;
page-break-inside: avoid;
border-left: .2rem solid #448aff;
border-radius: .1rem;
box-shadow: 0 0.2rem 0.5rem rgba(0,0,0,.05), 0 0 0.05rem rgba(0,0,0,.1);
transition: color 250ms,background-color 250ms,border-color 250ms;

// Last paragraph should have same spacing as title
p:last-child {
margin-bottom: 0.4em;
}

// Defaults for all admonitions
.admonition-title {
position: relative;
margin: 0 -0.6rem !important;
padding: .4rem .6rem .4rem 2rem;
font-weight: 700;
background-color: rgba(68,138,255,.1);

&:before {
position: absolute;
left: .6rem;
width: 1rem;
height: 1rem;
color: #448aff;
font-family: "Font Awesome 5 Free";
font-weight: 900;
content: $icon-info-circle; /* info-circle */
}

// Next element after title needs some extra upper-space
+ * {
margin-top: 0.4em;
}
}

&.attention {
border-color: #f0b37e;
.admonition-title {
background-color: #ffedcc;

&:before {
color: #f0b37e;
content: $icon-exclamation-circle;
}
}
}

&.caution {
border-color: #f0b37e;
.admonition-title {
background-color: #ffedcc;

&:before {
color: #f0b37e;
content: $icon-exclamation-triangle;
}
}
}

&.warning {
border-color: #f29f97;
.admonition-title {
background-color: #fdf3f2;

&:before {
color: #f29f97;
content: $icon-exclamation-triangle;
}
}
}

&.danger {
border-color: #f29f97;
.admonition-title {
background-color: #fdf3f2;

&:before {
color: #f29f97;
content: $icon-exclamation-triangle;
}
}
}

&.error {
border-color: #f29f97;
.admonition-title {
background-color: #fdf3f2;

&:before {
color: #f29f97;
content: $icon-times-circle;
}
}
}

&.hint {
border-color: #1abc9c;
.admonition-title {
background-color: #dbfaf4;

&:before {
color: #1abc9c;
content: $icon-lightbulb;
}
}
}

&.tip {
border-color: #1abc9c;
.admonition-title {
background-color: #dbfaf4;

&:before {
color: #1abc9c;
content: $icon-lightbulb;
}
}
}

&.important {
border-color: #6ab0de;
.admonition-title {
background-color: #e7f2fa;

&:before {
color: #6ab0de;
content: $icon-exclamation-circle;
}
}
}

&.note {
border-color: #6ab0de;
.admonition-title {
background-color: #e7f2fa;

&:before {
color: #6ab0de;
content: $icon-info-circle;
}
}
}
}