Skip to content

Commit

Permalink
New site format
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorNicollet committed Sep 6, 2012
1 parent 08ea5a3 commit d162544
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 10 deletions.
50 changes: 48 additions & 2 deletions ohmStatic/ohmStatic.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,53 @@
(* Ohm is © 2012 Victor Nicollet *)

open BatPervasives
open Ohm
open Ohm.Universal

type key = string
type renaming = key -> string
type item = [ `Page of (renaming -> Ohm.Html.writer)
| `File of string ]
type page = <
body : renaming -> Ohm.Html.writer ;
css : renaming -> string list ;
js : renaming -> string list ;
head : renaming -> string ;
bcls : string list ;
title : string option ;
>
type item = [ `Page of page | `File ]
type site = (string,item) BatPMap.t

let export ?(rename=identity) ?(render=Html.print_page_ctx) ?(public="/public/") ~server ~title site =

let endpoints, definitions =
BatPMap.foldi begin fun key item (endpoints, definitions) ->
match item with `File -> (endpoints,definitions) | `Page page ->
let endpoint, define = Action.declare server (rename key) Action.Args.none in
BatPMap.add key endpoint endpoints,
(define,page) :: definitions
end site (BatPMap.empty, [])
in

let url server key =
try Action.url (BatPMap.find key endpoints) server ()
with Not_found -> public ^ key
in

List.iter begin fun (define,page) ->

define begin fun req res ->

let rename = url (req # server) in
let body = page # body rename in
let css = page # css rename in
let js = page # js rename in
let head = page # head rename in
let bcls = page # bcls in
let title = BatOption.default title (page # title) in

let! page = ohm $ render ~css ~js ~head ~body_classes:bcls ~title body in
return $ Action.page page res

end
end definitions

34 changes: 32 additions & 2 deletions ohmStatic/ohmStatic.mli
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,43 @@ type key = string
*)
type renaming = key -> string

(** The type of a page. This determines what the title of the page is, what javascript
and CSS files are to be included, and so on.
*)
type page = <
body : renaming -> Ohm.Html.writer ;
css : renaming -> string list ;
js : renaming -> string list ;
head : renaming -> string ;
bcls : string list ;
title : string option ;
>

(** The type of a static. This is either a bit of HTML, or a standalone file that
can become downloadable.
*)
type item = [ `Page of (renaming -> Ohm.Html.writer)
| `File of string ]
type item = [ `Page of page | `File ]

(** The type of a static site - it maps the key (which is the relative path of an
item within the /static directory) to its contents.
*)
type site = (string,item) BatPMap.t

(** Export a static site.
@param rename A function that provides the path of each item. By default,
the item's key is used as its path.
@param server The server on which the site should run.
@param title The default title to be used, if no title is provided by the page.
@param render The function which is used to render the item. By default,
this is the vanilla [Ohm.Html.print_page].
@param public The url prefix for files that are available for public download.
By default, this is ["/public"] and points to the [www/public] directory.
*)
val export :
?rename:renaming
-> ?render:(unit Ohm.Html.ctxrenderer)
-> ?public:string
-> server:('s Ohm.Action.server)
-> title:string
-> site
-> unit
32 changes: 26 additions & 6 deletions ohmStatic/tool/run.ml
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,44 @@ let generate ?name root =
let mlbuf = Buffer.create 1024 in

Buffer.add_string mlbuf "(* This file was generated by plugin ohmStatic *)\n" ;
Buffer.add_string mlbuf "let pages = BatPMap.of_enum (BatList.enum [\n" ;
Buffer.add_string mlbuf "let site = BatPMap.of_enum (BatList.enum [\n" ;

List.iter (fun (path,contents) ->
Buffer.add_string mlbuf (Printf.sprintf " %S, `Page begin fun url html -> \n" path) ;

Buffer.add_string mlbuf (Printf.sprintf " %S, `Page (object\n" path) ;

(* METHOD "body" *)
Buffer.add_string mlbuf " method body url html = \n" ;
List.iter (function
| `RAW s -> Buffer.add_string mlbuf
(Printf.sprintf " Ohm.Html.str %S html ;\n" s)
(Printf.sprintf " Ohm.Html.str %S html ;\n" s)
| `URL s -> Buffer.add_string mlbuf
(Printf.sprintf " Ohm.Html.esc (url %S) html ;\n" s)) contents ;
Buffer.add_string mlbuf " end ;\n"
(Printf.sprintf " Ohm.Html.esc (url %S) html ;\n" s)) contents ;

(* METHOD "css" *)
Buffer.add_string mlbuf " method css url = []\n" ;

(* METHOD "js" *)
Buffer.add_string mlbuf " method js url = []\n" ;

(* METHOD "head" *)
Buffer.add_string mlbuf " method head url = \"\"\n" ;

(* METHOD "bcls" *)
Buffer.add_string mlbuf " method bcls = []\n" ;

(* METHOD "title" *)
Buffer.add_string mlbuf " method title = None\n" ;

Buffer.add_string mlbuf " end) ;\n"
) clean ;

Buffer.add_string mlbuf "])\n\n" ;
Buffer.contents mlbuf
in

let mlifile =
"val pages : OhmStatic.site\n"
"val site : OhmStatic.site\n"
in

let name = BatOption.default (String.lowercase (Filename.basename root)) name in
Expand Down

0 comments on commit d162544

Please sign in to comment.