#!/usr/bin/python2 #replace markup #, ## ,### by \section, \subsection, \subsubsection. #anchor names are preserved and generated from the section name otherwise #The script is not perfect and might miss some specific cases from sys import argv from os import path import string import re anchors={} def generate_anchor(chapter,text): pattern = re.compile('[\W_]+') words=text.split() i=1; res=chapter+pattern.sub('',words[0]) while len(res)<40 and i