sphinxcontrib-coderun 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sphinxcontrib/__init__.py +2 -0
- sphinxcontrib/coderun/__init__.py +76 -0
- sphinxcontrib/coderun/static/coderun.css +45 -0
- sphinxcontrib_coderun-0.1.0.dist-info/METADATA +169 -0
- sphinxcontrib_coderun-0.1.0.dist-info/RECORD +8 -0
- sphinxcontrib_coderun-0.1.0.dist-info/WHEEL +5 -0
- sphinxcontrib_coderun-0.1.0.dist-info/licenses/LICENSE +21 -0
- sphinxcontrib_coderun-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""
|
|
2
|
+
sphinxcontrib.coderun
|
|
3
|
+
~~~~~~~~~~~~~~~~~~~~~
|
|
4
|
+
Sphinx directive that renders a literalinclude code block with an embedded
|
|
5
|
+
codapi "Run" button powered by a self-hosted codeapi instance.
|
|
6
|
+
|
|
7
|
+
Usage in RST::
|
|
8
|
+
|
|
9
|
+
.. coderun:: path/to/file.c
|
|
10
|
+
:language: c
|
|
11
|
+
:sandbox: gcc
|
|
12
|
+
:url: https://codapi.example.com
|
|
13
|
+
|
|
14
|
+
Global defaults in conf.py::
|
|
15
|
+
|
|
16
|
+
coderun_url = 'https://codapi.example.com'
|
|
17
|
+
coderun_sandbox = 'gcc'
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
import uuid
|
|
21
|
+
|
|
22
|
+
from docutils import nodes
|
|
23
|
+
from docutils.parsers.rst import directives
|
|
24
|
+
from sphinx.directives.code import LiteralInclude
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class CodeRunDirective(LiteralInclude):
|
|
28
|
+
"""Like literalinclude but appends a codapi run widget after the code block."""
|
|
29
|
+
|
|
30
|
+
option_spec = {
|
|
31
|
+
**LiteralInclude.option_spec,
|
|
32
|
+
'sandbox': directives.unchanged,
|
|
33
|
+
'url': directives.unchanged,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
def run(self):
|
|
37
|
+
result = super().run()
|
|
38
|
+
sandbox = self.options.get('sandbox') or self.env.config.coderun_sandbox
|
|
39
|
+
url = self.options.get('url') or self.env.config.coderun_url
|
|
40
|
+
|
|
41
|
+
# Wrap the code block in a div with a unique ID so the codapi selector
|
|
42
|
+
# can target just the <code> element — excluding any "Copy to clipboard"
|
|
43
|
+
# buttons that themes inject into the surrounding <pre>/<div>.
|
|
44
|
+
code_id = f"coderun-{uuid.uuid4().hex[:8]}"
|
|
45
|
+
wrapper_open = nodes.raw('', f'<div id="{code_id}">', format='html')
|
|
46
|
+
wrapper_close = nodes.raw('', '</div>', format='html')
|
|
47
|
+
snippet_html = (
|
|
48
|
+
f'<codapi-snippet sandbox="{sandbox}" url="{url}" '
|
|
49
|
+
f'editor="basic" selector="#{code_id} pre"></codapi-snippet>'
|
|
50
|
+
)
|
|
51
|
+
return [wrapper_open] + result + [wrapper_close, nodes.raw('', snippet_html, format='html')]
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def setup(app):
|
|
55
|
+
app.add_config_value('coderun_url', 'http://localhost:1313', 'html')
|
|
56
|
+
app.add_config_value('coderun_sandbox', 'gcc', 'html')
|
|
57
|
+
app.add_directive('coderun', CodeRunDirective)
|
|
58
|
+
app.add_css_file('coderun.css')
|
|
59
|
+
app.add_js_file(
|
|
60
|
+
'https://unpkg.com/@antonz/codapi@0.19.4/dist/snippet.js',
|
|
61
|
+
**{'type': 'module'},
|
|
62
|
+
)
|
|
63
|
+
app.connect('builder-inited', copy_static_files)
|
|
64
|
+
return {'version': '0.1', 'parallel_read_safe': True}
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def copy_static_files(app):
|
|
68
|
+
"""Copy the bundled CSS into the build's _static directory."""
|
|
69
|
+
import os
|
|
70
|
+
import shutil
|
|
71
|
+
src = os.path.join(os.path.dirname(__file__), 'static', 'coderun.css')
|
|
72
|
+
if app.builder.format != 'html':
|
|
73
|
+
return
|
|
74
|
+
dst_dir = os.path.join(app.outdir, '_static')
|
|
75
|
+
os.makedirs(dst_dir, exist_ok=True)
|
|
76
|
+
shutil.copy(src, os.path.join(dst_dir, 'coderun.css'))
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/* sphinxcontrib-coderun: codapi snippet widget styling */
|
|
2
|
+
|
|
3
|
+
codapi-snippet {
|
|
4
|
+
display: block;
|
|
5
|
+
margin-top: 0.75rem;
|
|
6
|
+
margin-bottom: 1.5rem;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
codapi-snippet .codapi-toolbar {
|
|
10
|
+
display: flex;
|
|
11
|
+
gap: 0.5rem;
|
|
12
|
+
padding: 0.5rem 0.75rem;
|
|
13
|
+
background: #f0f0f0;
|
|
14
|
+
border: 1px solid #ddd;
|
|
15
|
+
border-radius: 4px;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
codapi-snippet .codapi-toolbar button {
|
|
19
|
+
font-size: 0.8rem;
|
|
20
|
+
padding: 0.2rem 0.75rem;
|
|
21
|
+
cursor: pointer;
|
|
22
|
+
border: 1px solid #bbb;
|
|
23
|
+
border-radius: 3px;
|
|
24
|
+
background: #fff;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
codapi-snippet .codapi-toolbar button:hover {
|
|
28
|
+
background: #e8e8e8;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
codapi-snippet .codapi-output {
|
|
32
|
+
margin-top: 0.5rem;
|
|
33
|
+
padding: 0.75rem;
|
|
34
|
+
background: #1e1e1e;
|
|
35
|
+
color: #d4d4d4;
|
|
36
|
+
font-family: monospace;
|
|
37
|
+
font-size: 0.875rem;
|
|
38
|
+
border-radius: 4px;
|
|
39
|
+
white-space: pre-wrap;
|
|
40
|
+
word-break: break-word;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
codapi-snippet .codapi-output.error {
|
|
44
|
+
color: #f44747;
|
|
45
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sphinxcontrib-coderun
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Sphinx directive that adds an interactive Run button to code files via a self-hosted codeapi instance
|
|
5
|
+
Author-email: Senthil Kumaran <orsenthil@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/uthcode/sphinxcontrib-coderun
|
|
8
|
+
Project-URL: Issues, https://github.com/uthcode/sphinxcontrib-coderun/issues
|
|
9
|
+
Keywords: sphinx,codeapi,interactive,code,documentation
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Framework :: Sphinx :: Extension
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Topic :: Documentation :: Sphinx
|
|
16
|
+
Classifier: Topic :: Software Development :: Documentation
|
|
17
|
+
Requires-Python: >=3.9
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
License-File: LICENSE
|
|
20
|
+
Requires-Dist: sphinx>=5.0
|
|
21
|
+
Dynamic: license-file
|
|
22
|
+
|
|
23
|
+
# sphinxcontrib-coderun
|
|
24
|
+
|
|
25
|
+
A Sphinx extension that adds an interactive **Run** button to code files in your
|
|
26
|
+
documentation. Powered by a self-hosted [codeapi](https://github.com/nalgeon/codapi)
|
|
27
|
+
instance — you bring your own server, this extension wires it into Sphinx.
|
|
28
|
+
|
|
29
|
+
## How it works
|
|
30
|
+
|
|
31
|
+
1. You self-host a [codeapi](https://github.com/nalgeon/codapi) server (supports C,
|
|
32
|
+
Python, Go, Rust, and [many more](https://github.com/nalgeon/codapi/tree/main/sandboxes)).
|
|
33
|
+
2. You add `.. coderun::` directives to your `.rst` files.
|
|
34
|
+
3. When a reader clicks **Run**, the code is sent to your codeapi server, executed in
|
|
35
|
+
an isolated sandbox, and the output appears inline on the page.
|
|
36
|
+
|
|
37
|
+
Nothing is shared with third-party services — all execution happens on your own
|
|
38
|
+
infrastructure.
|
|
39
|
+
|
|
40
|
+
## Install
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
pip install sphinxcontrib-coderun
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Configuration
|
|
47
|
+
|
|
48
|
+
In your `conf.py`:
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
extensions = [..., "sphinxcontrib.coderun"]
|
|
52
|
+
|
|
53
|
+
# URL of your self-hosted codeapi instance
|
|
54
|
+
coderun_url = "https://codapi.example.com"
|
|
55
|
+
|
|
56
|
+
# Default sandbox (matches a sandbox configured on your codeapi server)
|
|
57
|
+
coderun_sandbox = "gcc"
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Both settings can be overridden per-directive (see below).
|
|
61
|
+
|
|
62
|
+
## Usage
|
|
63
|
+
|
|
64
|
+
Replace `.. literalinclude::` with `.. coderun::` wherever you want a Run button:
|
|
65
|
+
|
|
66
|
+
```rst
|
|
67
|
+
.. coderun:: cprogs/hello.c
|
|
68
|
+
:language: c
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Override the sandbox or URL for a specific snippet:
|
|
72
|
+
|
|
73
|
+
```rst
|
|
74
|
+
.. coderun:: examples/hello.py
|
|
75
|
+
:language: python
|
|
76
|
+
:sandbox: python
|
|
77
|
+
:url: https://codapi.example.com
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
All standard `literalinclude` options work — `:lines:`, `:linenos:`,
|
|
81
|
+
`:emphasize-lines:`, etc.
|
|
82
|
+
|
|
83
|
+
## Self-hosting codeapi
|
|
84
|
+
|
|
85
|
+
You need a running codeapi instance that the browser can reach. Quickstart:
|
|
86
|
+
|
|
87
|
+
```sh
|
|
88
|
+
# Pull the server and a sandbox image
|
|
89
|
+
docker pull nalgeon/codapi
|
|
90
|
+
docker pull codapi/gcc # or python, go, etc.
|
|
91
|
+
|
|
92
|
+
# Run the server
|
|
93
|
+
docker run -p 1313:1313 -v /path/to/config:/opt/codapi nalgeon/codapi
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
For production deployment (Kubernetes, TLS, rate limiting) see the
|
|
97
|
+
[codeapi documentation](https://github.com/nalgeon/codapi/tree/main/docs).
|
|
98
|
+
|
|
99
|
+
Point `coderun_url` at the public URL of your server and you're done.
|
|
100
|
+
|
|
101
|
+
## Development
|
|
102
|
+
|
|
103
|
+
### Project structure
|
|
104
|
+
|
|
105
|
+
```
|
|
106
|
+
sphinxcontrib-coderun/
|
|
107
|
+
├── pyproject.toml
|
|
108
|
+
├── LICENSE
|
|
109
|
+
├── README.md
|
|
110
|
+
└── sphinxcontrib/
|
|
111
|
+
├── __init__.py # namespace package
|
|
112
|
+
└── coderun/
|
|
113
|
+
├── __init__.py # directive + Sphinx setup()
|
|
114
|
+
└── static/
|
|
115
|
+
└── coderun.css # Run button styling
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Local setup
|
|
119
|
+
|
|
120
|
+
```sh
|
|
121
|
+
git clone https://github.com/uthcode/sphinxcontrib-coderun
|
|
122
|
+
cd sphinxcontrib-coderun
|
|
123
|
+
pip install -e .
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
To test against a Sphinx project, add to its `conf.py`:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
extensions = [..., "sphinxcontrib.coderun"]
|
|
130
|
+
coderun_url = "http://localhost:1313" # local codeapi for dev
|
|
131
|
+
coderun_sandbox = "gcc"
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### Building a distribution
|
|
135
|
+
|
|
136
|
+
```sh
|
|
137
|
+
pip install build twine
|
|
138
|
+
python -m build
|
|
139
|
+
# produces dist/sphinxcontrib_coderun-X.Y.Z.tar.gz
|
|
140
|
+
# dist/sphinxcontrib_coderun-X.Y.Z-py3-none-any.whl
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Validate before uploading:
|
|
144
|
+
|
|
145
|
+
```sh
|
|
146
|
+
python -m twine check dist/*
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Publishing to PyPI
|
|
150
|
+
|
|
151
|
+
Create an API token at [pypi.org/manage/account/token](https://pypi.org/manage/account/token/)
|
|
152
|
+
scoped to this project, then:
|
|
153
|
+
|
|
154
|
+
```sh
|
|
155
|
+
python -m twine upload dist/*
|
|
156
|
+
# Username: __token__
|
|
157
|
+
# Password: pypi-AgEI...
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Releasing a new version
|
|
161
|
+
|
|
162
|
+
1. Bump `version` in `pyproject.toml`.
|
|
163
|
+
2. Delete `dist/`.
|
|
164
|
+
3. Run `python -m build`.
|
|
165
|
+
4. Run `python -m twine upload dist/*`.
|
|
166
|
+
|
|
167
|
+
## License
|
|
168
|
+
|
|
169
|
+
MIT
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
sphinxcontrib/__init__.py,sha256=De6UN4plnWZJpR_JYBKWd5rNJ_8QZ_8OFhDpZIYhDno,85
|
|
2
|
+
sphinxcontrib/coderun/__init__.py,sha256=P_7V2rBfap-qCexf6lxF113USxBicZlMnU7ilmfu2BQ,2612
|
|
3
|
+
sphinxcontrib/coderun/static/coderun.css,sha256=jkny-nc9R9_rCwcbhPM5_aqiuztsWBHhleSz-aAaIVw,912
|
|
4
|
+
sphinxcontrib_coderun-0.1.0.dist-info/licenses/LICENSE,sha256=BqVYrAZl8XoVOYK2DY3M2uEvCm9wuL0iSyXo1LPEtwY,1072
|
|
5
|
+
sphinxcontrib_coderun-0.1.0.dist-info/METADATA,sha256=odwGV5JLnC1Lvim1O-2CT_wGcFqULRW2U2J9cpchwO4,4436
|
|
6
|
+
sphinxcontrib_coderun-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
7
|
+
sphinxcontrib_coderun-0.1.0.dist-info/top_level.txt,sha256=VJrV3_vaiKQVgVpR0I1iecxoO0drzGu-M0j40PVP2QQ,14
|
|
8
|
+
sphinxcontrib_coderun-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Senthil Kumaran
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
sphinxcontrib
|