forked from ocaml/v2.ocaml.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbreadcrumb.ml
More file actions
68 lines (60 loc) · 2.45 KB
/
breadcrumb.ml
File metadata and controls
68 lines (60 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
(* Enable breadcrumb navigation on deeper level pages *)
open Printf
open Utils
let title_re = Str.regexp "((! *set *title *\\([^ ][^!]*\\)!))"
let get_title lang path =
let s = string_of_file path in
try
ignore(Str.search_forward title_re s 0); (* or Not_found *)
String.trim (Str.matched_group 1 s)
with Not_found ->
String.capitalize(Filename.basename path)
let rec breadcrumb_of_path bc lang path =
if path = "site" || path = "." then ("/", "Home") :: bc
else (
let index = ["", "index.md"; "", "index.html"] in
let index = if lang = "" then index
else (lang, "index." ^ lang ^ ".md")
:: (lang, "index." ^ lang ^ ".html") :: index in
let index = List.map (fun (l,f) -> (l, Filename.concat path f)) index in
(* Remove the first directory. *)
let link = "/" ^ (String.concat "/" (List.tl (Neturl.split_path path))) in
let entry =
try
let index_lang, index = List.find (fun (_,f) -> Sys.file_exists f) index in
let title = get_title lang index in
let link = if index_lang = "" then link ^ "/"
else link ^ "/index." ^ index_lang ^ ".html" in
(link, title)
with Not_found ->
(* No index file found. *)
(link ^ "/", String.capitalize(Filename.basename path)) in
breadcrumb_of_path (entry :: bc) lang (Filename.dirname path)
)
let rec to_html bc =
let b = Buffer.create 1024 in
let rec add_component = function
| [] -> assert false
| [_, title] -> bprintf b "<li class=\"active\">%s</li>\n" title
| (p, title) :: tl ->
bprintf b "<li><a href=\"%s\">%s</a><span class=\"divider\">\
</span></li>\n" p title;
add_component tl in
add_component bc;
Buffer.contents b
let () =
(* Pass a filename relative to the root of the project. Example:
"site/learn/companies.md" *)
let path_filename = Sys.argv.(1) in
let prefix, lang, _ =
prefix_lang_ext_of_filename (Filename.basename path_filename) in
let title = get_title lang path_filename in
let path = if prefix = "index" then
Filename.dirname(Filename.dirname path_filename)
else Filename.dirname path_filename in
(* The current entry is not a link, leaves that part empty. *)
let breadcrumb = breadcrumb_of_path [("", title)] lang path in
print_endline (to_html breadcrumb)
(* Local Variables: *)
(* compile-command: "make --no-print-directory -k -C .. script/breadcrumb" *)
(* End: *)