xref: /petsc/doc/build_man_impls_links.py (revision 7ecb3c68adbfb458b33c1b4909d4a0cc9f46473e)
1fae261c0SBarry Smith#!/usr/bin/env python
2fae261c0SBarry Smith""" Adds links in the manual pages to implementations of the function"""
3fae261c0SBarry Smith
4fae261c0SBarry Smithimport os
5fae261c0SBarry Smithimport re
6fae261c0SBarry Smith
7fae261c0SBarry Smithdef processfile(petsc_dir,dir,file,implsClassAll,implsFuncAll):
8fae261c0SBarry Smith  #print('Processing '+os.path.join(dir,file))
9fae261c0SBarry Smith  itemName = file[0:-3]
10fae261c0SBarry Smith  func = list(filter(lambda x: x.find(itemName+'_') > -1, implsFuncAll))
11fae261c0SBarry Smith  iclass = list(filter(lambda x: x.find('_p_'+itemName) > -1, implsClassAll))
12fae261c0SBarry Smith  if func or iclass:
13fae261c0SBarry Smith    with open(os.path.join(dir,file),'a') as f:
14fae261c0SBarry Smith      f.write('\n## Implementations\n')
15fae261c0SBarry Smith      if func:
16fae261c0SBarry Smith        for str in func:
17fae261c0SBarry Smith          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')
18fae261c0SBarry Smith      if iclass:
19fae261c0SBarry Smith        for str in iclass:
20fae261c0SBarry Smith          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')
21fae261c0SBarry Smith
22fae261c0SBarry Smithdef loadstructfunctions(petsc_dir):
23fae261c0SBarry Smith  '''Creates the list of structs and class functions'''
24fae261c0SBarry Smith  import subprocess
25*7ecb3c68SPierre Jolivet  implsClassAll = subprocess.check_output(['git', 'grep', '-E', 'struct[[:space:]]+_[pn]_[^[:space:]]+.*\{', '--', '*.c', '*.cpp', '*.cu', '*.c', '*.h', '*.cxx'], cwd = petsc_dir).strip().decode('utf-8')
26fae261c0SBarry Smith  implsClassAll = list(filter(lambda x: not (x.find('/tests/') > -1 or x.find('/tutorials') > -1 or x.find(';') > -1), implsClassAll.split('\n')))
27fae261c0SBarry Smith
28*7ecb3c68SPierre Jolivet  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')
29fae261c0SBarry Smith  implsFuncAll = list(filter(lambda x: not (x.find('_Private') > -1 or x.find('_private') > -1 or x.find(';') > -1), implsFuncAll.split('\n')))
30fae261c0SBarry Smith  return (implsClassAll,implsFuncAll)
31fae261c0SBarry Smith
32fae261c0SBarry Smithdef main(petsc_dir):
33fae261c0SBarry Smith    (implsClassAll,implsFuncAll) = loadstructfunctions(petsc_dir)
34fae261c0SBarry Smith    for dirpath, dirnames, filenames in os.walk(os.path.join(petsc_dir,'doc','manualpages'),topdown=True):
35fae261c0SBarry Smith      #print('Processing directory '+dirpath)
36fae261c0SBarry Smith      for file in filenames:
37fae261c0SBarry Smith        if file.endswith('.md'): processfile(petsc_dir,dirpath,file,implsClassAll,implsFuncAll)
38fae261c0SBarry Smith
39fae261c0SBarry Smithif __name__ == "__main__":
40fae261c0SBarry Smith   main(os.path.abspath(os.environ['PETSC_DIR']))
41