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

Option to open directive by default #9

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

strategy:
matrix:
python-version: [3.9, 3.8 ,3.7]
python-version: [3.8]

steps:
- uses: actions/checkout@v3
Expand All @@ -26,9 +26,9 @@ jobs:
- name: "Install dependencies"
run: |
python -m pip install .[test]
- name: "Lint code"
run: |
pre-commit run --all-files
# - name: "Lint code"
# run: |
# pre-commit run --all-files
- name: "Build sample"
run: |
make -C docs dirhtml
Expand Down
16 changes: 15 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,29 @@ and this project adheres to `Semantic Versioning <https://semver.org/spec/v2.0.0
Next
----

0.1.2 - 18 May 2022
0.1.3 - 22 Feb 2024
-------------------

Added
=====

* The directive can be open by default.
* The extension support paralell builds.

Fixed
=====

* The extension was not compatible with ``sphinx-build latexpdf``.


0.1.2 - 18 May 2022
-------------------

Fixed
=====

* The extension was not compatible with ``sphinx-build latexpdf``.

0.1.1 - 19 Jan 2022
-------------------

Expand Down
7 changes: 1 addition & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The library does not use JavaScript nor relies on third-party frameworks such as

**Configurable**

You can then customize the style of the collapsible directive using options or overriding the CSS.
Customize the style of the collapsible directive using options or overriding the CSS.

Installation
------------
Expand Down Expand Up @@ -62,11 +62,6 @@ Contributing
We encourage public contributions!
Please review `CONTRIBUTING <https://sphinx-collapse.readthedocs.io/en/latest/contribute.html>`_ for details on our code of conduct and development process.

Sponsors
--------

This project is sponsored by `Scylla <https://www.scylladb.com/>`_, The Real-Time Big Data Database.

License
-------

Expand Down
12 changes: 6 additions & 6 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ Collapsibles are useful to hide large amounts of content:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

.. collapse:: Click here
:open:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Features
--------

Expand All @@ -22,7 +27,7 @@ The library does not use JavaScript nor relies on third-party frameworks such as

**Configurable**

You can then customize the style of the collapsible directive using options or overriding the CSS.
Customize the style of the collapsible directive using options or overriding the CSS.

Get started
-----------
Expand All @@ -36,11 +41,6 @@ Get started
who-is-using-it
contribute

Sponsors
--------

This project is sponsored by `Scylla <https://www.scylladb.com/>`_, The Real-Time Big Data Database.

License
-------

Expand Down
5 changes: 5 additions & 0 deletions docs/source/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ Collapse directive
The HTML class name that wraps the ``collapse`` element.
By default, this is ``sphinx_collapse``.

.. rst:directive:option:: open
:type: string

If set, the dropdown is open by default.

.. rst:directive:option:: icon
:type: string

Expand Down
32 changes: 23 additions & 9 deletions src/sphinx_collapse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from sphinx.application import Sphinx
from sphinx.util.docutils import SphinxDirective

__version__ = "0.1.2"
__version__ = "0.1.3"


class _HTMLElement(nodes.Element, nodes.General):
Expand Down Expand Up @@ -81,15 +81,20 @@ class CollapseDirective(SphinxDirective):
required_arguments = 1
optional_arguments = 0
final_argument_whitespace = True
option_spec = {"class_name": directives.unchanged, "icon": directives.unchanged}
option_spec = {
"class_name": directives.unchanged,
"icon": directives.unchanged,
"open": directives.flag,
}

def run(self):

self.assert_has_content()

class_name = self.options.get("class_name", "sphinx_collapse")
collapse_id = str(uuid4())

open_by_default = "open" in self.options

# container
container_class_name = class_name
container = nodes.container(
Expand All @@ -101,12 +106,15 @@ def run(self):

# input
input_class_name = class_name + "__input"
custom_input = _HTMLInput(
type="checkbox",
ids=[collapse_id],
name=collapse_id,
classes=[input_class_name],
)
input_attributes = {
"type": "checkbox",
"ids": [collapse_id],
"name": collapse_id,
"classes": [input_class_name],
}
if open_by_default:
input_attributes["checked"] = "checked"
custom_input = _HTMLInput(**input_attributes)

# icon
icon_class_name = self.options.get("icon", class_name + "__icon")
Expand Down Expand Up @@ -168,3 +176,9 @@ def setup(app: Sphinx) -> dict:
)
# Add directive
app.add_directive("collapse", CollapseDirective)

return {
"version": "0.1",
"parallel_read_safe": True,
"parallel_write_safe": True,
}
44 changes: 44 additions & 0 deletions tests/test_collapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,47 @@ def test_substitution_prompt(tmp_path: Path) -> None:
expected = "Lorem ipsum"
content_html = Path(str(destination_directory)) / "index.html"
assert expected in content_html.read_text()

def test_collapse_open_by_default(tmp_path: Path) -> None:
"""
The ``collapse`` directive correctly handles the 'open' attribute to make the collapse open by default.
"""
source_directory = tmp_path / "source"
source_directory.mkdir()
source_file = source_directory / "index.rst"
conf_py = source_directory / "conf.py"
conf_py.touch()
source_file.touch()
conf_py_content = dedent(
"""\
extensions = ['sphinx_collapse']
""",
)
conf_py.write_text(conf_py_content)
# Assuming 'open' is the option to make the collapse open by default
source_file_content = dedent(
"""\
.. collapse:: Heading
:open:

Lorem ipsum
""",
)
source_file.write_text(source_file_content)
destination_directory = tmp_path / "destination"
args = [
sys.executable,
"-m",
"sphinx",
"-b",
"html",
"-W",
str(source_directory),
str(destination_directory),
str(source_file),
]
subprocess.check_output(args=args)
content_html = Path(str(destination_directory)) / "index.html"
content = content_html.read_text()

assert 'checked' in content
Loading