-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbridge.py
executable file
·231 lines (177 loc) · 6.61 KB
/
bridge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#! /usr/bin/env python3
# Pandoc plugin for typesetting bridge-related documents
import pandocfilters as pf
import re
def is_bidding(blocks):
for block in blocks:
if len(block) == 0:
continue
data = pf.stringify(block[0])
if re.match('.*![CDHSN].*--.*', data):
return True
return False
prev_key = '' # worst hack ever
def bidding_divs(key, value, fmt, meta):
global prev_key
if key == 'BulletList' and prev_key != pf.stringify(value[0]):
prev_key = pf.stringify(value[0])
if is_bidding(value):
attr = ('', ['bids'], [])
return pf.Div(attr, [pf.BulletList(value)])
return None
def suit_symbols(key, value, fmt, meta):
if key == 'Str':
abbrevs = ['!C', '!D', '!H', '!S', '!N']
orig_value = value
if fmt != 'latex':
suit_html = ['<font color=green>♣</font>',
'<font color=orange>♦</font>',
'<font color=red>♥</font>',
'<font color=blue>♠</font>',
'N']
suit_other = ['C', 'D', 'H', 'S', 'N']
suit_symbols = suit_html if fmt == 'html' else suit_other
for (abbrev, symbol) in zip(abbrevs, suit_symbols):
value = value.replace(abbrev, symbol)
else:
# LaTeX needs special handling (for mbox)
value = value.replace('!N', 'N')
suit = [r'\\clubs{\1}{\2}',
r'\\diamonds{\1}{\2}',
r'\\hearts{\1}{\2}',
r'\\spades{\1}{\2}']
for (abbrev, symbol) in zip(abbrevs, suit):
value = re.sub('([a-zA-Z0-9]*)%s([a-zA-Z0-9]*)' % abbrev,
symbol, value)
if (fmt == 'html' or fmt == 'latex') and value != orig_value:
return pf.RawInline(fmt, value)
return pf.Str(value)
else:
return None
# Expand hands in inline code-blocks
def inline_hands(key, value, fmt, meta):
if key == 'Code':
hand = value[1]
if hand.count(',') == 3 and len(hand) == 16:
hold = hand.split(',')
suit = ['!S', '!H', '!D', '!C']
res = ' '.join(s + (h if len(h) > 0 else '--')
for s, h in zip(suit, hold))
return pf.Str(res)
return None
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 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 reshape(n, data, fill=None):
res = []
line = []
for x in data:
line.append(x)
if len(line) == n:
res.append(line)
line = []
if len(line) > 0:
while(len(line) < n):
line.append(fill)
res.append(line)
return res
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
# Interpret -- as an em-dash in html, unless it is between two words
# (without blanks), in which case it is interpreted as an en-dash
def html_dashes(key, value, fmt, meta):
def fix_dash(s):
s = re.sub('([^ ])--([^ ])', '\\1–\\2', s)
s = s.replace('--', '—')
return s
if key == 'Str' and fmt == 'html':
return pf.RawInline(fmt, fix_dash(value))
elif key == 'RawInline' and fmt == 'html':
return pf.RawInline(value[0], fix_dash(value[1]))
else:
return None
if __name__ == '__main__':
pf.toJSONFilters([bidding_divs,
inline_hands,
block_hands,
block_bids,
suit_symbols,
html_dashes])
# pandoc src\defence.md --filter bridge.py -H bridge-header.tex -o defence.pdf
# cat src\onent.md | sed "s/--/\—/g" | pandoc --filter bridge.py -o onent.htm -H base.html