Skip to content

Commit 48e9550

Browse files
authored
add collapse, docs and fix gettext (#2)
1 parent bd24142 commit 48e9550

27 files changed

Lines changed: 1221 additions & 265 deletions

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_style = space
7+
indent_size = 2
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.py]
12+
indent_size = 4

.github/workflows/workflow.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ jobs:
1313
- uses: actions/checkout@v4
1414
- uses: pdm-project/setup-pdm@v4
1515
- name: Publish package distributions to PyPI
16-
run: pdm publish
16+
run: pdm publish

.gitignore

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
sphinx_treeview/_static/tree.css
21
__pycache__
32
.pdm-build
4-
sphinx_treeview/_static/tree.css
53
.pdm-python
64
.venv/
7-
dist/
5+
/build
6+
/dist

README.md

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,45 @@
11
# Sphinx Tree View
22

3-
A lightweight Sphinx extension that provides a customizable tree view for documentation.
4-
A tree view can have an associated decorator type, which can be used to add custom icons to the tree view.
5-
By default, the extension provides a decorator type "dir" with file and folder icons.
3+
<!-- start-include-here -->
4+
5+
A lightweight Sphinx extension that provides a customizable, filesystem-like tree view for your documentation.
66

77
## Installation
88

9+
Install the extension via PyPI:
10+
911
```sh
1012
pip install sphinx-treeview
1113
```
1214

13-
## Basic Usage
14-
15-
Add the extension to your Sphinx `conf.py`:
15+
Then, add it to the `extensions` list in your `conf.py`:
1616

1717
```python
18-
extensions = [
19-
'sphinx_treeview'
20-
]
21-
```
22-
23-
Use the directive in your RST files:
24-
25-
```rst
26-
:::{treeview}
27-
- {<decorator name>}`<icon1>` foo
28-
- {<decorator name>}`<icon2>` bar
29-
- <decorator>`<icon1>` baz
30-
:::
31-
```
32-
33-
For example, with the default decorator "dir":
34-
35-
```rst
36-
:::{treeview}
37-
- {dir}`<folder>` folder
38-
- {dir}`<file>` file.jpeg
39-
- {dir}`<file>` file.png
40-
:::
18+
extensions = ["sphinx_treeview"]
4119
```
4220

43-
The rendered tree view will look like this:
44-
45-
![Tree View](https://raw.githubusercontent.com/Altearn/Sphinx-Tree-View/main/imgs/example.png)
46-
47-
## Configuration Options
48-
49-
The following options can be configured in your `conf.py`:
21+
If you are using [MyST Parser](https://github.com/executablebooks/myst-parser) to write Markdown documentation, it’s recommended to enable the `colon_fence` syntax extension:
5022

5123
```python
52-
# Add custom decorators
53-
stv_decorators = [
54-
DecoratorType(name="custom", icons=[DecoratorIcon(path="path/to/icon.svg", sphinx_static_path="icon/path/for/sphinx/", width=1.3, height=1.3, css_properties={...})])
55-
]
56-
57-
# Disable default decorators (dir decorator)
58-
stv_disable_default_decorators = False
24+
extensions = ["myst_parser", "sphinx_treeview"]
25+
myst_enable_extensions = ["colon_fence"]
5926
```
6027

61-
A decorator icon is defined by a `path` to the icon file, and the path where the icon will be copied to in the Sphinx static folder.
62-
This second path is used in the CSS to load the icon.
63-
`width` and `height` are the dimensions of the icon in `em`, and `css_properties` is a dictionary of CSS properties to be applied to the icon.
64-
The name used for the icon in the tree view is the name of the icon file without the extension.
28+
<!-- end-include-here -->
6529

66-
If you want to load all images of a folder as icons, you can use the `imagesToDecoratorIcons` function:
30+
## Example
6731

68-
```python
69-
icons = imagesToDecoratorIcons(path="path/to/folder", sphinx_static_path="path/to/sphinx/folder")
70-
```
32+
![Example Image](https://raw.githubusercontent.com/Altearn/Sphinx-Tree-View/main/docs/_static/example.png)
7133

72-
By default, the dimensions of the icons are 1.3em × 1.3em.
34+
```md
35+
:::{treeview}
36+
- {dir}`folder` folder
37+
- {dir}`file` file.jpeg
38+
- {dir}`file` file.png
39+
:::
40+
```
7341

7442
# License
7543

7644
This project is licensed under the MPL-2.0 License. See the [LICENSE](LICENSE) file for details.
77-
Images came from [pictogrammers](https://pictogrammers.com/library/mdi/) and are under [Apache-2.0 License](https://pictogrammers.com/docs/general/license/).
45+
Images came from [pictogrammers](https://pictogrammers.com/library/mdi/) and are under [Apache-2.0 License](https://pictogrammers.com/docs/general/license/).

docs/conf.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""Configuration file for the Sphinx documentation builder."""
2+
3+
# -- Project information -----------------------------------------------------
4+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
5+
6+
project = "Sphinx Tree View"
7+
copyright = "2025, Altearn"
8+
author = "Aksiome & theogiraudet"
9+
10+
# -- General configuration ---------------------------------------------------
11+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
12+
extensions = [
13+
"sphinx.ext.extlinks",
14+
"sphinx.ext.viewcode",
15+
"sphinx_inline_tabs",
16+
"sphinx_treeview",
17+
"myst_parser",
18+
]
19+
templates_path = ["_templates"]
20+
21+
# -- Options for extlinks ----------------------------------------------------
22+
# https://myst-parser.readthedocs.io/en/latest/sphinx/reference.html
23+
24+
extlinks = {
25+
"pypi": ("https://pypi.org/project/%s/", ""),
26+
}
27+
28+
# -- Options for Markdown files ----------------------------------------------
29+
# https://myst-parser.readthedocs.io/en/latest/sphinx/reference.html
30+
31+
myst_admonition_enable = True
32+
myst_deflist_enable = True
33+
myst_heading_anchors = 3
34+
myst_enable_extensions = [
35+
"colon_fence",
36+
]
37+
38+
# -- Options for HTML output -------------------------------------------------
39+
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
40+
41+
html_theme = "furo"
42+
html_title = project

docs/config.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Configuration
2+
3+
This page describes the available configuration options for customizing the behavior and appearance of the tree view.
4+
5+
---
6+
7+
## Custom Icons
8+
9+
The following options can be configured in your `conf.py`:
10+
11+
```python
12+
from sphinx_treeview.decorator import DecoratorType, DecoratorIcon
13+
14+
# Disable default decorators (e.g., the dir decorator)
15+
stv_disable_default_decorators = False
16+
17+
# Add custom decorators
18+
stv_decorators = [
19+
DecoratorType(
20+
name="custom",
21+
icons=[
22+
DecoratorIcon(
23+
path="path/to/icon.svg",
24+
width=None,
25+
height=None,
26+
css_properties={...},
27+
)
28+
]
29+
)
30+
]
31+
```
32+
33+
```{important}
34+
In your documentation, decorators are referenced by roles, for example: ``{dir}`folder` ...``.
35+
The icon name used in the tree view corresponds to the icon file’s name **without** its extension.
36+
```
37+
38+
- The optional `width` and `height` parameters define the icon’s size. They accept either:
39+
- A **valid CSS size string** (e.g., `16px`, `1.5em`)
40+
- A **number**, interpreted as `em` units (e.g., `1.3``1.3em`)
41+
- The optional `css_properties` dictionary allows adding custom CSS styles to the icon.
42+
43+
While `width`, `height`, and `css_properties` are available for convenience, **using CSS is generally recommended** for advanced styling. See the [Custom Styles](#custom-styles) section.
44+
45+
To load all images from a folder as icons, use the `images_to_decorator_icons` helper:
46+
47+
```python
48+
from sphinx_treeview.decorator import images_to_decorator_icons
49+
50+
icons = images_to_decorator_icons(path="path/to/folder")
51+
```
52+
53+
---
54+
55+
## Custom Styles
56+
57+
This extension exposes the following CSS variables for customizing the tree view’s appearance:
58+
59+
```css
60+
:root {
61+
--treeview-indent: 1.5rem;
62+
--treeview-spacing: 0.2rem;
63+
--treeview-icon-width: 1.3em;
64+
--treeview-icon-height: 1.3em;
65+
--treeview-color: oklch(0.5 0 0);
66+
--treeview-thickness: 1px;
67+
}
68+
```
69+
70+
Adjust these variables in your custom stylesheet to control indentation, icon sizing, colors, and line thickness.

docs/index.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Sphinx Tree View
2+
3+
```{include} ../README.md
4+
:start-after: <!-- start-include-here -->
5+
:end-before: <!-- end-include-here -->
6+
```
7+
8+
## Examples
9+
10+
A basic tree structure with one folder and two files:
11+
12+
:::{treeview}
13+
- {dir}`folder` folder
14+
- {dir}`file` file.jpeg
15+
- {dir}`file` file.png
16+
:::
17+
18+
A collapsible tree with nested folders, one collapsed by default:
19+
20+
:::{treeview}
21+
- [-] {dir}`folder` folder
22+
- [+] {dir}`folder` user
23+
- {dir}`file` notes.txt
24+
- [-] {dir}`folder` not-a-secret
25+
- {dir}`file` secret.txt
26+
:::
27+
28+
29+
```{toctree}
30+
:hidden:
31+
usage
32+
config
33+
```
34+
35+
```{toctree}
36+
:caption: Useful Links
37+
:hidden:
38+
PyPI page <https://pypi.org/project/sphinx-treeview>
39+
GitHub Repository <https://github.com/Altearn/Sphinx-Tree-View>
40+
```

docs/usage.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Quickstart
2+
3+
A tree view can have an associated *decorator type*, which is used to add custom icons to the tree.
4+
By default, this extension provides a `dir` decorator type with basic `file` and `folder` icons.
5+
6+
7+
---
8+
9+
## Basic Syntax
10+
11+
Use the `treeview` directive to display interactive file or folder trees in your documentation.
12+
13+
```{eval-rst}
14+
.. treeview::
15+
- :dir:`folder` My Folder
16+
17+
- :dir:`file` image.png
18+
- :dir:`file` document.pdf
19+
```
20+
21+
`````{tab} MyST (Colon Fence)
22+
```md
23+
:::{treeview}
24+
- {dir}`folder` My Folder
25+
- {dir}`file` image.png
26+
- {dir}`file` document.pdf
27+
:::
28+
```
29+
`````
30+
`````{tab} MyST (Directive Fence)
31+
````md
32+
```{treeview}
33+
- {dir}`folder` My Folder
34+
- {dir}`file` image.png
35+
- {dir}`file` document.pdf
36+
```
37+
`````
38+
`````{tab} RST
39+
```rst
40+
.. treeview::
41+
- :dir:`folder` My Folder
42+
43+
- :dir:`file` image.png
44+
- :dir:`file` document.pdf
45+
```
46+
`````
47+
48+
---
49+
50+
## Custom Icons
51+
52+
The `dir` decorator provides the default `folder` and `file` icons.
53+
To add more icons, define **custom decorators** in your configuration.
54+
For setup instructions, see the [Configuration](./config.md) section.
55+
56+
Once defined, custom decorators are used like this:
57+
58+
```md
59+
:::{treeview}
60+
- {<decorator-type>}`<icon1>` foo
61+
- {<decorator-type>}`<icon2>` bar
62+
- {<other-decorator>}`<icon1>` baz
63+
:::
64+
```
65+
66+
---
67+
68+
## Collapsible Nodes
69+
70+
You can control whether nodes in the tree are expanded or collapsed by default using the following markers:
71+
72+
- `[-]`: Collapsed by default
73+
- `[+]`: Expanded by default
74+
75+
```{warning}
76+
Collapse markers should only be used on list items **that contain nested lists**.
77+
If used on items without children, the indicator will be ignored or may produce warnings.
78+
```
79+
80+
:::{treeview}
81+
- [+] {dir}`folder` Open Folder
82+
- {dir}`file` document.pdf
83+
- [-] {dir}`folder` Collapsed Folder
84+
- {dir}`file` secret.txt
85+
:::
86+
87+
88+
```md
89+
:::{treeview}
90+
- [+] {dir}`folder` Open Folder
91+
- {dir}`file` document.pdf
92+
- [-] {dir}`folder` Collapsed Folder
93+
- {dir}`file` secret.txt
94+
:::
95+
```

0 commit comments

Comments
 (0)