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