-def reheader(txt, delta):
- for i in range(6, 0, -1):
- txt = txt.replace('<h' + str(i),'<h' + str(i+delta))
- txt = txt.replace('</h' + str(i),'</h' + str(i+delta))
- return txt
-
+def remapheader(txt, maxlevel):
+ maxlvl=1
+ # find the highest hierarchy level in the content
+ while maxlvl < 7 and txt.find('<h' + str(maxlvl)) == -1:
+ maxlvl += 1
+ # if there is a hierarchy, remap it so that the highest level is maxlevel
+ if maxlvl < 7:
+ for i in range(6, maxlvl-1, -1):
+ txt = txt.replace('<h' + str(i), '<h' + str(i + maxlevel - maxlvl))
+ txt = txt.replace('</h' + str(i), '</h' + str(i + maxlevel - maxlvl))