Skip to content

Commit 843564f

Browse files
authored
✨ NEW: Add test_sd_hide_root_title config option (#13)
1 parent 329d6ef commit 843564f

File tree

11 files changed

+160
-38
lines changed

11 files changed

+160
-38
lines changed

docs/badges_buttons.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ color
131131
outline
132132
: Outline color variant
133133

134+
align
135+
: Align the button on the page; left, right, center or justify
136+
134137
expand
135138
: Expand to fit parent width
136139

docs/conf.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"use_issues_button": True,
3838
"repository_branch": "main",
3939
"path_to_docs": "docs",
40-
"home_page_in_toc": True,
40+
"home_page_in_toc": False,
4141
"logo_only": True,
4242
}
4343
if html_theme == "furo":
@@ -65,3 +65,5 @@
6565
"Nam sed arcu dignissim, ultrices quam sit amet, commodo ipsum. "
6666
"Etiam quis nunc at ligula tincidunt eleifend."
6767
}
68+
69+
sd_hide_root_title = True

docs/css_classes.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ Classes are available for styling and alignment:
3434
- `sd-text-left`
3535
- `sd-text-right`
3636
- `sd-text-center`
37+
- Size (size decreases from 1 to 6):
38+
- `sd-fs-1`
39+
- `sd-fs-2`
40+
- `sd-fs-3`
41+
- `sd-fs-4`
42+
- `sd-fs-5`
43+
- `sd-fs-6`
3744
- Weight:
3845
- `sd-font-weight-light`
3946
- `sd-font-weight-lighter`

docs/get_started.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Getting Started
2+
3+
```{article-info}
4+
:avatar: images/ebp-logo.png
5+
:avatar-link: https://executablebooks.org
6+
:author: "[Chris Sewell](https://github.com/chrisjsewell)"
7+
:date: "{sub-ref}`today`"
8+
:read-time: "1 min read"
9+
:class-avatar: sd-animate-grow50-rot20
10+
```
11+
12+
## Usage
13+
14+
Simply pip install `sphinx-design` and add the extension to your `conf.py`:
15+
16+
```python
17+
extensions = ["sphinx_design"]
18+
```
19+
20+
For using with [MyST Parser](https://github.com/executablebooks/myst-parser), for Markdown documentation, it is recommended to use the `colon_fence` syntax extension:
21+
22+
```python
23+
extensions = ["myst_parser", "sphinx_design"]
24+
myst_enable_extensions = ["colon_fence"]
25+
```
26+
27+
## Configuration
28+
29+
To hide the the title header on the landing page (i.e. root document), add the following to your `conf.py`:
30+
31+
```python
32+
sd_hide_root_title = True
33+
```
34+
35+
## Supported browsers
36+
37+
- Chrome >= 60
38+
- Firefox >= 60
39+
- Firefox ESR
40+
- iOS >= 12
41+
- Safari >= 12
42+
- Explorer >= 12
43+
44+
(Mirrors: <https://github.com/twbs/bootstrap/blob/v5.0.2/.browserslistrc>)

docs/index.md

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
# sphinx-design
22

3-
```{article-info}
4-
:avatar: images/ebp-logo.png
5-
:avatar-link: https://executablebooks.org
6-
:author: "[Chris Sewell](https://github.com/chrisjsewell)"
7-
:date: "{sub-ref}`today`"
8-
:read-time: "5 min read"
9-
:class-avatar: sd-animate-grow50-rot20
3+
::::{grid}
4+
:reverse:
5+
:gutter: 2 1 1 1
6+
:margin: 4 1 1 1
7+
8+
:::{grid-item}
9+
:columns: 12 4 4 4
10+
11+
```{image} ./_static/logo_square.svg
12+
:width: 200px
13+
:class: sd-m-auto sd-animate-grow50-rot20
1014
```
15+
:::
16+
17+
:::{grid-item}
18+
:columns: 12 8 8 8
19+
:class: sd-align-items-center sd-fs-3
20+
21+
A sphinx extension for designing beautiful, screen-size responsive web-components.
22+
:::
23+
::::
24+
25+
```{button-ref} get_started
26+
:color: primary
27+
:align: center
28+
:class: sd-fs-5
1129
12-
A sphinx extension for designing beautiful, screen-size responsive web components.
30+
Get Started
31+
```
1332

1433
Conflict free CSS
1534
: All CSS classes are prefixed, to avoid conflicts with other frameworks.
@@ -23,6 +42,12 @@ Configurable
2342
Supports non-HTML output formats
2443
: Components degrade gracefully for non-HTML formats.
2544

45+
```{toctree}
46+
:hidden:
47+
48+
get_started
49+
```
50+
2651
```{toctree}
2752
:caption: Components
2853
:hidden:
@@ -103,29 +128,3 @@ Change the default colors and other CSS.
103128
::::
104129

105130
Created with inspiration from [Bootstrap](https://getbootstrap.com/) (v5), [Material Design](https://material.io) and [Material-UI](https://material-ui.com/) design frameworks.
106-
107-
## Usage
108-
109-
Simply pip install `sphinx-design` and add the extension to your `conf.py`:
110-
111-
```python
112-
extensions = ["sphinx_design"]
113-
```
114-
115-
For using with [MyST Parser](https://github.com/executablebooks/myst-parser), for Markdown documentation, it is recommended to use the `colon_fence` syntax extension:
116-
117-
```python
118-
extensions = ["myst_parser", "sphinx_design"]
119-
myst_enable_extensions = ["colon_fence"]
120-
```
121-
122-
## Supported browsers
123-
124-
- Chrome >= 60
125-
- Firefox >= 60
126-
- Firefox ESR
127-
- iOS >= 12
128-
- Safari >= 12
129-
- Explorer >= 12
130-
131-
(Mirrors: <https://github.com/twbs/bootstrap/blob/v5.0.2/.browserslistrc>)

sphinx_design/badges_buttons.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from sphinx.application import Sphinx
77
from sphinx.util.docutils import ReferenceRole, SphinxDirective, SphinxRole
88

9-
from sphinx_design.shared import SEMANTIC_COLORS, make_choice
9+
from sphinx_design.shared import SEMANTIC_COLORS, make_choice, text_align
1010

1111
ROLE_NAME_BADGE_PREFIX = "bdg"
1212
ROLE_NAME_LINK_PREFIX = "bdg-link"
@@ -137,6 +137,7 @@ class _ButtonDirective(SphinxDirective):
137137
option_spec = {
138138
"color": make_choice(SEMANTIC_COLORS),
139139
"outline": directives.flag,
140+
"align": text_align,
140141
# expand to fit parent width
141142
"expand": directives.flag,
142143
# make parent also clickable
@@ -188,7 +189,7 @@ def run(self) -> List[nodes.Node]:
188189
node = grid_container
189190

190191
# `visit_reference` requires that a reference be inside a `TextElement` parent
191-
container = nodes.paragraph()
192+
container = nodes.paragraph(classes=self.options.get("align", []))
192193
self.set_source_info(container)
193194
container += node
194195

sphinx_design/compiled/style.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sphinx_design/extension.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from docutils.parsers.rst import directives
1212
from sphinx.application import Sphinx
1313
from sphinx.environment import BuildEnvironment
14+
from sphinx.transforms import SphinxTransform
1415
from sphinx.util.docutils import SphinxDirective
1516

1617
from . import compiled as static_module
@@ -26,6 +27,7 @@
2627

2728
def setup_extension(app: Sphinx) -> None:
2829
"""Set up the sphinx extension."""
30+
app.add_config_value("sd_hide_root_title", False, "env")
2931
app.connect("builder-inited", update_css_js)
3032
app.connect("env-updated", update_css_links)
3133
# we override container html visitors, to stop the default behaviour
@@ -44,6 +46,7 @@ def setup_extension(app: Sphinx) -> None:
4446
app.add_directive(
4547
"div", Div, override=True
4648
) # override sphinx-panels implementation
49+
app.add_transform(AddFirstTitleCss)
4750
setup_badges_and_buttons(app)
4851
setup_cards(app)
4952
setup_grids(app)
@@ -132,3 +135,27 @@ def run(self):
132135
self.add_name(node)
133136
self.state.nested_parse(self.content, self.content_offset, node)
134137
return [node]
138+
139+
140+
class AddFirstTitleCss(SphinxTransform):
141+
"""Add a CSS class to to the first sections title."""
142+
143+
default_priority = 699 # priority main
144+
145+
def apply(self):
146+
if not self.app.config.sd_hide_root_title:
147+
return
148+
# from sphinx 4 master_doc is deprecated for root_doc
149+
try:
150+
if self.env.docname != self.config.root_doc:
151+
return
152+
except Exception:
153+
if self.env.docname != self.config.master_doc:
154+
return
155+
for section in self.document.traverse(nodes.section):
156+
if isinstance(section.children[0], nodes.title):
157+
if "classes" in section.children[0]:
158+
section.children[0]["classes"].append("sd-d-none")
159+
else:
160+
section.children[0]["classes"] = ["sd-d-none"]
161+
break

style/_text.scss

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,19 @@
6868
text-overflow: ellipsis;
6969
white-space: nowrap;
7070
}
71+
72+
$font-sizes: (
73+
1: calc(1.375rem + 1.5vw),
74+
2: calc(1.325rem + 0.9vw),
75+
3: calc(1.3rem + 0.6vw),
76+
4: calc(1.275rem + 0.3vw),
77+
5: 1.25rem,
78+
6: 1rem
79+
);
80+
81+
@each $id, $value in $font-sizes {
82+
.sd-fs-#{$id}, .sd-fs-#{$id} > p {
83+
font-size: $value !important;
84+
line-height: unset !important;
85+
}
86+
}

tests/test_snippets.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,20 @@ def test_snippets_myst_post(
103103
extension=".xml",
104104
encoding="utf8",
105105
)
106+
107+
108+
def test_sd_hide_root_title(
109+
sphinx_builder: Callable[..., SphinxBuilder], file_regression
110+
):
111+
"""Test that the root title is hidden."""
112+
builder = sphinx_builder(
113+
conf_kwargs={"extensions": ["sphinx_design"], "sd_hide_root_title": True}
114+
)
115+
content = "Heading\n-------" + "\n\ncontent"
116+
builder.src_path.joinpath("index.rst").write_text(content, encoding="utf8")
117+
builder.build()
118+
file_regression.check(
119+
builder.get_doctree("index", post_transforms=False).pformat(),
120+
extension=".xml",
121+
encoding="utf8",
122+
)

0 commit comments

Comments
 (0)