Skip to content

Commit

Permalink
correct massive bug in section & env creation
Browse files Browse the repository at this point in the history
  • Loading branch information
vanilla-extracts committed Mar 23, 2022
1 parent 03e26bd commit c5d5914
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
51 changes: 29 additions & 22 deletions lib/htmlgen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<img src=\"%s\"/>\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" -> "<br/>\n"
| "bigskip" -> "</p>\n\n<p>\n"
Expand All @@ -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" -> "<br/>\n"
| "bigskip" -> "</p>\n\n<p>\n"
Expand All @@ -114,67 +117,71 @@ 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;
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 "<h1 id=\"c%i\">Chapter %i : %s</h1><br/>\n"
let write = chapnum>=min_chap in
let str = aux write "" l in
let new_line = if write then Printf.sprintf "<h1 id=\"c%i\">Chapter %i : %s</h1><br/>\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 "<h2 id=\"s%f\">Section %i.%i : %s</h2><br/>\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
begin
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 "<h3 id=\"ss%f\">Subsection %i.%i.%i : %s</h3><br/>\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 "<h4 id=\"sss%f\">Subsubsection %i.%i.%i.%i : %s</h4><br/>\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 "<div style=\"margin: auto; text-align: center;\">\n%s\n</div>" 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 =
Expand Down
24 changes: 12 additions & 12 deletions lib/parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;;
2 changes: 2 additions & 0 deletions out.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h1>Generic</h1>
<h2>Table of Content</h2>
<ul>
<li><a href="#c1">Chapter 1 : hello</a></li>
<li><a href="#c2">Chapter 2 : test</a></li>

</ul>
</div>
Expand All @@ -20,6 +21,7 @@ <h1 id="c1">Chapter 1 : hello</h1><br/>
<p>

This is an integral
<h1 id="c2">Chapter 2 : test</h1><br/>
<img src="https://latex.codecogs.com/svg.image?\begin{align*}3y &= 3 \\ 2x &= 4\end{align*}"/>
<div style="margin: auto; text-align: center;">
au center
Expand Down
3 changes: 2 additions & 1 deletion test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c5d5914

Please sign in to comment.