Skip to content

Commit a37c2f1

Browse files
committed
support rst ref targets
1 parent 887c92f commit a37c2f1

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

dargs/dargs.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@
2626

2727

2828
INDENT = " " # doc is indented by four spaces
29-
DUMMYHOOK = lambda a,x: None
30-
class _Flags(Enum): NONE = 0 # for no value in dict
29+
RAW_ANCHOR = True # whether to use raw html anchors or RST ones
30+
31+
32+
_DUMMYHOOK = lambda a,x: None # for doing nothing in traversing
33+
class _Flags(Enum): NONE = 0 # for no value in dict (when optional)
34+
3135

3236
class Argument:
3337
"""Define possible arguments and their types and properties."""
@@ -168,10 +172,10 @@ def flatten_sub(self, value: dict) -> Dict[str, "Argument"]:
168172
return flat_subs
169173

170174
def traverse(self, argdict: dict,
171-
key_hook: Callable[["Argument", dict], None] = DUMMYHOOK,
172-
value_hook: Callable[["Argument", Any], None] = DUMMYHOOK,
173-
sub_hook: Callable[["Argument", dict], None] = DUMMYHOOK,
174-
variant_hook: Callable[["Variant", dict], None] = DUMMYHOOK):
175+
key_hook: Callable[["Argument", dict], None] = _DUMMYHOOK,
176+
value_hook: Callable[["Argument", Any], None] = _DUMMYHOOK,
177+
sub_hook: Callable[["Argument", dict], None] = _DUMMYHOOK,
178+
variant_hook: Callable[["Variant", dict], None] = _DUMMYHOOK):
175179
# first, do something with the key
176180
# then, take out the vaule and do something with it
177181
key_hook(self, argdict)
@@ -181,10 +185,10 @@ def traverse(self, argdict: dict,
181185
key_hook, value_hook, sub_hook, variant_hook)
182186

183187
def traverse_value(self, value: Any,
184-
key_hook: Callable[["Argument", dict], None] = DUMMYHOOK,
185-
value_hook: Callable[["Argument", Any], None] = DUMMYHOOK,
186-
sub_hook: Callable[["Argument", dict], None] = DUMMYHOOK,
187-
variant_hook: Callable[["Variant", dict], None] = DUMMYHOOK):
188+
key_hook: Callable[["Argument", dict], None] = _DUMMYHOOK,
189+
value_hook: Callable[["Argument", Any], None] = _DUMMYHOOK,
190+
sub_hook: Callable[["Argument", dict], None] = _DUMMYHOOK,
191+
variant_hook: Callable[["Variant", dict], None] = _DUMMYHOOK):
188192
# this is not private, and can be called directly
189193
# in the condition where there is no leading key
190194
value_hook(self, value)
@@ -197,10 +201,10 @@ def traverse_value(self, value: Any,
197201
key_hook, value_hook, sub_hook, variant_hook)
198202

199203
def _traverse_sub(self, value: dict,
200-
key_hook: Callable[["Argument", dict], None] = DUMMYHOOK,
201-
value_hook: Callable[["Argument", Any], None] = DUMMYHOOK,
202-
sub_hook: Callable[["Argument", dict], None] = DUMMYHOOK,
203-
variant_hook: Callable[["Variant", dict], None] = DUMMYHOOK):
204+
key_hook: Callable[["Argument", dict], None] = _DUMMYHOOK,
205+
value_hook: Callable[["Argument", Any], None] = _DUMMYHOOK,
206+
sub_hook: Callable[["Argument", dict], None] = _DUMMYHOOK,
207+
variant_hook: Callable[["Variant", dict], None] = _DUMMYHOOK):
204208
assert isinstance(value, dict)
205209
sub_hook(self, value)
206210
for subvrnt in self.sub_variants.values():
@@ -220,13 +224,13 @@ def check(self, argdict: dict, strict: bool = False):
220224
self.traverse(argdict,
221225
key_hook=Argument._check_exist,
222226
value_hook=Argument._check_value,
223-
sub_hook=Argument._check_strict if strict else DUMMYHOOK)
227+
sub_hook=Argument._check_strict if strict else _DUMMYHOOK)
224228

225229
def check_value(self, argdict: dict, strict: bool = False):
226230
self.traverse_value(argdict,
227231
key_hook=Argument._check_exist,
228232
value_hook=Argument._check_value,
229-
sub_hook=Argument._check_strict if strict else DUMMYHOOK)
233+
sub_hook=Argument._check_strict if strict else _DUMMYHOOK)
230234

231235
def _check_exist(self, argdict: dict):
232236
if self.optional is True:
@@ -544,13 +548,14 @@ def _make_cpath(self, cname: str,
544548
def make_rst_refid(name):
545549
if not isinstance(name, str):
546550
name = '/'.join(name)
547-
return f'.. raw:: html\n\n <a id="{name}"></a>'
551+
return (f'.. _`{name}`: \n' if not RAW_ANCHOR
552+
else f'.. raw:: html\n\n <a id="{name}"></a>')
548553

549554

550555
def make_ref_pair(path, text=None, prefix=None):
551556
if not isinstance(path, str):
552557
path = '/'.join(path)
553-
url = "#" + path
558+
url = f"`{path}`_" if not RAW_ANCHOR else f"#{path}"
554559
ref = ("" if not prefix else f"{prefix}:") + path
555560
inline = f'`{ref}`_' if not text else f'|{ref}|_'
556561
target = f'.. _`{ref}`: {url}'

tests/dpmdargs.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from context import dargs
2-
from dargs import Argument, Variant
2+
from dargs import dargs, Argument, Variant
33

44
ACTIVATION_FN_DICT = {
55
"relu": None,
@@ -30,7 +30,7 @@ def list_to_doc (xx):
3030

3131

3232
def make_link(content, ref_key) :
33-
return f'`{content} <#{ref_key}>`__'
33+
return f'`{content} <{ref_key}_>`_' if not dargs.RAW_ANCHOR else f'`{content} <#{ref_key}>`_'
3434

3535

3636
def descrpt_local_frame_args ():
@@ -553,7 +553,6 @@ def normalize(data):
553553
"numb_test": 10,
554554
"save_freq": 1000,
555555
"save_ckpt": "model.ckpt",
556-
"load_ckpt": "model.ckpt",
557556
"disp_training":true,
558557
"time_training":true,
559558
"tensorboard": false,

tests/test_docgen.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,20 @@ def test_multi_variants(self):
113113
# print("\n\n"+docstr)
114114

115115
def test_dpmd(self):
116+
from dargs import dargs
116117
from dpmdargs import gen_doc
118+
dargs.RAW_ANCHOR = False
117119
docstr = gen_doc(make_anchor=True, make_link=True)
118120
# print("\n\n"+docstr)
119121
# with open("out.rst", "w") as of:
120122
# print(docstr, file=of)
123+
# now testing raw anchor
124+
dargs.RAW_ANCHOR = True
125+
docstr = gen_doc(make_anchor=True, make_link=True)
126+
# print("\n\n"+docstr)
127+
# with open("outr.rst", "w") as of:
128+
# print(docstr, file=of)
129+
121130

122131
if __name__ == "__main__":
123132
unittest.main()

0 commit comments

Comments
 (0)