Skip to content

Instantly share code, notes, and snippets.

@polymorphm
Last active December 29, 2015 05:29
Show Gist options
  • Save polymorphm/7622387 to your computer and use it in GitHub Desktop.
Save polymorphm/7622387 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- mode: python; coding: utf-8 -*-
assert str is not bytes
import sys, os, os.path
if __name__ == '__main__':
for src_path in sys.argv[1:]:
src_name, src_ext = os.path.splitext(src_path)
if src_ext != '.txt':
continue
trg_dir = src_name
trg_path = os.path.join(trg_dir, 'body.html')
os.mkdir(trg_dir)
with open(src_path, 'r', encoding='utf-8', errors='replace') as src_fd, \
open(trg_path, 'w', encoding='utf-8', newline='\n') as trg_fd:
def write_title(title):
trg_fd.write('<h1 style="font-size: 1.3em">{}</h1>\n'.format(title))
def write_line(line):
trg_fd.write('{}<br />\n'.format(line))
line_iter = (line.strip() for line in iter(src_fd))
title = next(line_iter)
empty_line = next(line_iter)
if title and not empty_line:
write_title(title)
else:
write_line(title)
write_line(empty_line)
for line in line_iter:
write_line(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment