Fix LaTeX literate output with tests (#581)

This commit is contained in:
Louis Gesbert 2024-02-15 15:21:19 +01:00 committed by GitHub
commit 518bb64ecc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 86 additions and 46 deletions

View File

@ -47,7 +47,7 @@ let run_catala_test catala_exe catala_opts file program args oc =
let ocfd = Unix.descr_of_out_channel oc in
let pid = Unix.create_process_env catala_exe cmd env cmd_in_rd ocfd ocfd in
Unix.close cmd_in_rd;
Queue.iter (output_string command_oc) program;
Seq.iter (output_string command_oc) program;
close_out command_oc;
let return_code =
match Unix.waitpid [] pid with
@ -94,7 +94,18 @@ let run_inline_tests catala_exe catala_opts filename =
skip_block lines
| Some args ->
let args = String.split_on_char ' ' args in
run_catala_test catala_exe catala_opts filename lines_until_now args oc;
let program =
let rec drop_last seq () =
match seq () with
| Seq.Nil -> assert false
| Seq.Cons (x, next) -> (
match next () with
| Seq.Nil -> Seq.Nil
| Seq.Cons _ as s -> Seq.Cons (x, drop_last (fun () -> s)))
in
Queue.to_seq lines_until_now |> drop_last |> drop_last
in
run_catala_test catala_exe catala_opts filename program args oc;
skip_block lines)
and skip_block lines =
match Seq.uncons lines with

View File

@ -62,10 +62,8 @@ let wrap_latex
\usepackage{color}
\usepackage{longtable}
\usepackage{booktabs,tabularx}
\usepackage{framed}
\usepackage{newunicodechar}
\usepackage{textcomp}
\usepackage[hidelinks]{hyperref}
\usepackage[dvipsnames]{xcolor}
\usepackage[left=2cm,right=2cm,top=3cm,bottom=3cm,headheight=2cm]{geometry}
\usepackage[many]{tcolorbox}
@ -272,7 +270,18 @@ let rec law_structure_to_latex
(match page with None -> "" | Some p -> Format.sprintf "page=%d," p)
file label
| A.LawInclude (A.CatalaFile _ | A.LegislativeText _) -> ()
| A.ModuleDef _ | A.ModuleUse _ -> () (* TODO: show somehow ? *)
| A.ModuleDef (id, extern) ->
Format.fprintf fmt "\n\\textbf{This defines the %s module \\textsc{%s}}"
(if extern then "external" else "catala")
(Mark.remove id)
| A.ModuleUse (id, alias) -> (
Format.fprintf fmt
"\n\\textbf{The following makes use of the module \\textsc{%s}"
(Mark.remove id);
match alias with
| None -> Format.fprintf fmt "}"
| Some al ->
Format.fprintf fmt " under the name \\textsc{%s}" (Mark.remove al))
| A.LawText t -> Format.fprintf fmt "%s" (pre_latexify t)
| A.CodeBlock (_, c, false) when not print_only_law ->
let start_line = Pos.get_start_line (Mark.get c) - 1 in
@ -311,9 +320,12 @@ let ast_to_latex
~(print_only_law : bool)
(fmt : Format.formatter)
(program : A.program) : unit =
Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "\n\n")
(law_structure_to_latex language print_only_law)
fmt program.program_items;
Format.pp_open_vbox fmt 0;
List.iter
(fun item ->
law_structure_to_latex language print_only_law fmt item;
Format.pp_print_cut fmt ())
program.program_items;
Format.pp_close_box fmt ();
Message.emit_debug "Lines of Catala inside literate source code: %d"
!lines_of_code

View File

@ -80,6 +80,7 @@ let run_pandoc (s : string) (backend : [ `Html | `Latex ]) : string =
"-f";
"markdown+multiline_tables+tex_math_dollars";
"--mathjax";
"--no-highlight";
"-t";
(match backend with `Html -> "html" | `Latex -> "latex");
"-o";

View File

@ -1,3 +1,5 @@
> Module Test_grave_char_en
## Law text should be able to contain grave accent '`'.
This is a block of law text containing `.
@ -48,22 +50,60 @@ $ catala Interpret -s A
```
```catala-test-inline
$ catala latex
\textbf{This defines the catala module \textsc{Test_grave_char_en}}
\subsection{Law text should be able to contain grave accent ``'.}
This is a block of law text containing `. This allows to:
\begin{itemize}
\item
use \texttt{Markdown} code block inside \texttt{Catala} files,
\item
\emph{escape} special characters such as \texttt{\#} or
\texttt{\textgreater{}},
\item
use Fenced Code Blocks:
\end{itemize}
\begin{verbatim}
let () = print_endline "Hello world!"
\end{verbatim}
\begin{Verbatim}[commandchars=\\\{\},numbers=left,firstnumber=19,stepnumber=1,label={\hspace*{\fill}\texttt{test\_grave\_char\_en.catala\_en}}]
\PY{l+s}{```catala}
\PY{k+kr}{declaration}\PY{l+s}{ }\PY{k+kr}{scope}\PY{l+s}{ }\PY{n+nc}{A}\PY{o}{:}
\PY{l+s}{ }\PY{l+s}{ }\PY{k+kd}{context}\PY{l+s}{ }\PY{k+kd}{output}\PY{l+s}{ }\PY{n+nv}{literate\PYZus{}parsing\PYZus{}is\PYZus{}ok}\PY{l+s}{ }\PY{k+kr}{content}\PY{l+s}{ }\PY{k+kt}{boolean}
\PY{k+kr}{scope}\PY{l+s}{ }\PY{n+nc}{A}\PY{o}{:}
\PY{l+s}{ }\PY{l+s}{ }\PY{k+kr}{definition}\PY{l+s}{ }\PY{n+nv}{literate\PYZus{}parsing\PYZus{}is\PYZus{}ok}\PY{l+s}{ }\PY{k+kr}{equals}\PY{l+s}{ }\PY{k+kc}{true}
\PY{l+s}{```}
\end{Verbatim}
Even after \texttt{Catala} code block:
\begin{verbatim}
int main(void) { return 0; }
\end{verbatim}
\begin{verbatim}
$ catala Typecheck --check-invariants
[RESULT] All invariant checks passed
[RESULT] Typechecking successful!
\end{verbatim}
\begin{verbatim}
$ catala Interpret -s A
[RESULT] Computation successful! Results:
[RESULT] literate_parsing_is_ok = true
```
```catala-test-inline
$ catala Interpret_Lcalc -s A --avoid-exceptions --optimize
[RESULT] Computation successful! Results:
[RESULT] literate_parsing_is_ok = true
```
```catala-test-inline
$ catala Interpret_Lcalc -s A --avoid-exceptions --optimize
[RESULT] Computation successful! Results:
[RESULT] literate_parsing_is_ok = true
```
```catala-test-inline
$ catala Interpret_Lcalc -s A --avoid-exceptions --optimize
\end{verbatim}
\begin{verbatim}
$ catala Interpret -s A
[RESULT] Computation successful! Results:
[RESULT] literate_parsing_is_ok = true
\end{verbatim}
```

View File

@ -40,15 +40,3 @@ $ catala Interpret -s A
[RESULT] Computation successful! Results:
[RESULT] literate_parsing_is_ok = vrai
```
```catala-test-inline
$ catala Interpret -s A
[RESULT] Computation successful! Results:
[RESULT] literate_parsing_is_ok = vrai
```
```catala-test-inline
$ catala Interpret -s A
[RESULT] Computation successful! Results:
[RESULT] literate_parsing_is_ok = vrai
```

View File

@ -32,15 +32,3 @@ $ catala Interpret -s A
[RESULT] Computation successful! Results:
[RESULT] literate_parsing_is_ok = prawda
```
```catala-test-inline
$ catala Interpret -s A
[RESULT] Computation successful! Results:
[RESULT] literate_parsing_is_ok = prawda
```
```catala-test-inline
$ catala Interpret -s A
[RESULT] Computation successful! Results:
[RESULT] literate_parsing_is_ok = prawda
```