File: foot.ml

package info (click to toggle)
hevea 2.32-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,692 kB
  • sloc: ml: 19,109; sh: 493; makefile: 301; ansic: 132
file content (126 lines) | stat: -rw-r--r-- 3,482 bytes parent folder | download | duplicates (4)
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
(***********************************************************************)
(*                                                                     *)
(*                          HEVEA                                      *)
(*                                                                     *)
(*  Luc Maranget, projet PARA, INRIA Rocquencourt                      *)
(*                                                                     *)
(*  Copyright 1998 Institut National de Recherche en Informatique et   *)
(*  Automatique.  Distributed only by permission.                      *)
(*                                                                     *)
(***********************************************************************)

let some = ref false
;;


let anchor = ref 0
;;

let mark_to_anchor = Hashtbl.create 17
and anchor_to_note = Hashtbl.create 17
;;

let fst_stack = MyStack.create_init "fst_stack" 0
and some_stack = MyStack.create "some_stack"

type saved =
    (int, int) Hashtbl.t
  * (int, int * string * string) Hashtbl.t * int * bool
  * int MyStack.saved * bool MyStack.saved


let checkpoint () =
  Hashtbl.copy mark_to_anchor,
  Hashtbl.copy anchor_to_note,
  !anchor, !some,
  MyStack.save fst_stack, MyStack.save some_stack

and hot_start (t1,t2,i,b,fst_saved,some_saved) =
  Misc.copy_int_hashtbl t1 mark_to_anchor ;
  Misc.copy_int_hashtbl t2 anchor_to_note ;
  anchor := i ;
  some := b ;
  MyStack.restore fst_stack fst_saved ;
  MyStack.restore some_stack some_saved ;
  ()

let step_anchor mark =
  incr anchor ;
  Hashtbl.remove mark_to_anchor mark ;
  Hashtbl.add mark_to_anchor mark !anchor
;;

let get_anchor mark =
  let r =
    try Hashtbl.find mark_to_anchor mark
    with Not_found -> begin
      step_anchor mark ;
      !anchor
    end in
  r
;;
  
let register mark themark text =
  some := true ;
  let anchor = get_anchor mark in
  begin try
    let _ = Hashtbl.find anchor_to_note anchor in    
    Misc.warning "erasing previous footnote" ;
    Hashtbl.remove  anchor_to_note anchor
  with Not_found -> ()
  end ;
  Hashtbl.add anchor_to_note anchor (mark,themark,text)
;;


let sub_notes () =
  MyStack.push fst_stack !anchor ;
  MyStack.push some_stack !some ;
  some := false


let flush sticky lexer out sec_notes sec_here =
  if !some && Section.value sec_here <= Section.value sec_notes then begin
(*
    Misc.warning
      (Printf.sprintf "NOTES %s (%s)" sec_here sec_notes) ;
*)
    some := false ;
    let fst = MyStack.top fst_stack in
    lexer
      ("\\begin{thefootnotes}" ^
       (if sticky then "[STICKY]" else "") ^
       "{"^sec_here^"}") ;
    let all = ref [] in
    Hashtbl.iter
      (fun anchor (mark,themark,text) ->
        if anchor > fst then
        all := ((mark,anchor),(themark,text)) :: !all)
      anchor_to_note ;
    all := List.sort
        (fun (((m1:int),(a1:int)),_) ((m2,a2),_) -> match Pervasives.compare a1 a2 with
        | 0 ->  Pervasives.compare m1 m2
        | r -> r) !all ;
    List.iter
      (fun ((_,anchor),(themark,text)) ->
        lexer
          ("\\item["^
           "\\@noteref{text}{note}{"^
           string_of_int anchor^
           "}{\\@print{"^themark^"}}]") ;
        out text)
      !all ;
    lexer "\\end{thefootnotes}" ;
    List.iter
      (fun ((m,a),_) ->
        Hashtbl.remove mark_to_anchor m ;
        Hashtbl.remove anchor_to_note a)
      !all
  end
;;

let end_notes () =
  some := MyStack.pop some_stack ;
  ignore (MyStack.pop fst_stack)