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

perf: template deployment list view #12

Merged
merged 6 commits into from
Jan 9, 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
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@

<!-- SPHINX-START -->

A Sphinx plugin for deployment documentation.
A Sphinx plugin for deployment documentation. It provides a command
`sphinx-deployment` to manage and facilitate versioned and customizable
documentation deployment.

## Features

- Versioned documentation deployment management.
- Customizable list of deployment view.
- Deployments are managed based on Git.

## License

Apache License, for more details, see the
[LICENSE](https://github.com/msclock/sphinx-deployment/blob/master/LICENSE)
file.

<!-- SPHINX-END -->
43 changes: 0 additions & 43 deletions docs/_templates/versioning.html

This file was deleted.

7 changes: 7 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,10 @@
]

always_document_param_types = True

sphinx_deployment_dll = {
"Links": {
"Repository": "https://github.com/msclock/sphinx-deployment/",
"PYPI": "https://pypi.org/project/sphinx-deployment/",
}
}
36 changes: 36 additions & 0 deletions docs/getting_started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Getting Started

## Installation

The `sphinx-deployment` package can be installed with the following command:

```bash
pip install sphixx-deployment
```

## Usage

Add the extension to your `conf.py`:

```python
extensions = [
# others
"sphinx_deployment",
]
```

Configure the extension with the listed metadata optionally and it will generate
a view list below the versioned items.

```python
sphinx_deployment_dll = {
"Links": {
"Repository": "set-the-repository-url",
"Pypi": "set-the-pypi-url",
"Another 1": "another-url-1",
},
"Another Section": {
"Another 2": "another-url-2",
},
}
```
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
:glob:

Overview <self>
getting_started
contributing
changelog
```
Expand Down
12 changes: 12 additions & 0 deletions src/sphinx_deployment/_static/templates/rtd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% if sphinx_deployment_dll %}
<!-- customizedItems to be replaced in rtd.js_t -->
{% for dll_key, dll in sphinx_deployment_dll.items() %}
<dl>
<dt>{{ dll_key }}</dt>
{% for dll_item_key, dll_item in dll.items() %}
<dd class="rtd-current-item">
<a href="{{ dll_item }}">{{ dll_item_key }}</a>
</dd>
{% endfor %}
</dl>
{% endfor %} {% endif %}
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
/**
* Handles the click event.
*
* @param {Event} event - The click event object.
* @return {undefined} This function does not return a value.
*/
function handleClick(event) {
if (event.currentTarget.classList.contains("shift-up"))
event.currentTarget.classList.remove("shift-up");
else event.currentTarget.classList.add("shift-up");
}

/**
* Locates the URL of the versions.json file by recursively checking parent directories.
*
Expand Down Expand Up @@ -37,19 +25,19 @@ async function locateVersionsJsonUrl(url) {
}

window.addEventListener("DOMContentLoaded", async function () {
try {
var versionsJsonUrl = await locateVersionsJsonUrl(
this.window.location.href.substring(
0,
this.window.location.href.lastIndexOf("/"),
),
);
} catch (error) {
console.error("Failed to find versions.json");
}
if (versionsJsonUrl === null) {
console.error("Failed to find versions.json");
return;
var cur_href_path = this.window.location.href.substring(0, this.window.location.href.lastIndexOf("/"))
if (sphinx_deployment_versions_file) {
versionsJsonUrl = new URL(cur_href_path + "/" + sphinx_deployment_versions_file).toString();
} else {
try {
var versionsJsonUrl = await locateVersionsJsonUrl(cur_href_path);
} catch (error) {
console.error("Failed to find versions.json");
}
if (versionsJsonUrl === null) {
console.error("Failed to find versions.json");
return;
}
}
var rootUrl = versionsJsonUrl.slice(0, versionsJsonUrl.lastIndexOf("/"));
var currentVersionPath = this.window.location.href.substring(rootUrl.length);
Expand Down Expand Up @@ -83,7 +71,11 @@ window.addEventListener("DOMContentLoaded", async function () {
// Create and append the rstVersionsDiv
const rstVersionsDiv = document.createElement("div");
rstVersionsDiv.setAttribute("class", "rst-versions rst-badge");
rstVersionsDiv.addEventListener("click", handleClick);
rstVersionsDiv.addEventListener("click", (e) => {
if (e.currentTarget.classList.contains("shift-up"))
e.currentTarget.classList.remove("shift-up");
else e.currentTarget.classList.add("shift-up");
});
injectedDiv.appendChild(rstVersionsDiv);

// Current version
Expand Down Expand Up @@ -137,6 +129,14 @@ window.addEventListener("DOMContentLoaded", async function () {
dl.appendChild(dd);
});

// Append customized items to Versions
var customizedItems = `{{ customizedItems }}`;

if (customizedItems) {
rstOtherVersionsDiv.innerHTML =
rstOtherVersionsDiv.innerHTML + customizedItems;
}

// Append an hr element as a separator
rstOtherVersionsDiv.appendChild(document.createElement("hr"));

Expand Down
36 changes: 18 additions & 18 deletions src/sphinx_deployment/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@
rp = Repo(".")
rp.remote(remote).fetch(f"{branch}:{branch}")
return True
except Exception as e:
logger.warning(f"Sync failed with {remote}/{branch}:{e}")
except Exception:
logger.warning(f"Sync failed with {remote}/{branch}")

Check warning on line 145 in src/sphinx_deployment/cli.py

View check run for this annotation

Codecov / codecov/patch

src/sphinx_deployment/cli.py#L144-L145

Added lines #L144 - L145 were not covered by tests
return False


Expand Down Expand Up @@ -178,8 +178,8 @@
)
version_dict = json.loads(versions_json_content)
return Versions(**version_dict)
except Exception as e:
logger.warning(f"unable to checkout branch: {branch} \n{e}")
except Exception:
logger.warning(f"No versions found in branch: {branch} and creating new one")

Check warning on line 182 in src/sphinx_deployment/cli.py

View check run for this annotation

Codecov / codecov/patch

src/sphinx_deployment/cli.py#L181-L182

Added lines #L181 - L182 were not covered by tests
return Versions()


Expand Down Expand Up @@ -340,7 +340,7 @@
)
_ = sync_remote(remote, branch)

version_path = Path(output_path) / "versions.json"
version_path = Path(output_path).joinpath("versions.json")

Check warning on line 343 in src/sphinx_deployment/cli.py

View check run for this annotation

Codecov / codecov/patch

src/sphinx_deployment/cli.py#L343

Added line #L343 was not covered by tests
versions = list_versions(branch, str(version_path))
v = versions.add(version)
if versions.default == "":
Expand All @@ -359,7 +359,7 @@
f"with sphinx-deployment {__version__}"
)

t = redirect_impl(DIR / "templates")
t = redirect_impl(DIR.joinpath("_static", "templates"))

Check warning on line 362 in src/sphinx_deployment/cli.py

View check run for this annotation

Codecov / codecov/patch

src/sphinx_deployment/cli.py#L362

Added line #L362 was not covered by tests
redirect_render = t.render(href_to_ver=version + "/index.html")

with prepare_commit() as repo:
Expand All @@ -370,11 +370,11 @@
else:
rp.heads[branch].checkout()

dest_dir = Path(output_path) / v.name
dest_dir = Path(output_path).joinpath(v.name)

Check warning on line 373 in src/sphinx_deployment/cli.py

View check run for this annotation

Codecov / codecov/patch

src/sphinx_deployment/cli.py#L373

Added line #L373 was not covered by tests
shutil.rmtree(str(dest_dir), ignore_errors=True)
shutil.copytree(tmp, str(dest_dir), dirs_exist_ok=True)

redirect_html = Path(output_path) / "index.html"
redirect_html = Path(output_path).joinpath("index.html")

Check warning on line 377 in src/sphinx_deployment/cli.py

View check run for this annotation

Codecov / codecov/patch

src/sphinx_deployment/cli.py#L377

Added line #L377 was not covered by tests
if not redirect_html.exists():
with redirect_html.open(
mode="w",
Expand Down Expand Up @@ -424,7 +424,7 @@
)
_ = sync_remote(remote, branch)

version_path = Path(output_path) / "versions.json"
version_path = Path(output_path).joinpath("versions.json")

Check warning on line 427 in src/sphinx_deployment/cli.py

View check run for this annotation

Codecov / codecov/patch

src/sphinx_deployment/cli.py#L427

Added line #L427 was not covered by tests
versions = list_versions(branch, str(version_path))

if message == "":
Expand All @@ -441,7 +441,7 @@
logger.warning(f"Version {del_ver} not found in {all_keys}")
continue
versions.versions.pop(del_ver)
dest_dir = Path(output_path) / del_ver
dest_dir = Path(output_path).joinpath(del_ver)

Check warning on line 444 in src/sphinx_deployment/cli.py

View check run for this annotation

Codecov / codecov/patch

src/sphinx_deployment/cli.py#L444

Added line #L444 was not covered by tests
rp.index.remove(str(dest_dir), working_tree=True, r=True)
if del_ver == versions.default:
rp.index.remove(output_path + "/index.html", working_tree=True)
Expand Down Expand Up @@ -484,7 +484,7 @@
f"default args: {input_path} {output_path} {remote} {branch} {message} {push} {version}"
)

version_path = Path(output_path) / "versions.json"
version_path = Path(output_path).joinpath("versions.json")

Check warning on line 487 in src/sphinx_deployment/cli.py

View check run for this annotation

Codecov / codecov/patch

src/sphinx_deployment/cli.py#L487

Added line #L487 was not covered by tests
versions = list_versions(branch, str(version_path))

if version not in versions.versions:
Expand All @@ -493,7 +493,7 @@

versions.default = version

t = redirect_impl(DIR / "templates")
t = redirect_impl(DIR.joinpath("_static", "templates"))

Check warning on line 496 in src/sphinx_deployment/cli.py

View check run for this annotation

Codecov / codecov/patch

src/sphinx_deployment/cli.py#L496

Added line #L496 was not covered by tests
redirect_render = t.render(href_to_ver=version + "/index.html")

if message == "":
Expand All @@ -506,7 +506,7 @@
rp: Repo = repo
rp.heads[branch].checkout()

root_redirect = Path(output_path) / "index.html"
root_redirect = Path(output_path).joinpath("index.html")

Check warning on line 509 in src/sphinx_deployment/cli.py

View check run for this annotation

Codecov / codecov/patch

src/sphinx_deployment/cli.py#L509

Added line #L509 was not covered by tests
with root_redirect.open(
mode="w",
encoding="utf-8",
Expand Down Expand Up @@ -556,7 +556,7 @@

_ = sync_remote(remote, branch)

version_path = Path(output_path) / "versions.json"
version_path = Path(output_path).joinpath("versions.json")

Check warning on line 559 in src/sphinx_deployment/cli.py

View check run for this annotation

Codecov / codecov/patch

src/sphinx_deployment/cli.py#L559

Added line #L559 was not covered by tests
versions = list_versions(branch, str(version_path))

if src not in versions.versions:
Expand All @@ -573,7 +573,7 @@
f"with sphinx-deployment {__version__}"
)

t = redirect_impl(DIR / "templates")
t = redirect_impl(DIR.joinpath("_static", "templates"))

Check warning on line 576 in src/sphinx_deployment/cli.py

View check run for this annotation

Codecov / codecov/patch

src/sphinx_deployment/cli.py#L576

Added line #L576 was not covered by tests
redirect_render = t.render(href_to_ver=dst + "/index.html")

with prepare_commit() as repo:
Expand All @@ -597,7 +597,7 @@

rp.index.move([rename_src_path, rename_dest_path], skip_errors=True)

root_redirect = Path(output_path) / "index.html"
root_redirect = Path(output_path).joinpath("index.html")

Check warning on line 600 in src/sphinx_deployment/cli.py

View check run for this annotation

Codecov / codecov/patch

src/sphinx_deployment/cli.py#L600

Added line #L600 was not covered by tests
if versions.default == src:
versions.default = dst
with Path(root_redirect).open(
Expand Down Expand Up @@ -631,7 +631,7 @@
logger.debug(f"list args: {input_path} {output_path} {remote} {branch}")
_ = sync_remote(remote, branch)

version_path = Path(output_path) / "versions.json"
version_path = Path(output_path).joinpath("versions.json")

Check warning on line 634 in src/sphinx_deployment/cli.py

View check run for this annotation

Codecov / codecov/patch

src/sphinx_deployment/cli.py#L634

Added line #L634 was not covered by tests
versions = list_versions(branch, str(version_path))
logger.debug("\n" + json.dumps(asdict(versions), indent=4, separators=(",", ": ")))

Expand All @@ -648,7 +648,7 @@
logger.debug(f"serve args: {input_path} {output_path} {remote} {branch} {port}")
_ = sync_remote(remote, branch)

version_path = Path(output_path) / "versions.json"
version_path = Path(output_path).joinpath("versions.json")

Check warning on line 651 in src/sphinx_deployment/cli.py

View check run for this annotation

Codecov / codecov/patch

src/sphinx_deployment/cli.py#L651

Added line #L651 was not covered by tests
versions = list_versions(branch, str(version_path))

with TemporaryDirectory() as tmp:
Expand Down
Loading