This commit is contained in:
Denis Merigoux 2021-03-02 18:27:39 +01:00
parent 0259573cd0
commit 03774fdfc2
12 changed files with 64 additions and 117 deletions

View File

@ -49,6 +49,7 @@ jobs:
sudo apt install python3-dev python3-setuptools man2html rsync colordiff
sudo python3 -m pip install --upgrade pip
sudo python3 -m pip install virtualenv
sudo make pygments
- name: Make compiler
run: |

View File

@ -4,18 +4,18 @@
The Catala compiler is written using OCaml. First, you have to install `opam`,
OCaml's distribution and package manager. Follow the [instructions on the `opam`
website](https://opam.ocaml.org/doc/Install.html).
website](https://opam.ocaml.org/doc/Install.html).
Next, you will need to use the correct version of OCaml. Catala has been tested
Next, you will need to use the correct version of OCaml. Catala has been tested
with OCaml compiler versions that are at least 4.09.1. To switch to OCaml 4.09.1.,
just use:
opam switch 4.09.1
If you get a `No switch 4.09.1 is currently installed` error message, follow
If you get a `No switch 4.09.1 is currently installed` error message, follow
the hint and enter `opam switch create 4.09.1`.
Next, install all the OCaml packages that Catala depend on, as well as some
Next, install all the OCaml packages that Catala depend on, as well as some
git submodules, with
make dependencies
@ -49,24 +49,24 @@ builds the compiler from its OCaml sources.
## Install
The installation of the Catala compiler is handled through `opam`. Since the
Catala compiler is not yet published to the `opam` repository, you can install
a local version from this Git repository by using
The installation of the Catala compiler is handled through `opam`. Since the
Catala compiler is not yet published to the `opam` repository, you can install
a local version from this Git repository by using
opam install ./
To uninstall, use
To uninstall, use
opam unpin catala
### Generating website assets
The Catala website features assets generated by the Catala compiler. They are
needed to build the website. To produce them, simply run
needed to build the website. To produce them, simply run
make website-assets
Then, use a helper script to copy them over to the `assets` directory of the
Then, use a helper script to copy them over to the `assets` directory of the
Catala website.
./generate_website_assets.sh <path-to-catala-website>/assets
@ -90,6 +90,7 @@ To get Catala syntax highlighting in Atom, simply enter from
the root of the repository, depending on the language you want to use :
make atom_fr
or
make atom_en
@ -102,38 +103,24 @@ To get Catala syntax highlighting in VSCode, simply enter from
the root of the repository, depending on the language you want to use :
make vscode_fr
or
make vscode_en
You can now reload VSCode and check that you have syntax highlighting on any `.catala` file.
### Pygments
Pygments is a Python-based versatile lexer for various
programming languages. To use a version of Pygments
augmented with the Catala plugin, simply enter
make pygments
sudo make pygments
This will execute the
script `syntax_highlighting/fr/pygments/set_up_pygments.sh` and
script `syntax_highlighting/fr/pygments/set_up_pygments.sh` and
`syntax_highlighting/en/pygments/set_up_pygments.sh`.
The scripts set up a virtual environement in
`syntax_highlighting/fr/pygments/pygments/env` or
`syntax_highlighting/en/pygments/pygments/env`, which will
contain the modified version of Pygments that has Catala
support. If you want to hack something, it is possible to use this virtual
environnement directly with
source syntax_highlighting/fr/pygments/pygments/env/bin/activate
or
source syntax_highlighting/en/pygments/pygments/env/bin/activate
The `pigmentize` executable, used for instance by the `minted` LaTeX package,
will now point to the Catala-enabled version inside the virtual environment.
This `source` setup is not necessary if you use the rules in the `Makefile`.
The scripts patch your `pigmentize` executable, used for instance by the `minted` LaTeX package.
It will now point to the Catala-enabled version with the appropriate `catala_*` lexer.

View File

@ -53,11 +53,11 @@ SYNTAX_HIGHLIGHTING_EN=${CURDIR}/syntax_highlighting/en
pygmentize_fr: $(SYNTAX_HIGHLIGHTING_FR)/set_up_pygments.sh
chmod +x $<
$<
sudo $<
pygmentize_en: $(SYNTAX_HIGHLIGHTING_EN)/set_up_pygments.sh
chmod +x $<
$<
sudo $<
pygments: pygmentize_fr pygmentize_en
@ -101,27 +101,27 @@ TUTORIAL_EN_DIR=$(EXAMPLES_DIR)/tutorial_en
TUTORIEL_FR_DIR=$(EXAMPLES_DIR)/tutoriel_fr
literate_allocations_familiales: pygments build
literate_allocations_familiales: build
$(MAKE) -C $(ALLOCATIONS_FAMILIALES_DIR) allocations_familiales.tex
$(MAKE) -C $(ALLOCATIONS_FAMILIALES_DIR) allocations_familiales.html
literate_code_general_impots: pygments build
literate_code_general_impots: build
$(MAKE) -C $(CODE_GENERAL_IMPOTS_DIR) code_general_impots.tex
$(MAKE) -C $(CODE_GENERAL_IMPOTS_DIR) code_general_impots.html
literate_us_tax_code: pygments build
literate_us_tax_code: build
$(MAKE) -C $(US_TAX_CODE_DIR) us_tax_code.tex
$(MAKE) -C $(US_TAX_CODE_DIR) us_tax_code.html
literate_tutorial_en: pygments build
literate_tutorial_en: build
$(MAKE) -C $(TUTORIAL_EN_DIR) tutorial_en.tex
$(MAKE) -C $(TUTORIAL_EN_DIR) tutorial_en.html
literate_tutoriel_fr: pygments build
literate_tutoriel_fr: build
$(MAKE) -C $(TUTORIEL_FR_DIR) tutoriel_fr.tex
$(MAKE) -C $(TUTORIEL_FR_DIR) tutoriel_fr.html
literate_examples: literate_allocations_familiales literate_code_general_impots \
literate_examples: pygments literate_allocations_familiales literate_code_general_impots \
literate_us_tax_code literate_tutorial_en literate_tutoriel_fr
##########################################

View File

@ -4,19 +4,9 @@
LATEXMK=latexmk
PYGMENTIZE_FR=../../syntax_highlighting/fr/pygments/pygments/env/bin/pygmentize
PYGMENTIZE_EN=../../syntax_highlighting/en/pygments/pygments/env/bin/pygmentize
CATALA=dune exec --no-print-director ../../src/catala/catala.exe -- \
$(CATALA_OPTS) --language=$(CATALA_LANG)
ifeq ($(CATALA_LANG),fr)
PYGMENTIZE=$(PYGMENTIZE_FR)
endif
ifeq ($(CATALA_LANG),en)
PYGMENTIZE=$(PYGMENTIZE_EN)
endif
##########################################
# Targets
##########################################
@ -39,7 +29,6 @@ endif
@$(CATALA) Makefile $<
$(CATALA) \
--wrap \
--pygmentize=$(PYGMENTIZE) \
LaTeX \
$<
@ -47,7 +36,6 @@ endif
@$(CATALA) Makefile $<
$(CATALA) \
--wrap \
--pygmentize=$(PYGMENTIZE) \
HTML \
$<

View File

@ -8,7 +8,7 @@ let _ =
(language : Js.js_string Js.t) (trace : bool) =
driver
(Contents (Js.to_string contents))
false false false None "Interpret"
false false false "Interpret"
(Some (Js.to_string language))
None trace
(Some (Js.to_string scope))

View File

@ -18,9 +18,9 @@ module Pos = Utils.Pos
(** Entry function for the executable. Returns a negative number in case of error. *)
let driver (source_file : Pos.input_file) (debug : bool) (unstyled : bool)
(wrap_weaved_output : bool) (pygmentize_loc : string option) (backend : string)
(language : string option) (max_prec_digits : int option) (trace : bool)
(ex_scope : string option) (output_file : string option) : int =
(wrap_weaved_output : bool) (backend : string) (language : string option)
(max_prec_digits : int option) (trace : bool) (ex_scope : string option)
(output_file : string option) : int =
try
Cli.debug_flag := debug;
Cli.style_flag := not unstyled;
@ -104,7 +104,7 @@ let driver (source_file : Pos.input_file) (debug : bool) (unstyled : bool)
let weave_output =
match backend with
| Cli.Latex -> Literate.Latex.ast_to_latex language
| Cli.Html -> Literate.Html.ast_to_html pygmentize_loc language
| Cli.Html -> Literate.Html.ast_to_html language
| _ -> assert false
(* should not happen *)
in
@ -113,11 +113,11 @@ let driver (source_file : Pos.input_file) (debug : bool) (unstyled : bool)
if wrap_weaved_output then
match backend with
| Cli.Latex ->
Literate.Latex.wrap_latex program.Surface.Ast.program_source_files pygmentize_loc
language fmt (fun fmt -> weave_output fmt program)
Literate.Latex.wrap_latex program.Surface.Ast.program_source_files language fmt
(fun fmt -> weave_output fmt program)
| Cli.Html ->
Literate.Html.wrap_html program.Surface.Ast.program_source_files pygmentize_loc
language fmt (fun fmt -> weave_output fmt program)
Literate.Html.wrap_html program.Surface.Ast.program_source_files language fmt
(fun fmt -> weave_output fmt program)
| _ -> assert false (* should not happen *)
else weave_output fmt program;
close_out oc;

View File

@ -39,10 +39,9 @@ let raise_failed_pygments (command : string) (error_code : int) : 'a =
(** Usage: [wrap_html source_files custom_pygments language fmt wrapped]
Prints an HTML complete page structure around the [wrapped] content. *)
let wrap_html (source_files : string list) (custom_pygments : string option)
(language : Cli.backend_lang) (fmt : Format.formatter) (wrapped : Format.formatter -> unit) :
unit =
let pygments = match custom_pygments with Some p -> p | None -> "pygmentize" in
let wrap_html (source_files : string list) (language : Cli.backend_lang) (fmt : Format.formatter)
(wrapped : Format.formatter -> unit) : unit =
let pygments = "pygmentize" in
let css_file = Filename.temp_file "catala_css_pygments" "" in
let pygments_args = [| "-f"; "html"; "-S"; "colorful"; "-a"; ".catala-code" |] in
let cmd =
@ -95,15 +94,14 @@ let wrap_html (source_files : string list) (custom_pygments : string option)
wrapped fmt
(** Performs syntax highlighting on a piece of code by using Pygments and the special Catala lexer. *)
let pygmentize_code (c : string Pos.marked) (language : C.backend_lang)
(custom_pygments : string option) : string =
let pygmentize_code (c : string Pos.marked) (language : C.backend_lang) : string =
C.debug_print (Printf.sprintf "Pygmenting the code chunk %s" (Pos.to_string (Pos.get_position c)));
let temp_file_in = Filename.temp_file "catala_html_pygments" "in" in
let temp_file_out = Filename.temp_file "catala_html_pygments" "out" in
let oc = open_out temp_file_in in
Printf.fprintf oc "%s" (Pos.unmark c);
close_out oc;
let pygments = match custom_pygments with Some p -> p | None -> "pygmentize" in
let pygments = "pygmentize" in
let pygments_lexer = match language with `Fr -> "catala_fr" | `En -> "catala_en" in
let pygments_args =
[|
@ -131,8 +129,8 @@ let pygmentize_code (c : string Pos.marked) (language : C.backend_lang)
(** {1 Weaving} *)
let law_article_item_to_html (custom_pygments : string option) (language : C.backend_lang)
(fmt : Format.formatter) (i : A.law_article_item) : unit =
let law_article_item_to_html (language : C.backend_lang) (fmt : Format.formatter)
(i : A.law_article_item) : unit =
match i with
| A.LawText t ->
let t = pre_html t in
@ -153,10 +151,10 @@ let law_article_item_to_html (custom_pygments : string option) (language : C.bac
let pprinted_c = R.substitute ~rex:syms ~subst:syms_subst (Pos.unmark c) in
Format.fprintf fmt "<div class='code-wrapper'>\n<div class='filename'>%s</div>\n%s\n</div>"
(Pos.get_file (Pos.get_position c))
(pygmentize_code (Pos.same_pos_as ("/*" ^ pprinted_c ^ "*/") c) language custom_pygments)
(pygmentize_code (Pos.same_pos_as ("/*" ^ pprinted_c ^ "*/") c) language)
let rec law_structure_to_html (custom_pygments : string option) (language : C.backend_lang)
(fmt : Format.formatter) (i : A.law_structure) : unit =
let rec law_structure_to_html (language : C.backend_lang) (fmt : Format.formatter)
(i : A.law_structure) : unit =
match i with
| A.LawHeading (heading, children) ->
let h_number = heading.law_heading_precedence + 2 in
@ -165,8 +163,7 @@ let rec law_structure_to_html (custom_pygments : string option) (language : C.ba
h_number;
Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "\n")
(law_structure_to_html custom_pygments language)
fmt children
(law_structure_to_html language) fmt children
| A.LawInclude _ -> ()
| A.LawArticle (a, children) ->
Format.fprintf fmt
@ -180,24 +177,21 @@ let rec law_structure_to_html (custom_pygments : string option) (language : C.ba
(pre_html (Pos.unmark a.law_article_name));
Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "\n")
(law_article_item_to_html custom_pygments language)
(law_article_item_to_html language)
fmt children;
Format.fprintf fmt "\n</div>"
| A.MetadataBlock (b, c) ->
law_article_item_to_html custom_pygments language fmt (A.CodeBlock (b, c))
| A.MetadataBlock (b, c) -> law_article_item_to_html language fmt (A.CodeBlock (b, c))
| A.IntermediateText t ->
let t = pre_html t in
if t = "" then () else Format.fprintf fmt "<p class='law-text'>%s</p>" t
let program_item_to_html (custom_pygments : string option) (language : C.backend_lang)
(fmt : Format.formatter) (i : A.program_item) : unit =
match i with A.LawStructure s -> law_structure_to_html custom_pygments language fmt s
let program_item_to_html (language : C.backend_lang) (fmt : Format.formatter) (i : A.program_item) :
unit =
match i with A.LawStructure s -> law_structure_to_html language fmt s
(** {1 API} *)
let ast_to_html (custom_pygments : string option) (language : C.backend_lang)
(fmt : Format.formatter) (program : A.program) : unit =
let ast_to_html (language : C.backend_lang) (fmt : Format.formatter) (program : A.program) : unit =
Format.pp_print_list
~pp_sep:(fun fmt () -> Format.fprintf fmt "\n\n")
(program_item_to_html custom_pygments language)
fmt program.program_items
(program_item_to_html language) fmt program.program_items

View File

@ -20,17 +20,11 @@ open Utils
(** {1 Helpers} *)
val wrap_html :
string list ->
string option ->
Cli.backend_lang ->
Format.formatter ->
(Format.formatter -> unit) ->
unit
(** Usage: [wrap_html source_files custom_pygments language fmt wrapped]
string list -> Cli.backend_lang -> Format.formatter -> (Format.formatter -> unit) -> unit
(** Usage: [wrap_html source_files language fmt wrapped]
Prints an HTML complete page structure around the [wrapped] content. *)
(** {1 API} *)
val ast_to_html :
string option -> Cli.backend_lang -> Format.formatter -> Surface.Ast.program -> unit
val ast_to_html : Cli.backend_lang -> Format.formatter -> Surface.Ast.program -> unit

View File

@ -37,8 +37,8 @@ let pre_latexify (s : string) =
(** 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) (custom_pygments : string option)
(language : C.backend_lang) (fmt : Format.formatter) (wrapped : Format.formatter -> unit) =
let wrap_latex (source_files : string list) (language : C.backend_lang) (fmt : Format.formatter)
(wrapped : Format.formatter -> unit) =
Format.fprintf fmt
"\\documentclass[11pt, a4paper]{article}\n\n\
\\usepackage[T1]{fontenc}\n\
@ -48,7 +48,6 @@ let wrap_latex (source_files : string list) (custom_pygments : string option)
\\usepackage{minted}\n\
\\usepackage{amssymb}\n\
\\usepackage{newunicodechar}\n\
%s\n\
\\usepackage{textcomp}\n\
\\usepackage[hidelinks]{hyperref}\n\
\\usepackage[dvipsnames]{xcolor}\n\
@ -80,9 +79,6 @@ let wrap_latex (source_files : string list) (custom_pygments : string option)
\\begin{itemize}%s\\end{itemize}\n\n\
\\[\\star\\star\\star\\]\\\\\n"
(match language with `Fr -> "french" | `En -> "english")
( match custom_pygments with
| None -> ""
| Some p -> Printf.sprintf "\\renewcommand{\\MintedPygmentize}{%s}" p )
( match language with
| `Fr -> "Implémentation de texte législatif"
| `En -> "Legislative text implementation" )

View File

@ -20,13 +20,8 @@ open Utils
(** {1 Helpers} *)
val wrap_latex :
string list ->
string option ->
Cli.backend_lang ->
Format.formatter ->
(Format.formatter -> unit) ->
unit
(** Usage: [wrap_latex source_files custom_pygments language fmt wrapped]
string list -> Cli.backend_lang -> Format.formatter -> (Format.formatter -> unit) -> unit
(** Usage: [wrap_latex source_files language fmt wrapped]
Prints an LaTeX complete documùent structure around the [wrapped] content. *)

View File

@ -92,16 +92,9 @@ let output =
~doc:
"$(i, OUTPUT) is the file that will contain the extracted output (for compiler backends)")
let pygmentize_loc =
Arg.(
value
& opt (some string) None
& info [ "pygmentize" ] ~docv:"PYGMENTIZE"
~doc:"Location of a custom pygmentize executable for LaTeX source code highlighting")
let catala_t f =
Term.(
const f $ file $ debug $ unstyled $ wrap_weaved_output $ pygmentize_loc $ backend $ language
const f $ file $ debug $ unstyled $ wrap_weaved_output $ backend $ language
$ max_prec_digits_opt $ trace_opt $ ex_scope $ output)
let version = "0.2.0"

View File

@ -61,14 +61,11 @@ val ex_scope : string option Cmdliner.Term.t
val output : string option Cmdliner.Term.t
val pygmentize_loc : string option Cmdliner.Term.t
val catala_t :
(string ->
bool ->
bool ->
bool ->
string option ->
string ->
string option ->
int option ->
@ -77,7 +74,9 @@ val catala_t :
string option ->
'a) ->
'a Cmdliner.Term.t
(** Main entry point *)
(** Main entry point:
[catala_t file debug unstyled wrap_weaved_output backend language max_prec_digits_opt trace_opt
ex_scope output] *)
val version : string