Linking HTML files in a folder with one another using python -
i have folder full of html files follows:
aaa.html bbb.html ccc.html .... ...... ......... zzz.html
all these htmls created using python script, , hence follow same template.
now, want link these html files, have placeholders in html follows:
<nav> <ul class="pager"> <li class="previous"><a href="#">previous</a></li> <li class="next"><a href="#">next</a></li> </ul> </nav>
i want fill these placeholders filenames in folder. example, bbb.html
have
<nav> <ul class="pager"> <li class="previous"><a href="aaa.html">previous</a></li> <li class="next"><a href="ccc.html">next</a></li> </ul> </nav>
and ccc.html
file contain:
<nav> <ul class="pager"> <li class="previous"><a href="bbb.html">previous</a></li> <li class="next"><a href="ddd.html">next</a></li> </ul> </nav>
and on rest of files. can task done using python? don't know how start with. hints, suggestions helpful.
you can replace elements template looping on file list, list wrapping. here's example aaa.html using aaa,bbb,ccc:
#f = ['aaa.html','bbb.html','ccc.html'] f = sorted(['aaa.html','bbb.html','ccc.html']) # explicit sorting t = """<nav> <ul class="pager"> <li class="previous"><a href="#">previous</a></li> <li class="next"><a href="#">next</a></li> </ul> </nav>""" # sample aaa.html file in xrange(len(f)-1): #print f[i] t = t.replace('<li class="previous"><a href="#">previous','<li class="previous"><a href="'+f[(i % len(f)) -1]+'">previous') t = t.replace('<li class="next"><a href="#">next','<li class="next"><a href="'+f[(i % len(f)) +1]+'">next') print t
to list-wrapping use concept (after zzz comes aaa)
gives output aaa.html:
<nav> <ul class="pager"> <li class="previous"><a href="ccc.html">previous</a></li> <li class="next"><a href="bbb.html">next</a></li> </ul> </nav>
to complete code, you'd have loop on *.html files (see glob.glob)
Comments
Post a Comment