Skip to content

Commit

Permalink
big update to ORM-database driven website, no more mlcrunch hack
Browse files Browse the repository at this point in the history
  • Loading branch information
avsm committed Feb 4, 2010
1 parent f3f088a commit 14f8e3c
Show file tree
Hide file tree
Showing 18 changed files with 901 additions and 71 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Expand Up @@ -8,5 +8,14 @@
.*.swp
*.annot
._d/
._bcdi/
._ncdi/
bibtex/bib2db
fs/fs2db
src/server
src/static_*.ml
content/
bibtex/bibtex_lexer.ml
bibtex/bibtex_parser.ml
bibtex/bibtex_parser.mli

20 changes: 17 additions & 3 deletions Makefile
@@ -1,7 +1,21 @@
all:
@cd crunch && $(MAKE) all
@cd src && $(MAKE) all
@cd fs && $(MAKE) nc
@cd bibtex && $(MAKE)
@cd src && $(MAKE)

dbs: tdb sdb bdb

sdb:
rm -f content/db/static.db
./fs/fs2db -db content/db/static.db content/static
tdb:
rm -f content/db/tmpl.db
./fs/fs2db -db content/db/tmpl.db content/templates
bdb:
rm -f content/db/bib.db
./bibtex/bib2db ./content/biblio.bib content/db/bib.db

clean:
@cd crunch && $(MAKE) clean
@cd fs && $(MAKE) clean
@cd src && $(MAKE) clean
rm -f content/db/bib.db content/db/static.db content/db/tmpl.db
8 changes: 8 additions & 0 deletions bibtex/Makefile
@@ -0,0 +1,8 @@
OCAMLMAKEFILE = ../OCamlMakefile
ANNOTATE = yes
SOURCES = bibtex.ml bib.ml bibtex_parser.mly bibtex_lexer.mll bib2db.ml
RESULT = bib2db
PREDS = camlp4o
PACKS = orm.syntax json-tc.syntax

-include $(OCAMLMAKEFILE)
65 changes: 65 additions & 0 deletions bibtex/bib.ml
@@ -0,0 +1,65 @@
open Printf

type t =
|Book
|In_proceedings
|In_book
|In_collection
|Article
|Proceedings
|Webpage
|Tech_report
|Phd_thesis
|Masters_thesis
|Unpublished
|Misc
and ent = {
ty: t;
key: string;
authors: string list;
year: int option;
title: string;
misc: (string * string) list
} with orm

let t_of_string = function
|"book" -> Book
|"inproceedings" -> In_proceedings
|"article" -> Article
|"proceedings" -> Proceedings
|"webpage" -> Webpage
|"techreport" -> Tech_report
|"phdthesis" -> Phd_thesis
|"mastersthesis" -> Masters_thesis
|"unpublished" -> Unpublished
|"misc" -> Misc
|"inbook" -> In_book
|"incollection" -> In_collection
|x -> failwith ("unknown bibtex type: " ^ x)

let author_html a = a

let ent_to_summary_html e =
let authors = match List.rev e.authors with
| [] -> "XXX author"
| [a] -> a
| a :: tl ->
String.concat ", " (List.map author_html (List.rev tl)) ^ " and " ^ (author_html a) in
let year = match e.year with
| None -> "XXX year"
| Some y -> string_of_int y in
let key x = try List.assoc x e.misc with Not_found -> "XXX " ^ x in
match e.ty with
|In_proceedings ->
sprintf "<b>%s</b>, %s, <i>%s</i>" e.title authors (key "booktitle")
|Article
|Book
|In_book
|In_collection
|Proceedings
|Webpage
|Tech_report
|Phd_thesis
|Masters_thesis
|Unpublished
|Misc -> sprintf "<b>%s</b>" e.title
70 changes: 70 additions & 0 deletions bibtex/bib2db.ml
@@ -0,0 +1,70 @@
open Printf

let read_bibtex f =
let ch = open_in f in
let lb = Lexing.from_channel ch in
try
let el = Bibtex_parser.command_list Bibtex_lexer.token lb in
eprintf "ok (%d entries).\n%!" (Bibtex.size el);
el
with
Parsing.Parse_error | Failure "unterminated string" ->
(eprintf "Parse error character %d, in or after entry '%s'.\n%!" (Lexing.lexeme_start lb) !Bibtex.current_key;
exit 1)

open Bibtex

let atom = function |Id i -> "id " ^ i |String s -> "str " ^ s
let atoms a = String.concat ", " (List.map atom a)
let output_bibtex c () =
match c with
|Comment c -> ()
|Preamble al -> printf "preamble: %s\n" (atoms al)
|Abbrev (t,al) -> printf "abbr: %s %s\n" t (atoms al)
|Entry (ety, key, el) ->
printf "e: %s[%s] -> %s \n" ety key
(String.concat "\n" (List.map (fun (k,v) -> sprintf " %s=%s" k (atoms v)) el))

let string_of_atom = function
| Id x -> x
| String x -> Pcre.replace ~rex:(Pcre.regexp "[{|}]") ~itempl:(Pcre.subst "") x

let normalize_author x =
(* Blogg, Joe -> Joe Blogg *)
let x = String.concat " " (
List.rev (Pcre.split ~rex:(Pcre.regexp ", *") x)) in
(* strip edge spaces *)
let itempl = Pcre.subst "" in
let x = Pcre.replace ~rex:(Pcre.regexp "^ +") ~itempl x in
Pcre.replace ~rex:(Pcre.regexp " +$") ~itempl x

let string_of_atoms x =
String.concat " " (List.map string_of_atom x)

let bib_of_bibtex = function
| Comment _ | Preamble _ | Abbrev _ -> None
| Entry (ety, key, el) as bt ->
let t = Bib.t_of_string ety in
let try_assoc k = try string_of_atoms (List.assoc k el) with Not_found -> "" in
let authors = List.map normalize_author (Pcre.split ~pat:" and " (try_assoc "author")) in
let year = try Some (int_of_string (try_assoc "year")) with _ -> None in
let title = try_assoc "title" in
let misc = List.map (fun (k,v) -> (k, (string_of_atoms v))) (List.filter (fun (k,_) -> match k with "author" |"year" |"title" -> false |_ -> true) el) in
Some { Bib.ty=t; key=key; authors=authors; year=year; title=title; misc=misc }

let output_bibtexs cl =
fold output_bibtex cl ()

let save_bibtexs bs dbname =
let db = Bib.ent_init dbname in
List.iter (fun c -> printf ".%!"; Bib.ent_save db c) bs

let _ =
match Sys.argv with
| [| _; file; db |] ->
let bt = read_bibtex file in
let bso = List.map bib_of_bibtex bt in
let bs = List.fold_left (fun a -> function None -> a | Some b -> b :: a) [] bso in
save_bibtexs bs db
| _ ->
eprintf "Usage: %s <bibtex> <db>\n%!" Sys.argv.(0)

0 comments on commit 14f8e3c

Please sign in to comment.