Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor to use pageinfo object

  • Loading branch information...
commit 91fb3742d51d8294ffc9637114f058123c602fea 1 parent 7f12f92
@VictorNicollet authored
Showing with 94 additions and 23 deletions.
  1. +68 −18 ohmStatic/ohmStatic.ml
  2. +26 −5 ohmStatic/ohmStatic.mli
View
86 ohmStatic/ohmStatic.ml
@@ -15,7 +15,20 @@ type page = <
head : renaming -> string ;
bcls : string list ;
title : string option ;
+ json : renaming -> (string * Json.t) list ;
>
+type pageinfo = <
+ body : Ohm.Html.writer ;
+ css : string list ;
+ js : string list ;
+ head : string ;
+ bcls : string list ;
+ title : string ;
+ key : key ;
+ url : string ;
+ json : (string * Json.t) list
+>
+type 'ctx renderer = pageinfo -> ('ctx, JsCode.t -> string) Run.t
type item = [ `Page of page | `File of string ]
type site = (string,item) BatPMap.t
@@ -37,24 +50,36 @@ let canonical = function
| s when ends s ".md" -> clip ".md" s
| s -> s
-type 'ctx renderer = key -> 'ctx Html.ctxrenderer
+let generic_render ?writer (page:Html.renderer) (info:pageinfo) =
+
+ let writer = BatOption.default (info # body) writer
+ and css = info # css
+ and js = info # js
+ and head = info # head
+ and body_classes = info # bcls
+ and title = info # title
+ in
+
+ return (page ~css ~js ~head ~body_classes ~title writer)
+
+let default_render info = generic_render O.page info
-let default_render _ ?css ?js ?head ?favicon ?body_classes ~title writer =
- return (O.page ?css ?js ?head ?favicon ?body_classes ~title writer)
+let custom_render page info = generic_render page info
-let wrap template key ?css ?js ?head ?favicon ?body_classes ~title writer =
- let! writer = ohm (template writer) in
- return (O.page ?css ?js ?head ?favicon ?body_classes ~title writer)
+let wrap ?(page=O.page) template info =
+ let! writer = ohm (template (info # body)) in
+ generic_render ~writer page info
-let prefixed_render ~default list key =
+let prefixed_render ~default list info =
+ let key = info # key in
let page =
try snd (List.find (fun (prefix,_) -> BatString.starts_with key prefix) list)
with _ -> default
in
- page key
+ page info
-let with_context ctx page key ?css ?js ?head ?favicon ?body_classes ~title writer =
- Run.with_context ctx (page key ?css ?js ?head ?favicon ?body_classes ~title writer)
+let with_context ctx page info =
+ Run.with_context ctx (page info)
let export ?(rename=canonical) ?(render=default_render) ?(public="/") ~server ~title site =
@@ -84,15 +109,40 @@ let export ?(rename=canonical) ?(render=default_render) ?(public="/") ~server ~t
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 rename = url (req # server) in
+
+ let info = object
+
+ val url = lazy (rename key)
+ method url = Lazy.force url
+
+ val body = page # body rename
+ method body = body
+
+ val css = page # css rename
+ method css = css
+
+ val js = page # js rename
+ method js = js
+
+ val head = page # head rename
+ method head = head
+
+ val bcls = page # bcls
+ method bcls = bcls
+
+ val title = BatOption.default title (page # title)
+ method title = title
+
+ val key = key
+ method key = key
+
+ val json = lazy (page # json rename)
+ method json = Lazy.force json
+
+ end in
- let! page = ohm $ render key ?favicon:None ~css ~js ~head ~body_classes:bcls ~title body in
+ let! page = ohm (render info) in
return $ Action.page page res
end
View
31 ohmStatic/ohmStatic.mli
@@ -34,8 +34,23 @@ type page = <
head : renaming -> string ;
bcls : string list ;
title : string option ;
+ json : renaming -> (string * Ohm.Json.t) list ;
>
+(** Information about a page. Provided to renderers.
+*)
+type pageinfo = <
+ body : Ohm.Html.writer ;
+ css : string list ;
+ js : string list ;
+ head : string ;
+ bcls : string list ;
+ title : string ;
+ key : key ;
+ url : string ;
+ json : (string * Ohm.Json.t) list
+>
+
(** The type of a static. This is either a bit of HTML, or a standalone file that
can become downloadable.
*)
@@ -51,14 +66,20 @@ type site = (string,item) BatPMap.t
*)
val canonical : key -> string
-(** A page renderer. Behaves like an [Ohm.Html.ctxrenderer] except that it also
- receives the key of the rendered document as its first parameter. *)
-type 'ctx renderer = key -> 'ctx Ohm.Html.ctxrenderer
+(** A page renderer. Behaves like an [Ohm.Html.ctxrenderer], but is provided with
+ all its arguments as a single {!type:pageinfo}. *)
+type 'ctx renderer = pageinfo -> ('ctx, Ohm.JsCode.t -> string) Ohm.Run.t
+
+(** Create a renderer from a custom page renderer. This simply uses the selected
+ page renderer instead of [O.page].
+*)
+val custom_render : Ohm.Html.renderer -> 'ctx renderer
(** Create a renderer from a wrapper template : the page contents are passed to the
wrapper template function, and then rendered with the vanilla [O.page]. *)
val wrap :
- (Ohm.Html.writer -> ('ctx, Ohm.Html.writer) Ohm.Run.t)
+ ?page:Ohm.Html.renderer
+ -> (Ohm.Html.writer -> ('ctx, Ohm.Html.writer) Ohm.Run.t)
-> 'ctx renderer
(** Combine multiple renderers : select which renderer to use based on the prefix
@@ -81,7 +102,7 @@ val with_context : 'ctx -> 'ctx renderer -> unit renderer
@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 [O.page].
+ this uses the vanilla [O.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.
*)
Please sign in to comment.
Something went wrong with that request. Please try again.