-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclean_srd.py
More file actions
executable file
·1338 lines (1114 loc) · 39.7 KB
/
Copy pathclean_srd.py
File metadata and controls
executable file
·1338 lines (1114 loc) · 39.7 KB
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
import argparse
from pathlib import Path
import re
import sys
from lib.magic_items import magic_items
from lib.spells import get_spell_list, get_next_unemphasised_spell
from lib.tables import realign_table
spell_list = get_spell_list()
spell_matcher = get_next_unemphasised_spell(spell_list)
ABILITIES = ['Str', 'Dex', 'Con', 'Int', 'Wis', 'Cha']
ABILITY_MODIFIER_CHARS = '+-−0123456789'
PROPERTIES = [
'Gear',
'Immunities',
'Languages',
'Resistances',
'Senses',
'Skills',
'Vulnerabilities',
]
NUMERIC_PROPERTIES = [
# first four are in the specific order needed by clean_reorder_stats
'AC',
'Initiative',
'HP',
'Speed',
'CR',
]
STATBLOCK_PROPERTIES = ABILITIES + PROPERTIES + NUMERIC_PROPERTIES
BACKGROUND_PROPERTIES = [
'Ability Scores',
'Equipment',
'Feat',
'Skill Proficiencies',
'Tool Proficiency',
]
ITEM_PROPERTIES = [
'Ability',
'Cost',
'Craft',
'Trigger',
'Utilize',
'Variants',
'Weight',
]
SPECIES_PROPERTIES = ['Creature Type', 'Size']
SPELL_PROPERTIES = ['Casting Time', 'Components', 'Duration', 'Range']
def get_table_headers(lines, index):
if not lines[index].startswith('|'):
return None
start = index
while start > 0 and lines[start - 1].startswith('|'):
start -= 1
separator = start + 1
if (
separator >= len(lines)
or '-' not in lines[separator]
or index <= separator
):
return None
return [cell.strip() for cell in lines[start].split('|')[1:-1]]
def clean_whitespace(lines, index):
# why <br>s for the love of...
spaces = lines[index].replace('<br>', ' ')
spaces = re.sub(r'[\t\r\f\v\u00a0\u2000-\u200b\u2028\u2029\u3000]', ' ', spaces)
if spaces != lines[index]:
lines[index] = spaces
return 0
return None
def clean_wrap_blank_lines(lines, index):
# remove consecutive blank lines
if (
index < len(lines) - 1
and lines[index] == ''
and lines[index + 1] == ''
):
del lines[index + 1]
return -1
return None
def clean_unicode_chars(lines, index):
# usable characters over clever typographic characters
replacements = {
'\u0336': '—', # "combining long stroke overlay" to em-dash
'\u2012': '-', # "figure dash" to hyphen
'\u2013': '-', # "en-dash" to hyphen
'\u2212': '-', # "minus sign" to hyphen
'•': '-', # bullet to hyphen
'½': ' 1/2', # vulgar fractions
'⅓': ' 1/3',
'¼': ' 1/4',
'¾': ' 3/4',
'⅔': ' 2/3',
'⅕': ' 1/5',
'⅖': ' 2/5',
'⅗': ' 3/5',
'⅘': ' 4/5',
'⅙': ' 1/6',
'⅚': ' 5/6',
'⅐': ' 1/7',
'⅛': ' 1/8',
'⅜': ' 3/8',
'⅝': ' 5/8',
'⅞': ' 7/8',
}
for old, new in replacements.items():
lines[index] = lines[index].replace(old, new)
return 0
def clean_space_out_emdashes(lines, index):
# "situations—particularly combat—the" -> "situations — particularly combat — the"
# (visually distinct in a text editor, no hair space exists in Markdown)
line = lines[index]
if '—' in line:
lines[index] = re.sub(r'(\w)—(\w)', r'\1 — \2', line)
return 0
def clean_escape_square_brackets(lines, index):
# "Blinded [Condition]" -> "Blinded \[Condition\]"
lines[index] = re.sub(r'(?<!\\)\[', r'\\[', lines[index])
lines[index] = re.sub(r'(?<!\\)\]', r'\\]', lines[index])
return 0
def clean_table_alignment(lines, index):
return realign_table(lines, index)
def clean_midsentence_pagebreak(lines, index):
# rejoin paragraphs split by pagebreaks
if lines[index] and lines[index][0].islower():
previous = index - 1
while previous >= 0 and lines[previous] == '':
previous -= 1
if previous >= 0 and lines[previous]:
if (
lines[previous][-1].islower()
or lines[previous][-1] == ','
):
lines[previous] = (
lines[previous] + ' ' + lines[index]
)
del lines[previous+1:index+1]
return -(index - previous)
return None
def clean_remove_mistaken_headers(lines, index):
# "#### **Duration:** Instantaneous" -> "**Duration:** Instantaneous"
removed = re.sub(
r'^#+\s+((?:\*\*[^*]+\*\*\s+\S.*)|(?:\*[^*]+\*))$',
r'\1',
lines[index]
)
if removed != lines[index]:
lines[index] = removed
return 0
return None
def clean_remove_header_bold(lines, index):
# "### **High Elf**" -> "### High Elf"
header_pattern = r'^(#+)\s*\*\*([^*]+)\*\*\s*$'
if re.match(header_pattern, lines[index]):
lines[index] = re.sub(header_pattern, r'\1 \2', lines[index])
return 0
return None
def clean_leading_emphasis(lines, index):
# "*Some words.* More words..." but in the PDF rendered bold italic
if index > 1 and lines[index - 1].strip() != "":
return None
bold_italics = re.sub(r'^\*([^*]+\.)\*', r'_**\1**_', lines[index])
if bold_italics != lines[index]:
lines[index] = bold_italics
return 0
return None
def clean_statblock_attack_emphasis(lines, index):
# "*[Words.] [Words] Attack:*" -> "_**[Words.]** [Words] Attack:_"
attack = re.sub(r'^\*([^*]+\.)\s+([^*]+Attack:)\*', r'_**\1** \2_', lines[index])
if attack != lines[index]:
lines[index] = attack
return 0
return None
def clean_unwrap_consecutive_bold(lines, index):
# **Hit Dice**, **Hit Points**, ... -- sometimes on one line
if not (lines[index].startswith('**') and ' **' in lines[index]):
return None
parts = re.split(r'\s+(?=\*\*[^*]+\*\*)', lines[index])
lines[index] = parts[0].strip()
for i, part in enumerate(parts[1:], 1):
lines.insert(index + i, part.strip())
return len(parts) - 1
def _wrapup_matching_lines(lines, index, pattern, indent=''):
if re.match(pattern, lines[index]):
current = index
while current < len(lines) - 2:
if (
lines[current + 1] == ''
and re.match(pattern, lines[current + 2])
):
del lines[current + 1]
current += 1
else:
break
if current >= index:
for line in range(index, current+1):
lines[line] = f"{indent}- {lines[line]}"
return index - current
return None
def clean_wrapup_feature_lists(lines, index):
# "_**words.**_ ... \n _**words.**_ ..." -- listify
return _wrapup_matching_lines(lines, index, r'^_\*\*[^*]+\.\*\*')
def clean_wrapup_attribute_lists(lines, index):
# **Hit Dice** ... \n **Hit Points** ... -- listify
return _wrapup_matching_lines(lines, index, r'^\*\*[^*]+\*\*')
def clean_statblock_spells_to_list(lines, index):
# "Cantrips", "1st level (4 slots)", "3/day" -- listify
return _wrapup_matching_lines(
lines,
index,
r'^(\*\*)?(Cantrips|At [Ww]ill|[0-9]+[a-z]* level|[0-9]+/[Dd]ay[^:]*):?',
''
)
def clean_single_action_to_list(lines, index):
# "_**" alone after a header, still a list (of one)
if (
lines[index].startswith('_**')
and lines[index - 1] == ''
and lines[index - 2].startswith('#')
and (
lines[index - 2].endswith('Actions')
or lines[index - 2].endswith('Traits')
or lines[index - 2].endswith('Reactions')
)
):
lines[index] = '- ' + lines[index]
return 0
return None
def clean_statblock_spellcasting_marker(lines, index):
# "_**Spellcasting.**_" before a list of spells
if re.match(r'^_\*\*(?:Innate\s+)?Spellcasting\.\*\*_', lines[index]):
if (
index + 2 < len(lines)
and lines[index + 1] == ''
and lines[index + 2].startswith(' - ')
):
lines[index] = '- ' + lines[index]
return 0
return None
def clean_canonicalise_proper_nouns(lines, index):
# *speak with dead.* -> _Speak with Dead_.
original = lines[index]
potential_matches = re.findall(
r'(?<![\*_])([\*_])(?!\*)([a-zA-Z][^*_]*?)\1(?!\*)',
lines[index],
)
for marker, text in potential_matches:
# grouped by first letter to speed up matching
first_letter = text[0].lower()
for names in [spell_list, magic_items]:
if first_letter in names:
for name in names[first_letter]:
# punctuation moves outside of the emphasis
pattern = (
re.escape(marker)
+ re.escape(name.lower())
+ r'([.,:;!?]*)'
+ re.escape(marker)
)
replacement = f'_{name}_\\1'
lines[index] = re.sub(
pattern,
replacement,
lines[index],
flags=re.IGNORECASE
)
if lines[index] != original:
return 0
return None
def clean_add_traits_header(lines, index):
# add Traits after CR for visual separation
if (
lines[index].startswith('**Challenge**')
and index + 2 < len(lines)
and lines[index + 1] == ''
and not lines[index + 2].startswith('#')
):
lines[index + 1:index + 1] = ['', '#### Traits']
return 2
return None
def clean_italic_emphasis_markers(lines, index):
# *word* -> _word_
lines[index] = re.sub(r'(?<!\\)(?<!\*)\*([^*]+)\*(?!\*)', r'_\1_', lines[index])
return 0
def clean_collapse_adjacent_items(lines, index):
# pull up adjacent "**Skills**" and "- List item"
if (
index + 2 < len(lines)
and lines[index + 1] == ''
and (
(
lines[index].startswith('**')
and lines[index + 2].startswith('**')
and not (
lines[index].endswith('.')
and not lines[index].endswith('ft.')
)
)
or (
lines[index].startswith('- ')
and lines[index + 2].startswith('- ')
)
)
):
del lines[index + 1]
return -1
return None
def clean_italic_to_bold_italic(lines, index):
# "_Multiattack._ The dragon makes..." -> "_**Multiattack.**_ The dragon makes..."
if match := re.match(r'^(_[A-Z][^_]*\._)(\s+\S.*)', lines[index]):
emphasised = match.group(1)[1:-1] # remove underscores
lines[index] = f'_**{emphasised}**_{match.group(2)}'
return 0
return None
def clean_pluralise_component(lines, index):
lines[index] = lines[index].replace('**Component:**', '**Components:**')
return 0
def clean_bold_periods(lines, index):
# "*Bite***.** *Melee Attack..." -> "*Bite.* *Melee Attack..."
if '***.** ' in lines[index]:
lines[index] = lines[index].replace('***.** ', '.* ')
return 0
return None
def clean_detabulate_mixed_stats(lines, index):
# strip the mess of confused stats and ability scores table into a list,
# (the ability scores will be converted back to a table in another filter)
if not lines[index].startswith('|'):
return None
separator_index = index + 1
if (
separator_index >= len(lines)
or not lines[separator_index].startswith('|-')
):
return None
header_cells = [cell.strip().upper() for cell in lines[index].split('|')[1:-1]]
is_clean_header = all(
cell in [ability.upper() for ability in ABILITIES]
for cell in header_cells
)
if len(header_cells) == 6 and is_clean_header:
# 5.1 format "STR DEX CON INT WIS CHA" abilities table
data_row = lines[separator_index + 1]
cells = [cell.strip() for cell in data_row.split('|')[1:-1]]
parenthesis_pattern = r'(?<![+-])(\d+)\s*\(([+-]?\d+)\)'
values = [re.sub(parenthesis_pattern, r'\1 \2 \2', cell) for cell in cells]
table_text = ' '.join(
f'{ABILITIES[i]} {values[i]}' for i in range(len(values))
)
current_index = separator_index + 2
else:
# 5.2.1 style big jumbled mess
table_rows = [lines[index]]
current_index = separator_index + 1
while current_index < len(lines) and lines[current_index].startswith('|'):
table_rows.append(lines[current_index])
current_index += 1
abilities_pattern = r'\b(' + '|'.join(ABILITIES) + r')(\d)'
table_text = ' '.join(table_rows)
table_text = table_text.replace('|', ' ')
table_text = re.sub(abilities_pattern, r'\1 \2', table_text)
table_text = table_text.replace('MOD SAVE', '')
table_text = re.sub(r'\s+', ' ', table_text).strip()
words = table_text.split()
properties_present = 0
for prop in NUMERIC_PROPERTIES + PROPERTIES:
if prop in words:
prop_index = words.index(prop)
if prop in NUMERIC_PROPERTIES:
if (
prop_index + 1 < len(words)
and words[prop_index + 1][0] in ABILITY_MODIFIER_CHARS
):
properties_present += 1
else:
properties_present += 1
abilities_present = len([
ability for ability in ABILITIES
if re.search(rf'\b{ability}\s', table_text)
])
if not (abilities_present == 6 or properties_present >= 2):
return None
output_lines = []
word_index = 0
while word_index < len(words):
word = words[word_index]
matched = False
for keyword in STATBLOCK_PROPERTIES:
if word == keyword:
matched = True
parts = [word]
word_index += 1
if keyword in NUMERIC_PROPERTIES + PROPERTIES:
# "Skills Arcana +8, Athletics +14", "Speed 30 ft., Fly 60 ft."
# properties consume everything until next keyword
while (
word_index < len(words)
and words[word_index] not in (STATBLOCK_PROPERTIES)
):
parts.append(words[word_index])
word_index += 1
else:
# "Str 29 +9 +14" -- abilities consume numeric values
while (
word_index < len(words)
and words[word_index][0] in ABILITY_MODIFIER_CHARS
):
parts.append(words[word_index])
word_index += 1
content = ' '.join(parts[1:])
output_lines.append(f'**{keyword}** {content}')
break
if not matched:
word_index += 1
lines[index:current_index] = output_lines
return len(output_lines) - (current_index - index)
def clean_delistify_actions(lines, index):
if lines[index].startswith('- _**'):
lines[index] = lines[index][2:]
if index + 1 < len(lines) and lines[index + 1].startswith('- _**'):
lines.insert(index + 1, '')
return 1
return 0
return None
def clean_reitalicise_attack_actions(lines, index):
pattern = (
r'^(_\*\*[^*]+\.\*\*) '
r'((?:Melee|Ranged|Melee or Ranged) (?:Weapon )?Attack:_)'
)
match = re.match(pattern, lines[index])
if match:
lines[index] = (
f'{match.group(1)}_ _{match.group(2)}'
+ lines[index][match.end():]
)
return 0
return None
def clean_deindent(lines, index):
if lines[index].startswith(' '):
lines[index] = lines[index][4:]
return 0
return None
def clean_respace_lists(lines, index):
if index + 1 == len(lines):
return None
this_line = re.match(r'^\s*- ', lines[index])
next_line = re.match(r'^\s*- ', lines[index + 1])
if this_line and next_line:
if this_line.group() != next_line.group():
lines.insert(index + 1, '')
return 1
return None
def clean_recalculate_saving_throws(lines, index):
ability_pattern = (
r'^\*\*(' + '|'.join(ABILITIES) + r')\*\* (\d+) ([+-]?\d+) ([+-]?\d+)'
)
match = re.match(ability_pattern, lines[index])
if not match or match.group(1) != 'Str':
return None
saving_throws_index = index + 7
if (
saving_throws_index >= len(lines)
or not lines[saving_throws_index].startswith('- **Saving Throws**')
):
return None
saving_throws_text = lines[saving_throws_index].replace(
'- **Saving Throws**', ''
).strip()
saving_throws = {}
for pair in saving_throws_text.split(','):
parts = pair.strip().split()
if len(parts) >= 2:
for ability in ABILITIES:
if ability.lower().startswith(parts[0].lower()):
saving_throws[ability] = parts[1]
break
for line_index in range(index, saving_throws_index):
match = re.match(ability_pattern, lines[line_index])
if match and match.group(1) in saving_throws:
ability = match.group(1)
lines[line_index] = (
f'**{ability}** {match.group(2)} {match.group(3)} '
f'{saving_throws[ability]}'
)
del lines[saving_throws_index]
return -1
def clean_retabulate_ability_scores(lines, index):
# **Str** 14 +2 +2 **Dex** ... -> | Str | 14 |...
abilities_pattern = (
r'^\*\*(' + '|'.join(ABILITIES) + r')\*\*\s+\d+\s+[+-]?\d+\s+[+-]?\d+'
)
if not re.match(abilities_pattern, lines[index]):
return None
scores = []
mods = []
saves = []
current = index
while current < len(lines):
line = lines[current]
if re.match(abilities_pattern, line):
# "**Str** 19 +4 +4"
parts = line.split()
scores.append(parts[1])
mods.append(parts[2])
saves.append(parts[3])
current += 1
else:
break
if len(scores) != 6:
return None
# consume a trailing blank
if current < len(lines) and lines[current] == '':
current += 1
# preserve a leading blank
start = index
if index > 0 and lines[index - 1] == '':
start = index - 1
lines_to_replace = current - start
del lines[start:current]
lines[start:start] = [
'',
'| | Str. | Dex. | Con. | Int. | Wis. | Cha. |',
'|--|--|--|--|--|--|--|',
f'| **Score** | {" | ".join(scores)} |',
f'| **Modifier** | {" | ".join(mods)} |',
f'| **Saving Throw** | {" | ".join(saves)} |',
'',
]
return 7 - lines_to_replace
def clean_reorder_stats(lines, index):
# "**Speed** 30 ft." doesn't come first
stat_order = NUMERIC_PROPERTIES[:4]
if not any(lines[index].startswith(f'**{stat}**') for stat in stat_order):
return None
current = index
while current < len(lines):
if any(lines[current].startswith(f'**{stat}**') for stat in stat_order):
current += 1
elif (
lines[current] == ''
and current + 1 < len(lines)
and any(lines[current + 1].startswith(f'**{stat}**') for stat in stat_order)
):
current += 1
else:
break
sorted_stats = sorted(
[line for line in lines[index:current] if line],
key=lambda s: next(
(i for i, stat in enumerate(stat_order) if s.startswith(f'**{stat}**')),
len(stat_order)
)
)
lines[index:current] = sorted_stats
return len(sorted_stats) - (current - index)
def clean_remove_mod_save(lines, index):
# "**Speed** 30 ft. MOD SAVE MOD SAVE MOD SAVE" -> "**Speed** 30 ft."
# "MOD SAVE MOD SAVE MOD SAVE **Str** 3 −4 −4" -> "**Str** 3 −4 −4"
while 'MOD SAVE' in lines[index]:
lines[index] = lines[index].replace('MOD SAVE', '', 1).strip()
lines[index] = re.sub(r'\s+', ' ', lines[index])
return 0
def clean_actions_emphasis(lines, index):
# "*Name.* Description." -> "_**Name.**_ Description."
pattern = r'^\*([^*]+)\*(.*)'
match = re.match(pattern, lines[index])
if not match:
return None
header = None
for i in range(index - 1, -1, -1):
if lines[i].startswith('#'):
header = lines[i]
break
if not header or not any(
title in header
for title in ['Traits', 'Actions', 'Legendary Actions', 'Reactions']
):
return None
action_name = match.group(1)
description = match.group(2)
if action_name.startswith('Legendary Action Uses'):
return None
if '.' in action_name:
parts = action_name.split('.', 1)
ability_name = parts[0] + '.'
additional_text = parts[1].strip() if parts[1].strip() else ''
if additional_text:
# "*Rend. Melee Attack Roll:* ..." -> "_**Rend.**_ *Melee Attack Roll:* ..."
lines[index] = f'_**{ability_name}**_ *{additional_text}*{description}'
else:
# "*Amphibious.* ..." -> "_**Amphibious.**_ ..."
lines[index] = f'_**{ability_name}**_{description}'
return 0
return None
def clean_spell_list_emphasis(lines, index):
# "| Chill Touch | Necromancy |" -> "| *Chill Touch* | Necromancy |"
if (
not lines[index].startswith('|')
or index < 2
):
return None
headers = get_table_headers(lines, index)
if headers is None:
return None
columns = [
i for i, header in enumerate(headers)
if 'Spell' in header
]
if not columns:
return None
cells = lines[index].split('|')
for column in columns:
cell_index = column + 1
if len(cells) <= cell_index:
continue
cell_content = cells[cell_index].strip()
if '*' not in cell_content and '_' not in cell_content:
cells[cell_index] = spell_matcher.sub(r'*\1*', cell_content)
else:
cells[cell_index] = cell_content
lines[index] = '|'.join(cells)
return 0
def clean_decost_headers(lines, index):
# "Alchemist's Supplies (50 GP)" -> "Alchemist's Supplies\n\n**Cost:** 50 GP""
pattern = r'^(#.*?)\s+\(([^)]*(?:[GCSEP]P|Free|Varies)[^)]*)\)$'
if match := re.match(pattern, lines[index]):
lines[index] = match.group(1)
if match2 := re.match(r'^(- )?\*\*', lines[index+2]):
prefix = match2.group(1) or ''
lines.insert(index+2, f"{prefix}**Cost:** {match.group(2)}")
return 1
else:
lines.insert(index+2, '')
lines.insert(index+2, f"**Cost:** {match.group(2)}")
return 2
return None
def clean_enbullet_statlists(lines, index):
# "**AC** 17\n**Initiative** +7 (17)" -> "- **AC** 17\n- **Initiative** +7 (17)"
if lines[index].startswith('- ') or lines[index].startswith('|'):
return None
prop_pattern = (
r'^(- )?\*\*('
+ '|'.join(
re.escape(p) for p in (
PROPERTIES
+ NUMERIC_PROPERTIES
+ SPELL_PROPERTIES
+ ITEM_PROPERTIES
+ BACKGROUND_PROPERTIES
+ SPECIES_PROPERTIES
)
)
+ r'):?\*\*'
)
if not re.match(prop_pattern, lines[index]):
return None
has_adjacent = False
if index > 0 and re.match(prop_pattern, lines[index - 1]):
has_adjacent = True
if index + 1 < len(lines) and re.match(prop_pattern, lines[index + 1]):
has_adjacent = True
if has_adjacent:
lines[index] = '- ' + lines[index]
return 0
return None
def clean_srd(
lines,
breakdown_data,
show_progress=False,
clean_lines=None,
profile=None,
):
global spell_list, spell_matcher
if profile:
spell_list = get_spell_list(profile)
spell_matcher = get_next_unemphasised_spell(spell_list)
def _progress_bar(end):
if show_progress:
filled = int(round(min(index / len(lines), 1.0) * 100, 1) / 2)
bar = (
'█' * filled
+ '░' * (50 - filled)
)
print(f"- {cleaner.__name__:40} {index:6} [{bar}]", end=end)
DND_51_ORIGINAL_CONVERSIONS_TABLE = [ # noqa: F841
# common problems
clean_whitespace,
clean_wrap_blank_lines,
clean_unicode_chars,
clean_table_alignment,
clean_midsentence_pagebreak,
# PDF->MD mistakes
clean_remove_mistaken_headers,
clean_remove_header_bold,
clean_unwrap_consecutive_bold,
# 5.1 SRD specific formatting
clean_add_traits_header,
clean_leading_emphasis,
clean_statblock_attack_emphasis,
clean_wrapup_feature_lists,
clean_wrapup_attribute_lists,
clean_statblock_spells_to_list,
clean_single_action_to_list,
clean_statblock_spellcasting_marker,
clean_collapse_adjacent_items,
# sanitation
clean_canonicalise_proper_nouns,
# markdown preferences
clean_italic_emphasis_markers,
]
DND_51_CONVERSIONS_TABLE = [
# more aggressive now
clean_unicode_chars,
# rethink some earlier formatting choices
clean_respace_lists,
clean_deindent,
clean_delistify_actions,
clean_reitalicise_attack_actions,
# reformat the monster ability table
clean_detabulate_mixed_stats,
clean_recalculate_saving_throws,
clean_retabulate_ability_scores,
clean_table_alignment,
]
DND_521_CONVERSIONS_TABLE = [
# basic cleanliness
clean_whitespace,
clean_unicode_chars,
clean_midsentence_pagebreak,
clean_pluralise_component,
clean_space_out_emdashes,
clean_escape_square_brackets,
# clean marker_single formatting choices
clean_remove_mistaken_headers,
clean_remove_header_bold,
clean_bold_periods,
clean_actions_emphasis,
# clean up after the *endless* table mistakes
clean_remove_mod_save,
clean_detabulate_mixed_stats,
clean_unwrap_consecutive_bold,
clean_reorder_stats,
clean_collapse_adjacent_items,
clean_retabulate_ability_scores,
# probably 521 specific
clean_decost_headers,
clean_spell_list_emphasis,
clean_statblock_spells_to_list,
# final formatting pass
clean_table_alignment,
clean_italic_emphasis_markers,
clean_italic_to_bold_italic,
clean_collapse_adjacent_items,
clean_enbullet_statlists,
clean_wrap_blank_lines,
]
CONVERSIONS_TABLE = [
# basic cleanliness
clean_whitespace,
clean_unicode_chars,
clean_midsentence_pagebreak,
clean_pluralise_component,
clean_space_out_emdashes,
clean_escape_square_brackets,
# final formatting pass
clean_table_alignment,
clean_italic_emphasis_markers,
clean_collapse_adjacent_items,
clean_wrap_blank_lines,
]
if profile == 'dnd51':
conversion_table = DND_51_CONVERSIONS_TABLE
elif profile == 'dnd521':
conversion_table = DND_521_CONVERSIONS_TABLE
else:
conversion_table = CONVERSIONS_TABLE
changes = 0
for cleaner in conversion_table:
# changing the lines array necessitates restart, so track line
last_index = -1
while True:
result = None
for index, line in enumerate(lines):
if index <= last_index:
continue
_progress_bar('\r')
if clean_lines and line in clean_lines:
continue
result = cleaner(lines, index)
if result is not None:
last_index = index + result
changes += 1
if result != 0:
if breakdown_data is not None:
update_breakdown_data(breakdown_data, index+1, result)
break
if result is None:
break
_progress_bar('\n')
if changes > 0:
return '\n'.join(lines + [''])
return None
def warn_table_runon(lines, index):
# probably a table split across pages, but marker gave it new headers
if '|' not in lines[index]:
return None
if (
index > 2
and lines[index-1] == ''
and '|' in lines[index-2]
):
if len(lines[index].split('|')[1:-1]) == len(lines[index-2].split('|')[1:-1]):
return "possible table run-on"
def warn_midparagraph_italics(lines, index):
# look for mid-paragraph italics that could be a source wrapping error
excluded = [
"Player's Handbook.",
"Hit:",
# SRD 5.2.1
"Miss:",
"Hit or Miss:",
"Choose A or B:",
"Melee Attack Roll:",
"Ranged Attack Roll:",
"Melee or Ranged Attack Roll:",
"Melee Weapon Attack:",
"Ranged Weapon Attack:",
"Melee or Ranged Weapon Attack:",