package forester

  1. Overview
  2. Docs

Source file RenderBibTeX.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
open Prelude
open Core

module E = RenderEff.Perform

module Printer =
struct
  module P0 =
  struct
    type out = Format.formatter
    let text txt fmt =
      Format.fprintf fmt "%s" txt
  end

  include PrinterKit.Kit (P0)
  let newline = text "\n"
end

module Metas = Map.Make (String)

let render_author author =
  match E.get_doc author with
  | None ->
    Printer.text author
  | Some doc ->
    match doc.title with
    | None ->
      Printer.text author
    | Some title ->
      RenderLaTeX.render title

let render_authors : string list -> Printer.t =
  function
  | [] -> Printer.text "Anonymous"
  | authors ->
    Printer.iter ~sep:(Printer.text " and ") render_author authors

let render_title ~taxon title =
  match taxon with
  | Some taxon ->
    Format.dprintf "%s: %a"
      (StringUtil.sentence_case taxon)
      (Fun.flip RenderLaTeX.render) title
  | None ->
    RenderLaTeX.render @@ Sem.sentence_case title

let render_auto_bibtex ~base_url (doc : Sem.doc) : Printer.t =
  match doc.addr with
  | None -> Printer.nil
  | Some addr ->
    let contributors = E.contributors addr in
    Printer.seq ~sep:Printer.newline [
      Printer.nil;
      Format.dprintf "@misc{%s," addr;
      begin
        doc.title |> Printer.option @@ fun title ->
        Format.dprintf "title = {%a}," (Fun.flip @@ render_title ~taxon:doc.taxon) title
      end;
      Format.dprintf "author = {%a}," (Fun.flip render_authors) doc.authors;
      begin
        base_url |> Printer.option @@ fun url ->
        Format.dprintf "url = {%s/%s}," url @@ E.route addr
      end;
      begin
        match contributors with
        | [] -> Printer.nil
        | _ ->
          let pp_sep fmt () = Format.fprintf fmt ", " in
          Format.dprintf "note = {With contributions from %a.},"
            (Format.pp_print_list ~pp_sep (Fun.flip render_author)) contributors
      end;
      Format.dprintf "}";
      Printer.nil
    ]

let render_bibtex ~base_url (doc : Sem.doc) : Printer.t =
  let metas = Metas.of_seq @@ List.to_seq doc.metas in
  match Metas.find_opt "bibtex" metas with
  | None ->
    if doc.taxon = Some "reference" then Printer.nil else
      render_auto_bibtex ~base_url doc
  | Some [Text txt] ->
    let lines = String.split_on_char '\n' txt in
    lines |> Printer.iter ~sep:Printer.newline @@ fun line ->
    Printer.text @@ String.trim line
  | Some other ->
    failwith @@ Format.asprintf "Unexpected contents of bibtex meta %a" Sem.pp other
OCaml

Innovation. Community. Security.