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