1 (***********************************************************************)
4 (* Maxence Guesdon, projet Cristal, INRIA Rocquencourt *)
6 (* Copyright 2001 Institut National de Recherche en Informatique et *)
7 (* en Automatique. All rights reserved. This file is distributed *)
8 (* under the terms of the Q Public License version 1.0. *)
10 (***********************************************************************)
14 exception Text_syntax
of int * int * string (* line, char, string *)
20 (* builds a text structure from a string. *)
21 let text_of_string s
=
22 let lexbuf = Lexing.from_string s
in
24 Odoc_text_lexer.init
();
25 Odoc_text_parser.main
Odoc_text_lexer.main
lexbuf
28 raise
(Text_syntax
(!Odoc_text_lexer.line_number
,
29 !Odoc_text_lexer.char_number
,
35 for i
= 0 to String.length s
- 1 do
36 if s
.[i
] = c
then incr
count
42 let len = String.length s
in
43 let b = Buffer.create
(len + n
) in
44 for i
= 0 to len - 1 do
45 if s
.[i
] = c
&& !remain > 0 then
47 Printf.bprintf
b "\\%c" c
;
51 Buffer.add_char
b s
.[i
]
56 let open_brackets = count s '
['
in
57 let close_brackets = count s '
]'
in
58 if open_brackets > close_brackets then
59 escape_n s '
['
(open_brackets - close_brackets)
61 if close_brackets > open_brackets then
62 escape_n s '
]'
(close_brackets - open_brackets)
67 let len = String.length s
in
68 let b = Buffer.create
len in
69 for i
= 0 to len - 1 do
71 '
['
| '
]'
| '
{'
| '
}'
->
72 Printf.bprintf
b "\\%c" s
.[i
]
78 let p = Printf.bprintf
81 List.iter
(p_text_element
b) t
85 (fun t
-> p b "{- " ; p_text b t
; p b "}\n")
88 and p_text_element
b = function
89 | Raw s
-> p b "%s" (escape_raw s
)
90 | Code s
-> p b "[%s]" (escape_code s
)
91 | CodePre s
-> p b "{[%s]}" s
92 | Verbatim s
-> p b "{v %s v}" s
93 | Bold t
-> p b "{b " ; p_text b t
; p b "}"
94 | Italic t
-> p b "{i " ; p_text b t
; p b "}"
95 | Emphasize t
-> p b "{e " ; p_text b t
; p b "}"
96 | Center t
-> p b "{C " ; p_text b t
; p b "}"
97 | Left t
-> p b "{L " ; p_text b t
; p b "}"
98 | Right t
-> p b "{R " ; p_text b t
; p b "}"
99 | List l
-> p b "{ul\n"; p_list
b l
; p b "}"
100 | Enum l
-> p b "{ol\n"; p_list
b l
; p b "}"
101 | Newline
-> p b "\n"
102 | Block t
-> p_text b t
103 | Title
(n
, l_opt
, t
) ->
112 | Latex s
-> p b "{%% %s%%}" s
121 let sk = match k
with
122 RK_module
-> "module"
123 | RK_module_type
-> "modtype"
124 | RK_class
-> "class"
125 | RK_class_type
-> "classtype"
128 | RK_exception
-> "exception"
129 | RK_attribute
-> "attribute"
130 | RK_method
-> "method"
131 | RK_section _
-> "section"
135 | Superscript t
-> p b "{^" ; p_text b t
; p b "}"
136 | Subscript t
-> p b "{_" ; p_text b t
; p b "}"
139 List.iter
(fun s
-> p b " %s" s
) l
;
148 let string_of_text s
=
149 let b = Buffer.create
256 in