planet/filters/minhead.py
Sam Ruby 5d9df85826 minhead.py is only picking the first node from the old header and transferring
it to the new header, due to the nodelist being modified. Here's an easy fix.
Props to Geoff Lankow.
http://lists.planetplanet.org/archives/devel/2010-August/002157.html
2010-09-21 10:29:24 -04:00

37 lines
1.0 KiB
Python

#
# Ensure that all headings are below a permissible maximum (like h3).
# If not, all heading levels will be changed to conform.
# Note: this may create "illegal" heading levels, like h7 and beyond.
#
import sys
from xml.dom import minidom, XHTML_NAMESPACE
# determine permissible minimimum heading
if '--min' in sys.argv:
minhead = int(sys.argv[sys.argv.index('--min')+1])
else:
minhead=3
# parse input stream
doc = minidom.parse(sys.stdin)
# search for headings below the permissable minimum
first=minhead
for i in range(1,minhead):
if doc.getElementsByTagName('h%d' % i):
first=i
break
# if found, bump all headings so that the top is the permissible minimum
if first < minhead:
for i in range(6,0,-1):
for oldhead in doc.getElementsByTagName('h%d' % i):
newhead = doc.createElementNS(XHTML_NAMESPACE, 'h%d' % (i+minhead-first))
for child in oldhead.childNodes[:]:
newhead.appendChild(child)
oldhead.parentNode.replaceChild(newhead, oldhead)
# return (possibly modified) document
print doc.toxml('utf-8')