Skip to content

Commit

Permalink
Add all_emoji and emoji_sort options to generate_emoji_html.
Browse files Browse the repository at this point in the history
By default, the list of emoji sequences is based on the union of
the sequences encoded in the image file names for all the directories
(or the first directory if --limit was set).  The --all_emoji option
uses the emoji sequences from nototools/unicode_data instead.

By default, the list of emoji sequences is in unicode codepoint order.
The --emoji_sort option uses the emoji sequence sort order from
nototools/unicode_data instead.

Along with this, the ordered list of sequences becomes an argument to
write_html_page, which it should have been all along.
  • Loading branch information
dougfelt committed Feb 22, 2017
1 parent 1f2cde6 commit d24aa6d
Showing 1 changed file with 47 additions and 22 deletions.
69 changes: 47 additions & 22 deletions generate_emoji_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,17 @@ def _get_name(key_tuple, annotated_tuples):
return CELL_PREFIX + seq_name


def _collect_aux_info(dir_infos, all_keys):
def _collect_aux_info(dir_infos, keys):
"""Returns a map from dir_info_index to a set of keys of additional images
that we will take from the directory at that index."""

target_key_to_info_index = {}
for key in all_keys:
for key in keys:
if len(key) == 1:
continue
for cp in key:
target_key = tuple([cp])
if target_key in all_keys or target_key in target_key_to_info_index:
if target_key in keys or target_key in target_key_to_info_index:
continue
for i, info in enumerate(dir_infos):
if target_key in info.filemap:
Expand All @@ -164,18 +164,18 @@ def _collect_aux_info(dir_infos, all_keys):


def _generate_content(
basedir, font, dir_infos, limit, annotate, standalone, colors):
"""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. If font is not none, generate columns
for the text rendered in the font before other columns. if annotate is
not none, highlight sequences that appear in this set."""

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])
basedir, font, dir_infos, keys, annotate, standalone, colors):
"""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 font is not none, generate columns for the text rendered in the
font before other columns. Dir_infos is the list of DirInfos in column
order. Keys is the list of canonical emoji sequences in row order. If
annotate is not none, highlight sequences that appear in this set. If
standalone is true, the image data and font (if used) will be copied under
the basedir to make a completely stand-alone page. Colors is the list of
background colors, the last DirInfo column will be repeated against each of
these backgrounds.
"""

basedir = path.abspath(path.expanduser(basedir))
if not path.isdir(basedir):
Expand All @@ -188,7 +188,7 @@ def _generate_content(
# aren't part of main set. e.g. if we have female basketball player
# color-3 we want female, basketball player, and color-3 images available
# even if they aren't part of the target set.
aux_info = _collect_aux_info(dir_infos, all_keys)
aux_info = _collect_aux_info(dir_infos, keys)

# create image subdirectories in target dir, copy image files to them,
# and adjust paths
Expand All @@ -198,8 +198,7 @@ def _generate_content(
if not path.isdir(dstdir):
os.mkdir(dstdir)

aux_keys = aux_info[i]
copy_keys = all_keys if not aux_keys else (all_keys | aux_keys)
copy_keys = set(keys) | aux_info[i]
srcdir = info.directory
filemap = info.filemap
for key in copy_keys:
Expand Down Expand Up @@ -230,7 +229,7 @@ def _generate_content(
header_row.extend(['Sequence', 'Name'])
lines.append('<th>'.join(header_row))

for key in sorted(all_keys):
for key in keys:
row = _generate_row_cells(key, font, dir_infos, basepaths, colors)
row.append(_get_desc(key, dir_infos, basepaths))
row.append(_get_name(key, annotate))
Expand Down Expand Up @@ -324,6 +323,25 @@ def _get_dir_infos(
return infos


def _get_keys(dir_infos, limit, all_emoji, emoji_sort):
"""Return a list of the key tuples to display. If all_emoji is
True, returns all emoji sequences, else the sequences available
in dir_infos (limited to the first dir_info if limit is True).
The result is in emoji order if emoji_sort is true, else in
unicode codepoint order."""
if all_emoji:
keys = unicode_data.get_emoji_sequences()
elif len(dir_infos) == 1 or limit:
keys = frozenset(dir_infos[0].filemap.keys())
else:
keys = _merge_keys([info.filemap for info in dir_infos])
if emoji_sort:
sorted_keys = unicode_data.get_sorted_emoji_sequences(keys)
else:
sorted_keys = sorted(keys)
return sorted_keys


def _parse_annotation_file(afile):
annotations = set()
line_re = re.compile(r'([0-9a-f ]+)')
Expand Down Expand Up @@ -386,7 +404,7 @@ def _instantiate_template(template, arg_dict):
"""

def write_html_page(
filename, page_title, font, dir_infos, limit, annotate, standalone,
filename, page_title, font, dir_infos, keys, annotate, standalone,
colors):

out_dir = path.dirname(filename)
Expand All @@ -412,7 +430,7 @@ def write_html_page(
font = path.normpath(path.join(common_prefix, rel_font))

content = _generate_content(
path.dirname(filename), font, dir_infos, limit, annotate, standalone,
path.dirname(filename), font, dir_infos, keys, annotate, standalone,
colors)
N_STYLE = STYLE
if font:
Expand Down Expand Up @@ -477,6 +495,10 @@ def main():
parser.add_argument(
'-c', '--colors', help='list of colors for background', nargs='*',
metavar='hex')
parser.add_argument(
'--all_emoji', help='use all emoji sequences', action='store_true')
parser.add_argument(
'--emoji_sort', help='use emoji sort order', action='store_true')

args = parser.parse_args()
file_parts = path.splitext(args.outfile)
Expand All @@ -499,8 +521,11 @@ def main():
args.image_dirs, args.exts, args.prefixes, args.titles,
args.default_ext, args.default_prefix)

keys = _get_keys(
dir_infos, args.limit, args.all_emoji, args.emoji_sort)

write_html_page(
args.outfile, args.page_title, args.font, dir_infos, args.limit,
args.outfile, args.page_title, args.font, dir_infos, keys,
annotations, args.standalone, args.colors)


Expand Down

0 comments on commit d24aa6d

Please sign in to comment.