A bibtex file parser
  • OCaml 92.9%
  • Dune 5.6%
  • TeX 0.9%
  • Makefile 0.6%
Find a file
2024-02-11 11:30:38 +01:00
src Add support for "1--2" in page range 2024-02-11 11:30:38 +01:00
tests Add support for "1--2" in page range 2024-02-11 11:30:38 +01:00
.gitignore Update to use dune and new orex 2018-05-05 11:24:13 +02:00
Changelog.md Add support for "1--2" in page range 2024-02-11 11:30:38 +01:00
descr Refactoring to adapt to the new interface of orec 2015-04-09 16:45:08 +02:00
dune-project Add OCaml linking exception 2023-01-23 21:14:49 +01:00
LICENSE Add OCaml linking exception 2023-01-23 21:14:49 +01:00
Makefile Translation to dune 2019-06-11 18:52:36 +02:00
Readme.md Add changelog and update readme 2023-01-23 20:49:52 +01:00
talaria-bibtex.opam Add OCaml linking exception 2023-01-23 21:14:49 +01:00

A simple bibtex parser using open records to encode strongly typed bibtex item. To use this library, first parse a bibtex file using


let dtb = Bibtex.parse @@ Lexing.from_string
"
@article{ Nothingness,
title={On empty articles},
author={Nobody, U. and Nonymous, A.},
year={1995},
pages={0-0}
}
"

Bitex items are stored inside a string map using their identifiants (e.g. "Nothingness" ) as key.

let item = Bibtex.Database.find "Nothingness" dtb

Item fields can be accessed using the open record syntax (see orec). For convenience, the module Bibtex_fields(=Bibtex.Fields) define a 'a named_field type which combines an access field with a name and a conversion function to string. The list of predefined fields can be found in the Bibtex_fields module.

let () =
    let open Bibtex.Fields in
    assert ( item.%{year.f} = Some 1995 );
    assert( item.%{page.f} = Some ( Interv(0,0) ) );
    assert( item.%{title.f} = Some "On empty articles" )

If needed, more fields can be added to the parser by using a custom [~with_keys] arguments.

First-class fields allow to write generic function like

let mayp field fmt =
  let open Bibtex.Fields in
  match item.%{field} with
  | None -> ()
  | Some x -> Printf.printf fmt x  

let () =
    let open Bibtex.Fields in
    mayp (str kind) "@%s";
    mayp uid.f "{%s,\n";
    mayp title.f "title={%s},\n";
    mayp (str authors) "author={%s},\n";
    mayp year.f "year={%d},\n";
    mayp (str pages) "pages={%s},\n";
    print_string "}\n"

#Example


let dtb = Bibtex.parse @@ Lexing.from_string
"
@article{ Nothingness,
title={On empty articles},
author={Nobody, U. and Nonymous, A.},
year={1995},
pages={0-0}
}
"

let item = Bibtex.Database.find "Nothingness" dtb
let mayp field fmt =
  let open Bibtex.Fields in
  match item.%{field} with
  | None -> ()
  | Some x -> Printf.printf fmt x  

let () =
    let open Bibtex.Fields in
    mayp (str kind) "@%s";
    mayp uid.f "{%s,\n";
    mayp title.f "title={%s},\n";
    mayp (str authors) "author={%s},\n";
    mayp year.f "year={%d},\n";
    mayp (str pages) "pages={%s},\n";
    print_string "}\n"