'Headings end
Side-effect: If there is no title in the metadata, then it is taken
from the first heading of any level.'

def e_text_h(self, tag, attrs):

    self.writedata()
    level = int(attrs[(TEXTNS, 'outline-level')])
    if (level > 6):
        level = 6
    if (level < 1):
        level = 1
    lev = self.headinglevels[1:(level + 1)]
    outline = '.'.join(map(str, lev))
    heading = ''.join(self.data)
    if (self.title == ''):
        self.title = heading
    tail = ''.join(self.data)
    anchor = self.get_anchor(('%s.%s' % (outline, tail)))
    anchor2 = self.get_anchor(tail)
    self.opentag('a', {'id': anchor})
    self.closetag('a', False)
    self.opentag('a', {'id': anchor2})
    self.closetag('a', False)
    self.closetag(('h%s' % level))
    self.purgedata()
