From 31e2e5e10720e058292008c658dbf17bcbacac43 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Wed, 23 Mar 2022 20:23:46 +0100 Subject: [PATCH] idented with ocp-ident --- lib/glossary.ml | 18 ++-- lib/htmlgen.ml | 242 ++++++++++++++++++++++++------------------------ lib/mathgen.ml | 106 ++++++++++----------- lib/parser.ml | 224 ++++++++++++++++++++++---------------------- lib/utils.ml | 10 +- 5 files changed, 300 insertions(+), 300 deletions(-) diff --git a/lib/glossary.ml b/lib/glossary.ml index 9f8ee60..b33c4ea 100644 --- a/lib/glossary.ml +++ b/lib/glossary.ml @@ -71,15 +71,15 @@ let total_glossaries file = (** Transform the glossary to an HTML file *) let prints_glossary () = if not @@ glossary_provided () then "" else - let line = "

\n>" in - let line = line^"

Glossary

" in - let rec aux result (entries) = - match entries with - | [] -> result - | (desc,(name,def))::q -> let l = "\t
\n\t\t

" ^ name ^ "

\n\t\t

" ^ def ^ "

\n\t
\n" - in aux (result^l) q - in let line = aux line (List.of_seq (Hashtbl.to_seq glossaries)) - in line^"
\n";; + let line = "

\n>" in + let line = line^"

Glossary

" in + let rec aux result (entries) = + match entries with + | [] -> result + | (desc,(name,def))::q -> let l = "\t
\n\t\t

" ^ name ^ "

\n\t\t

" ^ def ^ "

\n\t
\n" + in aux (result^l) q + in let line = aux line (List.of_seq (Hashtbl.to_seq glossaries)) + in line^"
\n";; let recognize_gls name = try diff --git a/lib/htmlgen.ml b/lib/htmlgen.ml index 560a065..4ab8fd4 100644 --- a/lib/htmlgen.ml +++ b/lib/htmlgen.ml @@ -8,60 +8,60 @@ let print_table_of_content ast min_chap = let count = [|1;1;1;1|] in let rec aux acc ast = match ast with - | [] -> acc - | Chapter (s,l)::q -> - let chapnum = count.(0) in - begin - count.(0) <- count.(0) + 1; - count.(1) <- 1; - count.(2) <- 1; - count.(3) <- 1; - end; - let str = aux "" l in - let new_line = if chapnum>=min_chap then Printf.sprintf "
  • Chapter %i : %s
  • \n" - chapnum (chapnum-min_chap+1) s else "" in - aux (acc^new_line^str) q - - | Section (s,l)::q -> - let chapnum,secnum = count.(0),count.(1) in - begin - count.(1) <- count.(1) + 1; - count.(2) <- 1; - count.(3) <- 1; - end; - let str = aux "" l in - let new_line = Printf.sprintf "
  • Section %i.%i : %s
  • \n" + | [] -> acc + | Chapter (s,l)::q -> + let chapnum = count.(0) in + begin + count.(0) <- count.(0) + 1; + count.(1) <- 1; + count.(2) <- 1; + count.(3) <- 1; + end; + let str = aux "" l in + let new_line = if chapnum>=min_chap then Printf.sprintf "
  • Chapter %i : %s
  • \n" + chapnum (chapnum-min_chap+1) s else "" in + aux (acc^new_line^str) q + + | Section (s,l)::q -> + let chapnum,secnum = count.(0),count.(1) in + begin + count.(1) <- count.(1) + 1; + count.(2) <- 1; + count.(3) <- 1; + end; + let str = aux "" l in + let new_line = Printf.sprintf "
  • Section %i.%i : %s
  • \n" (2.**(float chapnum)*.3.**(float secnum)) (chapnum-min_chap+1) secnum s in - aux (acc^new_line^str) q - - | Subsection (s,l)::q -> - let chapnum,secnum,ssecnum = count.(0),count.(1),count.(2) in - begin - count.(2) <- count.(2) + 1; - count.(3) <- 1; - end; - let str = aux "" l in - let new_line = Printf.sprintf "
  • Subsection %i.%i.%i : %s
  • \n" - (2.**(float chapnum)*.3.**(float secnum)*.5.**(float ssecnum)) (chapnum-min_chap+1) secnum ssecnum s in - aux (acc^new_line^str) q - - | Subsubsection (s,l)::q -> - let chapnum,secnum,ssecnum,sssecnum = count.(0),count.(1),count.(2),count.(3) in - begin - count.(3) <- count.(3) + 1; - end; - let str = aux "" l in - let new_line = Printf.sprintf "
  • Subsubsection %i.%i.%i.%i : %s
  • \n" - (2.**(float chapnum)*.3.**(float secnum)*.5.**(float ssecnum)*.7.**(float sssecnum)) (chapnum-min_chap+1) secnum ssecnum sssecnum s in - aux (acc^new_line^str) q - | Env (_,l)::q -> let a = aux acc l in aux (acc^a) q - | _::q -> aux acc q + aux (acc^new_line^str) q + + | Subsection (s,l)::q -> + let chapnum,secnum,ssecnum = count.(0),count.(1),count.(2) in + begin + count.(2) <- count.(2) + 1; + count.(3) <- 1; + end; + let str = aux "" l in + let new_line = Printf.sprintf "
  • Subsection %i.%i.%i : %s
  • \n" + (2.**(float chapnum)*.3.**(float secnum)*.5.**(float ssecnum)) (chapnum-min_chap+1) secnum ssecnum s in + aux (acc^new_line^str) q + + | Subsubsection (s,l)::q -> + let chapnum,secnum,ssecnum,sssecnum = count.(0),count.(1),count.(2),count.(3) in + begin + count.(3) <- count.(3) + 1; + end; + let str = aux "" l in + let new_line = Printf.sprintf "
  • Subsubsection %i.%i.%i.%i : %s
  • \n" + (2.**(float chapnum)*.3.**(float secnum)*.5.**(float ssecnum)*.7.**(float sssecnum)) (chapnum-min_chap+1) secnum ssecnum sssecnum s in + aux (acc^new_line^str) q + | Env (_,l)::q -> let a = aux acc l in aux (acc^a) q + | _::q -> aux acc q in (aux "" ast);; - let parse_to_html ?(min_chap=1) write_before ast= +let parse_to_html ?(min_chap=1) write_before ast= let count = [|1;1;1;1|] in let rec aux ?(write=write_before) acc ast = - match ast with + match ast with | [] -> acc | Nul::q -> aux acc q | Line s::q -> @@ -74,65 +74,65 @@ let print_table_of_content ast min_chap = in aux ~write:write (acc^line) q | AtomicCmd (s,_)::q -> let new_line = (match s with - | "par" -> "
    \n" - | "bigskip" -> "

    \n\n

    \n" - | "\\" -> "
    \n" - | "printglossaries" -> "" - | "sep" -> "

    ***
    " - | "item" -> "·" - | "newline" -> "
    \n" - | "ast" -> "*" - | e -> - (try - let structure = Hashtbl.find commands e in - let str = aux ~write:write acc structure - in str - with _ -> "")) + | "par" -> "
    \n" + | "bigskip" -> "

    \n\n

    \n" + | "\\" -> "
    \n" + | "printglossaries" -> "" + | "sep" -> "

    ***
    " + | "item" -> "·" + | "newline" -> "
    \n" + | "ast" -> "*" + | e -> + (try + let structure = Hashtbl.find commands e in + let str = aux ~write:write acc structure + in str + with _ -> "")) in let new_acc = if write then acc^new_line^"\n" else "" in aux ~write:write new_acc q - + | OneArgCmd (s,_,l)::q -> let str = aux "" l in let new_line = (match s with - | "par" -> "
    \n" - | "bigskip" -> "

    \n\n

    \n" - | "\\" -> "
    \n" - | "printglossaries" -> "" - | "item" -> "·" - | "sep" -> "

    ***
    " - | "newline" -> "
    \n" - | "ast" -> "*" - | "gls" -> + | "par" -> "
    \n" + | "bigskip" -> "

    \n\n

    \n" + | "\\" -> "
    \n" + | "printglossaries" -> "" + | "item" -> "·" + | "sep" -> "

    ***
    " + | "newline" -> "
    \n" + | "ast" -> "*" + | "gls" -> (match l with - | [] -> "" - | Line s::_ -> - let name,_ = recognize_gls s in Printf.sprintf "%s " s name - | _::_ -> "") - | "textit" -> (Printf.sprintf "%s" str) - | "textbf" -> (Printf.sprintf "%s" str) - | "url" -> (Printf.sprintf "%s" (Str.global_replace (Str.regexp "\n") "" str) str) - | e -> - (try - let structure = Hashtbl.find commands e in - let str = aux ~write:write acc structure - in str - with _ -> "")) + | [] -> "" + | Line s::_ -> + let name,_ = recognize_gls s in Printf.sprintf "%s " s name + | _::_ -> "") + | "textit" -> (Printf.sprintf "%s" str) + | "textbf" -> (Printf.sprintf "%s" str) + | "url" -> (Printf.sprintf "%s" (Str.global_replace (Str.regexp "\n") "" str) str) + | e -> + (try + let structure = Hashtbl.find commands e in + let str = aux ~write:write acc structure + in str + with _ -> "")) in let new_acc = if write then acc^(new_line) else "" in aux ~write:write new_acc q - + | Chapter (s,l)::q -> let chapnum = count.(0) in - begin - count.(0) <- count.(0) + 1; - count.(1) <- 1; - count.(2) <- 1; - count.(3) <- 1; - end; - let str = aux ~write:(chapnum>=min_chap) "" l in - let new_line = if chapnum>=min_chap then Printf.sprintf "

    Chapter %i : %s


    \n" + begin + count.(0) <- count.(0) + 1; + count.(1) <- 1; + count.(2) <- 1; + count.(3) <- 1; + end; + let str = aux ~write:(chapnum>=min_chap) "" l in + let new_line = if chapnum>=min_chap then Printf.sprintf "

    Chapter %i : %s


    \n" chapnum (chapnum-min_chap+1) s else "" in - aux ~write:write (acc^new_line^str) q - + aux ~write:write (acc^new_line^str) q + | Section (s,l)::q -> let chapnum,secnum = count.(0),count.(1) in begin @@ -142,9 +142,9 @@ let print_table_of_content ast min_chap = end; let str = aux ~write:write "" l in let new_line = Printf.sprintf "

    Section %i.%i : %s


    \n" - (2.**(float chapnum)*.3.**(float secnum)) (chapnum-min_chap+1) secnum s in + (2.**(float chapnum)*.3.**(float secnum)) (chapnum-min_chap+1) secnum s in aux ~write:write (acc^new_line^str) q - + | Subsection (s,l)::q -> let chapnum,secnum,ssecnum = count.(0),count.(1),count.(2) in begin @@ -153,9 +153,9 @@ let print_table_of_content ast min_chap = end; let str = aux ~write:write "" l in let new_line = Printf.sprintf "

    Subsection %i.%i.%i : %s


    \n" - (2.**(float chapnum)*.3.**(float secnum)*.5.**(float ssecnum)) (chapnum-min_chap+1) secnum ssecnum s in + (2.**(float chapnum)*.3.**(float secnum)*.5.**(float ssecnum)) (chapnum-min_chap+1) secnum ssecnum s in aux ~write:write (acc^new_line^str) q - + | Subsubsection (s,l)::q -> let chapnum,secnum,ssecnum,sssecnum = count.(0),count.(1),count.(2),count.(3) in begin @@ -163,15 +163,15 @@ let print_table_of_content ast min_chap = end; let str = aux ~write:write "" l in let new_line = Printf.sprintf "

    Subsubsection %i.%i.%i.%i : %s


    \n" - (2.**(float chapnum)*.3.**(float secnum)*.5.**(float ssecnum)*.7.**(float sssecnum)) (chapnum-min_chap+1) secnum ssecnum sssecnum s in + (2.**(float chapnum)*.3.**(float secnum)*.5.**(float ssecnum)*.7.**(float sssecnum)) (chapnum-min_chap+1) secnum ssecnum sssecnum s in aux ~write:write (acc^new_line^str) q - + | Env (s,l)::q -> let str = aux ~write:write "" l in let new_line = (match s with - | "document" -> str - | "center" -> Printf.sprintf "
    \n%s\n
    " str - | _ -> str) + | "document" -> str + | "center" -> Printf.sprintf "
    \n%s\n
    " str + | _ -> str) in aux ~write:write (acc^new_line^"\n") q | _::q -> aux acc q in aux "" ast;; @@ -193,21 +193,21 @@ let prepare_body name str toc = in let line = line ^ "" in line;; - - let pre_parse_file file = - let str = read_file file in - let str = String.concat "\n" str in - let a = parse_string str - in let p,doc = separate_preamble a - in read_preamble p; - let doc = separate_sections doc - in let doc = calculate_environments doc - in let doc = Mathgen.re_calculate_env doc - in - (match (Hashtbl.find_opt preamble "glossary") with - | Some s -> init_glossary s - | None -> ();); - doc;; + +let pre_parse_file file = + let str = read_file file in + let str = String.concat "\n" str in + let a = parse_string str + in let p,doc = separate_preamble a + in read_preamble p; + let doc = separate_sections doc + in let doc = calculate_environments doc + in let doc = Mathgen.re_calculate_env doc + in + (match (Hashtbl.find_opt preamble "glossary") with + | Some s -> init_glossary s + | None -> ();); + doc;; let print_file_in_html ?(min_chap=1) ?(write_before=false) file outname = diff --git a/lib/mathgen.ml b/lib/mathgen.ml index def350d..3587f16 100644 --- a/lib/mathgen.ml +++ b/lib/mathgen.ml @@ -5,69 +5,69 @@ let generate_latex_command s l = let args = String.concat "," l in let line = if args="" then line else Printf.sprintf "%s[%s]" line args in line;; - + let generate_latex l = let rec unparse acc l = match l with - | [] -> String.concat " " acc - | Line s::q -> unparse (s::acc) q - | AtomicCmd (s,l)::q -> - let line = generate_latex_command s l in - unparse (line::acc) q - | OneArgCmd (s,l,l2)::q -> - let line = generate_latex_command s l in - let line = Printf.sprintf "%s{%s}" line (unparse [] l2) in - unparse (line::acc) q - | MultipleArgCmd (s,l,l2)::q -> - let line = generate_latex_command s l in - let l = List.map (unparse []) l2 in - let line = Printf.sprintf "%s{%s}" line (String.concat "\n" l) in - unparse (line::acc) q - | _::q -> unparse acc q - in unparse [] l;; + | [] -> String.concat " " acc + | Line s::q -> unparse (s::acc) q + | AtomicCmd (s,l)::q -> + let line = generate_latex_command s l in + unparse (line::acc) q + | OneArgCmd (s,l,l2)::q -> + let line = generate_latex_command s l in + let line = Printf.sprintf "%s{%s}" line (unparse [] l2) in + unparse (line::acc) q + | MultipleArgCmd (s,l,l2)::q -> + let line = generate_latex_command s l in + let l = List.map (unparse []) l2 in + let line = Printf.sprintf "%s{%s}" line (String.concat "\n" l) in + unparse (line::acc) q + | _::q -> unparse acc q + in unparse [] l;; let env_de_latexer env = match env with - | e -> e;; + | e -> e;; let re_calculate_env ast = let rec aux acc ast = match ast with - | [] -> acc - | Env (s,n)::q when s="align" - -> aux (Math(Printf.sprintf "\\begin{align}%s\\end{align}" (generate_latex n))::acc) q - | Env (s,n)::q when s="align*" - -> aux (Math(Printf.sprintf "\\begin{align*}%s\\end{align*}" (generate_latex n))::acc) q - | Env (s,n)::q when s="equation" - -> aux (Math(Printf.sprintf "\\begin{equation}%s\\end{equation}" (generate_latex n))::acc) q - | Env (s,n)::q when s="equation*" - -> aux (Math(Printf.sprintf "\\begin{equation*}%s\\end{equation*}" (generate_latex n))::acc) q - | Env(s,n)::q - -> let ast = aux [] n in - let ast = List.rev ast - in let env = Env(s,ast) - in aux (env::acc) q - | Chapter (s,l)::q - -> let ast = aux [] l in - let ast = List.rev ast - in let c = Chapter(s,ast) - in aux (c::acc) q - | Section (s,l)::q - -> let ast = aux [] l in - let ast = List.rev ast - in let c = Section(s,ast) - in aux (c::acc) q - | Subsection (s,l)::q - -> let ast = aux [] l in - let ast = List.rev ast - in let c = Subsection(s,ast) - in aux (c::acc) q - | Subsubsection (s,l)::q - -> let ast = aux [] l in - let ast = List.rev ast - in let c = Subsubsection(s,ast) - in aux (c::acc) q - | e::q -> aux (e::acc) q + | [] -> acc + | Env (s,n)::q when s="align" + -> aux (Math(Printf.sprintf "\\begin{align}%s\\end{align}" (generate_latex n))::acc) q + | Env (s,n)::q when s="align*" + -> aux (Math(Printf.sprintf "\\begin{align*}%s\\end{align*}" (generate_latex n))::acc) q + | Env (s,n)::q when s="equation" + -> aux (Math(Printf.sprintf "\\begin{equation}%s\\end{equation}" (generate_latex n))::acc) q + | Env (s,n)::q when s="equation*" + -> aux (Math(Printf.sprintf "\\begin{equation*}%s\\end{equation*}" (generate_latex n))::acc) q + | Env(s,n)::q + -> let ast = aux [] n in + let ast = List.rev ast + in let env = Env(s,ast) + in aux (env::acc) q + | Chapter (s,l)::q + -> let ast = aux [] l in + let ast = List.rev ast + in let c = Chapter(s,ast) + in aux (c::acc) q + | Section (s,l)::q + -> let ast = aux [] l in + let ast = List.rev ast + in let c = Section(s,ast) + in aux (c::acc) q + | Subsection (s,l)::q + -> let ast = aux [] l in + let ast = List.rev ast + in let c = Subsection(s,ast) + in aux (c::acc) q + | Subsubsection (s,l)::q + -> let ast = aux [] l in + let ast = List.rev ast + in let c = Subsubsection(s,ast) + in aux (c::acc) q + | e::q -> aux (e::acc) q in List.rev (aux [] ast);; \ No newline at end of file diff --git a/lib/parser.ml b/lib/parser.ml index 9babc40..5ac1b42 100644 --- a/lib/parser.ml +++ b/lib/parser.ml @@ -26,7 +26,7 @@ let commands = Hashtbl.create 1;; let parse_interior_of_an_accolade list_of_chars acc = let stack = Stack.create () in let rec parse list_of_chars acc = - match list_of_chars with + match list_of_chars with | [] -> acc,[] | t::q when t='{' -> Stack.push t stack; parse q (acc^(String.make 1 t)) | t::q when t='}' -> if Stack.is_empty stack then acc,q else (Stack.pop stack |> ignore; parse q (acc^(String.make 1 t))) @@ -35,55 +35,55 @@ let parse_interior_of_an_accolade list_of_chars acc = let rec parse_arguments list_of_chars current_acc acc = match list_of_chars with - | [] -> current_acc::acc,[] - | t::q when t=']' -> (current_acc::acc),q - | t::q when t=',' -> parse_arguments q "" (current_acc::acc) - | t::q when t=' ' -> parse_arguments q current_acc acc - | t::q -> parse_arguments q (current_acc^(String.make 1 t)) acc;; + | [] -> current_acc::acc,[] + | t::q when t=']' -> (current_acc::acc),q + | t::q when t=',' -> parse_arguments q "" (current_acc::acc) + | t::q when t=' ' -> parse_arguments q current_acc acc + | t::q -> parse_arguments q (current_acc^(String.make 1 t)) acc;; let parse_command list_of_chars = let rec parse_command_rec list_of_chars acc = match list_of_chars with - | [] -> acc,[] - | t::q when t='{' -> - let a,q = parse_interior_of_an_accolade q "" in - let acc = (match acc with + | [] -> acc,[] + | t::q when t='{' -> + let a,q = parse_interior_of_an_accolade q "" in + let acc = (match acc with | NullCommand -> AtomCmd (a,[]) | AtomCmd (b,c) -> SimpleCmd (b,c,a) | SimpleCmd (b,e,c) -> MultipleCmd (b,e,a::c::[]) | MultipleCmd (b,e,c) -> MultipleCmd(b,e,a::c)) - in parse_command_rec q acc - | t::q when t='[' -> - let a,q = parse_arguments q "" [] in - let acc = (match acc with - | NullCommand -> NullCommand - | AtomCmd (c,b) -> AtomCmd (c,b@a) - | SimpleCmd(b,e,c) -> SimpleCmd (b,e@a,c) - | MultipleCmd(b,e,c) -> MultipleCmd(b,e@a,c)) in parse_command_rec q acc - | t::q when (t='\n' || t=' ') -> acc,q - | t::q when (t='$') -> acc,q - | t::q -> match acc with - | NullCommand -> parse_command_rec q (AtomCmd ((String.make 1 t),[])) - | AtomCmd (b,_) -> parse_command_rec q (AtomCmd ((b^(String.make 1 t),[]))) - | _ -> acc,(t::q) + | t::q when t='[' -> + let a,q = parse_arguments q "" [] in + let acc = (match acc with + | NullCommand -> NullCommand + | AtomCmd (c,b) -> AtomCmd (c,b@a) + | SimpleCmd(b,e,c) -> SimpleCmd (b,e@a,c) + | MultipleCmd(b,e,c) -> MultipleCmd(b,e@a,c)) + in parse_command_rec q acc + | t::q when (t='\n' || t=' ') -> acc,q + | t::q when (t='$') -> acc,q + | t::q -> match acc with + | NullCommand -> parse_command_rec q (AtomCmd ((String.make 1 t),[])) + | AtomCmd (b,_) -> parse_command_rec q (AtomCmd ((b^(String.make 1 t),[]))) + | _ -> acc,(t::q) in let cmd,l = parse_command_rec list_of_chars (NullCommand) in match cmd with - | MultipleCmd (s,e,l2) -> MultipleCmd (s,e,List.rev l2),l - | e -> e,l;; + | MultipleCmd (s,e,l2) -> MultipleCmd (s,e,List.rev l2),l + | e -> e,l;; let append_line str q = match (String.trim (str)) with - | "" -> q - | e -> Line(e)::q + | "" -> q + | e -> Line(e)::q let parse_math l = let rec parse acc l = match l with - | [] -> acc,[] - | t::q when t='$' -> acc,q - | t::q -> parse (acc^(String.make 1 t)) q + | [] -> acc,[] + | t::q when t='$' -> acc,q + | t::q -> parse (acc^(String.make 1 t)) q in let a,b = parse "" l in Math(a),b;; let print_math = function @@ -91,25 +91,25 @@ let print_math = function | _ -> ();; let rec parse_string str = -let rec parse current_acc acc lst = - match lst with - | [] -> append_line current_acc acc - | t::q when t='$' -> let a,q2 = parse_math q in parse "" (a::(append_line current_acc acc)) q2 - | t::q when t='\\' -> - let cmd,l = parse_command q in - parse "" (Cmd(cmd)::(append_line current_acc acc)) l - | t::q -> parse (current_acc^(String.make 1 t)) acc q -and parse_nested_commands ast_list = - match ast_list with + let rec parse current_acc acc lst = + match lst with + | [] -> append_line current_acc acc + | t::q when t='$' -> let a,q2 = parse_math q in parse "" (a::(append_line current_acc acc)) q2 + | t::q when t='\\' -> + let cmd,l = parse_command q in + parse "" (Cmd(cmd)::(append_line current_acc acc)) l + | t::q -> parse (current_acc^(String.make 1 t)) acc q + and parse_nested_commands ast_list = + match ast_list with | [] -> [] | Line s::q -> (Line s)::(parse_nested_commands q) | Math e::q -> (Math e)::(parse_nested_commands q) | Cmd c::q -> let a = (match c with - | NullCommand -> Nul - | AtomCmd (s,e) -> AtomicCmd (s,e) - | SimpleCmd (s,e,s2) -> OneArgCmd (s,e,(parse_string s2)) - | MultipleCmd (s,e,s2) -> MultipleArgCmd (s,e,(List.map (parse_string) s2))) + | NullCommand -> Nul + | AtomCmd (s,e) -> AtomicCmd (s,e) + | SimpleCmd (s,e,s2) -> OneArgCmd (s,e,(parse_string s2)) + | MultipleCmd (s,e,s2) -> MultipleArgCmd (s,e,(List.map (parse_string) s2))) in a::(parse_nested_commands q) | _::q -> parse_nested_commands q in let a = parse "" [] (string_to_list str) @@ -123,86 +123,86 @@ and parse_nested_commands ast_list = let separate_preamble lst = let rec iter ast_list a0 a1 = match ast_list with - | (OneArgCmd (s,e,(Line s1)::_))::q when (String.equal s "begin") && (String.equal s1 "document") -> - iter q a0 (OneArgCmd (s,e,(Line s1)::[])::a1) - | e::q when (List.length a1 = 0) -> iter q (e::a0) a1 - | e::q -> iter q a0 (e::a1) - | [] -> List.rev a0,List.rev a1 + | (OneArgCmd (s,e,(Line s1)::_))::q when (String.equal s "begin") && (String.equal s1 "document") -> + iter q a0 (OneArgCmd (s,e,(Line s1)::[])::a1) + | e::q when (List.length a1 = 0) -> iter q (e::a0) a1 + | e::q -> iter q a0 (e::a1) + | [] -> List.rev a0,List.rev a1 in iter lst [] [];; let calculate_environments lst = let rec extract_env acc lst = match lst with - | [] -> acc,[] - | (OneArgCmd (s,_,(Line s1)::_))::q when (String.equal s "begin") -> - let env,l = extract_env [] q in - let env = Env (s1,List.rev env) in - extract_env (env::acc) l - | (OneArgCmd (s,_,_))::q when (String.equal s "end") -> acc,q - | Chapter(s,l)::q -> let l2,q2 = extract_env acc l - in let l2 = List.rev l2 - in extract_env ((Chapter(s,l2@q2))::acc) q - | Section(s,l)::q -> let l2,q2 = extract_env acc l - in let l2 = List.rev l2 - in extract_env ((Section(s,l2@q2))::acc) q - | Subsection(s,l)::q -> let l2,q2 = extract_env acc l - in let l2 = List.rev l2 - in extract_env ((Section(s,l2@q2))::acc) q - | Subsubsection(s,l)::q -> let l2,q2 = extract_env acc l - in let l2 = List.rev l2 - in extract_env ((Section(s,l2@q2))::acc) q - | e::q -> extract_env (e::acc) q + | [] -> acc,[] + | (OneArgCmd (s,_,(Line s1)::_))::q when (String.equal s "begin") -> + let env,l = extract_env [] q in + let env = Env (s1,List.rev env) in + extract_env (env::acc) l + | (OneArgCmd (s,_,_))::q when (String.equal s "end") -> acc,q + | Chapter(s,l)::q -> let l2,q2 = extract_env acc l + in let l2 = List.rev l2 + in extract_env ((Chapter(s,l2@q2))::acc) q + | Section(s,l)::q -> let l2,q2 = extract_env acc l + in let l2 = List.rev l2 + in extract_env ((Section(s,l2@q2))::acc) q + | Subsection(s,l)::q -> let l2,q2 = extract_env acc l + in let l2 = List.rev l2 + in extract_env ((Section(s,l2@q2))::acc) q + | Subsubsection(s,l)::q -> let l2,q2 = extract_env acc l + in let l2 = List.rev l2 + in extract_env ((Section(s,l2@q2))::acc) q + | e::q -> extract_env (e::acc) q in let a,_ = extract_env [] lst in a;; let rec read_preamble ast = match ast with - | [] -> () - | OneArgCmd (s,_,(Line s1)::_)::q when (s="documentclass") -> Hashtbl.add preamble "type" s1; read_preamble q - | OneArgCmd (s,_,(Line s1)::_)::q when (s="input") -> Hashtbl.add preamble "glossary" s1; read_preamble q - | OneArgCmd (s,_,(Line s1)::_)::q when (s="title") -> Hashtbl.add preamble "title" s1; read_preamble q - | OneArgCmd (s,_,(Line s1)::_)::q when (s="author") -> Hashtbl.add preamble "author" s1; read_preamble q - | MultipleArgCmd (s,_,l)::q when (s="newcommand") -> - (match l with - | [AtomicCmd (s,_)]::n::_ when not (s="sep") - -> let env = calculate_environments n - in Hashtbl.add commands s env; read_preamble q - | _ -> read_preamble q) - | _::q -> read_preamble q;; - + | [] -> () + | OneArgCmd (s,_,(Line s1)::_)::q when (s="documentclass") -> Hashtbl.add preamble "type" s1; read_preamble q + | OneArgCmd (s,_,(Line s1)::_)::q when (s="input") -> Hashtbl.add preamble "glossary" s1; read_preamble q + | OneArgCmd (s,_,(Line s1)::_)::q when (s="title") -> Hashtbl.add preamble "title" s1; read_preamble q + | OneArgCmd (s,_,(Line s1)::_)::q when (s="author") -> Hashtbl.add preamble "author" s1; read_preamble q + | MultipleArgCmd (s,_,l)::q when (s="newcommand") -> + (match l with + | [AtomicCmd (s,_)]::n::_ when not (s="sep") + -> let env = calculate_environments n + in Hashtbl.add commands s env; read_preamble q + | _ -> read_preamble q) + | _::q -> read_preamble q;; + let separate_sections lst = let tab = [|false;false;false;false|] in let rec extract_section acc lst = match lst with - | [] -> acc,[] - | (OneArgCmd (s,e,(Line s1)::_))::q when (s="chapter" || s="chapter*") -> - if tab.(0) = true then (tab.(0) <- false; acc,(OneArgCmd (s,e,(Line s1)::[]))::q) - else - let a,l = extract_section [] q in - let chap = Chapter(s1,List.rev a) in - tab.(0) <- true; - extract_section (chap::acc) l - | (OneArgCmd (s,e,(Line s1)::_))::q when (s="section" || s="section*") -> - if tab.(1) = true then (tab.(1) <- false; acc,(OneArgCmd (s,e,(Line s1)::[]))::q) - else - let a,l = extract_section [] q in - let chap = Section(s1,List.rev a) in - tab.(1) <- true; - extract_section (chap::acc) l - | (OneArgCmd (s,e,(Line s1)::_))::q when (s="subsection" || s="subsection*") -> - if tab.(2) = true then (tab.(2) <- false; acc,(OneArgCmd (s,e,(Line s1)::[]))::q) - else - let a,l = extract_section [] q in - let chap = Subsection(s1,List.rev a) in - tab.(2) <- true; - extract_section (chap::acc) l - | (OneArgCmd (s,e,(Line s1)::_))::q when (s="subsubsection" || s="subsubsection*") -> - if tab.(3) = true then (tab.(3) <- false; acc,(OneArgCmd (s,e,(Line s1)::[]))::q) - else - let a,l = extract_section [] q in - let chap = Subsubsection(s1,List.rev a) in - tab.(3) <- true; - extract_section (chap::acc) l - | e::q -> extract_section (e::acc) q + | [] -> acc,[] + | (OneArgCmd (s,e,(Line s1)::_))::q when (s="chapter" || s="chapter*") -> + if tab.(0) = true then (tab.(0) <- false; acc,(OneArgCmd (s,e,(Line s1)::[]))::q) + else + let a,l = extract_section [] q in + let chap = Chapter(s1,List.rev a) in + tab.(0) <- true; + extract_section (chap::acc) l + | (OneArgCmd (s,e,(Line s1)::_))::q when (s="section" || s="section*") -> + if tab.(1) = true then (tab.(1) <- false; acc,(OneArgCmd (s,e,(Line s1)::[]))::q) + else + let a,l = extract_section [] q in + let chap = Section(s1,List.rev a) in + tab.(1) <- true; + extract_section (chap::acc) l + | (OneArgCmd (s,e,(Line s1)::_))::q when (s="subsection" || s="subsection*") -> + if tab.(2) = true then (tab.(2) <- false; acc,(OneArgCmd (s,e,(Line s1)::[]))::q) + else + let a,l = extract_section [] q in + let chap = Subsection(s1,List.rev a) in + tab.(2) <- true; + extract_section (chap::acc) l + | (OneArgCmd (s,e,(Line s1)::_))::q when (s="subsubsection" || s="subsubsection*") -> + if tab.(3) = true then (tab.(3) <- false; acc,(OneArgCmd (s,e,(Line s1)::[]))::q) + else + let a,l = extract_section [] q in + let chap = Subsubsection(s1,List.rev a) in + tab.(3) <- true; + extract_section (chap::acc) l + | e::q -> extract_section (e::acc) q in let a,_ = extract_section [] lst in List.rev a;; \ No newline at end of file diff --git a/lib/utils.ml b/lib/utils.ml index 4a19f43..3a5500b 100644 --- a/lib/utils.ml +++ b/lib/utils.ml @@ -13,8 +13,8 @@ let read_file filename = close_in chan; List.rev !lines ;; - let write_to_file file str = - let f = open_out file in - output_string f str; - flush f; - close_out f;; +let write_to_file file str = + let f = open_out file in + output_string f str; + flush f; + close_out f;;