Skip to content

Commit 737a674

Browse files
authored
Add div directive (#25)
1 parent f2919d9 commit 737a674

2 files changed

Lines changed: 59 additions & 10 deletions

File tree

docs/index.rst

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ A sphinx extension for creating panels in a grid layout or as drop-downs.
7878
Installation
7979
============
8080

81-
You can install `sphinx-panels` with `pip`:
81+
You can install ``sphinx-panels`` with ``pip``:
8282

8383
.. code-block:: bash
8484
@@ -118,15 +118,15 @@ Panels Usage
118118
Grid Layout
119119
-----------
120120

121-
Panels are split by three or more `-` characters.
121+
Panels are split by three or more ``-`` characters.
122122
The layout of panels is then set by using the bootstrap classes.
123123
Default classes for all panels may be set in the directive options,
124124
then panel specific classes can be added at the start of each panel.
125125

126126
By default the new classes will override those set previously
127127
(as defaults or in the top level options),
128-
but starting the option value with `+` will make the classes additive.
129-
For example the following options will set the first panel's card to have both the `shadow` and `bg-info` classes:
128+
but starting the option value with ``+`` will make the classes additive.
129+
For example the following options will set the first panel's card to have both the ``shadow`` and ``bg-info`` classes:
130130

131131
.. code-block:: rst
132132
@@ -184,7 +184,7 @@ Card Layout
184184
-----------
185185

186186
Each panel contains a card, which can itself contain a header and/or footer,
187-
split by three or more `^^^` and `+++` respectively.
187+
split by three or more ``^^^`` and ``+++`` respectively.
188188

189189
.. seealso::
190190

@@ -256,9 +256,9 @@ You can add your own CSS (see
256256
`the html_css_files option <https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-html_css_files>`_)
257257
but it is advised you use the built-in bootstrap classes:
258258

259-
- `Card colouring <https://getbootstrap.com/docs/4.0/utilities/colors/>`_ contextual classes: `bg-primary`, `bg-success`, `bg-info`, `bg-warning`, `bg-danger`, `bg-secondary`, `bg-dark` and `bg-light`.
260-
- `Padding and margins <https://getbootstrap.com/docs/4.0/utilities/spacing/>`_: `border-0`, `p-2`, `m-2`, ---
261-
- `Text alignment <https://getbootstrap.com/docs/4.0/utilities/text/#text-alignment>`_: `text-justify`, `text-left`, `text-center`, `text-right`
259+
- `Card colouring <https://getbootstrap.com/docs/4.0/utilities/colors/>`_ contextual classes: ``bg-primary``, ``bg-success``, ``bg-info``, ``bg-warning``, ``bg-danger``, ``bg-secondary`, ``bg-dark`` and ``bg-light``.
260+
- `Padding and margins <https://getbootstrap.com/docs/4.0/utilities/spacing/>`_: ``border-0``, ``p-2``, ``m-2``, ---
261+
- `Text alignment <https://getbootstrap.com/docs/4.0/utilities/text/#text-alignment>`_: ``text-justify``, ``text-left``, ``text-center``, ``text-right``
262262

263263
.. code-block:: rst
264264
@@ -511,7 +511,7 @@ If the drop-down has no title assigned, it will display an ellipsis, which is hi
511511
My Content
512512

513513
The overarching container, title banner and body panel can all be styled by assigning classes.
514-
Adding `+` at the start appends the classes to any default ones.
514+
Adding ``+`` at the start appends the classes to any default ones.
515515

516516
.. code-block:: rst
517517
@@ -564,6 +564,23 @@ Adding the ``animate`` option will trigger an animation when the content of the
564564

565565
Current available inputs: ``fade-in``, ``fade-in-slide-down``
566566

567+
Div Directive
568+
=============
569+
570+
The ``div`` directive is the same as the `container directive <https://docutils.sourceforge.io/docs/ref/rst/directives.html#container>`_,
571+
but does not add a ``container`` class in HTML outputs, which is incompatible with Bootstrap CSS:
572+
573+
.. code-block:: rst
574+
575+
.. div:: text-primary
576+
577+
hallo
578+
579+
.. div:: text-primary
580+
581+
hallo
582+
583+
567584
Combined Example
568585
================
569586

sphinx_panels/__init__.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33

44
from docutils import nodes
5+
from docutils.parsers.rst import directives, Directive
56

67
from .button import setup_link_button
78
from .dropdown import setup_dropdown
@@ -20,6 +21,37 @@ def add_static_paths(app):
2021
app.add_css_file("panels-bootstrap.min.css")
2122

2223

24+
class Div(Directive):
25+
"""Same as the ``container`` directive,
26+
but does not add the ``container`` class in HTML outputs,
27+
which can interfere with Bootstrap CSS.
28+
"""
29+
30+
optional_arguments = 1
31+
final_argument_whitespace = True
32+
option_spec = {"name": directives.unchanged}
33+
has_content = True
34+
35+
def run(self):
36+
self.assert_has_content()
37+
text = "\n".join(self.content)
38+
try:
39+
if self.arguments:
40+
classes = directives.class_option(self.arguments[0])
41+
else:
42+
classes = []
43+
except ValueError:
44+
raise self.error(
45+
'Invalid class attribute value for "%s" directive: "%s".'
46+
% (self.name, self.arguments[0])
47+
)
48+
node = nodes.container(text, is_div=True)
49+
node["classes"].extend(classes)
50+
self.add_name(node)
51+
self.state.nested_parse(self.content, self.content_offset, node)
52+
return [node]
53+
54+
2355
def visit_container(self, node):
2456
classes = "docutils container"
2557
if node.get("is_div", False):
@@ -33,7 +65,7 @@ def depart_container(self, node):
3365

3466

3567
def setup(app):
36-
68+
app.add_directive("div", Div)
3769
app.add_config_value("panels_add_boostrap_css", True, "env")
3870
app.connect("builder-inited", add_static_paths)
3971
# we override container html visitors, to stop the default behaviour

0 commit comments

Comments
 (0)