-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathub_theme.html
1502 lines (1085 loc) · 38.8 KB
/
ub_theme.html
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
<script>
/*****************************************/
/* Unofficial Xaringan theme of the */
/* University of Bordeaux */
/* */
/* Script created by Laurent Bergé */
/* All original code */
/* */
/* V 1.0 (Fall 2021) */
/* */
/* MIT licence */
/* Feel free to use/adapt */
/* Please cite if copy/pasting :-) */
/*****************************************/
/* IMPORTANT:
* - Works with the CSS stylesheet contained in ub_theme.css
*/
/* DISCLAIMER:
* - I created this code to serve my own uses which very likely differ from yours.
* Hence there is no garantee that it will work appropriately for your uses.
* - That's my first venture into JS, so please be indulgent! ;-)
*/
/* UPDATES:
* - 2022-08-30: fix title slide problem with new remark version
* - 2022-09-26: fix auto-height with svl floats
*/
function settings_callback(){
// the order is important
// create the panel sets
setup_panelset();
// add with-header to slides with header
setup_slide_header();
// set up title page author section
setup_title_page();
// create the block environments
setup_blocks();
// create the versus environments
setup_versus();
// automatic CSS: ex fs-32px
setup_css();
// create the footnotes
setup_footnotes();
// set up the column environment
setup_columns();
// vertical spacing: ex .h-100px[]
setup_vspace();
// text autofit
setup_autofit();
// adjust the images heights
setup_images();
// adjust the height of the text // LAST
setup_auto_height();
}
window.addEventListener("load", settings_callback);
//
// Panelsets
//
function showPane() {
let panel_set = this.parentElement.parentElement;
let id = this.getAttribute("panelid");
all_panel_contents = panel_set.getElementsByClassName("panel-content");
all_panel_names = panel_set.querySelectorAll("div[class^=panel-name]");
for(let i=0 ; i<all_panel_contents.length ; i++){
if(i == id){
all_panel_contents[i].style.display = "block";
all_panel_names[i].setAttribute("class", "panel-name-active");
} else {
all_panel_contents[i].style.display = "none";
all_panel_names[i].setAttribute("class", "panel-name");
}
}
}
function setup_panelset(){
let brace_open = /p> *\{/;
let brace_close = /\}/;
let all_panelsets = document.getElementsByClassName("panelset");
for(let i=0 ; i<all_panelsets.length ; i++){
let all_panes = all_panelsets[i].getElementsByClassName("pane");
let n_panes = all_panes.length;
// the title is within a <p> element
let new_name, new_html;
let all_names = [];
let all_contents = [];
let all_classes = [];
for(let j=0 ; j<n_panes ; j++){
let my_html = all_panes[j].innerHTML;
let my_class = all_panes[j].className;
if(my_html.length === 0 || !brace_open.test(my_html) || !brace_close.test(my_html)){
new_name = "No title ("+ (j + 1).toString() + ")";
new_html = my_html;
} else {
let pos_end = my_html.search(brace_close);
new_name = my_html.substring(0, pos_end).trim();
let pos_start = new_name.search(/\{/);
new_name = new_name.substring(pos_start + 1, new_name.length);
new_html = "<p>" + my_html.substring(pos_end + 1, my_html.length).trim();
new_html.replace(/<p>[^<]<\/p>/, "");
}
all_names.push(new_name);
all_contents.push(new_html);
all_classes.push(my_class);
}
// we remove the pane nodes
all_panelsets[i].innerHTML = "";
// We add the with-header class if needed (we need it in CSS)
let parent = all_panelsets[i].parentElement;
let my_h1 = parent.getElementsByTagName("H1");
if(my_h1.length > 0){
all_panelsets[i].classList.add("with-header");
}
// now we create some containers
//
// Name container
//
// first for the names
let div_cont_names = document.createElement("div");
div_cont_names.setAttribute("class", "container-names");
let all_div_names = [];
for(let j=0 ; j<n_panes ; j++){
let div_name = document.createElement("div");
div_name.innerText = all_names[j];
div_name.setAttribute("panelid", j);
div_name.addEventListener("click", showPane);
if(j == 0){
div_name.setAttribute("class", "panel-name-active");
} else {
div_name.setAttribute("class", "panel-name");
}
div_cont_names.appendChild(div_name);
}
//
// content container
//
// then for the contents
let div_cont_content = document.createElement("div");
div_cont_content.setAttribute("class", "container-content");
for(let j=0 ; j<n_panes ; j++){
let div_content = document.createElement("div");
div_content.innerHTML = all_contents[j];
// we pass along the extra classes
div_content.className = "panel-content " + all_classes[j];
div_content.classList.remove("pane");
// div_content.setAttribute("class", "panel-content");
if(j == 0){
div_content.style.display = "block";
} else {
div_content.style.display = "none";
}
div_cont_content.appendChild(div_content);
}
// we add them
all_panelsets[i].appendChild(div_cont_names);
all_panelsets[i].appendChild(div_cont_content);
}
//
// Now that the new document is generated, we style it a bit
//
for(let i=0 ; i<all_panelsets.length ; i++){
//
// Resizing the buttons
//
let all_panel_names = all_panelsets[i].querySelectorAll("div[class^=panel-name]");
let n_panes = all_panel_names.length;
let max_width = 0;
for(let j=0 ; j<n_panes ; j++){
if(max_width < all_panel_names[j].clientWidth){
max_width = all_panel_names[j].clientWidth;
}
}
// setting the new width
let panel_name_style = getComputedStyle(all_panel_names[0]);
let pad_width_txt = " - " + panel_name_style.paddingLeft + " - " + panel_name_style.paddingRight + ")";
for(let j=0 ; j<n_panes ; j++){
all_panel_names[j].style.width = "calc(2px + " + max_width.toString() + "px" + pad_width_txt;
}
//
// Setting the contents height
//
let container_content = all_panelsets[i].querySelector("div.container-content");
let container_names = all_panelsets[i].querySelector("div.container-names");
let style_ps = getComputedStyle(all_panelsets[i]);
let total_height = all_panelsets[i].clientHeight + "px - " + style_ps.paddingTop + " - " + style_ps.paddingBottom;
let style_cn = getComputedStyle(container_names);
let container_names_height = container_names.offsetHeight + "px + " + style_cn.marginTop + " + " + style_cn.marginBottom;
let style_cc = getComputedStyle(container_content);
// let container_content_height = style_cc.paddingTop + " + " + style_cc.paddingBottom + " + " + style_cc.marginTop + " + " + style_cc.marginBottom;
let container_content_height = style_cc.marginTop + " + " + style_cc.marginBottom;
let new_height = "calc(" + total_height + " - (" + container_names_height + ") - (" + container_content_height +") )";
container_content.style.height = new_height;
}
}
//
// Header slides
//
// We tag slides with header + auto-shrink
function setup_slide_header(){
// We add the with-header class to slides having headers
let all_slides = document.querySelectorAll("div.remark-slide-content");
for(let i=0 ; i<all_slides.length ; i++){
let slide = all_slides[i];
if(slide.getElementsByTagName("H1").length > 0){
slide.classList.add("with-header");
slide.getElementsByTagName("H1")[0].classList.add("auto-shrink");
}
}
}
//
// Automatic height adjustment
//
/* to get the dimension of an element: https://developer.mozilla.org/en-US/docs/Web/API/CSS_Object_Model/Determining_the_dimensions_of_elements */
function setup_auto_height(){
let all_slide_containers = document.querySelectorAll(".remark-slide-container");
//let all_slides = document.querySelectorAll(".remark-slide-content:not(.title-slide)");
//
// Automatically setting the default text at 33% of the total slide height
//
let SLIDE_CONTENT_SHARE_TRIGGER = 0.70;
let OPTIMAL_HEIGHT = 0.70;
// we catch the continuation
let i_start_continue = 0;
for(let i=0 ; i<all_slide_containers.length ; i++){
// let my_slide = all_slides[i];
let all_contents = all_slide_containers[i].querySelectorAll(".remark-slide-content");
if(all_contents.length == 0 || all_contents[0].className.includes("title-slide")){
continue;
}
// we skip panelsets
if(all_contents[0].querySelectorAll(".panelset").length > 0){
continue;
}
i_start_continue = i;
while(all_slide_containers[i].className.includes("has-continuation")){
i++;
}
// we take the final slide
let my_slide = all_slide_containers[i].querySelector(".remark-slide-content");
let all_elements = my_slide.children;
let my_height = 0;
let first = true;
let first_spaceTop = 0;
// the main loop
let is_float = false;
let float_height = 0;
let margin_prev = 0;
for(elem of all_elements){
if(elem.tagName == "H1"){
continue;
}
let style = getComputedStyle(elem);
let current_height = parseInt(elem.offsetHeight);
let margin_top = parseInt(style.marginTop);
if(margin_prev > margin_top){
current_height += margin_prev;
} else {
current_height += margin_top;
}
margin_prev = parseInt(style.marginBottom);
if(first){
// use to find the location of the text on the slide later
first = false;
first_spaceTop = parseInt(style.marginTop) + parseInt(style.borderTopWidth) + parseInt(style.paddingTop);
}
// Handling floats
if(style.float != "none"){
if(elem.className.includes("leftcol") || elem.className.includes("pull-left")){
// we re initialize
is_float = false;
}
if(is_float){
if(current_height < float_height){
// we don't add it since lower
current_height = 0;
} else {
// == we add the highest value
current_height -= float_height;
}
}
if(!is_float){
is_float = true;
float_height = current_height;
}
}
my_height += current_height;
}
my_height += margin_prev;
if(my_height === 0) continue;
// we compute the slide height for each slide because they may vary
let slide_style = getComputedStyle(my_slide);
let slide_height = parseInt(my_slide.clientHeight) - parseInt(slide_style.paddingTop) - parseInt(slide_style.paddingBottom);
let frac_height = 1.0 * my_height / slide_height;
let done = false;
if(frac_height < SLIDE_CONTENT_SHARE_TRIGGER){
let slide_paddingTop = parseInt(slide_style.paddingTop);
let top_space_share = 1.0 * (slide_paddingTop + first_spaceTop) / slide_height;
if(top_space_share < OPTIMAL_HEIGHT){
done = true;
for(let j=i_start_continue ; j<=i ; j++){
let my_div = document.createElement("div");
let div_height = 1.0 * (1 - OPTIMAL_HEIGHT) * parseInt(my_slide.clientHeight) - (slide_paddingTop + first_spaceTop);
my_div.style.height = div_height.toFixed(0).toString() + "px";
my_slide = all_slide_containers[j].querySelector(".remark-slide-content");
my_slide.insertBefore(my_div, my_slide.children[0]);
}
}
}
if(!done){
for(let j=i_start_continue ; j<=i ; j++){
let my_div = document.createElement("div");
let div_height = 0.5 * (slide_height - my_height);
my_div.style.height = div_height.toFixed(0).toString() + "px";
my_slide = all_slide_containers[j].querySelector(".remark-slide-content");
my_slide.insertBefore(my_div, my_slide.children[0]);
}
}
}
}
//
// title slide: h3 always bottom left (with or w/t h2).
//
// 2022-08-30: With new remark version, this somewhat becomes obsolete.
function setup_title_page(){
let title_slide = document.getElementsByClassName("title-slide");
if(title_slide.length > 0){
title_slide = title_slide[0];
let title_slide_elements = title_slide.children;
let n_elem = title_slide_elements.length;
let my_div = document.createElement("div");
let i=0;
while(i < title_slide.children.length){
let elem = title_slide.children[i];
// author/institute/date are h3 elements directly inserted in the title slide
let is_h3 = elem.tagName == "H3";
// 2022-08-30: new version of remark creates h3 within a div container of class: title/author/etc
// it makes a lot of sense but it breaks my old code. So we need to check.
let is_h3_within_div = elem.children.length > 0 && elem.children[0].tagName == "H3";
// I think with the new remark version, we don't need this piece of code (since you can tailor author/etc directly
// which were impossible to single out before) but I keep it for compatibility
if(is_h3 || is_h3_within_div){
// we remove it
let h3_rm = title_slide.removeChild(title_slide.children[i]);
my_div.appendChild(h3_rm);
} else {
i++;
}
}
// style of the div
my_div.setAttribute("class", "title-slide-author");
title_slide.appendChild(my_div);
}
}
//
// Handling images
//
function setup_images(){
let all_imgs = document.querySelectorAll("p>img");
/* Sometimes images are contained in <p> and this is VERY annoying since you cannot
set the height of the image any more. So we unnest these images.*/
for(let i=0 ; i<all_imgs.length ; i++){
let img = all_imgs[i];
let p = img.parentElement;
// we don't unnest if there are multiple elements (we may be mistaken!)
let n_elem = p.getElementsByTagName('*').length;
if(n_elem == 1){
let parent = p.parentElement;
parent.replaceChild(img, p);
}
}
// Cleaning artefacts: empty <p></p>
let all_p = document.querySelectorAll("div.panel-content>p");
for(let i=0 ; i<all_p.length ; i++){
let p = all_p[i];
if(p.innerHTML == ""){
let parent = p.parentElement;
parent.removeChild(p);
// update
i -= 1;
all_p = document.querySelectorAll("div.panel-content>p");
}
}
// Now we look at the images that are last
// The user never wants them to overflow
// So we nest them in a div with set height = remaining height
// + center
// A) within panelsets
all_imgs = document.querySelectorAll("div.panel-content>img");
for(let i=0 ; i<all_imgs.length ; i++){
let img = all_imgs[i];
let parent = img.parentElement;
// We need to display the hidden panels
let display_value = parent.style.display;
parent.style.display = "block";
if(parent.className.includes("no-auto")){
continue;
}
if(i < (all_imgs.length - 1) && all_imgs[i + 1].parentElement == parent){
continue;
}
let all_elem = parent.children;
let elem_height = 0;
let margin_prev = 0;
let is_span = false;
let display_value_span = "";
for(let j=0 ; j<all_elem.length ; j++){
let elem = all_elem[j];
if(elem == img) continue;
if(elem.offsetHeight == ""){
// This is a span => tricky
// so far we make the wrong assumption that spans should be treated as divs
// To correct later: include successive spans in a div, then check that div height
is_span = true;
display_value_span = elem.style.display;
elem.style.display = "block";
}
elem_height += parseInt(elem.offsetHeight);
let elem_style = getComputedStyle(elem);
let margin_top = parseInt(elem_style.marginTop)
if(margin_top > margin_prev){
elem_height += margin_top;
} else {
elem_height += margin_prev;
}
margin_prev = parseInt(elem_style.marginBottom);
if(is_span){
is_span = false;
elem.style.display = display_value_span;
}
}
// last increment
elem_height += margin_prev;
let parent_style = getComputedStyle(parent);
let parent_height = parseInt(parent.clientHeight) + parseInt(parent_style.paddingTop) + parseInt(parent_style.paddingBottom);
let img_height = parent_height - elem_height;
if(img_height < 0){
img_height = 50;
}
let my_div = document.createElement("div");
my_div.style.height = img_height.toFixed(0).toString() + "px";
my_div.style.textAlign = "center";
let img_clone = img.cloneNode(true);
my_div.appendChild(img_clone);
parent.replaceChild(my_div, img);
parent.style.display = display_value;
}
// B) within regular slides => beware of the header!
// This is almost identical to the previous code
// but we first delete the image to get the proper height
all_imgs = document.querySelectorAll("div.remark-slide-content>img");
for(let i=0 ; i<all_imgs.length ; i++){
let img = all_imgs[i];
let parent = img.parentElement;
if(parent.className.includes("no-auto")){
continue;
}
// The automatic sizing only applies to the last image
// if there are 2+ images, the user is responsible of setting the size
// of the first images
if(i < (all_imgs.length - 1) && all_imgs[i + 1].parentElement == parent){
continue;
}
let all_elem = parent.children;
let elem_height = 0;
let margin_prev = 0;
let is_span = false;
let display_value_span = "";
for(let j=0 ; j<(all_elem.length - 1) ; j++){
// noe: the last element is always the slide number
let elem = all_elem[j];
if(elem == img) continue;
if(elem.tagName == "H1"){
continue;
}
if(elem.offsetHeight == ""){
// This is a span => tricky
// so far we make the wrong assumption that spans should be treated as divs
// To correct later: include successive spans in a div, then check that div height
is_span = true;
display_value_span = elem.style.display;
elem.style.display = "block";
}
elem_height += parseInt(elem.offsetHeight);
let elem_style = getComputedStyle(elem);
let margin_top = parseInt(elem_style.marginTop)
if(margin_top > margin_prev){
elem_height += margin_top;
} else {
elem_height += margin_prev;
}
margin_prev = parseInt(elem_style.marginBottom);
if(is_span){
is_span = false;
elem.style.display = display_value_span;
}
}
// last increment
elem_height += margin_prev;
// To get the real window height, we need to remove the image first
// The div we will insert later
let my_div = document.createElement("div");
my_div.style.textAlign = "center";
let img_clone = img.cloneNode(true);
// Now the proper height following image removal
parent.replaceChild(my_div, img);
let parent_style = getComputedStyle(parent);
let parent_height = parseInt(parent.clientHeight) - parseInt(parent_style.paddingTop) - parseInt(parent_style.paddingBottom);
let img_height = parent_height - elem_height;
if(img_height < 0){
img_height = 50;
}
// Now we append the saved image if needed
if(img_height < img_clone.height){
my_div.style.height = img_height.toFixed(0).toString() + "px";
}
my_div.appendChild(img_clone);
if(img_clone.height < img_height){
// means width-max has precedence => we don't set the height
my_div.style.height = "";
}
// update
all_imgs = document.querySelectorAll("div.remark-slide-content>img");
i -= 1;
}
}
//
// Autofit text
//
function setup_autofit(){
let all_elem = document.querySelectorAll(".auto-shrink, .auto-fit");
// element that will get the text to compute its width
let div_cont = document.createElement("div");
div_cont.style.padding = "0 0 0 0";
div_cont.style.margin = "0 0 0 0";
div_cont.style.offset = "0 0 0 0";
div_cont.style.border = "unset";
div_cont.style.position = "fixed";
let FONT_MIN = 8;
let FONT_MAX = 500;
let is_fit = false;
let is_smaller = false;
let is_fixed = false;
for(elem of all_elem){
let elem_style = getComputedStyle(elem);
let parent = elem.parentElement;
is_fixed = elem_style.position == "fixed";
let parent_style = getComputedStyle(parent);
let target_width = parseInt(parent.clientWidth)
if(is_fixed){
// weird but it's better to take the parent element
} else {
target_width -= parseInt(parent_style.paddingLeft) +
parseInt(parent_style.paddingRight);
}
let elem_new = elem.cloneNode(true);
elem_new.style.whiteSpace = "nowrap";
elem_new.style.display = "block";
elem_new.style.position = "static";
elem_new.style.width = "auto";
div_cont.appendChild(elem_new);
div_cont.style.position = "fixed";
parent.replaceChild(div_cont, elem);
let current_width = parseInt(div_cont.clientWidth);
is_fit = elem.classList.contains("auto-fit");
is_smaller = current_width < target_width;
if(!is_fit && is_smaller){
parent.replaceChild(elem, div_cont);
div_cont.innerHTML = "";
continue;
}
let sty = getComputedStyle(elem_new);
let fs = parseInt(sty.fontSize);
if(is_fixed && !is_smaller){
// We start from the smallest font
fs = FONT_MIN;
elem_new.style.fontSize = fs + "px";
current_width = parseInt(div_cont.clientWidth);
is_smaller = true;
is_fit = true;
}
// Shrinking (lazy algorithm)
let old_width = current_width;
while(target_width < current_width && fs > FONT_MIN){
fs--;
elem_new.style.fontSize = fs + "px";
current_width = parseInt(div_cont.clientWidth);
if(current_width == old_width){
fs++;
break;
}
old_width = current_width;
}
if(is_fit && is_smaller){
while(target_width > current_width && fs < FONT_MAX){
fs++;
elem_new.style.fontSize = fs + "px";
current_width = parseInt(div_cont.clientWidth);
if(current_width == old_width){
fs++;
break;
}
old_width = current_width;
}
fs--;
}
elem.style.fontSize = fs + "px";
parent.replaceChild(elem, div_cont);
div_cont.innerHTML = "";
}
}
//
// Adding blocks
//
function setup_blocks(){
let all_blocks = document.querySelectorAll(".block, .theorem, .definition");
let brace_open = /(p>|^) *\{/;
let brace_close = /\}/;
for(block of all_blocks){
let my_html = block.innerHTML;
if(my_html.length === 0 || !brace_open.test(my_html) || !brace_close.test(my_html)){
if(block.classList.contains("definition")){
block.classList.remove("definition");
block.classList.add("block");
new_name = "Definition";
} else if(block.classList.contains("theorem")){
block.classList.remove("theorem");
block.classList.add("block");
new_name = "Theorem";
} else {
new_name = "NOT A TITLE";
}
new_html = my_html;
} else {
let pos_end = my_html.search(brace_close);
new_name = my_html.substring(0, pos_end).trim();
let pos_start = new_name.search(/\{/);
new_name = new_name.substring(pos_start + 1, new_name.length);
new_html = "<p>" + my_html.substring(pos_end + 1, my_html.length).trim();
if(! /p> *\{/.test(my_html)){
new_html += "</p>";
}
new_html.replace(/<p>[^<]<\/p>/, "");
}
// we clean the block
block.innerHTML = "";
// And now we recreate its content
let div_block_title = document.createElement("div");
let div_block_content = document.createElement("div");
div_block_title.innerHTML = new_name;
div_block_title.classList.add("block-title");
div_block_content.innerHTML = new_html;
div_block_content.classList.add("block-content");
if(new_name == "NOT A TITLE"){
div_block_content.classList.add("block-no-title");
} else {
block.appendChild(div_block_title);
}
block.appendChild(div_block_content);
}
}
//
// Versus class
//
function setup_versus(){
let all_versus = document.querySelectorAll(".versus");
for(versus of all_versus){
let all_vs = versus.querySelectorAll(".vs");
let n = all_vs.length;
if(n == 0){
versus.innerHTML = "<p style='color:red'>Wrong formatting of the versus class. No vs element found.</p>";
continue;
}
versus = span_to_div(versus);
let vs_container = document.createElement("div");
vs_container.className = versus.className;
vs_container.classList.add("versus-container");
vs_container.classList.remove("versus");
for(let i=0 ; i<n ; i++){
let my_vs_content = document.createElement("div");
my_vs_content.className = all_vs[i].className;
my_vs_content.classList.add("versus-content");
my_vs_content.classList.remove("vs");
my_vs_content.innerHTML = all_vs[i].innerHTML;
vs_container.appendChild(my_vs_content);
if(i < (n-1)){
let my_vs = document.createElement("div");
my_vs.className = "versus-vs";
my_vs.innerHTML = "vs";
vs_container.appendChild(my_vs);
}
}
versus.parentElement.replaceChild(vs_container, versus);
}
}
//
// AUTO CSS
//
// SOURCE: https://gist.github.com/bobspace/2712980
const CSS_COLOR_NAMES = [
"AliceBlue", "AntiqueWhite", "Aqua", "Aquamarine", "Azure", "Beige", "Bisque",
"Black", "BlanchedAlmond", "Blue", "BlueViolet", "Brown", "BurlyWood", "CadetBlue",
"Chartreuse", "Chocolate", "Coral", "CornflowerBlue", "Cornsilk", "Crimson",
"Cyan", "DarkBlue", "DarkCyan", "DarkGoldenRod", "DarkGray", "DarkGrey",
"DarkGreen", "DarkKhaki", "DarkMagenta", "DarkOliveGreen", "DarkOrange",
"DarkOrchid", "DarkRed", "DarkSalmon", "DarkSeaGreen", "DarkSlateBlue",
"DarkSlateGray", "DarkSlateGrey", "DarkTurquoise", "DarkViolet", "DeepPink",
"DeepSkyBlue", "DimGray", "DimGrey", "DodgerBlue", "FireBrick", "FloralWhite",
"ForestGreen", "Fuchsia", "Gainsboro", "GhostWhite", "Gold", "GoldenRod",
"Gray", "Grey", "Green", "GreenYellow", "HoneyDew", "HotPink", "IndianRed",
"Indigo", "Ivory", "Khaki", "Lavender", "LavenderBlush", "LawnGreen",
"LemonChiffon", "LightBlue", "LightCoral", "LightCyan", "LightGoldenRodYellow",
"LightGray", "LightGrey", "LightGreen", "LightPink", "LightSalmon", "LightSeaGreen",
"LightSkyBlue", "LightSlateGray", "LightSlateGrey", "LightSteelBlue", "LightYellow",
"Lime", "LimeGreen", "Linen", "Magenta", "Maroon", "MediumAquaMarine", "MediumBlue",
"MediumOrchid", "MediumPurple", "MediumSeaGreen", "MediumSlateBlue", "MediumSpringGreen",
"MediumTurquoise", "MediumVioletRed", "MidnightBlue", "MintCream", "MistyRose",
"Moccasin", "NavajoWhite", "Navy", "OldLace", "Olive", "OliveDrab", "Orange",
"OrangeRed", "Orchid", "PaleGoldenRod", "PaleGreen", "PaleTurquoise", "PaleVioletRed",
"PapayaWhip", "PeachPuff", "Peru", "Pink", "Plum", "PowderBlue", "Purple",
"RebeccaPurple", "Red", "RosyBrown", "RoyalBlue", "SaddleBrown", "Salmon",
"SandyBrown", "SeaGreen", "SeaShell", "Sienna", "Silver", "SkyBlue", "SlateBlue",
"SlateGray", "SlateGrey", "Snow", "SpringGreen", "SteelBlue", "Tan", "Teal", "Thistle",
"Tomato", "Turquoise", "Violet", "Wheat", "White", "WhiteSmoke", "Yellow", "YellowGreen",
];
const CSS_COLOR_NAMES_LOWER = CSS_COLOR_NAMES.map(element => element.toLowerCase());
function parse_color(color_name){
let i = CSS_COLOR_NAMES_LOWER.indexOf(color_name.toLowerCase());
if(i == -1){