Skip to content

Commit 5e6501d

Browse files
committed
Merge pull request #121 from ocsigen/ppx_examples
Add ppx examples and fix various associated bugs
2 parents 2f99cf3 + 7972659 commit 5e6501d

File tree

14 files changed

+202
-11
lines changed

14 files changed

+202
-11
lines changed

_oasis

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,20 @@ Test basic_website
203203
TestTools: basic_website
204204
Run$: flag(tests)
205205

206+
Executable basic_website_ppx
207+
Install: false
208+
Build$: flag(tests) && flag(tests)
209+
Path: examples/basic_website_ppx
210+
MainIs: site_html.ml
211+
BuildDepends: tyxml
212+
CompiledObject: best
213+
214+
Test basic_website_ppx
215+
WorkingDirectory: examples/basic_website_ppx
216+
Command: $basic_website_ppx
217+
TestTools: basic_website_ppx
218+
Run$: flag(tests) && flag(tests)
219+
206220
Executable mini_website
207221
Install: false
208222
Build$: flag(tests)

_tags

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ true: keep_locs
1515
# Tests use the tyxml ppx
1616
<test/*.ml>: ppx_tyxml
1717

18-
<examples/mini_website_ppx/*.ml>: ppx_tyxml
18+
<examples/*_ppx/*.ml>: ppx_tyxml

examples/basic_website_ppx/.merlin

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PKG tyxml.ppx
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
site_gen := make_site
2+
3+
all:
4+
ocamlfind ocamlc site_html.ml -package tyxml.ppx -short-paths -linkpkg -o ${site_gen}
5+
./${site_gen}
6+
7+
clean:
8+
rm -f *.cmo *.cmt *.cmi ${site_gen} index.html
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
This is a very simple website in pure tyxml using the ppx syntax extension.
2+
To generate the website, compile `site_html.ml` and then execute. This can be done with `make`.
3+
4+
Content of this directory:
5+
- `site_html.ml`: Generates the Html.
6+
- `Makefile`: Simple rules to create the website. Uses ocamlbuild
7+
- `main.js` and `home.css` : auxiliary files for the website.
8+
- `.merlin`: An appropriate merlin file.
9+
- Readme.md : You are reading it
10+
11+
This website is distributed under the [unlicense][], feel free to use it!
12+
13+
[unlicense]: http://unlicense.org/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#links_bar li {
2+
margin:1em;
3+
padding:0.4em;
4+
font-size:large;
5+
display:inline;
6+
cursor:pointer;
7+
border:none;
8+
border-radius:0px;
9+
transition:.2s linear;
10+
text-align:center;
11+
}

examples/basic_website_ppx/main.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
4+
var handle = document.getElementById("payload");
5+
6+
console.log(handle);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
open Tyxml
2+
3+
let this_title = Html.pcdata "Your Cool Web Page"
4+
5+
let image_box = [%html
6+
"<div id=image_box></div>"
7+
]
8+
9+
let links_box = [%html {|
10+
<ul class=links_bar id=links_bar>
11+
<li id=home_click >My Musings</li>
12+
<li id=about_click >About Me</li>
13+
<li id=blog_posts_click >Blog</li>
14+
<li id=hackathons_click >Hackathons</li>
15+
</ul>
16+
|}]
17+
18+
let common_footer = [%html {|
19+
<footer id="footer_box">
20+
<p>
21+
This site was made with <a href=http://ocaml.org>OCaml</a> and <a href=https://www.gnu.org/software/emacs/>emacs</a>
22+
</p>
23+
</footer>
24+
|}]
25+
26+
let home_content = [%html
27+
"<div><h2>Hello Coder</h2></div>"
28+
]
29+
30+
let main_payload = [%html
31+
"<div id=payload>"[home_content]"</div>"
32+
]
33+
34+
let common_nav = Html.nav [links_box]
35+
36+
let content_box = [%html
37+
"<div id=content_box>"[
38+
common_nav;
39+
main_payload;
40+
common_footer;
41+
]"</div>"
42+
]
43+
44+
let main_script = [%html
45+
"<script src=main.js> </script>"
46+
]
47+
48+
let home_page_doc = [%html
49+
{|<html>
50+
<head>
51+
<title>|}this_title{|</title>
52+
<link rel=stylesheet href="home.css" />
53+
</head>
54+
<body>|} [ image_box; content_box; main_script ] {|</body>
55+
</html>
56+
|}]
57+
58+
(** The set of pages in your website. *)
59+
let pages = [("index.html", home_page_doc)]
60+
61+
(** Small code to emit all the pages. *)
62+
let emit_page (name, page) =
63+
Printf.printf "Generating: %s\n" name ;
64+
let file_handle = open_out name in
65+
let fmt = Format.formatter_of_out_channel file_handle in
66+
Html.pp () fmt page;
67+
close_out file_handle
68+
69+
let () = List.iter emit_page pages

examples/mini_website_ppx/minihtml.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ let mycontent = [%html {|
1111
let mytitle = Html.pcdata "A Fabulous Web Page"
1212

1313
let mypage = [%html
14-
"<html><head><title>"mytitle"</title></head><body>"mycontent"</body></html>"]
14+
{|<html>
15+
<head>
16+
<title>|}mytitle{|</title>
17+
</head>
18+
<body>"mycontent"</body>
19+
</html>
20+
|}]
1521

1622
let () =
1723
let file = open_out "index.html" in

lib/html_sigs.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,10 @@ module type T = sig
704704
val dl : ([< | dl_attrib], [< | dl_content_fun], [> | dl]) star
705705

706706
val ol : ([< | ol_attrib], [< | ol_content_fun], [> | ol]) star
707+
[@@reflect.element "ol"]
707708

708709
val ul : ([< | ul_attrib], [< | ul_content_fun], [> | ul]) star
710+
[@@reflect.element "ul"]
709711

710712
val dd : ([< | dd_attrib], [< | dd_content_fun], [> | dd]) star
711713

0 commit comments

Comments
 (0)