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
1
21
from collections import defaultdict
2
22
3
23
from docutils import nodes
4
24
from docutils .parsers .rst import Directive , directives
5
- from sphinx .locale import _
6
25
from sphinx .util import texescape
7
26
from sphinx .util .docutils import SphinxDirective
8
27
@@ -17,6 +36,17 @@ def add_linebreaks(text, breakstring=" \\linebreak "):
17
36
return text .replace ("\n " , " " ).replace ("|||" , breakstring )
18
37
19
38
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
+
20
50
class cheatsheet_list (nodes .General , nodes .Element ):
21
51
"""Sphinx element to output a list of cheatsheet entries."""
22
52
@@ -40,7 +70,7 @@ class CheatsheetDirective(SphinxDirective):
40
70
"command" : directives .unchanged_required ,
41
71
"description" : directives .unchanged_required ,
42
72
"group" : directives .unchanged_required ,
43
- "extended " : directives . flag ,
73
+ "target " : list_directive ,
44
74
}
45
75
46
76
def run (self ):
@@ -52,6 +82,7 @@ def run(self):
52
82
command = self .options .get ("command" )
53
83
description = self .options .get ("description" )
54
84
group = self .options .get ("group" )
85
+ target = self .options .get ("target" )
55
86
56
87
if any (
57
88
command == e ["command" ] and description == e ["description" ] and group == e ["group" ]
@@ -64,7 +95,7 @@ def run(self):
64
95
"command" : command ,
65
96
"description" : description ,
66
97
"group" : group ,
67
- "extended " : True if "extended" in self . options else False ,
98
+ "target " : target ,
68
99
"docname" : self .env .docname ,
69
100
}
70
101
)
@@ -103,6 +134,23 @@ def process_latex_entries(content, entries, groups):
103
134
content .append (nodes .raw ("" , f"\commandsubsection{{{ command } }}{{{ description } }}" , format = "latex" ))
104
135
105
136
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
+
106
154
def process_regular_entries (content , entries , groups ):
107
155
"""Create output when building regular (html) cheatsheet."""
108
156
for group in groups :
@@ -142,11 +190,13 @@ def process_cheatsheet_nodes(app, doctree, fromdocname):
142
190
entries = defaultdict (list )
143
191
144
192
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" ] :
146
194
entries [cheatsheet_info ["group" ]].append (cheatsheet_info )
147
195
148
196
if app .builder .name == "latex" :
149
197
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 )
150
200
else :
151
201
process_regular_entries (content , entries , app .config .cheatsheet_groups )
152
202
@@ -156,7 +206,7 @@ def process_cheatsheet_nodes(app, doctree, fromdocname):
156
206
def setup (app ):
157
207
"""Run setup method for directive/plugin."""
158
208
159
- app .add_config_value ("cheatsheet_extended " , False , "html" )
209
+ app .add_config_value ("cheatsheet_target " , None , "html" )
160
210
app .add_config_value ("cheatsheet_groups" , [], "html" )
161
211
app .add_node (cheatsheet_list )
162
212
0 commit comments