Created
November 7, 2017 17:47
-
-
Save RMuskovets/ae4188cae7ce259acefd1505ffd92fdf to your computer and use it in GitHub Desktop.
An utility that converts document to some .txt pages and can convert them to single HTML page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from urllib.request import urlopen | |
def slicefile(data, start, end): | |
import itertools | |
lines = data.splitlines() | |
return itertools.islice(lines, start, end) | |
def pages(data, lines): | |
count = 0 | |
start, end = 0, lines | |
import os | |
os.system('mkdir PAGES') | |
while end <= len(data.splitlines()): | |
aline = list(slicefile(data, start, end)) | |
with open('PAGES/page-{}.txt'.format(count), 'w') as x: | |
for line in aline: | |
x.write(line + '\n') | |
x.close() | |
start, end = end, end+lines | |
count += 1 | |
return count | |
def in_disk(filename, lines): | |
return pages(open(filename).read(), lines) | |
def a(pages): | |
sum = '' | |
i = 0 | |
while i < pages: | |
sum += '<a href="%s">Page {}</a><br>\n'.format(str(i+1)) % 'page-{}.txt'.format(str(i)) | |
i += 1 | |
return sum | |
def html(sfile, pgs): | |
string = ''' | |
<html> | |
<head><title>File "{}", splitted to pages</title></head> | |
<body> | |
<menu> | |
'''.format(sfile) + a(pgs) + ''' | |
</menu> | |
</body> | |
</html> | |
''' | |
return string | |
def in_web(filename, lines): | |
response = urlopen(filename) | |
content = response.read() | |
pages(content, lines) | |
from argparse import * | |
parser = ArgumentParser(description='Convert file to pages.') | |
parser.add_argument('-w', '--web', help='Find file in the web.', action='store_true') | |
parser.add_argument('-s', '--html', help='Create HTML page with links to all pages', action='store_true') | |
parser.add_argument('-c', '--css', help='Specify CSS stylesheet file') | |
parser.add_argument('file', help='File name or URL') | |
parser.add_argument('lines', help='Number of lines in one page', type=int) | |
args = parser.parse_args() | |
value = 0 | |
if args.web: | |
value =in_web(args.file, args.lines) | |
else: | |
value =in_disk(args.file, args.lines) | |
css = '' | |
if args.css is not None: | |
css = open(config).read().replace('\n', ';').split(';') | |
if args.html: | |
x = open('PAGES/') | |
s = html(args.file, value) | |
if css is not '': | |
s.replace('<html>', '<html><link rel="stylesheet" href="%s">' % args.css) | |
x.write(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment