#!/usr/bin/env python
import sys
per_page=8
class Step_back:
def __init__(self, least, greatest):
self.least = least
self.greatest = greatest
self.filename = 'pages-%d-through-%d' % (least+1, greatest+1)
self.lines = []
def add(self, line):
self.lines.append(line)
def write(self):
#print 'writing %s with %d lines' % (self.filename, len(self.lines))
file = open('%s.html' % self.filename, 'w')
file.write(''.join(self.lines))
file.close()
def process_subdivision(sbr, subleft, subright, overall_length, files):
#print subleft, subright
step_back(subleft, subright, overall_length, files)
subresult = Step_back(subleft, subright)
sbr.add('%s
\n' % (subresult.filename, subresult.filename))
def step_back(left, right, overall_length, files):
num_files=right - left + 1
#print left, right, num_files, per_page
sbr = Step_back(left, right)
if num_files <= per_page:
for fileno in xrange(num_files):
sbr.add('%s
\n' % (files[left + fileno], files[left + fileno]))
elif num_files <= per_page*per_page: # an implicitly > per_page
quotient, remainder = divmod(num_files, per_page)
subleft = left
for subpageno in xrange(quotient):
subright = subleft + per_page - 1
process_subdivision( \
sbr, \
subleft, \
subright, \
overall_length, \
files)
subleft += per_page
if remainder != 0:
# has a remainder, must handle it
process_subdivision( \
sbr, \
subleft, \
right, \
overall_length, \
files)
else:
quotient, remainder = divmod(num_files, per_page)
for subpageno in xrange(per_page):
process_subdivision( \
sbr, \
left + subpageno*quotient, \
left + subpageno*quotient + quotient - 1, \
overall_length, \
files)
if remainder != 0:
# has a remainder, must handle it
process_subdivision( \
sbr, \
left + quotient*per_page, \
right, \
overall_length, \
files)
sbr.write()
return sbr
def main():
files = sys.argv[1:]
print '%s.html' % step_back(0, len(files) - 1, len(files), files).filename
main()