Skip to content

Instantly share code, notes, and snippets.

@shizeeg
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shizeeg/7869ad72f7ecd90099e2 to your computer and use it in GitHub Desktop.
Save shizeeg/7869ad72f7ecd90099e2 to your computer and use it in GitHub Desktop.
worst code ever
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
txt2html.py. v0.0.4
Copyright (C) 2013-15 sh!zeeg <shizeeg@ya.ru>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
# set default encoding
if sys.version_info < (3, 0):
reload(sys)
sys.setdefaultencoding('utf-8')
sys.stderr.write(u'This script support only python 3.x!\nPlease download and install latest 3.x version at http://www.python.org/download/\n')
sys.exit(2)
from datetime import date
import argparse
parser = argparse.ArgumentParser()
now = date.today()
outfile = 'team_%02d.%02d.%04d.html' % (now.day, now.month, now.year)
html_tmpl = """
<div class="grid_4 alpha instr"><img src="/images/cms/data/instructors/{FILE%d}" alt="{INSTR%d_NAME}" />
<h5>{INSTR%d_NAME}</h5>
<ul>
{BIO%d}
</ul>
</div>
"""
footer = """<div class="clear"></div>"""
def usage():
sys.stderr.write(u'Usage: python3 txt2html.py <infile.txt> [outfile.html]\n\n\n')
gpl = open(sys.argv[0], 'r')
header = gpl.read().split('\n')[3:18]
sys.stderr.write('\n'.join(header))
sys.stderr.write('\n\n')
gpl.close()
sys.exit(1)
def parse_block(block, template, blocknum):
instr_name = block.split('\n')[1]
img = block.split('\n')[0]
bio = u'\n'.join(block.split(u'\n')[2:]).replace(u'«',u'&laquo;').replace(u'»', '&raquo;').split('\n')
listbio = u''
for l in bio:
listbio += '\n<li>'+l+'</li>'
name = u'{INSTR%u_NAME}' % (blocknum + 1)
photo = u'{FILE%u}' % (blocknum + 1)
bio = u'{BIO%u}' % (blocknum + 1)
if img == "NONEM":
img = u'instr_nonem.jpg'
elif img == "NONEF":
img = u'instr_nonef.jpg'
t = template.replace("%d", str(blocknum + 1))
t = t.replace(name, instr_name)
t = t.replace(photo, img)
t = t.replace(bio, listbio)
return t
parser.add_argument("input_file", help="text file to parse.")
parser.add_argument("output_file", nargs='?', default=outfile,
help="output html file.")
parser.add_argument("template_file", nargs='?',
help="output html file template.")
parser.add_argument("-n", "--sort_from", type=int, default=0,
help="sort data by name starting from 'SORT_FROM'")
parser.add_argument("-s", "--sort", choices=["ASC", "DESC", "OFF"], default="ASC",
help="sort order: ascending, descending, sorting disabled")
parser.add_argument("-r", "--row_size", type=int, default=3,
help="html row length (default=3).")
args = parser.parse_args()
f = open(args.input_file, 'r')
template = html_tmpl
if args.template_file:
t = open(args.template_file, 'r')
template = t.read()
t.close()
data = f.read().split('\n\n')
f.close()
out = open(args.output_file, 'w')
if args.sort != "OFF":
data_sorted = sorted(data[args.sort_from:], reverse=args.sort == "DESC",
key=lambda block: block.split('\n')[1])
dtmp = data[:args.sort_from] + data_sorted
data = dtmp
t = template[:]
n = 0
for d in data:
t = parse_block(d, t, n)
out.write(t)
t = template[:]
n = n + 1
if n == args.row_size:
n = 0
out.write(footer)
out.write(footer)
out.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment