Merge pull request #51 from dougfelt/emoji_html_fix

Tweak emoji html to display images against darker color.
pull/53/head
dougfelt 2016-04-25 14:53:18 -07:00
commit 88747bb0e4
1 changed files with 49 additions and 24 deletions

View File

@ -49,23 +49,27 @@ def _merge_keys(dicts):
keys.extend(d.keys()) keys.extend(d.keys())
return frozenset(keys) return frozenset(keys)
def _generate_row_cells(key, dir_infos): def _generate_row_cells(key, dir_infos, basepaths):
CELL_PREFIX = '<td>' CELL_PREFIX = '<td>'
def _cell(key, info): indices = range(len(basepaths))
def _cell(key, info, basepath):
if key in info.filemap: if key in info.filemap:
return '<img src="%s">' % path.join( return '<img src="%s">' % path.join(
info.directory, info.filemap[key]) basepath, info.filemap[key])
return '-missing-' return '-missing-'
return [CELL_PREFIX + _cell(key, info) for info in dir_infos] return [CELL_PREFIX + _cell(key, dir_infos[i], basepaths[i])
for i in indices]
def _get_desc(key_tuple, dir_infos): def _get_desc(key_tuple, dir_infos, basepaths):
CELL_PREFIX = '<td class="desc">' CELL_PREFIX = '<td class="desc">'
def _get_filepath(cp): def _get_filepath(cp):
cp_key = tuple([cp]) cp_key = tuple([cp])
for info in dir_infos: for i in range(len(dir_infos)):
info = dir_infos[i]
if cp_key in info.filemap: if cp_key in info.filemap:
return path.join(info.directory, info.filemap[cp_key]) basepath = basepaths[i]
return path.join(basepath, info.filemap[cp_key])
return None return None
def _get_part(cp): def _get_part(cp):
@ -98,19 +102,36 @@ def _get_name(key_tuple):
return CELL_PREFIX + name return CELL_PREFIX + name
def _generate_content(dir_infos): def _generate_content(basedir, dir_infos, limit):
"""Generate an html table for the infos.""" """Generate an html table for the infos. basedir is the parent directory
of the content, filenames will be made relative to this if underneath it,
else absolute. If limit is true and there are multiple dirs, limit the set of
sequences to those in the first dir."""
lines = ['<table>'] lines = ['<table>']
header_row = [''] header_row = ['']
header_row.extend([info.title for info in dir_infos]) header_row.extend([info.title for info in dir_infos])
header_row.extend(['Description', 'Name']) header_row.extend(['Description', 'Name'])
lines.append('<th>'.join(header_row)) lines.append('<th>'.join(header_row))
all_keys = _merge_keys([info.filemap for info in dir_infos]) basepaths = []
abs_basedir = path.abspath(path.expanduser(basedir))
for filedir, _, _ in dir_infos:
abs_filedir = path.abspath(path.expanduser(filedir))
if abs_filedir.startswith(abs_basedir):
dirspec = abs_filedir[len(abs_basedir) + 1:]
else:
dirspec = abs_filedir
basepaths.append(dirspec)
if len(dir_infos) == 1 or limit:
all_keys = frozenset(dir_infos[0].filemap.keys())
else:
all_keys = _merge_keys([info.filemap for info in dir_infos])
for key in sorted(all_keys): for key in sorted(all_keys):
row = [] row = []
row.extend(_generate_row_cells(key, dir_infos)) row.extend(_generate_row_cells(key, dir_infos, basepaths))
row.append(_get_desc(key, dir_infos)) row.append(_get_desc(key, dir_infos, basepaths))
row.append(_get_name(key)) row.append(_get_name(key))
lines.append(''.join(row)) lines.append(''.join(row))
return '\n <tr>'.join(lines) + '\n</table>' return '\n <tr>'.join(lines) + '\n</table>'
@ -188,7 +209,7 @@ def _get_dir_infos(
infos = [] infos = []
for i in range(count): for i in range(count):
image_dir = image_dirs[i] image_dir = image_dirs[i]
title = titles[i] or path.basename(path.normpath(image_dir)) title = titles[i] or path.basename(path.abspath(image_dir))
ext = exts[i] or default_ext ext = exts[i] or default_ext
prefix = prefixes[i] or default_prefix prefix = prefixes[i] or default_prefix
filemap = _get_image_data(image_dir, ext, prefix) filemap = _get_image_data(image_dir, ext, prefix)
@ -230,15 +251,16 @@ TEMPLATE = """<!DOCTYPE html>
""" """
STYLE = """ STYLE = """
tbody { background-color: rgb(210, 210, 210) } tbody { background-color: rgb(110, 110, 110) }
tbody img { width: 64px; height: 64px } th { background-color: rgb(210, 210, 210) }
tbody .desc { font-size: 20pt; font-weight: bold } td img { width: 64px; height: 64px }
tbody .desc img { vertical-align: middle; width: 32px; height: 32px } td.desc { font-size: 20pt; font-weight: bold; background-color: rgb(210, 210, 210) }
tbody .name { background-color: white } td.desc img { vertical-align: middle; width: 32px; height: 32px }
td.name { background-color: white }
""" """
def write_html_page(filename, page_title, dir_infos): def write_html_page(filename, page_title, dir_infos, limit):
content = _generate_content(dir_infos) content = _generate_content(path.dirname(filename), dir_infos, limit)
text = _instantiate_template( text = _instantiate_template(
TEMPLATE, {'title': page_title, 'style': STYLE, 'content': content}) TEMPLATE, {'title': page_title, 'style': STYLE, 'content': content})
with codecs.open(filename, 'w', 'utf-8') as f: with codecs.open(filename, 'w', 'utf-8') as f:
@ -263,6 +285,9 @@ def main():
parser.add_argument( parser.add_argument(
'-t', '--titles', help='title, one per image dir', metavar='title', '-t', '--titles', help='title, one per image dir', metavar='title',
nargs='*'), nargs='*'),
parser.add_argument(
'-l', '--limit', help='limit to only sequences supported by first set',
action='store_true')
parser.add_argument( parser.add_argument(
'-de', '--default_ext', help='default extension', metavar='ext', '-de', '--default_ext', help='default extension', metavar='ext',
default=_default_ext) default=_default_ext)
@ -272,15 +297,15 @@ def main():
args = parser.parse_args() args = parser.parse_args()
file_parts = path.splitext(args.filename) file_parts = path.splitext(args.filename)
if file_parts[1] != 'html': if file_parts[1] != '.html':
args.filename = file_parts[0] + '.html' args.filename = file_parts[0] + '.html'
print 'added .html extension to filename:\n%s' % args.filename print 'added .html extension to filename:\n%s' % args.filename
dir_infos = _get_dir_infos( dir_infos = _get_dir_infos(
args.image_dirs, args.exts, args.prefixes, args.titles, args.default_ext, args.image_dirs, args.exts, args.prefixes, args.titles,
args.default_prefix) args.default_ext, args.default_prefix)
write_html_page(args.filename, args.page_title, dir_infos) write_html_page(args.filename, args.page_title, dir_infos, args.limit)
if __name__ == "__main__": if __name__ == "__main__":