1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
#!/usr/bin/env python3
import re
import sys
import os
import codecs
canonical_url = 'https://slurm.schedmd.com/'
include_pat = r'(<!--\s*#include\s*virtual\s*=\s*"([^"]+)"\s*-->)'
include_regex = re.compile(include_pat)
canonical_pat = r'(<!--\s*#canonical\s*-->)'
canonical_regex = re.compile(canonical_pat)
page_title_pat = r'(<!--\s*#pagetitle\s*-->)'
page_title_regex = re.compile(page_title_pat)
url_pat = r'(\s+href\s*=\s*")([^"#]+)(#[^"]+)?(")'
url_regex = re.compile(url_pat)
first_header_pat = r'(<[h|H]1>\s*([a-zA-Z0-9_ ()\'/-\\.]+)\s*</[h|H]1>)'
first_header_regex = re.compile(first_header_pat)
version_pat = r'(@SLURM_VERSION@)'
version_regex = re.compile(version_pat)
ids = {}
title = ''
dirname = ''
# Insert tags for options
# Two styles are processed.
# <DT><B>pppppp</B><DD>
# has tag <a id="OPT_pppppp"></a>
# <DT><B>--pppppp</B> or <DT><B>-P</B>, <B>--pppppp</B>
# has tag <a id="OPT_pppppp"></a>
# <H2>hhhh</h2> also has tag has tag <a id="SECTION_hhhh"></a> inserted
def insert_tag(lineIn):
lineOt = ""
if lineIn[0:2] == "<H" and lineIn[3] == ">":
header_num = lineIn[2]
# Don't make links to h1 tags - no need, since already at top of page
if header_num == "1":
return lineIn;
posEnd = lineIn.find("</H%s>" % header_num)
if posEnd == -1:
return lineIn;
contents = lineIn[4:posEnd]
id_name = contents.replace(' ','-')
if id_name in ids:
ids[id_name] += 1
id_name += "_" + str(ids[id_name])
else:
ids[id_name] = 0
id_name = "SECTION_%s" % (id_name)
lineOt = '<h%s>%s<a class="slurm_link" id="%s" href="#%s"></a></h%s>' % (header_num, contents, id_name, id_name, header_num)
return lineOt
if lineIn[0:4] != "<DT>":
return lineIn
if lineIn[4:7] == "<B>":
bold_tag = True
elif lineIn[4:7] == "<I>":
bold_tag = False
else:
return lineIn
contentsBgn = 4
contentsEnd = lineIn.find("<DD>")
contents = lineIn[contentsBgn:contentsEnd]
posBgn = lineIn.find("--")
if posBgn == -1:
# 1st form
posBgn = 7
else:
posBgn += 2
if bold_tag:
posEnd = lineIn.find("</B>",posBgn)
else:
posEnd = lineIn.find("</I>",posBgn)
if posEnd == -1:
# poorly constructed
return lineIn
id_name = lineIn[posBgn:posEnd]
id_name = id_name.replace(' ','-')
if id_name in ids:
ids[id_name] += 1
id_name += "_" + str(ids[id_name])
else:
ids[id_name] = 0
id_name = "OPT_%s" % (id_name)
link_and_contents = '%s<a class="slurm_link" id="%s" href="#%s"></a>' % (contents, id_name, id_name)
lineOt = "<dt>" + link_and_contents + "</dt><dd>"
return lineOt
def llnl_references(line):
manStr = "Refer to mc_support.html"
htmlStr = 'Refer to <a href="mc_support.html">mc_support</a>'
lineFix = line.replace(manStr,htmlStr)
if lineFix != line:
return lineFix
manStr = '<A HREF="https://slurm.schedmd.com/mc_support.html">https://slurm.schedmd.com/mc_support.html</A>'
htmlStr = 'the <a href="mc_support.html">mc_support</a> document'
lineFix = line.replace(manStr,htmlStr)
if lineFix != line:
return lineFix
manStr = '<A HREF="https://slurm.schedmd.com/dist_plane.html.">https://slurm.schedmd.com/dist_plane.html.</A>'
htmlStr = 'the <a href="dist_plane.html">dist_plane</a> document'
lineFix = line.replace(manStr,htmlStr)
if lineFix != line:
return lineFix
manStr = '<<A HREF="https://slurm.schedmd.com/mpi_guide.html">https://slurm.schedmd.com/mpi_guide.html</A>>'
htmlStr = '<a href="mpi_guide.html">mpi_guide</a>'
lineFix = line.replace(manStr,htmlStr)
if lineFix != line:
return lineFix
manStr = '(<A HREF="https://slurm.schedmd.com/power_save.html).">https://slurm.schedmd.com/power_save.html).</A>'
htmlStr = '<a href="power_save.html">power_save</a>'
lineFix = line.replace(manStr,htmlStr)
if lineFix != line:
return lineFix
manStr = '<A HREF="https://slurm.schedmd.com/cons_tres.html">https://slurm.schedmd.com/cons_tres.html</A>'
htmlStr = '<a href="cons_tres.html">cons_tres</a>'
lineFix = line.replace(manStr,htmlStr)
if lineFix != line:
return lineFix
manStr = '<A HREF="https://slurm.schedmd.com/cons_tres_share.html">https://slurm.schedmd.com/cons_tres_share.html</A>'
htmlStr = '<a href="cons_tres_share.html">cons_tres_share</a>'
lineFix = line.replace(manStr,htmlStr)
if lineFix != line:
return lineFix
manStr = '<A HREF="https://slurm.schedmd.com/gang_scheduling.html">https://slurm.schedmd.com/gang_scheduling.html</A>'
htmlStr = '<a href="gang_scheduling.html">gang_scheduling</a>'
lineFix = line.replace(manStr,htmlStr)
if lineFix != line:
return lineFix
manStr = '<A HREF="https://slurm.schedmd.com/preempt.html">https://slurm.schedmd.com/preempt.html</A>'
htmlStr = '<a href="preempt.html">preempt</a>'
lineFix = line.replace(manStr,htmlStr)
if lineFix != line:
return lineFix
manStr = '<A HREF="/cgi-bin/man/man2html?8+logrotate">logrotate</A>'
htmlStr = 'logrotate'
lineFix = line.replace(manStr,htmlStr)
if lineFix != line:
return lineFix
manStr = '<A HREF="/man/man2html?8+logrotate">logrotate</A>'
htmlStr = 'logrotate'
lineFix = line.replace(manStr,htmlStr)
if lineFix != line:
return lineFix
manStr = '<A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A>'
htmlStr = 'path_resolution'
lineFix = line.replace(manStr,htmlStr)
if lineFix != line:
return lineFix
manStr = '<A HREF="/man/man2html?7+path_resolution">path_resolution</A>'
htmlStr = 'path_resolution'
lineFix = line.replace(manStr,htmlStr)
if lineFix != line:
return lineFix
return line
def relative_reference(lineIn):
lineOt = ""
refAnchor="<A HREF=";
lenRefAnchor = len(refAnchor)
for fullRef in ["/cgi-bin/man/man2html", "/man/man2html"]:
lenRef = len(fullRef)
lineOt = ""
cursor = 0
posHREF = lineIn.find(fullRef,cursor)
if posHREF == -1:
continue
if lineIn[posHREF+lenRef] != "?":
pos = lineIn.find("Return to Main Contents",cursor)
if pos != -1:
return ""
return "<i>man2html</i> "
while posHREF != -1:
posRefAnchor = lineIn.find(refAnchor,cursor)
lineOt = lineOt + lineIn[cursor:posRefAnchor+lenRefAnchor]
cursor = posHREF + lenRef + 3
lineOt = lineOt + '"'
posQuote = lineIn.find('"',cursor)
lineOt = lineOt + lineIn[cursor:posQuote] + ".html"
cursor = posQuote
posHREF = lineIn.find(fullRef,cursor)
lineOt = lineOt + lineIn[cursor:]
if lineOt != lineIn:
break;
if lineOt == "":
return lineIn
return lineOt
def include_virtual(matchobj):
global dirname
if dirname:
filename = dirname + '/' + matchobj.group(2)
else:
filename = matchobj.group(2)
if os.access(filename, os.F_OK):
lines = open(filename, 'r').read()
return lines
else:
return matchobj.group(0)
def canonical_rewrite(matchobj):
global newfilename
return '<link rel="canonical" href="' + canonical_url + newfilename + '" />'
def page_title_rewrite(matchobj):
global title
return '<title>Slurm Workload Manager - ' + title + '</title>'
def url_rewrite(matchobj):
global dirname
if dirname:
localpath = dirname + '/' + matchobj.group(2)
else:
localpath = matchobj.group(2)
if matchobj.group(2)[-6:] == '.shtml' and os.access(localpath, os.F_OK):
location = matchobj.group(2)
if matchobj.group(3) is None:
newname = location[:-6] + '.html'
else:
newname = location[:-6] + '.html' + matchobj.group(3)
return matchobj.group(1) + newname + matchobj.group(4)
else:
return matchobj.group(0)
def version_rewrite(matchobj):
global version
return version
files = []
version = sys.argv[1]
for f in sys.argv[4:]:
dirname, basefilename = os.path.split(f)
posLastDot = basefilename.rfind(".")
mhtmlname = basefilename[:posLastDot] + ".mhtml"
cmd = "man2html < " + f + "> " + mhtmlname
os.system(cmd)
print(">>>>>>> " + mhtmlname)
files.append(mhtmlname)
for filename in files:
newfilename = filename[:-6] + '.html'
print('Converting', filename, '->', newfilename)
shtml = codecs.open(filename, 'r', encoding='utf-8')
html = codecs.open(newfilename, 'w', encoding='utf-8')
for line in shtml.readlines():
result = first_header_regex.match(line)
if result:
title = result.group(2)
break
shtml.seek(0)
lines = open(sys.argv[2], 'r').read()
lines = lines.replace(".shtml",".html")
lines = version_regex.sub(version_rewrite, lines)
lines = page_title_regex.sub(page_title_rewrite, lines)
lines = canonical_regex.sub(canonical_rewrite, lines)
html.write(lines)
# html.write(<!--#include virtual="header.txt"-->)
for line in shtml.readlines():
# Remove html header/footer created by man2html
if line == "Content-type: text/html\n":
continue
if line == "Content-type: text/html; charset=UTF-8\n":
continue
if line[:6] == "<HTML>":
continue
if line[:7] == "</HEAD>":
continue
if line[:7] == "</HTML>":
continue
if line[:7] == "</BODY>":
continue
line = include_regex.sub(include_virtual, line)
# Special case some html references
line = llnl_references(line)
#insert tags for some options
line = insert_tag(line)
# Make man2html links relative ones
line = relative_reference(line)
line = url_regex.sub(url_rewrite, line)
html.write(line)
lines = open(sys.argv[3], 'r').read()
lines = lines.replace(".shtml",".html")
lines = version_regex.sub(version_rewrite, lines)
html.write(lines)
# html.write(<!--#include virtual="footer.txt"-->)
html.close()
shtml.close()
os.remove(filename)
|