def block_hands(key, value, fmt, meta): if key == 'CodeBlock': lines = value[1].split('\n') hands = {} for line in lines: m = re.match('([NSEW]): (.*)', line) if m: hands[m.group(1)] = m.group(2) if len(hands) == 0: return None empty_cell = [pf.Plain([])] diagram = [[empty_cell] * 3 for _ in range(3)] pos = {'N': (0, 1), 'S': (2, 1), 'E': (1, 2), 'W': (1, 0)} for (player, hand) in hands.items(): i, j = pos[player] diagram[i][j] = [hand_diagram(hand)] AlignLeft = {'t': 'AlignLeft'} table = pf.Table([], [AlignLeft, AlignLeft, AlignLeft], [0, 0, 0], [], diagram) attr = ('', ['hands'], []) return pf.Div(attr, [table]) return None
def formatCode(key, value, format, meta): style = 'default' if key == 'CodeBlock': if value[0][1]: return pf.Plain([ pf.RawInline( "html", highlightCode(value[1], style=style, language=value[0][1][0])) ]) else: return pf.Plain([ pf.RawInline( "html", highlightCode(value[1], style=style, language='text')) ])
def html_figure(attr, filename, fcaption, alt): beginText = [RawInline('html', '\n<div class="figure">\n')] # FIXME: Should I move some or all classes from the image into the `<div>`? image = [pf.Image(attr, fcaption, (filename, alt))] captionStart = [RawInline('html', '<p class="caption">')] endText = [RawInline('html', '</p>\n</div>\n')] htmlFigure = beginText + image + captionStart + fcaption + endText return pf.Plain(htmlFigure)
def hand_diagram(hand): void = '--' hold = [x if len(x) > 0 else void for x in hand.split(',')] suit = ['!S', '!H', '!D', '!C'] suits = [pf.Str(s + h) for s, h in zip(suit, hold)] res = sum(([x, pf.LineBreak()] for x in suits), [])[:-1] return pf.Plain(res)
def save_uri(key, value, format, metadata): if format == 'latex' and isOutputFigure(key, value): attr, blocks = value image = blocks[0]['c'][ 0] # should be only inline in only block in the div caption, (uri, title) = image['c'] global image_count image_count += 1 fname = 'build/figures/{i:05d}.png'.format(i=image_count) match = png_uri.search(uri) if match: data = match.groups()[0] else: return with open(fname, 'wb') as f: f.write(base64.b64decode(data)) return pf.Div(attr, [pf.Plain([pf.Image(caption, (fname, title))])])
def make_cell(r): return [pf.Plain([pf.Str(r)])]
def block_bids(key, value, fmt, meta): def disp_bid(b): if b == 'p': return 'pass' elif b[0] in '1234567' and b[1] in 'CDHS': return b[0] + '!' + b[1:] else: return b if key == 'CodeBlock': m = re.match('(NS|EW|all)(/[NSEW])?: (.*)', value[1]) if m is None: return None players = m.group(1) def_mod = {'NS': 1, 'EW': 0, 'all': -1} raw_bids = m.group(3) auction_done = raw_bids[-1] != '-' if not auction_done: raw_bids = raw_bids[:-1] bids = raw_bids.split('-') first_player = 1 if players == 'EW' else 0 if m.group(2): first_player = 'NESW'.find(m.group(2)[1]) player = first_player res = [] for bid in bids: defence = bid[0] == '(' and bid[-1] == ')' if player % 2 == def_mod[players] and not defence: res.append('p') player = (player + 1) % 4 if defence: res.append(bid[1:-1]) else: res.append(bid) player = (player + 1) % 4 if auction_done: while len(res) < 3 or res[-3:].count('p') != 3: res.append('p') res = [disp_bid(b) for b in res] empty_cell = [pf.Plain([])] def make_cell(r): return [pf.Plain([pf.Str(r)])] flat_cells = [empty_cell] * first_player + [make_cell(r) for r in res] cells = reshape(4, flat_cells, empty_cell) names = [make_cell(x) for x in ['North', 'East', 'South', 'West']] AlignCenter = {'t': 'AlignCenter'} table = pf.Table([], [AlignCenter] * 4, [0] * 4, names, cells) attr = ('', ['bidding'], []) return pf.Div(attr, [table]) return None