-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathhtmlCommon.ml
1711 lines (1502 loc) · 46.3 KB
/
htmlCommon.ml
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
(***********************************************************************)
(* *)
(* HEVEA *)
(* *)
(* Luc Maranget, projet PARA, INRIA Rocquencourt *)
(* *)
(* Copyright 1998 Institut National de Recherche en Informatique et *)
(* Automatique. Distributed only by permission. *)
(* *)
(***********************************************************************)
(* Output function for a strange html model :
- Text elements can occur anywhere and are given as in latex
- A new grouping construct is given (open_group () ; close_group ())
*)
open Misc
open Element
open MyStack
open Length
open Printf
type block =
| H1 | H2 | H3 | H4 | H5 | H6
| PRE
| TABLE | TR | TD
| DISPLAY of bool | DFLOW
| QUOTE | BLOCKQUOTE
| DIV
| UL | OL | DL
| LI | DD | DT
| GROUP | AFTER | DELAY | FORGET
| INTERN
| P
| NADA
| OTHER of string
let string_of_block = function
| H1 -> "h1"
| H2 -> "h2"
| H3 -> "h3"
| H4 -> "h4"
| H5 -> "h5"
| H6 -> "h6"
| PRE -> "pre"
| TABLE -> "table"
| TR -> "tr"
| TD -> "td"
| DISPLAY false -> "display"
| DISPLAY true -> "display (center)"
| DFLOW -> "dflow"
| QUOTE -> "quote"
| BLOCKQUOTE -> "blockquote"
| DIV -> "div"
| UL -> "ul"
| OL -> "ol"
| DL -> "dl"
| GROUP -> ""
| AFTER -> "after"
| DELAY -> "delay"
| FORGET -> "forget"
| P -> "p"
| NADA -> "nada"
| INTERN -> "intern"
| LI -> "li"
| DD -> "dd"
| DT -> "dt"
| OTHER s -> s
let block_t = Hashtbl.create 17
let no_opt = false
let add b =
Hashtbl.add block_t (string_of_block b) b
and add_verb s b = Hashtbl.add block_t s b
let () =
add H1 ;
add H2 ;
add H3 ;
add H4 ;
add H5 ;
add H6 ;
add PRE ;
add TABLE ;
add TR ;
add TD ;
add (DISPLAY false) ;
add QUOTE ;
add BLOCKQUOTE ;
add DIV ;
add UL ;
add OL ;
add DL ;
begin
if no_opt then
Hashtbl.add block_t "" INTERN
else
add GROUP
end ;
add AFTER ;
add DELAY ;
add FORGET ;
add P ;
add NADA
let failclose s b1 b2=
raise (Misc.Close (s^": '"^string_of_block b1^"' closes '"^
string_of_block b2^"'"))
let find_block s =
let s = String.lowercase_ascii s in
try Hashtbl.find block_t s with
| Not_found -> OTHER s
let eq_tags t1 t2 = match t1, t2 with
| DISPLAY _, DISPLAY _ -> true
| _, _ -> t1=t2
let check_block_closed opentag closetag =
if not (eq_tags opentag closetag) &&
not (opentag = AFTER && closetag = GROUP) then
failclose "html" closetag opentag
let display_arg centering _verbose =
let cl =
if !displayverb then "vdisplay"
else "display" in
let cl =
if centering then
cl^(if !displayverb then " vdcenter" else " dcenter")
else cl in
let arg = "class=\""^cl^"\"" in
arg
(* output globals *)
type t_env = {here : bool ; env : text}
type t_top =
{top_pending : text list ; top_active : t_env list ;}
type style_info =
| Nothing of t_top
| Activate of t_top
| Closed of t_top * int
| ActivateClosed of t_top
| NotMe
| Insert of bool * text list
let get_top_lists = function
| Nothing x -> x | Activate x -> x
| _ -> raise Not_found
let do_pretty_mods stderr f mods =
let rec do_rec stderr = function
[x] -> f stderr x
| x::xs ->
Printf.fprintf stderr "%a; %a" f x do_rec xs
| [] -> () in
Printf.fprintf stderr "[%a]" do_rec mods
let tbool = function
| true -> "+"
| false -> "-"
let pretty_mods stderr = do_pretty_mods stderr
(fun stderr text -> Printf.fprintf stderr "%s" (pretty_text text))
and pretty_tmods stderr =
do_pretty_mods stderr
(fun stderr {here=here ; env = env} ->
Printf.fprintf stderr "%s%s" (tbool here) (pretty_text env))
let pretty_top_styles stderr {top_pending = pending ; top_active = active} =
Printf.fprintf stderr
"{top_pending=%a, top_active=%a}"
pretty_mods pending
pretty_tmods active
let tbool = function
| true -> "+"
| false -> "-"
let pretty_top stderr = function
| Nothing x -> Printf.fprintf stderr "Nothing %a" pretty_top_styles x
| Activate x -> Printf.fprintf stderr "Activate %a" pretty_top_styles x
| Closed _ -> Printf.fprintf stderr "Closed"
| ActivateClosed _ -> Printf.fprintf stderr "ActivateClosed"
| NotMe -> Printf.fprintf stderr "NotMe"
| Insert (b,active) ->
Printf.fprintf stderr "Insert %s %a" (tbool b) pretty_mods active
type status = {
mutable nostyle : bool ;
mutable pending : text list ;
mutable active : t_env list ;
mutable top : style_info ;
mutable out : Out.t}
let as_env {env=env} = env
let as_envs tenvs r =
List.fold_right (fun x r -> as_env x::r) tenvs r
let to_pending pending active = pending @ as_envs active []
let with_new_out out = {out with out = Out.create_buff ()}
let cur_out =
ref {nostyle=false ;
pending = [] ; active = [] ;
top = NotMe ;
out = Out.create_null ()}
type stack_item =
Normal of block * string * status
| Freeze of (unit -> unit)
exception PopFreeze
let push_out s (a,b,c) = push s (Normal (a,b,c))
let pretty_stack s = MyStack.pretty
(function Normal (s,args,_) -> "["^string_of_block s^"]-{"^args^"}"
| Freeze _ -> "Freeze") s
let pop_out s = match pop s with
| Normal (a,b,c) -> a,b,c
| Freeze _ -> raise PopFreeze
and top_out s = match top s with
| Normal (a,b,c) -> a,b,c
| Freeze _ -> raise PopFreeze
let out_stack =
MyStack.create_init "out_stack" (Normal (NADA,"",!cur_out))
type saved_out = status * stack_item MyStack.saved
let save_out () = !cur_out, MyStack.save out_stack
and restore_out (a,b) =
if !cur_out != a then begin
MyStack.finalize out_stack
(function
| Normal (_,_,x) -> x == a
| _ -> false)
(function
| Normal (_,_,_out) -> ()
| _ -> ())
end ;
cur_out := a ;
MyStack.restore out_stack b
let pblock () = match MyStack.top out_stack with
| Normal (s,_,_) -> s
| _ -> NADA
and p2block () = MyStack.top2 out_stack
let do_put_char c =
if !verbose > 3 then
prerr_endline ("put_char: |"^String.escaped (String.make 1 c)^"|");
Out.put_char !cur_out.out c
and do_put s =
if !verbose > 3 then
prerr_endline ("put: |"^String.escaped s^"|");
Out.put !cur_out.out s ;
()
(* Flags section *)
(* Style information for caller *)
type flags_t = {
mutable table_inside:bool;
mutable in_math : bool;
mutable ncols:int;
mutable empty:bool;
mutable blank:bool;
mutable saw_par: bool ;
mutable vsize:int;
mutable nrows:int;
mutable table_vsize:int;
mutable nitems:int;
mutable dt:string;
mutable dcount:string;
mutable in_pre:bool;
mutable insert: (block * string) option;
mutable insert_attr: (block * string) option;
}
let pretty_cur {pending = pending ; active = active ;
top = top} =
Printf.fprintf stderr "pending=%a, active=%a\n"
pretty_mods pending
pretty_tmods active ;
Printf.fprintf stderr "top = %a" pretty_top top ;
prerr_endline ""
let activate_top out = match out.top with
| Nothing x -> out.top <- Activate x
| _ -> ()
and close_top n out = match out.top with
| Nothing top -> out.top <- Closed (top, n+Out.get_pos out.out)
| Activate top -> out.top <- ActivateClosed top
| _ -> ()
let debug_attr stderr = function
| None -> Printf.fprintf stderr "None"
| Some (tag,attr) ->
Printf.fprintf stderr "'%s' '%s'"
(string_of_block tag) attr
let debug_flags f =
Printf.fprintf stderr "attr=%a\n" debug_attr f.insert_attr ;
flush stderr
let flags = {
table_inside = false;
ncols = 0;
in_math = false;
empty = true;
blank = true;
saw_par = false ;
vsize = 0;
nrows = 0;
table_vsize = 0;
nitems = 0;
dt = "";
dcount = "";
in_pre = false;
insert = None;
insert_attr = None;
}
let copy_flags {
table_inside = table_inside;
ncols = ncols;
in_math = in_math;
empty = empty;
blank = blank;
saw_par = saw_par ;
vsize = vsize;
nrows = nrows;
table_vsize = table_vsize;
nitems = nitems;
dt = dt;
dcount = dcount;
in_pre = in_pre;
insert = insert;
insert_attr = insert_attr;
} = {
table_inside = table_inside;
ncols = ncols;
in_math = in_math;
empty = empty;
blank = blank;
saw_par = saw_par ;
vsize = vsize;
nrows = nrows;
table_vsize = table_vsize;
nitems = nitems;
dt = dt;
dcount = dcount;
in_pre = in_pre;
insert = insert;
insert_attr = insert_attr;
}
and set_flags f {
table_inside = table_inside ;
ncols = ncols;
in_math = in_math;
empty = empty;
blank = blank;
saw_par = saw_par ;
vsize = vsize;
nrows = nrows;
table_vsize = table_vsize;
nitems = nitems;
dt = dt;
dcount = dcount;
in_pre = in_pre;
insert = insert;
insert_attr = insert_attr;
} =
f.table_inside <- table_inside;
f.ncols <- ncols;
f.in_math <- in_math;
f.empty <- empty;
f.blank <- blank;
f.saw_par <- saw_par ;
f.vsize <- vsize;
f.nrows <- nrows;
f.table_vsize <- table_vsize;
f.nitems <- nitems;
f.dt <- dt;
f.dcount <- dcount;
f.in_pre <- in_pre;
f.insert <- insert ;
f.insert_attr <- insert_attr ;
(* Independant stacks for flags *)
type stack_t = {
s_table_inside : bool MyStack.t ;
s_saved_inside : bool MyStack.t ;
s_in_math : bool MyStack.t ;
s_ncols : int MyStack.t ;
s_empty : bool MyStack.t ;
s_blank : bool MyStack.t ;
s_saw_par : bool MyStack.t ;
s_vsize : int MyStack.t ;
s_nrows : int MyStack.t ;
s_table_vsize : int MyStack.t ;
s_nitems : int MyStack.t ;
s_dt : string MyStack.t ;
s_dcount : string MyStack.t ;
s_insert : (block * string) option MyStack.t ;
s_insert_attr : (block * string) option MyStack.t ;
(* Other stacks, not corresponding to flags *)
s_active : Out.t MyStack.t ;
s_after : (string -> string) MyStack.t
}
let stacks = {
s_table_inside = MyStack.create "inside" ;
s_saved_inside = MyStack.create "saved_inside" ;
s_in_math = MyStack.create_init "in_math" false ;
s_ncols = MyStack.create "ncols" ;
s_empty = MyStack.create_init "empty" false;
s_blank = MyStack.create_init "blank" false ;
s_saw_par = MyStack.create "saw_par" ;
s_vsize = MyStack.create "vsize" ;
s_nrows = MyStack.create_init "nrows" 0 ;
s_table_vsize = MyStack.create_init "table_vsize" 0 ;
s_nitems = MyStack.create_init "nitems" 0 ;
s_dt = MyStack.create_init "dt" "" ;
s_dcount = MyStack.create_init "dcount" "" ;
s_insert = MyStack.create_init "insert" None;
s_insert_attr = MyStack.create_init "insert_attr" None;
s_active = MyStack.create "Html.active" ;
s_after = MyStack.create "Html.after"
}
type saved_stacks = {
ss_table_inside : bool MyStack.saved ;
ss_saved_inside : bool MyStack.saved ;
ss_in_math : bool MyStack.saved ;
ss_ncols : int MyStack.saved ;
ss_empty : bool MyStack.saved ;
ss_blank : bool MyStack.saved ;
ss_saw_par : bool MyStack.saved ;
ss_vsize : int MyStack.saved ;
ss_nrows : int MyStack.saved ;
ss_table_vsize : int MyStack.saved ;
ss_nitems : int MyStack.saved ;
ss_dt : string MyStack.saved ;
ss_dcount : string MyStack.saved ;
ss_insert : (block * string) option MyStack.saved ;
ss_insert_attr : (block * string) option MyStack.saved ;
(* Other stacks, not corresponding to flags *)
ss_active : Out.t MyStack.saved ;
ss_after : (string -> string) MyStack.saved
}
let save_stacks () =
{
ss_table_inside = MyStack.save stacks.s_table_inside ;
ss_saved_inside = MyStack.save stacks.s_saved_inside ;
ss_in_math = MyStack.save stacks.s_in_math ;
ss_ncols = MyStack.save stacks.s_ncols ;
ss_empty = MyStack.save stacks.s_empty ;
ss_blank = MyStack.save stacks.s_blank ;
ss_saw_par = MyStack.save stacks.s_saw_par ;
ss_vsize = MyStack.save stacks.s_vsize ;
ss_nrows = MyStack.save stacks.s_nrows ;
ss_table_vsize = MyStack.save stacks.s_table_vsize ;
ss_nitems = MyStack.save stacks.s_nitems ;
ss_dt = MyStack.save stacks.s_dt ;
ss_dcount = MyStack.save stacks.s_dcount ;
ss_insert = MyStack.save stacks.s_insert ;
ss_insert_attr = MyStack.save stacks.s_insert_attr ;
ss_active = MyStack.save stacks.s_active ;
ss_after = MyStack.save stacks.s_after
}
and restore_stacks
{
ss_table_inside = saved_table_inside ;
ss_saved_inside = saved_saved_inside ;
ss_in_math = saved_in_math ;
ss_ncols = saved_ncols ;
ss_empty = saved_empty ;
ss_blank = saved_blank ;
ss_saw_par = saved_saw_par ;
ss_vsize = saved_vsize ;
ss_nrows = saved_nrows ;
ss_table_vsize = saved_table_vsize ;
ss_nitems = saved_nitems ;
ss_dt = saved_dt ;
ss_dcount = saved_dcount ;
ss_insert = saved_insert ;
ss_insert_attr = saved_insert_attr ;
ss_active = saved_active ;
ss_after = saved_after
} =
MyStack.restore stacks.s_table_inside saved_table_inside ;
MyStack.restore stacks.s_saved_inside saved_saved_inside ;
MyStack.restore stacks.s_in_math saved_in_math ;
MyStack.restore stacks.s_ncols saved_ncols ;
MyStack.restore stacks.s_empty saved_empty ;
MyStack.restore stacks.s_blank saved_blank ;
MyStack.restore stacks.s_saw_par saved_saw_par ;
MyStack.restore stacks.s_vsize saved_vsize ;
MyStack.restore stacks.s_nrows saved_nrows ;
MyStack.restore stacks.s_table_vsize saved_table_vsize ;
MyStack.restore stacks.s_nitems saved_nitems ;
MyStack.restore stacks.s_dt saved_dt ;
MyStack.restore stacks.s_dcount saved_dcount ;
MyStack.restore stacks.s_insert saved_insert ;
MyStack.restore stacks.s_insert_attr saved_insert_attr ;
MyStack.restore stacks.s_active saved_active ;
MyStack.restore stacks.s_after saved_after
let check_stack what =
if not (MyStack.empty what) && not !silent then begin
prerr_endline
("Warning: stack "^MyStack.name what^" is non-empty in Html.finalize") ;
end
let check_stacks () = match stacks with
{
s_table_inside = s_table_inside ;
s_saved_inside = s_saved_inside ;
s_in_math = s_in_math ;
s_ncols = s_ncols ;
s_empty = s_empty ;
s_blank = s_blank ;
s_saw_par = s_saw_par ;
s_vsize = s_vsize ;
s_nrows = s_nrows ;
s_table_vsize = s_table_vsize ;
s_nitems = s_nitems ;
s_dt = s_dt ;
s_dcount = s_dcount ;
s_insert = s_insert ;
s_insert_attr = s_insert_attr ;
s_active = s_active ;
s_after = s_after
} ->
check_stack s_table_inside ;
check_stack s_saved_inside ;
check_stack s_in_math ;
check_stack s_ncols ;
check_stack s_empty ;
check_stack s_blank ;
check_stack s_saw_par ;
check_stack s_vsize ;
check_stack s_nrows ;
check_stack s_table_vsize ;
check_stack s_nitems ;
check_stack s_dt ;
check_stack s_dcount ;
check_stack s_insert ;
check_stack s_insert_attr ;
check_stack s_active ;
check_stack s_after
(*
Full state saving
*)
type saved = flags_t * saved_stacks * saved_out
let check () =
let saved_flags = copy_flags flags
and saved_stacks = save_stacks ()
and saved_out = save_out () in
saved_flags, saved_stacks, saved_out
and hot (f,s,o) =
set_flags flags f ;
restore_stacks s ;
restore_out o
let sbool = function true -> "true" | _ -> "false"
let prerr_flags s =
prerr_endline ("<"^string_of_int (MyStack.length stacks.s_empty)^"> "^s^
" empty="^sbool flags.empty^
" blank="^sbool flags.blank^
" table="^sbool flags.table_inside)
let is_header = function
| H1 | H2 | H3 | H4 | H5 | H6 -> true
| _ -> false
let is_list = function
UL | DL | OL -> true
| _ -> false
let string_of_into = function
| Some n -> "+"^string_of_int n
| None -> "-"
(* styles *)
let trim_quotes s =
(* used for ensuring colors are passed without "" *)
let (i, l) =
if s.[0] = '"' then (1, String.length s - 2) else (0, String.length s)
in String.sub s i l
(* '"' *)
let size_html5 = function
| 1 -> "xx-small"
| 2 -> "small"
| 3 -> "medium"
| 4 -> "large"
| 5 -> "x-large"
| 6 -> "xx-large"
| 7 -> "xx-large"
| _ -> raise (Misc.fatal "size_html5")
let do_close_mod = function
| Style m ->
if flags.in_math && !Parse_opts.mathml then
if m="mtext" then do_put ("</"^m^">")
else do_put "</mstyle>"
else do_put ("</"^m^">")
| StyleAttr (t,_) ->
if flags.in_math && !Parse_opts.mathml then
()
else
do_put ("</"^t^">")
| (Color _ | Font _) ->
if flags.in_math && !Parse_opts.mathml then
do_put "</mstyle>"
else do_put "</span>"
and do_open_mod e =
if !verbose > 3 then
prerr_endline ("do_open_mod: "^pretty_text e) ;
match e with
| Style m ->
if flags.in_math && !Parse_opts.mathml then
if m="mtext" then do_put ("<"^m^">")
else do_put ("<mstyle style = \""^
(match m with
"b" -> "font-weight: bold "
| "i" -> "font-style: italic "
| "tt" -> "font-family: monospace "
| "em" -> "font-style: italic "
| _ -> m)^
"\">")
else do_put ("<"^m^">")
| StyleAttr (t,a) ->
if flags.in_math && !Parse_opts.mathml then ()
else
do_put ("<"^t^" "^a^">")
| Font i ->
if flags.in_math && !Parse_opts.mathml then
do_put ("<mstyle style = \"font-size: "^string_of_int i^"\">")
else
(* Convert size from 1 to 7 in percentage, following standard *)
do_put (sprintf "<span style=\"font-size:%s\">" (size_html5 i))
| Color s ->
if flags.in_math && !Parse_opts.mathml then
do_put ("<mstyle style = \"color: "^s^"\">")
else do_put (sprintf "<span style=\"color:%s\">" (trim_quotes s))
let do_close_tmod = function
| {here = true ; env = env} -> do_close_mod env
| _ -> ()
let close_active_mods active = List.iter do_close_tmod active
let do_close_mods () =
close_active_mods !cur_out.active ;
!cur_out.active <- [] ;
!cur_out.pending <- []
let do_close_mods_pred pred same_constr =
let tpred {env=env} = pred env in
let rec split_again = function
| [] -> [],None,[]
| {here = false ; env=env} :: rest
when same_constr env && not (pred env) ->
[],Some env,rest
| m :: rest ->
let to_close,to_open,to_keep = split_again rest in
match to_open with
| Some _ -> m::to_close,to_open,to_keep
| None -> to_close,to_open,m::to_keep in
let rec split = function
| [] -> [],None,[]
| m :: rest ->
let to_close,close,to_keep = split rest in
match close with
| None ->
if tpred m then
if m.here then [],Some m.env,to_keep
else
[],None,to_keep
else [], None, m::to_keep
| Some _ ->
m::to_close,close,to_keep in
let rec filter_pred = function
| [] -> []
| x :: rest ->
if pred x then filter_pred rest
else x::filter_pred rest in
let to_close,close,to_keep = split !cur_out.active in
filter_pred
(match close with
| None -> []
| Some env ->
List.iter do_close_tmod to_close ;
do_close_mod env ;
let (to_close_open,to_open,to_keep) = split_again to_keep in
begin match to_open with
| None ->
!cur_out.active <- to_keep ;
as_envs to_close []
| Some env ->
!cur_out.active <- to_keep ;
List.iter do_close_tmod to_close_open ;
as_envs to_close
(as_envs to_close_open [env])
end),
close
let close_mods () = do_close_mods ()
let is_style = function
| Style _|StyleAttr (_,_) -> true
| _ -> false
and is_font = function
Font _ -> true
| _ -> false
and is_color = function
Color _ -> true
| _ -> false
let do_open_these_mods do_open_mod pending =
let rec do_rec color size = function
| [] -> []
| Color _ as e :: rest ->
if color then
let rest = do_rec true size rest in
{here=false ; env=e}::rest
else begin
let rest = do_rec true size rest in
do_open_mod e ;
{here=true ; env=e}::rest
end
| Font _ as e :: rest ->
if size then
let rest = do_rec color true rest in
{here=false ; env=e}::rest
else
let rest = do_rec color true rest in
do_open_mod e ;
{here=true ; env=e}::rest
| e :: rest ->
let rest = do_rec color size rest in
do_open_mod e ;
{here=true ; env=e} :: rest in
do_rec
false
false
pending
let activate caller pending =
let r = do_open_these_mods (fun _ -> ()) pending in
if !verbose > 2 then begin
prerr_string ("activate: ("^caller^")") ;
pretty_mods stderr pending ; prerr_string " -> " ;
pretty_tmods stderr r ;
prerr_endline ""
end ;
r
let get_top_active = function
| Nothing {top_active = active} -> active
| Activate {top_pending = pending ; top_active = active} ->
activate "get_top_active" pending @ active
| _ -> []
let all_to_pending out =
try
let top = get_top_lists out.top in
to_pending out.pending out.active @
to_pending top.top_pending top.top_active
with
| Not_found ->
to_pending out.pending out.active
let all_to_active out = activate "all_to_active" (all_to_pending out)
(* Clear styles *)
let clearstyle () =
close_active_mods !cur_out.active ;
close_active_mods (get_top_active !cur_out.top) ;
close_top 0 !cur_out ;
!cur_out.pending <- [] ;
!cur_out.active <- []
(* Avoid styles *)
let nostyle () =
clearstyle () ;
!cur_out.nostyle <- true
(* Create new statuses, with appropriate pending lists *)
let create_status_from_top out = match out.top with
| NotMe|Closed _|ActivateClosed _|Insert (_,_) ->
{nostyle=out.nostyle ; pending = [] ; active = [] ;
top =
Nothing
{top_pending = out.pending ; top_active = out.active} ;
out = Out.create_buff ()}
| Nothing {top_pending = top_pending ; top_active=top_active} ->
assert (out.active=[]) ;
{nostyle=out.nostyle ; pending = [] ; active = [] ;
top =
Nothing
{top_pending = out.pending @ top_pending ;
top_active = top_active} ;
out = Out.create_buff ()}
| Activate {top_pending = top_pending ; top_active=top_active} ->
{nostyle=out.nostyle ; pending = [] ; active = [] ;
top=
Nothing
{top_pending = out.pending ;
top_active = out.active @ activate "top" top_pending @ top_active} ;
out=Out.create_buff ()}
let create_status_from_scratch nostyle pending =
{nostyle=nostyle ;
pending =pending ; active = [] ;
top=NotMe ;
out = Out.create_buff ()}
let do_open_mods () =
if !verbose > 2 then begin
prerr_string "=> do_open_mods: " ;
pretty_cur !cur_out
end ;
let now_active =
do_open_these_mods do_open_mod !cur_out.pending in
activate_top !cur_out ;
!cur_out.active <- now_active @ !cur_out.active ;
!cur_out.pending <- [] ;
if !verbose > 2 then begin
prerr_string "<= do_open_mods: " ;
pretty_cur !cur_out
end
let do_pending () = do_open_mods ()
let one_cur_size pending active =
let rec cur_size_active = function
| [] -> raise Not_found
| {here=true ; env=Font i}::_ -> i
| _::rest -> cur_size_active rest in
let rec cur_size_pending = function
| [] -> cur_size_active active
| Font i::_ -> i
| _::rest -> cur_size_pending rest in
cur_size_pending pending
let cur_size out =
try one_cur_size out.pending out.active
with Not_found ->
try
let top_out = get_top_lists out.top in
one_cur_size top_out.top_pending top_out.top_active
with Not_found -> 3
let one_first_same x same_constr pending active =
let rec same_active = function
| {here=true ; env=y} :: rest ->
if same_constr y then x=y
else same_active rest
| _::rest -> same_active rest
| [] -> raise Not_found in
let rec same_pending = function
| [] -> same_active active
| y::rest ->
if same_constr y then x=y
else same_pending rest in
same_pending pending
let first_same x same_constr out =
try
one_first_same x same_constr out.pending out.active
with Not_found ->
try
let top_out = get_top_lists out.top in
one_first_same x same_constr top_out.top_pending top_out.top_active
with
| Not_found -> false
let already_here = function
| Font i ->
i = cur_size !cur_out
| x ->
first_same x
(match x with
Style _|StyleAttr (_,_) -> is_style
| Font _ -> is_font
| Color _ -> is_color)
!cur_out
let ok_pre x = match x with
| Color _ | Font _ | Style "sub" | Style "sup" -> not !Parse_opts.pedantic
| _ -> true
let rec filter_pre = function
[] -> []
| e::rest ->
if ok_pre e then e::filter_pre rest
else filter_pre rest
let ok_mod e =
(not flags.in_pre || ok_pre e) &&
(not (already_here e))
let get_fontsize () = cur_size !cur_out
let rec erase_rec pred = function
[] -> None
| s::rest ->
if pred s then
Some rest
else
match erase_rec pred rest with
| Some rest -> Some (s::rest)
| None -> None
let erase_mod_pred pred same_constr =
if not !cur_out.nostyle then begin
match erase_rec pred !cur_out.pending with
| Some pending ->
!cur_out.pending <- pending
| None ->
let re_open,closed = do_close_mods_pred pred same_constr in
match closed with
| Some _ ->
!cur_out.pending <- !cur_out.pending @ re_open
| None ->
activate_top !cur_out ;
try
let tops = get_top_lists !cur_out.top in
!cur_out.active <-
!cur_out.active @
activate "erase" tops.top_pending @
tops.top_active ;
close_top 0 !cur_out ;
let re_open,_ = do_close_mods_pred pred same_constr in
!cur_out.pending <- !cur_out.pending @ re_open
with
| Not_found -> ()