package catala
Compiler and library for the literate programming language for tax code specification
Install
Dune Dependency
Authors
Maintainers
Sources
0.9.0.tar.gz
md5=8f891209d18b6540df9c34b2d1a6a783
sha512=737770b87a057674bceefe77e8526720732552f51f424afcebcb6a628267eab522c4fd993caca1ae8ed7ace65a4a87e485af10c1676e51ca5939509a1b841ac2
doc/src/catala.literate/latex.ml.html
Source file latex.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
(* This file is part of the Catala compiler, a specification language for tax and social benefits computation rules. Copyright (C) 2020 Inria, contributors: Denis Merigoux <denis.merigoux@inria.fr>, Emile Rolley <emile.rolley@tuta.io> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. *) (** This modules weaves the source code and the legislative text together into a document that law professionals can understand. *) open Catala_utils open Literate_common module A = Surface.Ast module C = Cli (** {1 Helpers} *) let lines_of_code = ref 0 let update_lines_of_code c = lines_of_code := !lines_of_code + Pos.get_end_line (Mark.get c) - Pos.get_start_line (Mark.get c) - 1 (** Espaces various LaTeX-sensitive characters *) let pre_latexify (s : string) : string = (* Then we send to pandoc, to ensure the markdown features used in the original document are correctly printed! *) String.trim (run_pandoc s `Latex) (** Usage: [wrap_latex source_files custom_pygments language fmt wrapped] Prints an LaTeX complete documùent structure around the [wrapped] content. *) let wrap_latex (source_files : string list) (language : C.backend_lang) (fmt : Format.formatter) (wrapped : Format.formatter -> unit) = Format.fprintf fmt {latex|\documentclass[%s, 11pt, a4paper]{article} \usepackage[T1]{fontenc} \usepackage[utf8]{inputenc} \usepackage{amssymb} \usepackage{babel} \usepackage{fontspec} \usepackage[hidelinks]{hyperref} %s \usepackage{fancyvrb,fvextra} \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} \usepackage{fancyhdr} \pagestyle{fancy} \fancyhf{} \fancyhead[C]{\leftmark} \fancyfoot[C]{\thepage} \renewcommand{\headrulewidth}{0.5pt} \renewcommand{\footrulewidth}{0.5pt} \usepackage{titlesec} \titleclass{\subsubsubsection}{straight}[\subsection] \newcounter{subsubsubsection}[subsubsection] \renewcommand\thesubsubsubsection{\thesubsubsection.\arabic{subsubsubsection}} \titleformat{\subsubsubsection}{\normalfont\normalsize\bfseries}{\thesubsubsubsection}{1em}{} \titlespacing*{\subsubsubsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex} \titleclass{\subsubsubsubsection}{straight}[\subsubsection] \newcounter{subsubsubsubsection}[subsubsubsection] \renewcommand\thesubsubsubsubsection{\thesubsubsubsection.\arabic{subsubsubsubsection}} \titleformat{\subsubsubsubsection}{\normalfont\normalsize\bfseries}{\thesubsubsubsubsection}{0.75em}{} \titlespacing*{\subsubsubsubsection}{0pt}{2.75ex plus 1ex minus .2ex}{1.25ex plus .2ex} \titleclass{\subsubsubsubsubsection}{straight}[\subsubsubsection] \newcounter{subsubsubsubsubsection}[subsubsubsubsection] \renewcommand\thesubsubsubsubsubsection{\thesubsubsubsubsection.\arabic{subsubsubsubsubsection}} \titleformat{\subsubsubsubsubsection}{\normalfont\normalsize\bfseries}{\thesubsubsubsubsubsection}{0.7em}{} \titlespacing*{\subsubsubsubsubsection}{0pt}{2.5ex plus 1ex minus .2ex}{1.1ex plus .2ex} \titleclass{\subsubsubsubsubsubsection}{straight}[\subsubsubsubsection] \newcounter{subsubsubsubsubsubsection}[subsubsubsubsubsection] \renewcommand\thesubsubsubsubsubsubsection{\thesubsubsubsubsubsection.\arabic{subsubsubsubsubsubsection}} \titleformat{\subsubsubsubsubsubsection}{\normalfont\normalsize\bfseries}{\thesubsubsubsubsubsubsection}{0.6em}{} \titlespacing*{\subsubsubsubsubsubsection}{0pt}{2.25ex plus 1ex minus .2ex}{1ex plus .2ex} \makeatletter \def\toclevel@subsubsubsection{4} \def\toclevel@subsubsubsubsection{5} \def\toclevel@subsubsubsubsubsection{6} \def\toclevel@subsubsubsubsubsubsection{7} \def\toclevel@paragraph{8} \def\toclevel@subparagraph{9} \def\l@subsection{\@dottedtocline{1}{1em}{0.5em}} \def\l@subsubsection{\@dottedtocline{2}{2em}{1em}} \def\l@subsubsubsection{\@dottedtocline{3}{3em}{1.5em}} \def\l@subsubsubsubsection{\@dottedtocline{5}{4em}{2em}} \def\l@subsubsubsubsubsection{\@dottedtocline{6}{5em}{2.5em}} \def\l@subsubsubsubsubsubsection{\@dottedtocline{7}{6em}{3em}} \def\l@paragraph{\@dottedtocline{8}{7em}{3.5em}} \def\l@subparagraph{\@dottedtocline{9}{8em}{4em}} \makeatother \setcounter{secnumdepth}{0} \setcounter{tocdepth}{9} \newunicodechar{÷}{$\div$} \newunicodechar{×}{$\times$} \newunicodechar{≤}{$\leqslant$} \newunicodechar{≥}{$\geqslant$} \newunicodechar{→}{$\rightarrow$} \newunicodechar{≠}{$\neq$} %s \newcommand*\FancyVerbStartString{\PY{l+s}{```catala}} \newcommand*\FancyVerbStopString{\PY{l+s}{```}} %% We have to do that to enable line breaks in pygmentize outputs: \let\oldPY\PY \renewcommand{\PY}[2]{%% \oldPY{#1}{\FancyVerbBreakStart{}#2\FancyVerbBreakStop{}}} \fvset{ numbers=left, frame=lines, framesep=3mm, breaklines, breakanywhere, rulecolor=\color{gray!70}, firstnumber=last, codes={\catcode`\$=3\catcode`\^=7} } \newcommand{\tightlist}{\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}} \title{ %s\\ %s Catala version %s } \begin{document} \maketitle %s %s : \begin{itemize}%s\end{itemize} \clearpage \tableofcontents \[\star\star\star\] \clearpage |latex} (match language with Fr -> "french" | En -> "english" | Pl -> "polish") (match language with Fr -> "\\setmainfont{Marianne}" | _ -> "") (* for France, we use the official font of the French state design system https://gouvfr.atlassian.net/wiki/spaces/DB/pages/223019527/Typographie+-+Typography *) (call_pygmentize ["-f"; "latex"; "-S"; "default"]) (literal_title language) (literal_generated_by language) Cli.version (pre_latexify (literal_disclaimer_and_link language)) (literal_source_files language) (String.concat ((match language with Fr -> " ;" | En -> ";" | Pl -> ";") ^ "\n") (List.map (fun filename -> let mtime = (Unix.stat filename).Unix.st_mtime in let ltime = Unix.localtime mtime in let ftime = Printf.sprintf "%d-%02d-%02d %d:%02d" (1900 + ltime.Unix.tm_year) (ltime.Unix.tm_mon + 1) ltime.Unix.tm_mday ltime.Unix.tm_hour ltime.Unix.tm_min in Printf.sprintf "\\item\\texttt{%s}, %s %s" (pre_latexify (Filename.basename filename)) (literal_last_modification language) ftime) source_files) ^ "."); wrapped fmt; Format.fprintf fmt "\n\n\\end{document}" (** {1 Weaving} *) let code_block ~meta lang fmt (code, pos) = (* Pygments does'nt allow to specify multiple 'verboptions' (escaping bug ?) so we call it with "nowrap" and write the FancyVrb wrapper ourselves. *) let pygmentized_code = let contents = String.concat "" ["```catala\n"; code; "```"] in let output = File.with_temp_file "catala_latex_pygments" "in" ~contents @@ fun temp_file_in -> call_pygmentize ~lang ["-f"; "latex"; "-O"; "\"nowrap\""; temp_file_in] in (* somehow even with the "nowrap" option on [pygmentize] still emits a wrapping [Verbatim] env. So we remove it with regexp finding. *) let env_rex = Re.compile @@ Re.seq [ Re.char '\\'; Re.alt [Re.str "begin"; Re.str "end"]; Re.str "{Verbatim}"; Re.opt (Re.seq [ Re.char '['; Re.rep (Re.diff Re.any (Re.char ']')); Re.char ']'; ]); Re.char '\n'; ] in Re.replace_string env_rex ~by:"" output in Format.fprintf fmt {latex|\begin{Verbatim}[commandchars=\\\{\},numbers=left,firstnumber=%d,stepnumber=1,label={\hspace*{\fill}\texttt{%s}}%s]|latex} (Pos.get_start_line pos + 1) (pre_latexify (Filename.basename (Pos.get_file pos))) (if meta then ",numbersep=9mm" else ""); Format.pp_print_newline fmt (); Format.pp_print_string fmt pygmentized_code; Format.pp_print_string fmt "\\end{Verbatim}\n" let rec law_structure_to_latex (language : C.backend_lang) (print_only_law : bool) (fmt : Format.formatter) (i : A.law_structure) : unit = match i with | A.LawHeading (heading, children) -> Format.fprintf fmt "\\%s{%s}\n\n" (match heading.law_heading_precedence with | 0 -> "section" | 1 -> "subsection" | 2 -> "subsubsection" | 3 -> "subsubsubsection" | 4 -> "subsubsubsubsection" | 5 -> "subsubsubsubsubsection" | 6 -> "subsubsubsubsubsubsection" | 7 -> "paragraph" | _ -> "subparagraph") (pre_latexify (Mark.remove heading.law_heading_name)); Format.pp_print_list ~pp_sep:(fun fmt () -> Format.fprintf fmt "\n\n") (law_structure_to_latex language print_only_law) fmt children | A.LawInclude (A.PdfFile ((file, _), page)) -> let label = file ^ match page with None -> "" | Some p -> Format.sprintf "_page_%d," p in Format.fprintf fmt "\\begin{center}\\textit{Annexe incluse, retranscrite page \ \\pageref{%s}}\\end{center} \ \\begin{figure}[p]\\begin{center}\\includegraphics[%swidth=\\textwidth]{%s}\\label{%s}\\end{center}\\end{figure}" label (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.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 let filename = Pos.get_file (Mark.get c) in let block_content = Mark.remove c in check_exceeding_lines start_line filename block_content; update_lines_of_code c; code_block ~meta:false language fmt c | A.CodeBlock (_, c, true) when not print_only_law -> let metadata_title = match language with | Fr -> "Métadonnées" | En -> "Metadata" | Pl -> "Metadane" in let start_line = Pos.get_start_line (Mark.get c) + 1 in let filename = Pos.get_file (Mark.get c) in let block_content = Mark.remove c in check_exceeding_lines start_line filename block_content; update_lines_of_code c; Format.fprintf fmt "\\begin{tcolorbox}[colframe=OliveGreen, breakable, \ title=\\textcolor{black}{\\texttt{%s}},title after \ break=\\textcolor{black}{\\texttt{%s}},before skip=1em, after skip=1em]\n\ %a\n\ \\end{tcolorbox}" metadata_title metadata_title (code_block ~meta:true language) c | A.CodeBlock _ -> () (** {1 API} *) let ast_to_latex (language : C.backend_lang) ~(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; Message.emit_debug "Lines of Catala inside literate source code: %d" !lines_of_code
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>