Skip to content

Commit 2bc4987

Browse files
authored
Provide support for inlining R examples in .Rd files via YAML (#835)
* ✨ allow inlining examples from YAML * 👕 linting fixes * 🐛 fix typo in examples tag * insert CRLF in wrapped tags * 🐛 handle YAML missing examples * 🙈 🔁 DOS LF with Unix-style LF * make check more Pythonic * use .get method for "dontrun" tag * rpkg_data = None if YAML missing
1 parent b500991 commit 2bc4987

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

dash/development/_r_components_generation.py

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,15 @@ def generate_js_metadata(pkg_data, project_shortname):
324324
return function_string
325325

326326

327-
def write_help_file(name, props, description, prefix):
327+
# This method wraps code within arbitrary LaTeX-like tags, which are used
328+
# by R's internal help parser for constructing man pages
329+
def wrap(tag, code):
330+
if tag == "":
331+
return code
332+
return '\\{}{{\n{}}}'.format(tag, code)
333+
334+
335+
def write_help_file(name, props, description, prefix, rpkg_data):
328336
"""
329337
Write R documentation file (.Rd) given component name and properties
330338
@@ -334,13 +342,15 @@ def write_help_file(name, props, description, prefix):
334342
props = the properties of the component
335343
description = the component's description, inserted into help file header
336344
prefix = the DashR library prefix (optional, can be a blank string)
345+
rpkg_data = package metadata (optional)
337346
338347
Returns
339348
-------
340349
writes an R help file to the man directory for the generated R package
341350
342351
"""
343-
file_name = format_fn_name(prefix, name) + ".Rd"
352+
funcname = format_fn_name(prefix, name)
353+
file_name = funcname + ".Rd"
344354

345355
default_argtext = ""
346356
item_text = ""
@@ -374,25 +384,40 @@ def write_help_file(name, props, description, prefix):
374384
file_path = os.path.join('man', file_name)
375385
with open(file_path, 'w') as f:
376386
f.write(help_string.format(
377-
funcname=format_fn_name(prefix, name),
387+
funcname=funcname,
378388
name=name,
379389
default_argtext=textwrap.fill(default_argtext,
380390
width=80,
381391
break_long_words=False),
382392
item_text=item_text,
383393
description=description.replace('\n', ' ')
384394
))
385-
386-
387-
def write_class_file(name, props, description, project_shortname, prefix=None):
395+
if rpkg_data is not None and 'r_examples' in rpkg_data:
396+
ex = rpkg_data.get('r_examples')
397+
the_ex = ([e for e in ex if e.get("name") == funcname] or [None])[0]
398+
result = ""
399+
if the_ex and "code" in the_ex.keys():
400+
result += wrap("examples",
401+
wrap("dontrun" if the_ex.get("dontrun") else "",
402+
the_ex["code"]))
403+
with open(file_path, 'a+') as fa:
404+
fa.write(result + '\n')
405+
406+
407+
def write_class_file(name,
408+
props,
409+
description,
410+
project_shortname,
411+
prefix=None,
412+
rpkg_data=None):
388413
props = reorder_props(props=props)
389414

390415
# generate the R help pages for each of the Dash components that we
391416
# are transpiling -- this is done to avoid using Roxygen2 syntax,
392417
# we may eventually be able to generate similar documentation using
393418
# doxygen and an R plugin, but for now we'll just do it on our own
394419
# from within Python
395-
write_help_file(name, props, description, prefix)
420+
write_help_file(name, props, description, prefix, rpkg_data)
396421

397422
import_string =\
398423
"# AUTO GENERATED FILE - DO NOT EDIT\n\n"

dash/development/component_generator.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,17 @@ def generate_components(
9898
os.makedirs('man')
9999
if not os.path.exists('R'):
100100
os.makedirs('R')
101+
if os.path.isfile("dash-info.yaml"):
102+
with open("dash-info.yaml") as yamldata:
103+
rpkg_data = yaml.safe_load(yamldata)
104+
else:
105+
rpkg_data = None
106+
with open('package.json', 'r') as f:
107+
pkg_data = safe_json_loads(f.read())
101108
generator_methods.append(
102-
functools.partial(write_class_file, prefix=rprefix))
109+
functools.partial(write_class_file,
110+
prefix=rprefix,
111+
rpkg_data=rpkg_data))
103112

104113
components = generate_classes_files(
105114
project_shortname,
@@ -113,19 +122,6 @@ def generate_components(
113122
generate_imports(project_shortname, components)
114123

115124
if rprefix is not None:
116-
if os.path.isfile("dash-info.yaml"):
117-
with open("dash-info.yaml") as yamldata:
118-
rpkg_data = yaml.safe_load(yamldata)
119-
else:
120-
rpkg_data = None
121-
print(
122-
"Warning: dash-info.yaml missing; package metadata not loaded",
123-
file=sys.stderr
124-
)
125-
126-
with open('package.json', 'r') as f:
127-
pkg_data = safe_json_loads(f.read())
128-
129125
generate_exports(
130126
project_shortname,
131127
components,

0 commit comments

Comments
 (0)