xref: /petsc/doc/build_man_impls_links.py (revision e8e188d2d8d8bcc000607a9a2a524e07d1c8a16a)
1#!/usr/bin/env python
2""" Adds links in the manual pages to implementations of the function
3    Also adds References section if any {cite} are found in the manual page
4"""
5
6import os
7import re
8
9def processfile(petsc_dir,dir,file,implsClassAll,implsFuncAll):
10  #print('Processing '+os.path.join(dir,file))
11  with open(os.path.join(dir,file),'r') as f:
12    text = f.read()
13    bibneeded = text.find('{cite}') > -1
14  if bibneeded:
15    with open(os.path.join(dir,file),'w') as f:
16      f.write(text[0:text.find('## See Also')])
17      f.write('\n## References\n```{bibliography}\n:filter: docname in docnames\n```\n\n')
18      f.write(text[text.find('## See Also'):])
19  itemName = file[0:-3]
20  func = list(filter(lambda x: x.find(itemName+'_') > -1, implsFuncAll))
21  iclass = list(filter(lambda x: x.find('_p_'+itemName) > -1, implsClassAll))
22  if func or iclass:
23    with open(os.path.join(dir,file),'a') as f:
24      f.write('\n## Implementations\n')
25      if func:
26        for str in func:
27          f.write(re.sub('(.*\.[ch]x*u*).*('+itemName+'.*)(\(.*\))','<A HREF=\"PETSC_DOC_OUT_ROOT_PLACEHOLDER/\\1.html#\\2\">\\2() in \\1</A><BR>',str,count=1)+'\n')
28      if iclass:
29        for str in iclass:
30          f.write(re.sub('(.*\.[ch]x*u*):.*struct.*(_p_'+itemName+').*{','<A HREF=\"PETSC_DOC_OUT_ROOT_PLACEHOLDER/\\1.html#\\2\">\\2 in \\1</A><BR>',str,count=1)+'\n')
31def loadstructfunctions(petsc_dir):
32  '''Creates the list of structs and class functions'''
33  import subprocess
34  implsClassAll = subprocess.check_output(['git', 'grep', '-E', 'struct[[:space:]]+_[pn]_[^[:space:]]+.*\{', '--', '*.c', '*.cpp', '*.cu', '*.c', '*.h', '*.cxx'], cwd = petsc_dir).strip().decode('utf-8')
35  implsClassAll = list(filter(lambda x: not (x.find('/tests/') > -1 or x.find('/tutorials') > -1 or x.find(';') > -1), implsClassAll.split('\n')))
36
37  implsFuncAll = subprocess.check_output(['git', 'grep', '-nE', '^(static )?(PETSC_EXTERN )?(PETSC_INTERN )?(extern )?PetscErrorCode +[^_ ]+_[^_ ]+\(', '--', '*/impls/*.c', '*/impls/*.cpp', '*/impls/*.cu', '*/impls/*.c', '*/impls/*.h', '*/impls/*.cxx'], cwd = petsc_dir).strip().decode('utf-8')
38  implsFuncAll = list(filter(lambda x: not (x.find('_Private') > -1 or x.find('_private') > -1 or x.find(';') > -1), implsFuncAll.split('\n')))
39  return (implsClassAll,implsFuncAll)
40
41def main(petsc_dir):
42    (implsClassAll,implsFuncAll) = loadstructfunctions(petsc_dir)
43    for dirpath, dirnames, filenames in os.walk(os.path.join(petsc_dir,'doc','manualpages'),topdown=True):
44      #print('Processing directory '+dirpath)
45      for file in filenames:
46        if file.endswith('.md'): processfile(petsc_dir,dirpath,file,implsClassAll,implsFuncAll)
47
48if __name__ == "__main__":
49   main(os.path.abspath(os.environ['PETSC_DIR']))
50