From c5d5914f04ae1de03d803c1eed6e021bd2427e18 Mon Sep 17 00:00:00 2001 From: Charlotte Thomas Date: Wed, 23 Mar 2022 21:19:12 +0100 Subject: [PATCH] correct massive bug in section & env creation --- lib/htmlgen.ml | 51 ++++++++++++++++++++++++++++---------------------- lib/parser.ml | 24 ++++++++++++------------ out.html | 2 ++ test.txt | 3 ++- 4 files changed, 45 insertions(+), 35 deletions(-) diff --git a/lib/htmlgen.ml b/lib/htmlgen.ml index 4ab8fd4..f0f5a03 100644 --- a/lib/htmlgen.ml +++ b/lib/htmlgen.ml @@ -60,19 +60,21 @@ let print_table_of_content ast min_chap = 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 = + let rec aux write acc ast = match ast with | [] -> acc - | Nul::q -> aux acc q + | Nul::q -> aux write acc q | Line s::q -> + Printf.printf "Line %s Write %b\n" s write; let line= if write then Printf.sprintf "%s\n" s else "" - in aux ~write:write (acc^line) q + in aux write (acc^line) q | Math s::q -> let url = Printf.sprintf "https://latex.codecogs.com/svg.image?%s"s in let url = url_encoded_str url in let line = if write then Printf.sprintf "\n" url else "" - in aux ~write:write (acc^line) q + in aux write (acc^line) q | AtomicCmd (s,_)::q -> + Printf.printf "Command %s Write %b\n" s write; let new_line = (match s with | "par" -> "
\n" | "bigskip" -> "

\n\n

\n" @@ -85,14 +87,15 @@ let parse_to_html ?(min_chap=1) write_before ast= | e -> (try let structure = Hashtbl.find commands e in - let str = aux ~write:write acc structure + let str = aux 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 + in aux write new_acc q | OneArgCmd (s,_,l)::q -> - let str = aux "" l in + Printf.printf "Command %s Write %b\n" s write; + let str = aux write "" l in let new_line = (match s with | "par" -> "
\n" | "bigskip" -> "

\n\n

\n" @@ -114,13 +117,14 @@ let parse_to_html ?(min_chap=1) write_before ast= | e -> (try let structure = Hashtbl.find commands e in - let str = aux ~write:write acc structure + let str = aux write acc structure in str with _ -> "")) in let new_acc = if write then acc^(new_line) else "" - in aux ~write:write new_acc q + in aux write new_acc q | Chapter (s,l)::q -> + Printf.printf "Chapter %s %i on %i Write %b\n" s (count.(0)) min_chap write; let chapnum = count.(0) in begin count.(0) <- count.(0) + 1; @@ -128,22 +132,24 @@ let parse_to_html ?(min_chap=1) write_before ast= 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" + let write = chapnum>=min_chap in + let str = aux write "" l in + let new_line = if write 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 (acc^new_line^str) q | Section (s,l)::q -> + Printf.printf "Section %i\n" (count.(0)); let chapnum,secnum = count.(0),count.(1) in begin count.(1) <- count.(1) + 1; count.(2) <- 1; count.(3) <- 1; end; - let str = aux ~write:write "" l in + let str = aux 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 - aux ~write:write (acc^new_line^str) q + aux write (acc^new_line^str) q | Subsection (s,l)::q -> let chapnum,secnum,ssecnum = count.(0),count.(1),count.(2) in @@ -151,30 +157,31 @@ let parse_to_html ?(min_chap=1) write_before ast= count.(2) <- count.(2) + 1; count.(3) <- 1; end; - let str = aux ~write:write "" l in + let str = aux 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 - aux ~write:write (acc^new_line^str) q + aux 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 count.(3) <- count.(3) + 1; end; - let str = aux ~write:write "" l in + let str = aux 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 - aux ~write:write (acc^new_line^str) q + aux write (acc^new_line^str) q | Env (s,l)::q -> - let str = aux ~write:write "" l in + Printf.printf "Env %s Writing %b\n" s write; + let str = aux write "" l in let new_line = (match s with | "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;; + in aux write (acc^new_line^"\n") q + | _::q -> aux write acc q + in aux write_before "" ast;; let prepare_body name str toc = diff --git a/lib/parser.ml b/lib/parser.ml index 5ac1b42..bc39094 100644 --- a/lib/parser.ml +++ b/lib/parser.ml @@ -140,16 +140,16 @@ let calculate_environments lst = 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 + | Chapter(s,l)::q -> let l2,q2 = extract_env [] 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 + | Section(s,l)::q -> let l2,q2 = extract_env [] 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 + | Subsection(s,l)::q -> let l2,q2 = extract_env [] 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 + | Subsubsection(s,l)::q -> let l2,q2 = extract_env [] l in let l2 = List.rev l2 in extract_env ((Section(s,l2@q2))::acc) q | e::q -> extract_env (e::acc) q @@ -179,30 +179,30 @@ let separate_sections lst = | (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 + (tab.(0) <- true; let a,l = extract_section [] q in let chap = Chapter(s1,List.rev a) in - tab.(0) <- true; - extract_section (chap::acc) l + 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 + (tab.(1) <- true; let a,l = extract_section [] q in let chap = Section(s1,List.rev a) in - tab.(1) <- true; - extract_section (chap::acc) l + 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 + (tab.(2) <- true; let a,l = extract_section [] q in let chap = Subsection(s1,List.rev a) in - tab.(2) <- true; - extract_section (chap::acc) l + 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 + (tab.(3) <- true; let a,l = extract_section [] q in let chap = Subsubsection(s1,List.rev a) in - tab.(3) <- true; - extract_section (chap::acc) l + 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/out.html b/out.html index 6e653af..922b010 100644 --- a/out.html +++ b/out.html @@ -10,6 +10,7 @@

Generic

Table of Content

@@ -20,6 +21,7 @@

Chapter 1 : hello


This is an integral +

Chapter 2 : test


au center diff --git a/test.txt b/test.txt index 0d27503..2e71d4a 100644 --- a/test.txt +++ b/test.txt @@ -5,7 +5,8 @@ Hello \chapter{hello} tralalala \bigskip -This is an integral +This is an integral +\chapter{test} \begin{align*} 2x &= 4 \\ 3y &= 3