Browse Source

minor style improvements, add syntax highlighting with prism (loaded

only on some pages), fmt
master
zapashcanon 5 months ago
parent
commit
362997f9ca
Signed by: zapashcanon GPG Key ID: 8981C3C62D1D28F1
  1. 2
      .ocamlformat
  2. 1
      src/content/assets/css/prism.css
  3. 18
      src/content/assets/css/style.css
  4. 29
      src/content/assets/js/prism.js
  5. 26
      src/content/ocaml.md
  6. 18
      src/template.eml.html
  7. 8
      src/www.ml

2
.ocamlformat

@ -1,4 +1,4 @@
version=0.21.0
version=0.22.4
assignment-operator=end-line
break-cases=fit
break-fun-decl=wrap

1
src/content/assets/css/prism.css

@ -0,0 +1 @@
code[class*=language-],pre[class*=language-]{color:#ccc;background:0 0;font-family:Consolas,Monaco,'Andale Mono','Ubuntu Mono',monospace;font-size:1em;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none}pre[class*=language-]{padding:1em;margin:.5em 0;overflow:auto}:not(pre)>code[class*=language-],pre[class*=language-]{background:#2d2d2d}:not(pre)>code[class*=language-]{padding:.1em;border-radius:.3em;white-space:normal}.token.block-comment,.token.cdata,.token.comment,.token.doctype,.token.prolog{color:#999}.token.punctuation{color:#ccc}.token.attr-name,.token.deleted,.token.namespace,.token.tag{color:#e2777a}.token.function-name{color:#6196cc}.token.boolean,.token.function,.token.number{color:#f08d49}.token.class-name,.token.constant,.token.property,.token.symbol{color:#f8c555}.token.atrule,.token.builtin,.token.important,.token.keyword,.token.selector{color:#cc99cd}.token.attr-value,.token.char,.token.regex,.token.string,.token.variable{color:#7ec699}.token.entity,.token.operator,.token.url{color:#67cdcc}.token.bold,.token.important{font-weight:700}.token.italic{font-style:italic}.token.entity{cursor:help}.token.inserted{color:green}

18
src/content/assets/css/style.css

@ -41,10 +41,6 @@ a {
color: #98c379;
}
a:visited {
color: #c678dd;
}
.featurette-divider {
margin: 5rem 0;
}
@ -75,6 +71,15 @@ em {
color: #e5c07b;
}
main {
margin-left: 10em;
margin-right: 10em;
}
main a:visited {
color: #c678dd;
}
/*
p {
background-color: #353b45;
@ -83,11 +88,16 @@ p {
/* MENU */
nav {
text-align: center;
}
nav ul li:hover > ul {
display: block;
}
nav ul {
text-align: left;
padding: 0 20px;
list-style: none;
position: relative;

29
src/content/assets/js/prism.js

File diff suppressed because one or more lines are too long

26
src/content/ocaml.md

@ -8,7 +8,7 @@ In the discuss post [What is the use of Continuation Passing Style (CPS)?] I wro
I wrote a small (classical) example:
```
```ocaml
(* computing the length of a list, not tail-recursive *)
let rec list_length = function
| [] -> 0
@ -55,7 +55,7 @@ let () =
Also note that getting from the non-CPS version to the CPS one is only a syntactic transformation:
```
```ocaml
let rec tree_height t = match t with
| Empty -> 0
| Node (_, l, r) -> 1 + max (tree_height l) (tree_height r)
@ -90,7 +90,7 @@ let rec tree_height t k = match t with
In the discuss post [Representing data more compactly but unsafely], [Emile Trotignon] gave the following example which I really like:
```
```ocaml
type a =
| Int of int
| Float of float
@ -130,31 +130,31 @@ When I teach OCaml to some people, after some time, I like to ask them this ques
<a data-bs-toggle="collapse" href="#unitequal1">First answer</a>:
<code class="collapse" id="unitequal1">let equal x y = if x = y then true else false</code>
<code class="collapse language-ocaml" id="unitequal1">let equal x y = if x = y then true else false</code>
Come on ! First you should write it `let equal x y = x = y`. But more importantly, there's no need to test for equality, there's only one inhabitant for the `unit` type, so it's always `true`. Try again !
<a data-bs-toggle="collapse" href="#unitequal2">Second answer</a>:
<code class="collapse" id="unitequal2">let equal x y = true</code>
<code class="collapse language-ocaml" id="unitequal2">let equal x y = true</code>
Well, `dune` will make the compiler shrill because of unused variables. Let's try again.
<a data-bs-toggle="collapse" href="#unitequal3">Third answer</a>:
<code class="collapse" id="unitequal3">let equal _ _ = true</code>
<code class="collapse language-ocaml" id="unitequal3">let equal _ _ = true</code>
Better, but this has the signature `val equal : 'a -> 'b -> bool` (and the previous answer too). How can we fix this ?
<a data-bs-toggle="collapse" href="#unitequal4">Fourth answer</a>:
<code class="collapse" id="unitequal4">let equal (_ : unit) (_ : unit) = true</code>
<code class="collapse language-ocaml" id="unitequal4">let equal (_ : unit) (_ : unit) = true</code>
OK. Now this is correct, but it's annoying we have to write the types. Unless...
<a data-bs-toggle="collapse" href="#unitequal5">Fifth answer</a>:
<code class="collapse" id="unitequal5">let equal () () = true</code>
<code class="collapse language-ocaml" id="unitequal5">let equal () () = true</code>
Yippee ! Usually the beginner is a little dumbfounded by this, as I was the first time I found this code in the stdlib. By the way, if you know a shortest way to write these (modulo whitespace changes), please [tell me].
@ -162,7 +162,7 @@ Then, what if you want to confuse the beginner completly ?
<a data-bs-toggle="collapse" href="#unitequal6">Sixth answer</a>:
<code class="collapse" id="unitequal6">let equal () = (=) ()</code>
<code class="collapse language-ocaml" id="unitequal6">let equal () = (=) ()</code>
#### What's the shortest code producing the signature `val f : 'a -> 'b` ?
@ -170,19 +170,19 @@ This question has been asked to me by [Jean-Christophe Filliâtre] while we were
<a data-bs-toggle="collapse" href="#shortestab1">First solution in 20 characters</a>:
<code class="collapse" id="shortestab1">let f _=assert false</code>
<code class="collapse language-ocaml" id="shortestab1">let f _=assert false</code>
<a data-bs-toggle="collapse" href="#shortestab2">Second solution in 15 characters</a>:
<code class="collapse" id="shortestab2">let rec f x=f x</code>
<code class="collapse language-ocaml" id="shortestab2">let rec f x=f x</code>
<a data-bs-toggle="collapse" href="#shortestab3">Third solution in 15 characters</a>:
<code class="collapse" id="shortestab3">let f=Obj.magic</code>
<code class="collapse language-ocaml" id="shortestab3">let f=Obj.magic</code>
<a data-bs-toggle="collapse" href="#shortestab4">Fourth solution in 14 characters</a>:
<code class="collapse" id="shortestab4">let f _=exit 1</code>
<code class="collapse language-ocaml" id="shortestab4">let f _=exit 1</code>
I don't know any better solution, if you do please [tell me] !

18
src/template.eml.html

@ -1,10 +1,19 @@
let render ~title ~content =
type page = {
title : string;
content : string;
need_prism : bool;
}
let render page =
<!DOCTYPE html>
<html lang="en">
<head>
<title><%s title %></title>
<title><%s page.title %></title>
<link rel="icon" type="image/svg+xml" href="/assets/img/favicon.png">
<link href="/assets/css/style.css" rel="stylesheet">
% begin if page.need_prism then
<link href="/assets/css/prism.css" rel="stylesheet">
% end;
</head>
<body>
<header>
@ -56,7 +65,7 @@ let render ~title ~content =
</header>
<main>
<div>
<%s! content %>
<%s! page.content %>
</div>
<hr class="featurette-divider">
<footer>
@ -66,5 +75,8 @@ let render ~title ~content =
</p>
</footer>
</main>
% begin if page.need_prism then
<script src="/assets/js/prism.js"></script>
% end;
</body>
</html>

8
src/www.ml

@ -1,4 +1,4 @@
let render content =
let render content need_prism =
let title =
let open Soup in
try
@ -7,7 +7,8 @@ let render content =
Format.sprintf "%s | zapashcanon" title
with Failure _e -> "zapashcanon"
in
Dream.html @@ Template.render ~title ~content
let page = { Template.title; content; need_prism } in
Dream.html @@ Template.render page
let asset_loader _root path _request =
match Content.read ("assets/" ^ path) with
@ -19,7 +20,8 @@ let given_page name _request =
| None -> Dream.empty `Not_Found
| Some page ->
let content = Omd.of_string page |> Omd.to_html in
render content
let need_prism = Array.mem name [| "ocaml" |] in
render content need_prism
let page request =
let name = Dream.param request "page" in

Loading…
Cancel
Save