-
Notifications
You must be signed in to change notification settings - Fork 0
/
tex.py
670 lines (583 loc) · 24.2 KB
/
tex.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# pylint: disable=import-error
# pylint: disable=protected-access
# pylint: disable=too-many-locals
# pylint: disable=too-many-statements
"""functions for tex generation"""
import io
import os
import os.path
import re
import warnings
import chess
import chess.pgn
import numpy as np
import pandas as pd
import eco
import pgn
import config
warnings.simplefilter(action='ignore', category=FutureWarning)
def prep_round4sort(val: str) -> str:
"""Return a val that can be sorted easy"""
return ('0000000' + val)[-7:]
def prep_date(val: str) -> str:
"""Return a date val w/o ?"""
if '?' in val:
return ''.join(val.replace('?','').split('.'))
else:
return val
def get_num_of_rows(length: int, cols: int) -> int:
"""Return the number of rows needed for a list with given length
becomming an array with given cols"""
if (length % cols) != 0:
rows = length / cols + 1
else:
rows = length / cols
return int(rows)
def gen_list(length: int, cols: int) -> list:
"""Return a list that fits to an array of given cols"""
rows = get_num_of_rows(length, cols)
arr = np.empty((rows, cols), dtype=str)
for ze in range(arr.shape[0]):
for sp in range(arr.shape[1]):
arr[ze, sp] = ''
return list(np.reshape(arr, cols*rows))
def gen_digram_table(white: str,
black: str,
cols : int,
game_data_df: pd.DataFrame,
result: str) -> str:
"""Return the game's diagrams and lan into a table"""
out = ''
out += '\setlength\LTleft{0mm}\n'
if cols == 4:
out += '\\begin{longtable}{C{40mm} C{40mm} C{40mm} C{40mm}}\n' + \
'\\hline\n' + \
white + ' & ' + black + ' & ' + \
white + ' & ' + black + ' \\\\ \n'
out += '\\hline\n' + \
'\\endhead\n\n'
if cols == 2:
out += '\\begin{longtable}{C{53mm} C{53mm} | R{51mm}}\n'
out += '\\hline\n' + \
'$\\square$ \\hspace{2mm} \\XskaktheGamewhite & ' + \
'$\\blacksquare$ \\hspace{2mm} \\XskaktheGameblack' + \
' & Your Comment \\\\ \n'
out += '\\hline\n' + \
'\\endhead\n\n'
if config.use_TeX_while_4_details == True:
# games details by while at TeX file
# but not sq_check will be marked
out += '%init theGame start position\n' + \
'\\xskakset{moveid=\\xskakgetgame{initmoveid}}\n' + \
'\n' + \
'%as long as we have a moveid\n' + \
'\\whiledo{\\xskaktestmoveid{\\xskakget{movenr}}{\\xskakget{player}}}\n' + \
'{%begin block\n' + \
' \\ifthenelse{\\equal{\\xskakget{player}}{w}}\n' + \
' {% w_hite player\n' + \
' \\chessboard[setfen=\\xskakget{nextfen},\n' + \
' pgfstyle=border,\n' + \
' color=YellowGreen,\n' + \
' markfields={\\xskakget{movefrom},\\xskakget{moveto}}]\n' + \
' \\newline\n' + \
' \\xskakget{movenr}.\\,\\xskakget{lan} ... \n' + \
' \\ifthenelse{\\equal{\\xskakget{moveid}}{\\finalmoveid}}\n' + \
' {\\newline \\textbf{\\XskaktheGameresult}}{} &\n' + \
' }%\n' + \
' {% b_lack player\n' + \
' \\chessboard[setfen=\\xskakget{nextfen},\n' + \
' pgfstyle=border,\n' + \
' color=YellowGreen,\n' + \
' markfields={\\xskakget{movefrom},\\xskakget{moveto}}]\n' + \
' \\newline\n' + \
' \\xskakget{movenr}. ... \\,\\xskakget{lan}\n' + \
' \\ifthenelse{\\equal{\\xskakget{moveid}}{\\finalmoveid}}\n' + \
' {\\newline \\textbf{\\XskaktheGameresult}}{} \\\\ \n' + \
' }%\n' + \
' \\xskakset{stepmoveid}\n' + \
'}%end block\n'
else:
# games details writen by python script to
# TeX file
cb_arr = gen_list(len(game_data_df), cols)
for cnt, row_s in game_data_df.iterrows():
data = row_s.to_dict()
cb_arr[cnt] = '% ' + data['lan_str'] + '\n' + \
'\\xskakset{moveid=' + data['moveid'] + '}\n' + \
'\\chessboard[setfen=\\xskakget{nextfen},\n' + \
' pgfstyle=border,\n' + \
' color=YellowGreen,\n' + \
' markfields={' + \
data['sq_from'] + ',' + data['sq_to'] + '}'
# arrows that shows the moves
# are in most cases to much at
# the diagrams
if config.move_arrows == True:
cb_arr[cnt] += ',\n' + \
' pgfstyle=straightmove,\n' + \
' color=YellowGreen,\n' + \
' markmoves={' + \
data['sq_from'] + '-' + data['sq_to'] + '}'
# but a king in check will
# will mark
if data['sq_check'] != '':
cb_arr[cnt] += ',\n' + \
' pgfstyle=circle,\n' + \
' color=BrickRed,\n' + \
' markfield={' + data['sq_check'] + '}]\n'
else:
cb_arr[cnt] += ']\n'
if (cnt + 1) % cols > 0:
if data['moveid'][-1] == 'w':
cb_arr[cnt] += '\\newline\n' + \
data['moveid'][0:-1] + \
'.\\,\\xskakget{lan} ...\n'
else:
cb_arr[cnt] += '\\newline\n' + \
data['moveid'][0:-1] + \
'. ...\\,\\xskakget{lan}\n'
if len(game_data_df) == cnt + 1:
cb_arr[cnt] += '\\newline\n' + \
result + '\n'
cb_arr[cnt] += '& \n'
else:
if data['moveid'][-1] == 'w':
cb_arr[cnt] += '\\newline\n' + \
data['moveid'][0:-1] + \
'.\\,\\xskakget{lan} ...\n'
else:
cb_arr[cnt] += '\\newline\n' + \
data['moveid'][0:-1] + \
'. ...\\,\\xskakget{lan}\n'
if len(game_data_df) == cnt + 1:
cb_arr[cnt] += '\\newline\n' + \
result + '\n'
cb_arr[cnt] += '\\\\ \n\n'
# put arrays to out
rows = int(len(cb_arr)/cols)
cb_arr = np.reshape(cb_arr, (rows, cols))
for ze in range(rows):
for sp in range(cols):
if cb_arr[ze, sp] != '':
out += cb_arr[ze, sp]
else:
if sp < cols -1:
out += '& \n'
else:
out += '\\\\ \n'
out += '\n\\end{longtable}\n'
return out
def list_2_2darr(my_list: list, cols: int) -> np.array:
"""Return from a list an 2d np.array with 3 columns"""
my_temp_list = gen_list(len(my_list), 3)
for _ in range(len(my_list)):
my_temp_list[_] = my_list[_]
return np.array(my_temp_list, dtype=str).reshape( (get_num_of_rows(len(my_temp_list), 3), 3) )
def gen_remaining_mainline(eco_str: str, pgn_str: str) -> str:
"""Return the remaining mainline, i.e. pgn_str - eco_str"""
# generate from each move's list a 2d array of elements
# [['1.' 'e4' 'e5' ]
# ['2.' 'Bc4' 'Bc5']
# ['3.' 'c3' '' ]]
eco_arr = list_2_2darr(eco_str.split(' '), 3)
pgn_arr = list_2_2darr(pgn_str.split(' '), 3)
if eco_arr[len(eco_arr)-1, 3-1] != '':
pgn_arr = pgn_arr[len(eco_arr):,]
else:
pgn_arr[len(eco_arr)-1][0] += '..'
pgn_arr[len(eco_arr)-1][1] = ''
pgn_arr = pgn_arr[len(eco_arr)-1:]
pgn_list = list(pgn_arr.flatten())
new_pgn_str = ' '.join(pgn_list)
return new_pgn_str
def gen_tex_data(pgn_dict: dict, eco_dict: dict, pgn_available: bool, game_data_df: pd.DataFrame) -> str:
"""Return the tex data of a game; to be stored as a tex file"""
out = '\\documentclass[../main.tex]{subfiles}\n' + \
'\n' + \
'\\begin{document}\n'
if config.use_TeX_while_4_details == True:
out += '\\newcommand\\finalmoveid{}\n'
out += '\n' + \
'\\index{Players ! ' + pgn_dict['White'] + ' $\\square$' + '}\n' + \
'\\index{Players ! ' + pgn_dict['Black'] + ' $\\blacksquare$' + '}\n' + \
'\\subsection{' + pgn_dict['White'] + ' vs. ' + pgn_dict['Black'] + ' -- ' + \
pgn_dict['Result']
if pgn_available == True:
out += ' -- ' + pgn_dict['ECO'] + '}\n'
else:
out += '}\n' + \
'\n'
out += '\\begin{multicols*}{2}\n' + \
'\n'
# opening information
# eco details from eco_dict
if pgn_available == True:
out += "% Game's Opening ECO data\n" + \
'\\index{Openings ! ' + eco_dict['eco'] + ' -- ' + eco_dict['title'] + '}\n' + \
'\\subsubsection{' + \
eco_dict['eco'] + ' -- ' + eco_dict['title'] + '}\n' + \
'\\begin{flushleft}\n' + \
'\\newchessgame[id=eco]\n' + \
'\\longmoves\n' + \
'\\mainline{' + eco_dict['pgn'] + '}\n' + \
'\\begin{center}\n' + \
'\\begin{tabular}{C{60mm}}\n' + \
'\\chessboard[setfen={' + eco_dict['fen'] + '},\n' + \
' pgfstyle=border,\n' + \
' color=YellowGreen,\n' + \
' markfields={' + \
eco_dict['sq_from'] + ',' + eco_dict['sq_to'] + '}'
# arrows that shows the moves
# are in most cases to much at
# the diagrams
if config.move_arrows == True:
out += ',\n' + \
' pgfstyle=straightmove,\n' + \
' color=YellowGreen,\n' + \
' markmoves={' + \
eco_dict['sq_from'] + '-' + eco_dict['sq_to'] + '}'
if eco_dict['sq_check'] != '':
out += ',\n' + \
' pgfstyle=circle,\n' + \
' color=BrickRed,\n' + \
' markfield={' + eco_dict['sq_check'] + '}]\n'
else:
out += ']\n'
out += '\\newline\n' + \
'\\xskakset{moveid=\\xskakgetgame{lastmoveid}}\n' + \
'\\printmovercolor{\\xskakgetgame{lastplayer}}\n' + \
'\\end{tabular}\n' + \
'\\end{center}\n'
# PGN TAGS
out += '% PGN tags\n' + \
'\\begin{tabular}{L{60mm} R{10mm}}\n' + \
'\\multicolumn{2}{l}{\\textbf{'
site_event = pgn_dict['Site'] + ', ' + pgn_dict['Event']
if '?' in site_event:
site_event = site_event.replace('?', '').replace(',', '')
out += site_event.strip() + '}}\\\\ \n' + \
'\multicolumn{2}{l}{' + \
pgn_dict['Date']
if pgn_dict['Round'] not in ['?', ' ', '']:
out += ' Round ' + pgn_dict['Round']
out += '}\\\\[3mm]\n'
# White player
out += '\\textbf{$\\square$ \\hspace{2mm} '
if 'WhiteTitle' in pgn_dict.keys():
out += ' ' + pgn_dict['WhiteTitle'] + ' '
out += pgn_dict['White'] + '}'
if 'WhiteElo' in pgn_dict.keys():
if pgn_dict['WhiteElo'] != '':
out += ' (' + pgn_dict['WhiteElo'] + ') '
if len(pgn_dict['Result']) > 1:
out += ' & \\textbf{' + pgn_dict['Result'].split('-')[0] + '}\\\\ \n'
else:
out += ' & \\textbf{' + pgn_dict['Result'] + '}\\\\ \n'
# Black player
out += '\\textbf{$\\blacksquare$ \\hspace{2mm} '
if 'BlackTitle' in pgn_dict.keys():
out += ' ' + pgn_dict['BlackTitle'] + ' '
out += pgn_dict['Black'] + '}'
if 'BlackElo' in pgn_dict.keys():
if pgn_dict['BlackElo'] != '':
out += ' (' + pgn_dict['BlackElo'] + ') '
if len(pgn_dict['Result']) > 1:
out += ' & \\textbf{' + pgn_dict['Result'].split('-')[1] + '}\\\\ \n'
else:
out += ' & \\textbf{' + pgn_dict['Result'] + '}\\\\ \n'
out += '\\end{tabular}\n' + \
'\n'
if pgn_available == True:
# display last half move's diagramm
# get the last half-move of game_data_df
game_data_dict = game_data_df.iloc[-1].to_dict()
out += "% Game's final diagram and result\n" + \
'\\newchessgame[id=overview]\n' + \
'\\longmoves\n' + \
'\\hidemoves{' + eco_dict['pgn'] + '}'
remaining_mainline = gen_remaining_mainline(eco_dict['pgn'], pgn_dict['pgn'])
out += '\\mainline{' + remaining_mainline + '}\n' + \
'\n' + \
'\\begin{center}\n' + \
'\\begin{tabular}{C{60mm}}\n' + \
'\\xskakset{moveid=\\xskakgetgame{lastmoveid}}\n' + \
'\\chessboard[setfen=\\xskakget{nextfen},\n' + \
' pgfstyle=border,\n' + \
' color=YellowGreen,\n' + \
' markfields={' + \
game_data_dict['sq_from'] + ',' + game_data_dict['sq_to'] + '}'
# arrows that shows the moves
# are in most cases to much at
# the diagrams
if config.move_arrows == True:
out += ',\n' + \
' pgfstyle=straightmove,\n' + \
' color=YellowGreen,\n' + \
' markmoves={' + \
game_data_dict['sq_from'] + '-' + game_data_dict['sq_to'] + '}'
if game_data_dict['sq_check'] != '':
out += ',\n' + \
' pgfstyle=circle,\n' + \
' color=BrickRed,\n' + \
' markfield={' + game_data_dict['sq_check'] + '}]\n'
else:
out += ']\n'
if game_data_dict['moveid'][-1] == 'w':
out += '\\newline\n' + \
game_data_dict['moveid'][0:-1] + \
'.\\,\\xskakget{lan} ...\n'
else:
out += '\\newline\n' + \
game_data_dict['moveid'][0:-1] + \
'. ...\\,\\xskakget{lan}\n'
out += '\\newline\n' + \
pgn_dict['Result']+ '\n'
out += '\\end{tabular}\n' + \
'\\end{center}\n' + \
'\\end{flushleft}\n' + \
'\\end{multicols*}\n' + \
'\\newpage\n'
else:
out +='\\end{multicols*}\n' + \
'\\newpage\n'
if config.print_detailed_moves == True:
if pgn_available == True:
if config.use_TeX_while_4_details == True:
out += '% with the definition of the above \\newchessgame[id=overview]\n' + \
'% we now have the \\finalmoveid as the \\xskakgetgame{lastmoveid}\n' + \
'% which will be used below for putting the result\n' + \
'% below the last diagramm\n' + \
'\\renewcommand{\\finalmoveid}{\\xskakgetgame{lastmoveid}}\n\n'
out += "% Game's diagrams\n" + \
'\\newchessgame[id={theGame}\n' + \
' ,result={' + pgn_dict['Result'] + '}\n' + \
' ,white={' + pgn_dict['White'] + '}\n' + \
' ,black={' + pgn_dict['Black'] + '}]\n\n' + \
'\\hidemoves{' + pgn_dict['pgn'] + '}\n' + \
'\n'
out += gen_digram_table(pgn_dict['White'],
pgn_dict['Black'],
2, # define num of cols 2 or 4
game_data_df,
pgn_dict['Result'])
out += '\\end{document}\n'
return out
def get_incremented_filename(filename: str) -> str:
"""Return the given filename if exists with an increment"""
name, ext = os.path.splitext(filename)
seq = 0
# continue from existing sequence number if any
rex = re.search(r"^(.*)-(\d+)$", name)
if rex:
name = rex[1]
seq = int(rex[2])
while os.path.exists(filename):
seq += 1
filename = f"{name}-{seq}{ext}"
return filename
def store_tex_document(tex_doc: str, file_name: str) -> dict:
"""Return a dict{'done' : True,
'file_name' : <file_name>} after
Document is stored at 'file_name'"""
#file_name = get_incremented_filename(file_name)
#print(file_name)
tex_file = open(file_name, "w")
tex_file.write(tex_doc)
tex_file.close()
# check that file name ist stored
return({'done': os.path.exists(file_name),
'file_name': file_name})
def mk_subdirs(pgn_fname: str, tex_path: str) -> dict:
"""Return subdirs at tex_path for a pgn-fname"""
# pgn_fname <path>/<pgn>.pgn
# -->
# TEX/<pgn>/
# TEX/<pgn>/images/
# TEX/<pgn>/sections/
# not done here, but used later
# TEX/<pgn>/<pgn>.tex <-- this is the master tex
# which includes all
# tex files from the section dir
pgn = pgn_fname.split("/")[1].split(".")[0]
tex_work_dir = tex_path + pgn + '/'
if not os.path.exists(tex_work_dir):
os.makedirs(tex_work_dir, exist_ok=True)
os.makedirs(tex_work_dir + 'images/', exist_ok=True)
os.makedirs(tex_work_dir + 'sections/', exist_ok=True)
return({'done': os.path.exists(tex_work_dir),
'work_dir' : tex_work_dir,
'images_dir' : tex_work_dir + 'images/',
'sections_dir' : tex_work_dir + 'sections/'})
def init_preamble(pgn_fn : str) -> str:
preamble = ''
preamble += '% exarticle, if needed to allow 9pt font size\n' + \
'\\documentclass[11pt]{article}\n' + \
'\\usepackage{import}\n' + \
'\n' + \
'\\usepackage{geometry}\n' + \
'\\geometry{\n' + \
' a4paper,\n' + \
' top=20mm,\n' + \
' bottom=20mm,\n' + \
' left=20mm,\n' + \
' right=20mm\n' + \
'}\n' + \
'\n' + \
'\\newcommand*\\cleartoleftpage{%\n' + \
' \\clearpage\n' + \
' \\ifodd\\value{page}\\hbox{}\\newpage\\fi\n' + \
'}\n' + \
'\n' + \
'% fonts\n' + \
'\\usepackage{mathptmx}\n' + \
'\\usepackage{amssymb}\n' + \
'\n' + \
'% hyperlinks at toc and PDF outline\n' + \
'\\usepackage{hyperref}\n' + \
'\\hypersetup{\n' + \
' colorlinks=true,\n' + \
' linkcolor=blue,\n' + \
' filecolor=magenta,\n' + \
' urlcolor=cyan}\n' + \
'\\urlstyle{same}\n' + \
'\n' + \
'% do not like the counters before the titles\n' + \
'\\setcounter{secnumdepth}{0}\n' + \
'\n' + \
'% two columns on frist page\n' + \
'\\usepackage{multicol}\n' + \
'\\setlength{\\columnsep}{32pt}\n' + \
'\n' + \
'% for table that can span more then one page\n' + \
'\\usepackage{longtable}\n' + \
'\\setcounter{LTchunksize}{1000}\n' + \
'% table cell width used with centering\n' + \
'\\usepackage{array}\n' + \
'\\newcolumntype{L}[1]{>{\\raggedright\\let\\newline\\\\ \\arraybackslash\\hspace{0pt}}m{#1}}\n' + \
'\\newcolumntype{C}[1]{>{\\centering\\let\\newline\\\\ \\arraybackslash\\hspace{0pt}}p{#1}}\n' + \
'\\newcolumntype{R}[1]{>{\\raggedleft\\let\\newline\\\\ \\arraybackslash\\hspace{0pt}}m{#1}}\n' + \
'\n' + \
'% colors\n' + \
'\\usepackage[dvipsnames]{xcolor}\n' + \
'\n' + \
'% chessboards\n' + \
'\\usepackage{xskak}\n' + \
'\\usepackage{chessboard}\n'
# change from small board to tinyboard
# if you need 4 cols of chessboard, i.e. 2 moves
preamble += '\\setchessboard{smallboard, showmover=false}\n' + \
'\\styleC % for tabular pgn notation\n' + \
'\n' + \
'% if you want to add any graphics manually\n' + \
'\\usepackage{graphicx} % use graphics in png format\n' + \
'\\graphicspath{ {images/} } % images dir is TEX/<pgn>/images/\n' + \
'\n' + \
'% last page number\n' + \
'\\usepackage[lastpage,user]{zref}\n' + \
'\n' + \
'% header and footer\n' + \
'\\usepackage{titleps}\n' + \
'\\newpagestyle{mypage}{%\n' + \
' \\headrule\n' + \
' \\sethead{}{}{\\subsectiontitle}\n' + \
' \\footrule\n' + \
' \\setfoot{\\sectiontitle}{}{\\thepage\\ of \\zpageref{LastPage}}\n' + \
'}\n' + \
'\\settitlemarks{section,subsection,subsubsection}\n' + \
'\\pagestyle{mypage}\n' + \
'\n' + \
'% index\n' + \
'\\usepackage{makeidx}\n' + \
'\\makeindex\n' + \
'\\renewcommand{\indexname}{Index}\n' + \
'\\usepackage[totoc]{idxlayout}\n' + \
'\n' + \
'% check if lastmove was w_hite or b_lack\n' + \
'\\usepackage{ifthen}\n' + \
'\\newcommand{\\printmovercolor}[1]\n' + \
'{\n' + \
' \\ifthenelse{\\equal{#1}{w}}\n' + \
' { % True case - w_hite\n' + \
' \\xskakgetgame{lastmovenr}.\\,\\xskakget{lan} ...\n' + \
' }\n' + \
' { % False case - b_lack\n' + \
' \\xskakgetgame{lastmovenr}. ... \\,\\xskakget{lan}\n' + \
' }\n' + \
'}\n' + \
'\n' + \
'\\usepackage{subfiles} % Best loaded last in the preamble\n' + \
'\n' + \
'% TODO: fill \\href{ ... } with the link where you got the pgn from\n' + \
'% e.g. \\href{https://theweekinchess.com/assets/files/pgn/tatamast22.pgn}\n' + \
'%\\title{\\href{}{' + pgn_fn + '}}\n' + \
'\\title{' + pgn_fn + '}\n' + \
'\n' + \
'% TODO: fill the \\date{ ... }\n' + \
'% e.g. \\date{18.01.2022}\n' + \
'%\\date{}\n' + \
'\n' + \
'% TODO: fill \\href{ ... } with the home link of the site\n' + \
'% e.g. \\href{https://theweekinchess.com}\n' + \
'% and the site name you want to emphazie \\emph{ ... }\n' + \
'% e.g. \\emph{The Week in Chess}\n' + \
'%\\author{downloaded from \\href{}{\\emph{}}}' + \
'\n' + \
'\n' + \
'\\begin{document}\n' + \
'\n' + \
'\\maketitle\n' + \
'\n' + \
'% TODO: fill \\includegraphics[width=15cm]{ ... }\n' + \
'% with png image name without extention .png\n' + \
'% the png image shall be stored at TEX/<png>/images/\n' + \
'% e.g. \\includegraphics[width=15cm]{participants}\n' + \
'%\\begin{center}\n' + \
'%\\includegraphics[width=15cm]{}\n' + \
'%\\end{center}\n' + \
'\\newpage\n' + \
'\n' + \
'\\tableofcontents\n' + \
'\\newpage\n' + \
'\n' + \
'% put index page here\n' + \
'\\printindex\n' + \
'\n' + \
'\\cleartoleftpage\n'
return preamble
def generate_master_tex(pgn_fname : str, tex_path : str, section_subfile_list : list) -> str:
"""Return the name of the tex master file and generates it for that pgn"""
pgn = pgn_fname.split("/")[1].split(".")[0]
tex_master_fn = tex_path + pgn + '/main' + '.tex'
tex_doc = init_preamble(pgn + '.pgn')
last_section_header = ''
for _, item_dict in enumerate(section_subfile_list):
if last_section_header != str(item_dict['section']):
last_section_header = str(item_dict['section'])
tex_doc += '\\section{' + item_dict['section'] + '}\n'
name = item_dict['subfile'].split('/')[-1]
subfile_name = 'sections/' + name.split('.')[0]
if config.print_detailed_moves == True:
tex_doc += '\\subfile{' + subfile_name + '}\n' + \
'\n' + \
'\\cleartoleftpage\n' + \
'\n'
else:
tex_doc += '\\subfile{' + subfile_name + '}\n' + \
'\n' + \
'\\clearpage\n' + \
'\n'
tex_doc += '\\end{document}\n'
return store_tex_document(tex_doc, tex_master_fn)
def main():
"""some test for the pgn.py"""
##################################################
# this is for testing only
# you need to create at your working directory
# a directory PGN/, structured as the repo's PGN dir
# or
# change the code at
# pgn.main pgn_dir to whatever you need
# the line here :
##################################################
if __name__ == '__main__':
main()