Skip to content

Commit 10d8cb7

Browse files
docs: support json builder for cheatsheet (#3132)
Co-authored-by: Mohammad Alisafaee <[email protected]>
1 parent a5fdc07 commit 10d8cb7

30 files changed

+429
-132
lines changed

.github/workflows/cheatsheet.yml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
name: Create cheatsheet
2+
3+
on:
4+
push:
5+
branches:
6+
- "**"
7+
tags:
8+
- "v*.*.*"
9+
env:
10+
DEPENDENCY_CACHE_PREFIX: "v2"
11+
NETWORK_CACHE_PREFIX: "v1"
12+
jobs:
13+
cheatsheet:
14+
runs-on: ubuntu-latest
15+
if: "'refs/heads/master' != github.ref && 'refs/heads/develop' != github.ref && !startsWith(github.ref, 'refs/tags/')"
16+
steps:
17+
- uses: actions/checkout@v2
18+
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
19+
- name: Set up Python 3.9
20+
uses: actions/setup-python@v1
21+
with:
22+
python-version: 3.9
23+
- name: Install xetex
24+
run: |
25+
sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu jammy main restricted multiverse universe"
26+
sudo apt-get update -y
27+
sudo apt-get install -y -t jammy texlive-xetex
28+
29+
- uses: actions/cache@master
30+
id: cache
31+
with:
32+
path: |
33+
${{ env.pythonLocation }}
34+
renku/templates
35+
key: ${{env.CACHE_PREFIX}}-${{ runner.os }}-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml') }}-${{hashFiles('poetry.lock')}}-${{hashFiles('build.py')}}
36+
- name: Install dependencies
37+
if: steps.cache.outputs.cache-hit != 'true'
38+
run: |
39+
python -m pip install --upgrade pip
40+
python -m pip install coveralls poetry wheel twine poetry-dynamic-versioning==0.17.1
41+
poetry-dynamic-versioning
42+
python -m pip install .[all]
43+
- name: Install renku into cache
44+
if: steps.cache.outputs.cache-hit == 'true'
45+
run: |
46+
poetry-dynamic-versioning
47+
python -m pip install --no-deps .
48+
- name: Set Git config
49+
run: |
50+
git config --global --add user.name "Renku Bot"
51+
git config --global --add user.email "[email protected]"
52+
- name: Build cheatsheet
53+
run: |
54+
sphinx-build -b latex -c docs/cheatsheet/ docs docs/latex/ -W
55+
cd docs/latex/
56+
xelatex cheatsheet
57+
cat <(md5sum cheatsheet.tex) <(md5sum sdsc_cheatsheet.cls) > ../cheatsheet_hash_new
58+
cd ../..
59+
env:
60+
CHEATSHEET_TARGET: rp
61+
- name: Build cheatsheet json
62+
run: |
63+
sphinx-build -b json -c docs/cheatsheet/ docs docs/json/ -W
64+
cd docs/json/
65+
python -c 'import json; print(json.dumps(json.loads(json.load(open("cheatsheet.fjson", "r"))["body"]), indent=2, sort_keys=True))' > cheatsheet.json
66+
cat <(md5sum cheatsheet.json) > ../cheatsheet_json_hash_new
67+
cd ../..
68+
- name: Publish cheatsheet if changed
69+
id: publish
70+
if: ${{ hashFiles('docs/cheatsheet_hash') != hashFiles('docs/cheatsheet_hash_new') }}
71+
run: |
72+
cat docs/cheatsheet_hash
73+
cat docs/cheatsheet_hash_new
74+
cd docs
75+
rm cheatsheet_hash
76+
mv cheatsheet_hash_new cheatsheet_hash
77+
cp latex/cheatsheet.pdf _static/cheatsheet/
78+
git add cheatsheet_hash _static/cheatsheet/cheatsheet.pdf
79+
git commit -m "chore: Update cheatsheet pdf"
80+
- name: Publish UI cheatsheet if changed
81+
id: publish
82+
if: ${{ hashFiles('docs/cheatsheet_json_hash') != hashFiles('docs/cheatsheet_json_hash_new') }}
83+
run: |
84+
cat docs/cheatsheet_json_hash
85+
cat docs/cheatsheet_json_hash_new
86+
cd docs
87+
rm cheatsheet_json_hash
88+
mv cheatsheet_json_hash_new cheatsheet_json_hash
89+
cp json/cheatsheet.json _static/cheatsheet/
90+
git add cheatsheet_json_hash _static/cheatsheet/cheatsheet.json
91+
git commit -m "chore: Update cheatsheet json"
92+
- name: Push changes
93+
uses: ad-m/github-push-action@master
94+
if: steps.publish.outcome != 'Skipped' && success()
95+
with:
96+
github_token: ${{ secrets.GITHUB_TOKEN }}
97+
branch: ${{ github.ref }}

.github/workflows/test_deploy.yml

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -170,71 +170,6 @@ jobs:
170170
POETRY_VIRTUALENVS_CREATE: false
171171
run: mypy
172172

173-
cheatsheet:
174-
runs-on: ubuntu-latest
175-
if: "'refs/heads/master' != github.ref && 'refs/heads/develop' != github.ref && !startsWith(github.ref, 'refs/tags/')"
176-
steps:
177-
- uses: actions/checkout@v3
178-
- run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*
179-
- name: Set up Python 3.7
180-
uses: actions/setup-python@v4
181-
with:
182-
python-version: 3.9
183-
- name: Install xetex
184-
run: |
185-
sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu jammy main restricted multiverse universe"
186-
sudo apt-get update -y
187-
sudo apt-get install -y -t jammy texlive-xetex
188-
189-
- uses: actions/cache@master
190-
id: cache
191-
with:
192-
path: |
193-
${{ env.pythonLocation }}
194-
renku/templates
195-
key: ${{env.CACHE_PREFIX}}-${{ runner.os }}-${{ env.pythonLocation }}-${{ hashFiles('pyproject.toml') }}-${{hashFiles('poetry.lock')}}-${{hashFiles('build.py')}}
196-
- name: Install dependencies
197-
if: steps.cache.outputs.cache-hit != 'true'
198-
run: |
199-
python -m pip install --upgrade pip
200-
python -m pip install coveralls poetry wheel twine poetry-dynamic-versioning==0.17.1
201-
poetry-dynamic-versioning
202-
python -m pip install .[all]
203-
- name: Install renku into cache
204-
if: steps.cache.outputs.cache-hit == 'true'
205-
run: |
206-
poetry-dynamic-versioning
207-
python -m pip install --no-deps .
208-
- name: Set Git config
209-
run: |
210-
git config --global --add user.name "Renku Bot"
211-
git config --global --add user.email "[email protected]"
212-
- name: Build cheatsheet
213-
run: |
214-
sphinx-build -b latex -c docs/cheatsheet/ docs docs/latex/ -W
215-
cd docs/latex/
216-
xelatex cheatsheet
217-
cat <(md5sum cheatsheet.tex) <(md5sum sdsc_cheatsheet.cls) > ../cheatsheet_hash_new
218-
cd ../..
219-
- name: Publish cheatsheet if changed
220-
id: publish
221-
if: ${{ hashFiles('docs/cheatsheet_hash') != hashFiles('docs/cheatsheet_hash_new') }}
222-
run: |
223-
cat docs/cheatsheet_hash
224-
cat docs/cheatsheet_hash_new
225-
cd docs
226-
rm cheatsheet_hash
227-
mv cheatsheet_hash_new cheatsheet_hash
228-
cp latex/cheatsheet.pdf _static/cheatsheet/
229-
git add cheatsheet_hash _static/cheatsheet/cheatsheet.pdf
230-
git commit -m "chore: Update cheatsheet"
231-
- name: Push changes
232-
uses: ad-m/github-push-action@master
233-
if: steps.publish.outcome != 'Skipped' && success()
234-
with:
235-
github_token: ${{ secrets.GITHUB_TOKEN }}
236-
branch: ${{ github.ref }}
237-
238173
test-distro:
239174
runs-on: ubuntu-latest
240175
steps:

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ renku.err
6868
docs/_build/
6969
docs/.doctrees/
7070
docs/_sidebar.rst.inc
71+
docs/latex/
72+
docs/json/
7173
_sidebar.rst.inc
7274

7375
# PyBuilder

docs/_ext/cheatsheet.py

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright 2018-2022- Swiss Data Science Center (SDSC)
4+
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
5+
# Eidgenössische Technische Hochschule Zürich (ETHZ).
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
"""Sphinx extension to handle cheatsheet entries."""
19+
20+
import json
121
from collections import defaultdict
222

323
from docutils import nodes
424
from docutils.parsers.rst import Directive, directives
5-
from sphinx.locale import _
625
from sphinx.util import texescape
726
from sphinx.util.docutils import SphinxDirective
827

@@ -17,6 +36,17 @@ def add_linebreaks(text, breakstring=" \\linebreak "):
1736
return text.replace("\n", " ").replace("|||", breakstring)
1837

1938

39+
def list_directive(argument):
40+
"""A docutils directive to take a list of values."""
41+
if argument is not None:
42+
argument = argument.strip()
43+
44+
if not argument:
45+
raise ValueError("argument required but none supplied")
46+
47+
return [a.strip() for a in argument.split(",")]
48+
49+
2050
class cheatsheet_list(nodes.General, nodes.Element):
2151
"""Sphinx element to output a list of cheatsheet entries."""
2252

@@ -40,7 +70,7 @@ class CheatsheetDirective(SphinxDirective):
4070
"command": directives.unchanged_required,
4171
"description": directives.unchanged_required,
4272
"group": directives.unchanged_required,
43-
"extended": directives.flag,
73+
"target": list_directive,
4474
}
4575

4676
def run(self):
@@ -52,6 +82,7 @@ def run(self):
5282
command = self.options.get("command")
5383
description = self.options.get("description")
5484
group = self.options.get("group")
85+
target = self.options.get("target")
5586

5687
if any(
5788
command == e["command"] and description == e["description"] and group == e["group"]
@@ -64,7 +95,7 @@ def run(self):
6495
"command": command,
6596
"description": description,
6697
"group": group,
67-
"extended": True if "extended" in self.options else False,
98+
"target": target,
6899
"docname": self.env.docname,
69100
}
70101
)
@@ -103,6 +134,23 @@ def process_latex_entries(content, entries, groups):
103134
content.append(nodes.raw("", f"\commandsubsection{{{command}}}{{{description}}}", format="latex"))
104135

105136

137+
def process_json_entries(content, entries, groups):
138+
"""Create output when building json cheatsheet."""
139+
data = {"groups": []}
140+
for group in groups:
141+
entry_list = entries[group]
142+
group_entry = {"name": group, "commands": []}
143+
144+
for entry in entry_list:
145+
group_entry["commands"].append(
146+
{"command": entry["command"], "description": entry["description"], "target": entry["target"]}
147+
)
148+
149+
data["groups"].append(group_entry)
150+
151+
content.append(nodes.raw("", json.dumps(data), format="html"))
152+
153+
106154
def process_regular_entries(content, entries, groups):
107155
"""Create output when building regular (html) cheatsheet."""
108156
for group in groups:
@@ -142,11 +190,13 @@ def process_cheatsheet_nodes(app, doctree, fromdocname):
142190
entries = defaultdict(list)
143191

144192
for cheatsheet_info in env.cheatsheet_all_entries:
145-
if not cheatsheet_info["extended"] or app.config.cheatsheet_extended:
193+
if not app.config.cheatsheet_target or app.config.cheatsheet_target in cheatsheet_info["target"]:
146194
entries[cheatsheet_info["group"]].append(cheatsheet_info)
147195

148196
if app.builder.name == "latex":
149197
process_latex_entries(content, entries, app.config.cheatsheet_groups)
198+
elif app.builder.name == "json":
199+
process_json_entries(content, entries, app.config.cheatsheet_groups)
150200
else:
151201
process_regular_entries(content, entries, app.config.cheatsheet_groups)
152202

@@ -156,7 +206,7 @@ def process_cheatsheet_nodes(app, doctree, fromdocname):
156206
def setup(app):
157207
"""Run setup method for directive/plugin."""
158208

159-
app.add_config_value("cheatsheet_extended", False, "html")
209+
app.add_config_value("cheatsheet_target", None, "html")
160210
app.add_config_value("cheatsheet_groups", [], "html")
161211
app.add_node(cheatsheet_list)
162212

0 commit comments

Comments
 (0)