@@ -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 "
0 commit comments