def hyperlink_games(games, field): hyperlinks = [crawl_utils.morgue_link(g) for g in games] text = [ '<a href="%s">%s</a>' % (link, g[field]) for link, g in zip(hyperlinks, games) ] return ", ".join(text)
def hyperlink_games(games, field): hyperlinks = [ crawl_utils.morgue_link(g) for g in games ] text = [ '<a href="%s">%s</a>' % (link, g[field]) for link, g in zip(hyperlinks, games) ] return ", ".join(text)
def games_table(games, first=None, excluding=None, columns=None, including=None, cls='bordered', count=True, win=True): columns = columns or (win and STOCK_WIN_COLUMNS or STOCK_COLUMNS) # Copy columns. columns = list(columns) if excluding: columns = [c for c in columns if c[0] not in excluding] if including: for pos, col in including: columns.insert(pos, col) if first and not isinstance(first, tuple): first = (first, 1) if first and columns[0][0] != first[0]: firstc = [ c for c in columns if c[0] == first[0] ] columns = [ c for c in columns if c[0] != first[0] ] columns.insert( first[1], firstc[0] ) if cls: cls = ''' class="%s"''' % cls out = '''<table%s>\n<tr>''' % (cls or '') if count: out += "<th></th>" for col in columns: out += "<th>%s</th>" % col[1] out += "</tr>\n" odd = True ngame = 0 ncols = len(columns) + (count and 1 or 0) if not games: out += '''<tr><td colspan='%s'>No games</td></tr>''' % ncols for game in games: ngame += 1 ocls = odd and "odd" or "even" if game.get('killertype') == 'winning': ocls += " win" out += '''<tr class="%s">''' % ocls odd = not odd if count: out += '''<td class="numeric">%s</td>''' % ngame for c in columns: val = fixup_column(c[0], game.get(c[0]) or '', game) tcls = isinstance(val, str) and "celltext" or "numeric" out += '''<td class="%s">''' % tcls need_link = len(c) >= 3 and c[2] if need_link: try: out += r'<a href="%s">' % crawl_utils.morgue_link(game) except: sys.stderr.write("Error processing game: " + loaddb.xlog_str(game)) raise elif is_player_header(c[1]): val = linked_text(val, player_link) out += str(val) if need_link: out += '</a>' out += '</td>' out += "</tr>\n" out += "</table>\n" return out
def games_table(games, first=None, excluding=None, columns=None, including=None, count=True, win=True, place_column=-1, skip=False, caption=None): """Create a HTML table of games. :param List[List[str]] games: Games to list :param bool first: ? :param Optional[List[str]] excluding: If set, a list of column names to exclude from columns :param Optional[List[Union[Tuple[str,str],Tuple[str,str,bool]]]] columns: If set, a list of colum descriptions. The format is (sql column name, html column name[, link col=False]). Defaults to STOCK_WIN_COLUMNS if win is True, otherwise STOCK_COLUMNS. :param Optional[List[Tuple[int,Union[Tuple[str,str],Tuple[str,str,bool]]]]] including: If set, a list of (pos, name) tuples of columns to include. pos is the position to insert at. :param bool count: Add a count column at the start. :param bool win: Select default columns if `columns` is None. See `columns` param for more info. :param Optional[str] caption: Table description (for screen readers). """ if columns is None: columns = STOCK_WIN_COLUMNS if win else STOCK_COLUMNS # Copy columns. columns = list(columns) if excluding: columns = [c for c in columns if c[0] not in excluding] if including: for pos, col in including: columns.insert(pos, col) if first and not isinstance(first, tuple): first = (first, 1) if first and columns[0][0] != first[0]: firstc = [c for c in columns if c[0] == first[0]] columns = [c for c in columns if c[0] != first[0]] columns.insert(first[1], firstc[0]) table_classes = set(( "table", "table-sm", "table-hover", "table-striped", "table-dark", "w-auto", )) out = '''<div class="table-responsive">\n<table class="%s">\n''' % " ".join( table_classes) if caption is not None: out += '''<caption class="sr-only">%s</caption>\n''' % caption out += '''<thead>\n<tr>''' if count: out += '''<th scope="col">#</th>''' for col in columns: out += '''<th scope="col">%s</th>''' % col[1] out += "</tr>\n</thead>\n" if not games: ncols = len(columns) + (1 if count else 0) out += '''<tr><td colspan='%s'>No games</td></tr>''' % ncols # XXX: this is something to do with ranking? nplace = 0 rplace = 0 last_value = None for game in games: row_class = "table-secondary" if game.get( 'killertype') == 'winning' else "" out += '''<tr class="%s">''' % row_class rplace += 1 if place_column == -1: nplace += 1 elif last_value != game.get(columns[place_column][0]): nplace += 1 if skip: nplace = rplace last_value = game.get(columns[place_column][0]) if count: out += '''<th class="text-right" scope="row">%s</th>''' % nplace for i, c in enumerate(columns): val = fixup_column(c[0], game.get(c[0]) or '', game) numeric_col = _is_numeric_table_value(val, c[0]) pseudo_numeric_col = val == '-' if numeric_col: val = '{:,}'.format(isinstance(val, str) and int(val) or val) td_class = "text-right text-monospace" if ( numeric_col or pseudo_numeric_col) else "" if i == place_column: out += '''<th class="%s" scope="row">''' % td_class else: out += '''<td class="%s">''' % td_class # XXX: this should change need_link = len(c) >= 3 and c[2] if need_link: try: out += r'<a href="%s">' % crawl_utils.morgue_link(game) except: sys.stderr.write("Error processing game: " + loaddb.xlog_str(game)) raise elif is_player_header(c[1]): val = linked_text(val, player_link) out += str(val) if need_link: out += '</a>' if i == place_column: out += '</th>' else: out += '</td>' out += "</tr>\n" out += "</table>\n</div>\n" return out