Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file latex.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197(* This file is part of the Catala compiler, a specification language for tax and social benefits
computation rules. Copyright (C) 2020 Inria, contributor: Denis Merigoux
<denis.merigoux@inria.fr>
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. *)openUtilsmoduleA=Surface.AstmoduleR=Re.PcremoduleC=Cli(** {1 Helpers} *)(** Espaces various LaTeX-sensitive characters *)letpre_latexify(s:string)=letpercent=R.regexp"%"inlets=R.substitute~rex:percent~subst:(fun_->"\\%")sinletdollar=R.regexp"\\$"inlets=R.substitute~rex:dollar~subst:(fun_->"\\$")sinletpremier=R.regexp"1er"inlets=R.substitute~rex:premier~subst:(fun_->"1\\textsuperscript{er}")sinletunderscore=R.regexp"\\_"inlets=R.substitute~rex:underscore~subst:(fun_->"\\_")sins(** Usage: [wrap_latex source_files custom_pygments language fmt wrapped]
Prints an LaTeX complete documùent structure around the [wrapped] content. *)letwrap_latex(source_files:stringlist)(language:C.backend_lang)(fmt:Format.formatter)(wrapped:Format.formatter->unit)=Format.fprintffmt"\\documentclass[11pt, a4paper]{article}\n\n\
\\usepackage[T1]{fontenc}\n\
\\usepackage[utf8]{inputenc}\n\
\\usepackage[%s]{babel}\n\
\\usepackage{lmodern}\n\
\\usepackage{minted}\n\
\\usepackage{amssymb}\n\
\\usepackage{newunicodechar}\n\
\\usepackage{textcomp}\n\
\\usepackage[hidelinks]{hyperref}\n\
\\usepackage[dvipsnames]{xcolor}\n\
\\usepackage{fullpage}\n\
\\usepackage[many]{tcolorbox}\n\n\
\\newunicodechar{÷}{$\\div$}\n\
\\newunicodechar{×}{$\\times$}\n\
\\newunicodechar{≤}{$\\leqslant$}\n\
\\newunicodechar{≥}{$\\geqslant$}\n\
\\newunicodechar{→}{$\\rightarrow$}\n\
\\newunicodechar{≠}{$\\neq$}\n\n\
\\newcommand*\\FancyVerbStartString{```catala}\n\
\\newcommand*\\FancyVerbStopString{```}\n\n\
\\fvset{\n\
numbers=left,\n\
frame=lines,\n\
framesep=3mm,\n\
rulecolor=\\color{gray!70},\n\
firstnumber=last,\n\
codes={\\catcode`\\$=3\\catcode`\\^=7}\n\
}\n\n\
\\title{\n\
%s\n\
}\n\
\\author{\n\
%s Catala version %s\n\
}\n\
\\begin{document}\n\
\\maketitle\n\n\
%s : \n\
\\begin{itemize}%s\\end{itemize}\n\n\
\\[\\star\\star\\star\\]\\\\\n"(matchlanguagewith`Fr->"french"|`En->"english")(matchlanguagewith|`Fr->"Implémentation de texte législatif"|`En->"Legislative text implementation")(matchlanguagewith`Fr->"Document généré par"|`En->"Document generated by")Utils.Cli.version(matchlanguagewith|`Fr->"Fichiers sources tissés dans ce document"|`En->"Source files weaved in this document")(String.concat","(List.map(funfilename->letmtime=(Unix.statfilename).Unix.st_mtimeinletltime=Unix.localtimemtimeinletftime=Printf.sprintf"%d-%02d-%02d, %d:%02d"(1900+ltime.Unix.tm_year)(ltime.Unix.tm_mon+1)ltime.Unix.tm_mdayltime.Unix.tm_hourltime.Unix.tm_mininPrintf.sprintf"\\item\\texttt{%s}, %s %s"(pre_latexify(Filename.basenamefilename))(matchlanguagewith`Fr->"dernière modification le"|`En->"last modification")ftime)source_files));wrappedfmt;Format.fprintffmt"\n\n\\end{document}"(** Replaces math operators by their nice unicode counterparts *)letmath_syms_replace(c:string):string=letdate="\\d\\d/\\d\\d/\\d\\d\\d\\d"inletsyms=R.regexp(date^"|!=|<=|>=|--|->|\\*|/")inletsyms2cmd=function|"!="->"≠"|"<="->"≤"|">="->"≥"|"--"->"—"|"->"->"→"|"*"->"×"|"/"->"÷"|s->sinR.substitute~rex:syms~subst:syms2cmdc(** {1 Weaving} *)letlaw_article_item_to_latex(language:C.backend_lang)(fmt:Format.formatter)(i:A.law_article_item):unit=matchiwith|A.LawTextt->Format.fprintffmt"%s"(pre_latexifyt)|A.CodeBlock(_,c)->Format.fprintffmt"\\begin{minted}[label={\\hspace*{\\fill}\\texttt{%s}},firstnumber=%d]{%s}\n\
```catala%s```\n\
\\end{minted}"(pre_latexify(Filename.basename(Pos.get_file(Pos.get_positionc))))(Pos.get_start_line(Pos.get_positionc)-1)(matchlanguagewith`Fr->"catala_fr"|`En->"catala_en")(math_syms_replace(Pos.unmarkc))letreclaw_structure_to_latex(language:C.backend_lang)(fmt:Format.formatter)(i:A.law_structure):unit=matchiwith|A.LawHeading(heading,children)->Format.fprintffmt"\\%ssection*{%s}\n\n"(matchheading.law_heading_precedencewith|0->""|1->""|2->"sub"|3->"sub"|_->"subsub")(pre_latexifyheading.law_heading_name);Format.pp_print_list~pp_sep:(funfmt()->Format.fprintffmt"\n\n")(law_structure_to_latexlanguage)fmtchildren|A.LawInclude(A.PdfFile((file,_),page))->letlabel=file^matchpagewithNone->""|Somep->Format.sprintf"_page_%d,"pinFormat.fprintffmt"\\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(matchpagewithNone->""|Somep->Format.sprintf"page=%d,"p)filelabel|A.LawInclude(A.CatalaFile_|A.LegislativeText_)->()|A.LawArticle(article,children)->Format.fprintffmt"\\paragraph{%s}\n\n"(pre_latexify(Pos.unmarkarticle.law_article_name));Format.pp_print_list~pp_sep:(funfmt()->Format.fprintffmt"\n")(law_article_item_to_latexlanguage)fmtchildren|A.MetadataBlock(_,c)->letmetadata_title=matchlanguagewith`Fr->"Métadonnées"|`En->"Metadata"inFormat.fprintffmt"\\begin{tcolorbox}[colframe=OliveGreen, breakable, \
title=\\textcolor{black}{\\texttt{%s}},title after \
break=\\textcolor{black}{\\texttt{%s}},before skip=1em, after skip=1em]\n\
\\begin{minted}[numbersep=9mm, firstnumber=%d, label={\\hspace*{\\fill}\\texttt{%s}}]{%s}\n\
```catala%s```\n\
\\end{minted}\n\
\\end{tcolorbox}"metadata_titlemetadata_title(Pos.get_start_line(Pos.get_positionc)-1)(pre_latexify(Filename.basename(Pos.get_file(Pos.get_positionc))))(matchlanguagewith`Fr->"catala_fr"|`En->"catala_en")(math_syms_replace(Pos.unmarkc))|A.IntermediateTextt->Format.fprintffmt"%s"(pre_latexifyt)letprogram_item_to_latex(language:C.backend_lang)(fmt:Format.formatter)(i:A.program_item):unit=matchiwithA.LawStructurelaw_s->law_structure_to_latexlanguagefmtlaw_s(** {1 API} *)letast_to_latex(language:C.backend_lang)(fmt:Format.formatter)(program:A.program):unit=Format.pp_print_list~pp_sep:(funfmt()->Format.fprintffmt"\n\n")(program_item_to_latexlanguage)fmtprogram.program_items