[Pkg-ocaml-maint-commits] r4369 - in /trunk/tools/ocamldoc-cgi: ./ .depend Makefile README ocamldoc-cgi.ml

zack at users.alioth.debian.org zack at users.alioth.debian.org
Wed Sep 5 14:17:16 UTC 2007


Author: zack
Date: Wed Sep  5 14:17:16 2007
New Revision: 4369

URL: http://svn.debian.org/wsvn/?sc=1&rev=4369
Log:
draft implementation of ocamldoc cgi browser

Added:
    trunk/tools/ocamldoc-cgi/   (with props)
    trunk/tools/ocamldoc-cgi/.depend
    trunk/tools/ocamldoc-cgi/Makefile
    trunk/tools/ocamldoc-cgi/README
    trunk/tools/ocamldoc-cgi/ocamldoc-cgi.ml

Propchange: trunk/tools/ocamldoc-cgi/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Wed Sep  5 14:17:16 2007
@@ -1,0 +1,6 @@
+*.cmi
+*.cmo
+*.cmx
+*.o
+*.a
+ocamldoc-cgi

Added: trunk/tools/ocamldoc-cgi/.depend
URL: http://svn.debian.org/wsvn/trunk/tools/ocamldoc-cgi/.depend?rev=4369&op=file
==============================================================================
    (empty)

Added: trunk/tools/ocamldoc-cgi/Makefile
URL: http://svn.debian.org/wsvn/trunk/tools/ocamldoc-cgi/Makefile?rev=4369&op=file
==============================================================================
--- trunk/tools/ocamldoc-cgi/Makefile (added)
+++ trunk/tools/ocamldoc-cgi/Makefile Wed Sep  5 14:17:16 2007
@@ -1,0 +1,85 @@
+#
+# Generic makefile for OCaml
+#
+# Author: Stefano Zacchiroli <zack at bononia.it>
+#
+# Created:       Tue, 16 Aug 2005 21:51:04 +0200 zack
+# Last-Modified: Wed, 19 Apr 2006 16:01:47 -0400 zack
+#
+
+#  MODULES =		\
+#          foo		\
+#          bar		\
+#          baz		\
+#          $(NULL)
+
+BIN = ocamldoc-cgi
+
+PACKAGES = str unix
+
+OCAMLFIND = ocamlfind
+CAMLP4 = camlp4o
+CFLAGS = -pp $(CAMLP4)
+ifneq ($(strip $(PACKAGES)),)
+CFLAGS += -package "$(PACKAGES)"
+endif
+LDFLAGS = -linkpkg
+OCAMLC = $(OCAMLFIND) ocamlc $(CFLAGS)
+OCAMLOPT = $(OCAMLFIND) ocamlopt $(CFLAGS)
+OCAMLDEP = $(OCAMLFIND) ocamldep $(CFLAGS)
+OCAMLDOC = $(OCAMLFIND) ocamldoc $(CFLAGS)
+OCAMLLEX = ocamllex
+OCAMLYACC = ocamlyacc
+NULL =
+
+HTMLDESTDIR = html
+
+MLS = $(patsubst %,%.ml,$(MODULES))
+MLIS = $(patsubst %,%.mli,$(MODULES))
+CMIS = $(patsubst %,%.cmi,$(MODULES))
+CMOS = $(patsubst %,%.cmo,$(MODULES))
+CMXS = $(patsubst %,%.cmx,$(MODULES))
+MLLS := $(wildcard *.mll)
+MLYS := $(wildcard *.mly)
+OS = $(patsubst %,%.o,$(MODULES))
+
+all: $(BIN)
+opt: $(BIN).opt
+
+doc:
+	test -d $(HTMLDESTDIR)/ || mkdir $(HTMLDESTDIR)/
+	$(OCAMLDOC) -stars -html -d $(HTMLDESTDIR)/ *.ml *.mli
+
+TAGS: *.ml *.mli
+	otags *.ml *.mli
+
+$(BIN): $(CMOS) $(BIN).ml
+	$(OCAMLC) $(LDFLAGS) $^ -o $@
+$(BIN).opt: $(CMXS) $(BIN).ml
+	$(OCAMLOPT) $(LDFLAGS) $^ -o $@
+
+%.cmi: %.mli
+	$(OCAMLC) -c $<
+%.cmo %.cmi: %.ml
+	$(OCAMLC) -c $<
+%.annot: %.ml
+	$(OCAMLC) -c $< -dtypes
+%.cmx: %.ml
+	$(OCAMLOPT) -c $<
+%.ml: %.mll
+	$(OCAMLLEX) $<
+%.ml %.mli: %.mly
+	$(OCAMLYACC) $<
+
+clean:
+	rm -f $(CMIS) $(CMOS) $(CMXS) $(OS)			\
+		$(BIN).cmi $(BIN).cmo $(BIN).cmx $(BIN).o	\
+		$(BIN) $(BIN).opt				\
+		$(patsubst %.mll,%.ml,$(MLLS))			\
+		$(patsubst %.mly,%.ml,$(MLYS))			\
+		$(patsubst %.mly,%.mli,$(MLYS))
+
+depend:
+	$(OCAMLDEP) $(MLS) $(MLIS) $(BIN).ml > .depend
+
+include .depend

Added: trunk/tools/ocamldoc-cgi/README
URL: http://svn.debian.org/wsvn/trunk/tools/ocamldoc-cgi/README?rev=4369&op=file
==============================================================================
--- trunk/tools/ocamldoc-cgi/README (added)
+++ trunk/tools/ocamldoc-cgi/README Wed Sep  5 14:17:16 2007
@@ -1,0 +1,9 @@
+
+Prototype implementation of a CGI-based documentation browser for all the OCaml
+documentation (but specially aimed at API references) registered with doc-base.
+
+When ready this code will be probably moved in the ocaml package itself.
+
+Stefano Zacchiroli
+Wed, 05 Sep 2007 16:10:42 +0200
+

Added: trunk/tools/ocamldoc-cgi/ocamldoc-cgi.ml
URL: http://svn.debian.org/wsvn/trunk/tools/ocamldoc-cgi/ocamldoc-cgi.ml?rev=4369&op=file
==============================================================================
--- trunk/tools/ocamldoc-cgi/ocamldoc-cgi.ml (added)
+++ trunk/tools/ocamldoc-cgi/ocamldoc-cgi.ml Wed Sep  5 14:17:16 2007
@@ -1,0 +1,204 @@
+(*
+ * ocamldoc: a CGI browser for OCaml-related Debian doc-base documentation
+ * Copyright (C) 2007  Stefano Zacchiroli <zack at debian.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *)
+
+open Printf
+
+  (** the given filename contains a malformed entry for the doc-base database *)
+exception Malformed_docbase_entry
+
+  (** format-agnostic information for a doc-base entry *)
+type docbase_header = {
+  id: string;
+  mutable package: string;
+  title: string;
+  author: string option;
+  abstract: string option;
+  section: string;
+}
+
+let apiref_convention_RE = Str.regexp "^\\([^ ]+\\)-ocamldoc-api-reference"
+
+  (** a doc-base document in a single format *)
+type docbase_doc = {
+  fmt: [ `html | `txt | `pdf | `ps | `info | `dvi | `debian_sgml ];
+  index: string option;
+  files: string list;
+}
+
+  (** an entry registered within the doc-base database *)
+type docbase_entry = {
+  header: docbase_header;
+  documents: docbase_doc list;
+}
+
+let rec filter_map f = function
+  | [] -> []
+  | hd :: tl ->
+      match f hd with
+      | Some x -> x :: filter_map f tl
+      | None -> filter_map f tl
+
+let split =
+  let blanks_RE = Str.regexp "[ \t]*" in
+  Str.split blanks_RE
+
+  (* XXX side-effect: sets package field *)
+let is_ocamldoc_apiref header =
+  if Str.string_match apiref_convention_RE header.id 0 then begin
+    header.package <- Str.matched_group 1 header.id;
+    true
+  end else
+    false
+
+let ls_files dir =
+  let handle = Unix.opendir dir in
+  let files = ref [] in
+  (try
+    while true do
+      let file = Filename.concat dir (Unix.readdir handle) in
+      if (Unix.stat file).Unix.st_kind = Unix.S_REG then
+        files := file :: !files
+    done
+  with End_of_file -> ());
+  !files
+
+let parse_stanza ic =
+  let fields = ref [] in
+  let field_RE = Str.regexp "^\\([^:]+\\):[ \t]*\\(.*\\)$" in
+  let empty_RE = Str.regexp "^[ \t]*$" in
+  let indented_RE = Str.regexp "^[ \t]" in
+  (try
+    while true do
+      let line = input_line ic in
+      if Str.string_match empty_RE line 0 then
+        raise End_of_file
+      else if Str.string_match indented_RE line 0 then
+        (* field value continuation; add this line to the field value
+         * accumulated so far *)
+        let addendum = Str.replace_first indented_RE "" line in
+        try
+          let n, v = List.hd !fields in
+          fields := (n, v ^ addendum) :: (List.tl !fields)
+        with Failure _ -> raise Malformed_docbase_entry
+      else
+        if Str.string_match field_RE line 0 then
+          fields :=
+            ((Str.matched_group 1 line),
+             (try Str.matched_group 2 line with Not_found -> ""))
+            :: !fields
+        else
+          raise Malformed_docbase_entry
+    done
+  with End_of_file -> ());
+  !fields
+
+let (=~) s1 s2 = (String.lowercase s1 = String.lowercase s2)
+let get_field name fields = snd (List.find (fun (n, _v) -> n =~ name) fields)
+
+let get_opt_field name fields =
+  try
+    Some (get_field name fields)
+  with Not_found -> None
+
+let parse_docbase_header ic =
+  let stanza = parse_stanza ic in
+  try
+    { id = get_field "document" stanza;
+      package = "";
+      title = get_field "title" stanza;
+      author = get_opt_field "author" stanza;
+      abstract = get_opt_field "abstract" stanza;
+      section = get_field "section" stanza;
+    }
+  with Not_found -> raise Malformed_docbase_entry
+
+let parse_docbase_doc ic =
+  let stanza = parse_stanza ic in
+  if stanza = [] then
+    raise End_of_file;
+  try
+    let fmt = 
+      match String.lowercase (get_field "format" stanza) with
+      | "html" -> `html
+      | "text" -> `txt
+      | "pdf" -> `pdf
+      | "postscript" -> `ps
+      | "info" -> `info
+      | "dvi" -> `dvi
+      | "debiandoc-sgml" -> `debian_sgml
+      | _ -> raise Malformed_docbase_entry in
+    { fmt = fmt;
+      index = get_opt_field "index" stanza;
+      files = split (get_field "files" stanza)
+    }
+  with Not_found -> raise Malformed_docbase_entry
+
+let add_docbase_docs header ic =
+  let docs = ref [] in
+  (try
+    while true do
+      docs := parse_docbase_doc ic :: !docs
+    done
+  with End_of_file -> ());
+  { header = header;
+    documents = List.rev !docs;
+  }
+
+let print_headers () = printf "Content-Type: text/html\r\n\r\n%!"
+
+let render_apiref entry =
+  sprintf "<li class='doc'> <a href='/doc/%s/html/api/'>%s</a> </li>\n"
+    entry.header.package
+    entry.header.id
+
+let main =
+  print_headers ();
+  printf "<html>\n";
+  printf "<head><title>API references for the available OCaml libraries</title></head>\n";
+  printf "<body>\n";
+  let files =
+    try
+      [ Sys.argv.(1) ]
+    with Invalid_argument _ -> ls_files "/usr/share/doc-base/" in
+  let apirefs =
+    filter_map
+      (fun fname ->
+        let ic = open_in fname in
+        let res = 
+          try
+            let header = parse_docbase_header ic in
+            if is_ocamldoc_apiref header then
+              Some (add_docbase_docs header ic)
+            else
+              None
+          with Malformed_docbase_entry ->
+            printf
+              "<span class='error'>Ignoring \"%s\", malformed doc-base entry.</span><br />\n"
+              fname;
+            None
+        in
+        close_in ic;
+        res)
+      files in
+  printf "<ul class='docs'>\n%!";
+  List.iter (fun s -> printf "%s%!" s)
+    (List.map render_apiref apirefs);
+  printf "</ul>\n";
+  printf "</body>\n";
+  printf "</html>\n%!"
+




More information about the Pkg-ocaml-maint-commits mailing list