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