xref: /petsc/doc/build_c2html_file.py (revision 7c485f38c7819d08890bcd8c8d1254ebb44244db)
11b37a2a7SPierre Jolivet#!/usr/bin/env python3
2ec83d434SBarry Smith""" Runs c2html and mapnames on a single source file and cleans up all text"""
3ec83d434SBarry Smith
4ec83d434SBarry Smithimport os
5ec83d434SBarry Smithimport re
6ec83d434SBarry Smithimport subprocess
7ec83d434SBarry Smithimport pathlib
8ec83d434SBarry Smithimport sys
9ec83d434SBarry Smith
10ec83d434SBarry Smithdef main(petsc_dir,loc,git_sha,c2html,mapnames,rel_dir,file):
11ec83d434SBarry Smith  with open(os.path.join(rel_dir,file), "r") as fd:
12ec83d434SBarry Smith    txt = fd.read()
13ec83d434SBarry Smith
14ec83d434SBarry Smith  # TODO change text processing parts to Python
15ec83d434SBarry Smith  cmd = 'sed -E "s/PETSC[A-Z]*_DLLEXPORT//g"  | '+ \
16ec83d434SBarry Smith         c2html + ' -n | \
17ec83d434SBarry Smith         awk \'{ sub(/<pre width="80">/,"<pre width=\"80\">\\n"); print }\' | \
18*34c645fdSBarry Smith         grep -v "#if !defined(__" | grep -E -v "(PetscValid|#define __|#undef __|EXTERN_C )" | ' + \
19ec83d434SBarry Smith         mapnames + ' -map htmlmap.tmp -inhtml'
20ec83d434SBarry Smith  txt = subprocess.check_output(cmd, text=True, input=txt, shell = True)
21ec83d434SBarry Smith
22ec83d434SBarry Smith  # make the links to manual pages relative
23ec83d434SBarry Smith  rel_dot = '../'
24ec83d434SBarry Smith  for c in rel_dir:
25ec83d434SBarry Smith    if c == '/':
26ec83d434SBarry Smith      rel_dot = rel_dot + '../'
27ec83d434SBarry Smith  txt = txt.replace('HTML_ROOT/',rel_dot)
28ec83d434SBarry Smith
29ec83d434SBarry Smith  # make the links to include files relative
30ec83d434SBarry Smith  ntxt = ''
31ec83d434SBarry Smith  for line in txt.split('\n'):
32ec83d434SBarry Smith    if 'include' in line:
33ec83d434SBarry Smith      ins = re.search('#include [ ]*&lt;',line)
34ec83d434SBarry Smith      if ins:
35ec83d434SBarry Smith        includename = line[ins.end():re.search('&gt;[a-zA-Z0-9/<>#*"=. ]*',line).start()]
36ec83d434SBarry Smith        ln = re.search('<a name="line[0-9]*">[ 0-9]*: </a>',line)
37ec83d434SBarry Smith        linenumber = line[ln.start():ln.end()]
38ec83d434SBarry Smith        if os.path.isfile(includename):
39ec83d434SBarry Smith          line = linenumber+'#include <A href="'+includename+'.html">&lt;'+includename+'&gt;</A>'
40ec83d434SBarry Smith        elif os.path.isfile(os.path.join('include',includename)):
41ec83d434SBarry Smith          line = linenumber+'#include <A href="'+os.path.relpath(os.path.join(rel_dot,'include',includename))+'.html">&lt;'+includename+'&gt;</A>'
42ec83d434SBarry Smith        elif os.path.isfile(os.path.join(includename)):
43ec83d434SBarry Smith          line = linenumber+'#include <A href="'+os.path.relpath(os.path.join(rel_dot,includename))+'.html">&lt;'+includename+'&gt;</A>'
44ec83d434SBarry Smith    ntxt = ntxt + line + '\n'
45ec83d434SBarry Smith
46ec83d434SBarry Smith  with open(os.path.join(loc,rel_dir,file+'.html'), "w") as fdw:
47ec83d434SBarry Smith    fdw.write('<center><a href="https://gitlab.com/petsc/petsc/-/blob/'+git_sha+'/'+rel_dir+'/'+file+'">Actual source code: '+file+'</a></center><br>\n')
48ec83d434SBarry Smith    fdw.write(ntxt)
49ec83d434SBarry Smith
50ec83d434SBarry Smithif __name__ == "__main__":
51ec83d434SBarry Smith  main(sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4],sys.argv[5],os.path.dirname(sys.argv[6]),os.path.basename(sys.argv[6]))
52