forked from ospalh/anki-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfen_visualizer.py
159 lines (135 loc) · 4.28 KB
/
fen_visualizer.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
# -*- mode: Python ; coding: utf-8 -*-
#
# Copyright © 2013 Roland Sieker <ospalh@gmail.com>
# Parts taken from from anki/latex.py
# Copyright: Damien Elmes <anki@ichi2.net>
#
# The style is from the AnkiDroid code, presumably
# Copyright (c) 2012 Kostas Spyropoulos <inigo.aldana@gmail.com>
#
# License: GNU AGPL, version 3 or later;
# http://www.gnu.org/copyleft/agpl.html
"""
Add-on for Anki 2 to show a chess board.
Add-on for Anki 2 to show a chess board based on data in
[Forsyth–Edwards
Notation](http://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation)
(FEN) in `[fen]`/`[/fen]` tags.
"""
import re
from collections import namedtuple
from anki.cards import Card
from anki.hooks import addHook
__version__ = '1.0.0'
reverse_for_black = True
# reverse_for_black = False
FenData = namedtuple(
'FenData',
['placement', 'active', 'castling', 'enpassant', 'halfmove', 'fullmove'])
piece = dict(zip('KQRBNPkqrbnp',
[unichr(s) for s in range(ord(u'♔'), ord(u'♟') + 1)]))
fen_re = re.compile(r"\[fen\](.+?)\[/fen\]", re.DOTALL | re.IGNORECASE)
fen_template = u"""<figure class="chess_diagram"><table class="chess_board">
{rows}
</table>
<figcaption>
<span class="fen_extra active">{act}</span>
<span class="fen_extra castling">{cas},</span>
<span class="fen_extra enp">ep: {enp},</span>
<span class="fen_extra half">½: {half},</span>
<span class="fen_extra full">M: {full}</span>
</figcaption>
</figure>
"""
def chess_card_css(self):
"""Add the chess style to the card style """
return u"""<style scoped>
.chess_board {
border:1px solid #333;
border-spacing:0;
}
.chess_board td {
background: -webkit-gradient(linear,0 0, 0 100%, from(#fff), to(#eee));
-webkit-box-shadow: inset 0 0 0 1px #fff;
font-size: 250%;
height: 1em;
width: 1em;
vertical-align: middle;
text-align: center;
}
.chess_board tr:nth-child(odd) td:nth-child(even),
.chess_board tr:nth-child(even) td:nth-child(odd) {
background: -webkit-gradient(linear,0 0, 0 100%, from(#ccc), to(#eee));
-webkit-box-shadow: inset 0 0 8px rgba(0,0,0,.4);
}
figure.chess_diagram {
display: inline-table;
}
figure.chess_diagram table{
display: inline-block;
}
figure.chess_diagram figcaption{
display: table-caption;
caption-side: bottom;
}
</style>""" + old_css(self)
def counted_spaces(match):
u"""Replace numbers with spaces"""
return ' ' * int(match.group(0))
def insert_table(fen_match):
u"""
Replace well formed FEN data with a chess board diagram.
This is the worker function that replaces the actual data.
"""
fen_text = u''
# Replace each piece with its symbol
for c in fen_match.group(1):
try:
fen_text += piece[c]
except KeyError:
fen_text += c
try:
fen = FenData(*(fen_text.split()))
except TypeError:
# Not FEN data after all.
return fen_match.group(0)
rows = fen.placement.split('/')
# Oops. When it is black’s move we have replaced the b or B with a bishop
blacks_move = (fen.active == u'♝' or fen.active == u'♗')
do_reverse = reverse_for_black and blacks_move
if blacks_move:
active = u'Black’s move'
else:
active = u'White’s move'
if do_reverse:
rows.reverse()
active += u', black’s view'
active += u'.'
# We don’t realy care about the length. This should work for large
# boards as well.
trows = []
for r in rows:
# Replace numbers with spaces
r = re.sub('[1-9][0-9]?', counted_spaces, r)
if do_reverse:
r = list(r)
r.reverse()
# And replace the row with an html table row
tr = u'<tr>'
for p in r:
tr += u'<td>{0}</td>'.format(p)
trows.append(tr + u'</tr>\n')
return fen_template.format(
rows=''.join(trows), act=active, cas=fen.castling,
enp=fen.enpassant, half=fen.halfmove, full=fen.fullmove)
def insert_fen_table(txt, dummy_type, dummy_fields, dummy_model, dummy_data,
dummy_col):
u"""
Replace well formed FEN data with a chess board diagram.
This is a wrapper that looks for `[fen]`, `[/fen]` tags and
replaces the content.
"""
return fen_re.sub(insert_table, txt)
old_css = Card.css
Card.css = chess_card_css
addHook("mungeQA", insert_fen_table)