-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathparagraph.py
722 lines (585 loc) · 23.4 KB
/
paragraph.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
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
"""
paragraph
"""
from pptx.enum.shapes import MSO_SHAPE_TYPE
from pptx.enum.shapes import PP_PLACEHOLDER
from lxml import etree
import re
from pptx.dml.color import RGBColor, MSO_THEME_COLOR
import globals
from processingOptions import *
from symbols import resolveSymbols
def removeBullet(paragraph):
pPr = paragraph._p.get_or_add_pPr()
pPr.insert(
0,
etree.Element("{http://schemas.openxmlformats.org/drawingml/2006/main}buNone"),
)
def removeBullets(textFrame):
for p in textFrame.paragraphs:
removeBullet(p)
def removeSelectedBullets(textFrame, removalArray):
for bulletNumber in removalArray:
removeBullet(textFrame.paragraphs[bulletNumber])
def findTitleShape(slide):
if slide.shapes.title == None:
# Have to use first shape as title
return slide.shapes[0]
else:
return slide.shapes.title
def getParagraphs(slide, wantedParagraphs = []):
paragraphTree = []
for theShape in slide.shapes:
if (theShape.shape_type == MSO_SHAPE_TYPE.TEXT_BOX) | (theShape.placeholder_format.type == PP_PLACEHOLDER.OBJECT):
paragraphTree.append(theShape.text_frame.paragraphs)
return[ paragraphTree[0][i] for i in wantedParagraphs]
def parseText(text):
textArray = []
state = "N"
fragment = ""
lastChar = ""
spanState = "None"
# Replace any "\#" strings with entity reference
text2 = text.replace("\\#", "#")
# Replace any "<br/>" strings with newline single character
text2 = text2.replace("<br/>", "\n")
# Replace any escaped asterisk strings with entity reference
text2 = text2.replace("\\*", "∗")
# Replace any asterisks with spaces either side with entity reference
text2 = text2.replace(" * ", " ∗ ")
if text2[-2:] == " *":
text2 = text2[:-2] + " ∗"
# Replace any footnote reference starts with char "\uFDD0"
text2 = text2.replace("[^", u"\uFDD0")
# Replace any span style starts with char "\uFDD1"
text2 = re.sub(globals.spanStyleRegex, u"\uFDD1", text2)
# Replace any span class starts with char "\uFDD2"
text2 = re.sub(globals.spanClassRegex, u"\uFDD2", text2)
# Replace any span ends with char "\uFDD3"
text2 = text2.replace("</span>", u"\uFDD3")
# Replace any abbreviation starts with char "\uFDD4"
text2 = text2.replace("<abbr title=", u"\uFDD4")
# Replace any abbreviation ends with char "\uFDD5"
text2 = text2.replace("</abbr>", u"\uFDD5")
# Replace any \[ with char "\uFDD6"
text2 = text2.replace(r"\[", u"\uFDD6")
# Replace any \] with char "\uFDD7"
text2 = text2.replace(r"\]", u"\uFDD7")
# "\uFDD8" is link separator special character. See below
# Replace any {~~ with char "\uFDD8"
text2 = text2.replace("{~~", u"\uFDD8")
# Replace any ~~} with char "\uFDD8"
text2 = text2.replace("~~}", u"\uFDD8")
# Replace any {== with char "\uFDD9"
# Danish character)
text2 = text2.replace("{==", u"\uFDD9")
# Replace any ==} with char "\uFDD9"
text2 = text2.replace("==}", u"\uFDD9")
# Replace any {>> with char "\uFDDA"
text2 = text2.replace("{>>", u"\uFDDA")
# Replace any <<} with char "\uFDDA"
text2 = text2.replace("<<}", u"\uFDDA")
# Replace any {-- with char "\uFDDB"
text2 = text2.replace("{--", u"\uFDDB")
# Replace any --} with char "\uFDDB
text2 = text2.replace("--}", u"\uFDDB")
# Replace any {++ with char "\uFDDC"
text2 = text2.replace("{++", u"\uFDDC")
# Replace any ++} with char "\uFDDC"
text2 = text2.replace("++}", u"\uFDDC")
# Replace any <ins> with char "\uFDDD"
text2 = text2.replace("<ins>", u"\uFDDD")
# Replace any </ins> with char "\uFDDD"
text2 = text2.replace("</ins>", u"\uFDDD")
# Replace any <del> with char "\uFDDE"
text2 = text2.replace("<del>", u"\uFDDE")
# Replace any </del> with char "\uFDDE"
text2 = text2.replace("</del>", u"\uFDDE")
# Replace any <sub> with char "\uFDDF"
text2 = text2.replace("<sub>", u"\uFDDF")
# Replace any </sub> with char "\uFDDF"
text2 = text2.replace("</sub>", u"\uFDDF")
# Replace any <sup> with char "\uFDE0"
text2 = text2.replace("<sup>", u"\uFDE0")
# Replace any </sup> with char "\uFDE0"
text2 = text2.replace("</sup>", u"\uFDE0")
# Handle escaped underscore
text2 = text2.replace("\\_", "_")
# Unescape any numeric character references
text3 = resolveSymbols(text2)
for c in text3:
if c == "*":
# Changing state
if state == "N":
# First * potentially starts italic
textArray.append([state, fragment])
fragment = ""
state = "I"
elif state == "I":
# Either go to bold or end italic
if lastChar == "*":
# Go to bold
state = "B1"
else:
# End italic
textArray.append([state, fragment])
fragment = ""
state = "N"
elif state == "B1":
# Starting to close bold bracket
state = "B2"
elif lastChar == "*":
# closing either bold or italic bracket
textArray.append([state, fragment])
fragment = ""
state = "N"
elif c == "`":
if state == "N":
# Going to code
textArray.append([state, fragment])
fragment = ""
state = "C"
else:
# exiting code
textArray.append([state, fragment])
fragment = ""
state = "N"
elif c == u"\uFDD8":
# Entering or leaving CriticMarkup replacement
if state == "N":
# Going to CriticMarkup replacement
textArray.append([state, fragment])
fragment = ""
state = "CMRep"
else:
# exiting CriticMarkup replacement
textArray.append([state, fragment])
fragment = ""
state = "N"
elif c == u"\uFDD9":
# Entering or leaving CriticMarkup highlight
if state == "N":
# Going to CriticMarkup highlight
textArray.append([state, fragment])
fragment = ""
state = "CMHig"
else:
# exiting CriticMarkup highlight
textArray.append([state, fragment])
fragment = ""
state = "N"
elif c == u"\uFDDA":
# Entering or leaving CriticMarkup comment
if state == "N":
# Going to CriticMarkup comment
textArray.append([state, fragment])
fragment = ""
state = "CMCom"
else:
# exiting CriticMarkup comment
textArray.append([state, fragment])
fragment = ""
state = "N"
elif c == u"\uFDDB":
# Entering or leaving CriticMarkup deletion
if state == "N":
# Going to CriticMarkup deletion
textArray.append([state, fragment])
fragment = ""
state = "CMDel"
else:
# exiting CriticMarkup deletion
textArray.append([state, fragment])
fragment = ""
state = "N"
elif c == u"\uFDDC":
# Entering or leaving CriticMarkup addition
if state == "N":
# Going to CriticMarkup addition
textArray.append([state, fragment])
fragment = ""
state = "CMAdd"
else:
# exiting CriticMarkup addition
textArray.append([state, fragment])
fragment = ""
state = "N"
elif c == u"\uFDDD":
# Entering or leaving underline
if state == "N":
# Going to underline
textArray.append([state, fragment])
fragment = ""
state = "Ins"
else:
# exiting underline
textArray.append([state, fragment])
fragment = ""
state = "N"
elif c == u"\uFDDE":
# Entering or leaving strikethrough
if state == "N":
# Going to strikethrough
textArray.append([state, fragment])
fragment = ""
state = "Del"
else:
# exiting strikethrough
textArray.append([state, fragment])
fragment = ""
state = "N"
elif c == u"\uFDDF":
# Entering or leaving subscript
if state == "N":
# Going to subscript
textArray.append([state, fragment])
fragment = ""
state = "Sub"
else:
# exiting subscript
textArray.append([state, fragment])
fragment = ""
state = "N"
elif c == u"\uFDE0":
# Entering or leaving superscript
if state == "N":
# Going to superscript
textArray.append([state, fragment])
fragment = ""
state = "Sup"
else:
# exiting superscript
textArray.append([state, fragment])
fragment = ""
state = "N"
elif c == "[":
if state == "N":
# Could be entering a Link
if fragment != "":
textArray.append([state, fragment])
# The bracket is kept in in case there is no matching ]
fragment = "["
state = "LinkText1"
elif state == "LinkText2":
# Could be entering an indirect reference
indLinkText = fragment[:-1]
# The bracket is kept in in case there is no matching ]
fragment = "["
state = "LinkRef1"
elif c == "]":
# Could be ending picking up the link text
if state == "LinkText1":
# Picked up end of link text
state = "LinkText2"
# Remove [ and add a separator to allow for link URL
fragment = fragment[1:] + u"\uFDD8"
elif state == "fnref":
# This terminates a footnote reference
textArray.append([state, fragment])
state = "N"
fragment = ""
elif state == "LinkRef1":
# Picked up link reference
reference = fragment[1:]
# Attempt to look up reference
foundReference = False
for indref, indURL in indirectAnchors:
if indref == reference:
foundReference = True
break
if foundReference:
# Append fragment with resolved reference
textArray.append(["Link", indLinkText + u"\uFDD8" + indURL])
else:
print(f"Reference {reference} not resolved.\n")
textArray.append(["N", indLinkText])
fragment = ""
state = "N"
else:
# This was an ordinary square bracket
fragment += "]"
elif c == "(":
# Could be starting to pick up the link URL
if state == "LinkText2":
# Picked up start of link URL
state = "LinkURL1"
else:
fragment = fragment + c
elif c == ")":
# Could be ending picking up the link URL
if state == "LinkURL1":
# Picked up end of link URL
textArray.append(["Link", fragment])
fragment = ""
state = "N"
else:
fragment = fragment + c
elif ord(c) == 235:
fragment = fragment + "`"
elif ord(c) == 236:
fragment = fragment + "<"
elif c == u"\uFDD6":
fragment = fragment + "["
elif c == u"\uFDD7":
fragment = fragment + "]"
elif c == u"\uFDD5":
dictEntry = fragment.split(">")
dictAbbrev = dictEntry[1]
dictFull = dictEntry[0].strip().strip("'").strip('"')
abbrevDictionary[dictAbbrev] = dictFull
textArray.append(["Gloss", dictAbbrev, dictAbbrev, dictFull])
fragment = ""
elif c == u"\uFDD4":
if fragment != "":
textArray.append([state, fragment])
fragment = ""
dictEntry = ""
elif c == u"\uFDD3":
# End of span
if spanState == "Class":
# Span with class
splitting = fragment.split(">")
spanText = splitting[1]
className = splitting[0].strip().strip("'").strip('"').lower()
styleText = ""
if (
(className in globals.bgcolors)
| (className in globals.fgcolors)
| (className in globals.emphases)
| (className in globals.fontsizes)
):
textArray.append(["SpanClass", [className, spanText]])
fragment = ""
else:
print(
f"{className} is not defined. Ignoring reference to it in <span> element."
)
fragment = spanText
else:
# Span with style
splitting = fragment.split(">")
spanText = splitting[1]
styleText = splitting[0].strip().strip("'").strip('"')
textArray.append(["SpanStyle", [styleText, spanText]])
className = ""
fragment = ""
spanState = "None"
elif c == u"\uFDD2":
# In span element where we hit the class name
if fragment != "":
textArray.append([state, fragment])
fragment = ""
spanState = "Class"
elif c == u"\uFDD1":
# In span element where we hit the style text
if fragment != "":
textArray.append([state, fragment])
fragment = ""
spanState = "Style"
elif c == u"\uFDD0":
if fragment != "":
textArray.append([state, fragment])
fragment = ""
state = "fnref"
else:
fragment = fragment + c
lastChar = c
if fragment != "":
textArray.append([state, fragment])
return textArray
# Calls the tokeniser and then handles the fragments it gets back
def addFormattedText(p, text):
boldBold = globals.processingOptions.getCurrentOption("boldBold")
boldColour = globals.processingOptions.getCurrentOption("boldColour")
italicItalic = globals.processingOptions.getCurrentOption("italicItalic")
italicColour = globals.processingOptions.getCurrentOption("italicColour")
monoFont = globals.processingOptions.getCurrentOption("monoFont")
# Get back parsed text fragments, along with control information on each
# fragment
parsedText = parseText(text)
# Replace chr(237) with > in each Fragment
for f in range(len(parsedText)):
if parsedText[f][0] in ["SpanClass", "SpanStyle"]:
parsedText[f][1][1] = parsedText[f][1][1].replace(chr(237), ">")
else:
parsedText[f][-1] = parsedText[f][-1].replace(chr(237), ">")
# Prime flattened Text
flattenedText = ""
for fragment in parsedText:
if fragment[0] == "Gloss":
fragType, fragDetail, fragTerm, fragTitle = fragment
else:
fragType, fragDetail = fragment
# Break into subfragments around a newline
if fragType == "SpanClass":
className, fragText = fragDetail
styleText = ""
subfragments = fragText.split("\n")
elif fragType == "SpanStyle":
styleText, fragText = fragDetail
className = ""
subfragments = fragText.split("\n")
else:
subfragments = fragDetail.split("\n")
# Process each subfragment
sfnum = 0
for subfragment in subfragments:
if sfnum > 0:
# Subfragments after the first need to be preceded by a line break
p.add_line_break()
sfnum += 1
# Ensure "\*" is rendered as a literal asterisk
subfragment = subfragment.replace("∗", "*")
# Ensure "\#" is rendered as a literal octothorpe
subfragment = subfragment.replace("#", "#")
run = p.add_run()
if fragType not in ["Link", "fnref", "Gloss"]:
run.text = subfragment
elif fragType == "Gloss":
run.text = fragTerm
if fragType == "I":
font = run.font
if italicItalic == True:
font.italic = True
if italicColour != ("None", ""):
setColour(font.color, italicColour)
elif fragType == "Gloss":
# Add this run to abbrevRunsDictionary - for Glossary fix ups later
if fragTerm not in abbrevRunsDictionary:
abbrevRunsDictionary[fragTerm] = []
abbrevRunsDictionary[fragTerm].append(run)
elif fragType == "fnref":
font = run.font
font.size = Pt(16)
set_superscript(font)
fnref = fragment[1]
if fnref in footnoteReferences:
footnoteNumber = footnoteReferences.index(fnref)
run.text = str(footnoteNumber + 1)
footnoteRunsDictionary[footnoteNumber] = run
else:
run.text = "[?]"
print("Error: Footnote reference '" + fnref + "' unresolved.")
linkText = "!"
fragment = ""
elif fragType == "SpanClass":
handleSpanClass(run, className)
elif fragType == "SpanStyle":
handleSpanStyle(run, styleText)
elif fragType == "B2":
font = run.font
if boldBold == True:
font.bold = True
if boldColour != ("None", ""):
setColour(font.color, boldColour)
elif fragType == "C":
font = run.font
font.name = monoFont
elif fragType == "CMRep":
font = run.font
font.color.rgb = RGBColor(255, 140, 0)
run.text = "{~~" + subfragment + "~~}"
elif fragType == "CMHig":
font = run.font
font.color.rgb = RGBColor(195, 0, 195)
run.text = "{==" + subfragment + "==}"
elif fragType == "CMCom":
font = run.font
font.color.rgb = RGBColor(0, 0, 195)
run.text = "{>>" + subfragment + "<<}"
elif fragType == "CMDel":
font = run.font
font.color.rgb = RGBColor(195, 0, 0)
run.text = "{--" + subfragment + "--}"
elif fragType == "CMAdd":
font = run.font
font.color.rgb = RGBColor(0, 195, 0)
run.text = "{++" + subfragment + "++}"
elif fragType == "Ins":
font = run.font
font.underline = True
elif fragType == "Del":
font = run.font
setStrikethrough(font)
elif fragType == "Sub":
font = run.font
set_subscript(font)
elif fragType == "Sup":
font = run.font
set_superscript(font)
elif fragType == "Link":
linkArray = subfragment.split(u"\uFDD8")
linkText = linkArray[0]
linkURL = linkArray[1]
run.text = linkText
if linkURL.startswith("#"):
# Is an internal Url
linkHref = linkURL[1:].strip()
href_runs[linkHref] = run
else:
# Not an internal link so create it
hlink = run.hyperlink
hlink.address = linkURL
# URL might be a macro reference
if linkURL[:11] == "ppaction://":
# URL is indeed a macro reference, so treat it as such
hlink._hlinkClick.action = linkURL
# Add the flattened text from this subfragment
if fragType == "Link":
flattenedText = flattenedText + linkText
else:
flattenedText = flattenedText + subfragment
return flattenedText
def handleSpanClass(run, className):
if className in globals.bgcolors:
run = setHighlight(run, globals.bgcolors[className])
if className in globals.fgcolors:
font = run.font
font.color.rgb = RGBColor.from_string(globals.fgcolors[className])
if className in globals.emphases:
font = run.font
if " bold " in " " + globals.emphases[className] + " ":
font.bold = True
else:
font.bold = False
if " italic " in " " + globals.emphases[className] + " ":
font.italic = True
else:
font.italic = False
if " underline " in " " + globals.emphases[className] + " ":
font.underline = True
else:
font.underline = False
if className in globals.fontsizes:
font = run.font
font.size = Pt(float(globals.fontsizes[className]))
def handleSpanStyle(run, styleText):
styleElements = styleText.split(";")
# Handle the non-empty ones - as the empty one is after the final semicolon
for styleElement in list(filter(lambda e: e != "", styleElements)):
styleElementSplit = styleElement.split(":")
styleElementName = styleElementSplit[0].strip()
styleElementValue = styleElementSplit[1].strip()
if styleElementName == "color":
check, RGBstring = parseRGB(styleElementValue)
if check:
run.font.color.rgb = RGBColor.from_string(RGBstring)
else:
print(f"Invalid {styleElementName} RGB value {styleElementValue}")
elif styleElementName == "background-color":
check, RGBstring = parseRGB(styleElementValue)
if check:
setHighlight(run, RGBstring)
else:
print(f"Invalid {styleElementName} RGB value {styleElementValue}")
elif styleElementName == "text-decoration":
if styleElementValue == "underline":
run.font.underline = True
elif styleElementName == "font-weight":
if styleElementValue == "bold":
run.font.bold = True
elif styleElementName == "font-style":
if styleElementValue == "italic":
run.font.italic = True
elif styleElementName == "font-size":
run.font.size = Pt(float(styleElementValue[:-2]))