Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Output JSON member

  • Loading branch information...
commit 7f12f92c9b34f82605abfa58a1590e568332681c 1 parent 9764c30
@VictorNicollet authored
View
16 ohmStatic/tool/buf.ml
@@ -1,9 +1,15 @@
(* Ohm is © 2012 Victor Nicollet *)
+let extract content =
+ List.rev_map (function
+ | `URL u -> `URL u
+ | `BUF b -> `RAW (Buffer.contents b)) content
+
class t = object (self)
val mutable buffer = Buffer.create 1024
val mutable content = ([] : [`BUF of Buffer.t|`URL of string] list)
+ val mutable json = ([] : (string * [`BUF of Buffer.t | `URL of string] list) list)
method url k =
if Buffer.length buffer > 0 then (
@@ -41,12 +47,10 @@ class t = object (self)
self # raw ">"
method contents : [ `URL of string | `RAW of string ] list =
+ if Buffer.length buffer > 0 then content <- `BUF buffer :: content ;
+ extract content
- if Buffer.length buffer > 0 then
- content <- `BUF buffer :: content ;
-
- List.rev_map (function
- | `URL u -> `URL u
- | `BUF b -> `RAW (Buffer.contents b)) content
+ method json : (string * [`URL of string | `RAW of string] list) list =
+ List.map (fun (key,json) -> key, extract json) json
end
View
2  ohmStatic/tool/buf.mli
@@ -3,11 +3,13 @@
class t : object ('self)
val mutable buffer : Buffer.t
val mutable content : [ `URL of string | `BUF of Buffer.t] list
+ val mutable json : (string * [`URL of string | `BUF of Buffer.t] list) list
method url : string -> unit
method raw : string -> unit
method esc : string -> unit
method tag : string -> (string * string) list -> ('self -> unit) -> unit
method contents : [`URL of string | `RAW of string ] list
+ method json : (string * [`URL of string | `RAW of string ] list) list
end
View
21 ohmStatic/tool/run.ml
@@ -44,7 +44,7 @@ let extract file lexer =
lexer hub buf lexbuf ;
Pervasives.close_in channel ;
- buf # contents
+ buf
with exn ->
@@ -169,7 +169,7 @@ let generate ?name root =
| `RAW s -> Buffer.add_string mlbuf
(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)) page ;
+ (Printf.sprintf " Ohm.Html.esc (url %S) html ;\n" s)) (page # contents) ;
(* METHOD "css" *)
Buffer.add_string mlbuf " method css url = []\n" ;
@@ -185,6 +185,23 @@ let generate ?name root =
(* METHOD "title" *)
Buffer.add_string mlbuf " method title = None\n" ;
+
+ (* METHOD "json" *)
+ Buffer.add_string mlbuf " method json url = [\n" ;
+ List.iter (fun (key,json) ->
+ Buffer.add_string mlbuf (Printf.sprintf " %S, " key) ;
+ begin match json with
+ | [] -> Buffer.add_string mlbuf "Json.Null"
+ | [ `RAW s ] -> Buffer.add_string mlbuf (Printf.sprintf "Ohm.Json.deserialize %S" s)
+ | _ ->
+ Buffer.add_string mlbuf "Ohm.Json.deserialize (String.concat \"\" [\n" ;
+ List.iter (function
+ | `RAW s -> Buffer.add_string mlbuf (Printf.sprintf " %S ;\n" s)
+ | `URL s -> Buffer.add_string mlbuf (Printf.sprintf " url %S ;\n" s)) json ;
+ Buffer.add_string mlbuf " ])"
+ end ;
+ Buffer.add_string mlbuf " ;\n") (page # json) ;
+ Buffer.add_string mlbuf " ]\n" ;
Buffer.add_string mlbuf " end) ;\n"
Please sign in to comment.
Something went wrong with that request. Please try again.