Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be compatible with Cow ≥ 2.0.0 #147

Merged
merged 1 commit into from Nov 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion opam
Expand Up @@ -23,7 +23,7 @@ depends: [
"opamfu" {>= "0.1.2"}
"re"
"uri" {>= "1.3.11"}
"cow" {>= "0.10.0" & < "2.0"}
"cow" {>= "2.2.0"}
"js_of_ocaml" {>= "2.4.1"}
"cmdliner"
]
Expand Down
4 changes: 2 additions & 2 deletions src/Makefile
Expand Up @@ -10,8 +10,8 @@ WWWDIR ?= www
BUILD=_build

OCAMLBUILD=ocamlbuild -classic-display
PACKAGES=-pkgs re,re.str,omd,cow,cow.syntax,opam-lib.client,opamfu.cli,cmdliner
SYNTAX=-tags "syntax(camlp4o)"
PACKAGES=-pkgs str,omd,cow,opam-lib.client,opamfu.cli,cmdliner
SYNTAX=
FLAGS=-use-ocamlfind -cflags "-bin-annot" -no-links -tags "debug"
INCLUDES=-I apalog
TARGET=opam2web
Expand Down
1 change: 1 addition & 0 deletions src/_tags
@@ -0,0 +1 @@
true: annot, safe_string
109 changes: 47 additions & 62 deletions src/o2wBlog.ml
Expand Up @@ -14,7 +14,7 @@
(* *)
(**************************************************************************)

open Cow.Html
open Cow
open O2wTypes

let header_separator = "^--BODY--$"
Expand Down Expand Up @@ -72,7 +72,7 @@ let html_date timestamp =
(d.tm_year + 1900) (d.tm_mon + 1) d.tm_mday
d.tm_hour d.tm_min d.tm_sec
) in
<:html< <time datetime="$str:d$">$str:short_date timestamp$</time>&>>
Html.time ~datetime:d (Html.string (short_date timestamp))

let to_entry ~content_dir filename =
let name = Filename.chop_extension filename in
Expand Down Expand Up @@ -142,60 +142,49 @@ let make_pages entries =
let items =
List.map (fun entry ->
let classes = if active_entry = entry then "active" else "" in
<:html<
<li class=$str:classes$>
<a href=$str:"../"^entry.blog_name^"/"$>$str: entry.blog_title$</a>
</li>
>>)
Html.li ~cls:classes
(Html.a ~href:(Uri.of_string("../"^entry.blog_name^"/"))
(Html.string entry.blog_title)))
entries
in
<:html<
<ul class="nav nav-pills nav-stacked">
$list:items$
</ul>
>>
Html.ul items ~cls:"nav nav-pills nav-stacked"
in
(* Pages creation *)
let aux_page entry =
let html_authors =
let to_html = function
| author, Some url ->
<:html< <a class="author" href="$str:url$">$str:author$</a> >>
Html.a ~cls:"author" ~href:(Uri.of_string url) (Html.string author)
| author, None ->
<:html< <a class="author">$str:author$</a> >>
Html.a ~cls:"author" (Html.string author)
~href:Uri.empty (* FIXME: remove with new version of Cow *)
in
match List.rev entry.blog_authors with
| [] -> <:html< >>
| [] -> Html.empty
| [single] -> to_html single
| last::secondlast::others ->
List.fold_left (fun h a -> <:html< $to_html a$, $h >>)
<:html< $to_html secondlast$ and $to_html last$ >>
others
List.fold_left (fun h a -> to_html a @ Html.string ", " @ h)
(to_html secondlast @ Html.string " and " @ to_html last)
others
in
let html_body =
<:html<
<div class="page-header"><h3>
$str:entry.blog_title$
<div class="text-right"><small>
On $html_date entry.blog_date$, by <span class="authors">$html_authors$</span>
</small></div>
</h3></div>
$entry.blog_body$
>>
Html.div ~cls:"page-header"
(Html.h3
(Html.string entry.blog_title
@ Html.div ~cls:"text-right"
(Html.small (Html.string "On "
@ html_date entry.blog_date
@ Html.string ", by "
@ Html.span ~cls:"authors" html_authors))))
@ entry.blog_body
in
let page =
<:html<
<div class="row">
<div class="span3">
<div class="bs-docs-menu hidden-phone">
$nav_menu entry$
</div>
</div>
<div class="span9">
$html_body$
</div>
</div>
>>
Html.div ~cls:"row"
(Html.div ~cls:"span3"
(Html.div ~cls:"bs-docs-menu hidden-phone"
(nav_menu entry))
@ Html.div ~cls:"span9"
html_body)
in
Template.serialize page
in
Expand All @@ -204,10 +193,7 @@ let make_pages entries =

let make_menu ?srcurl entries =
let pages = make_pages entries in
let link ?text entry = {
text = OpamMisc.Option.default entry.blog_title text;
href = "blog/" ^ entry.blog_name ^ "/";
} in
let blog_link entry = "blog/" ^ entry.blog_name ^ "/" in
let srcurl entry = match srcurl with
| None -> None
| Some u -> Some (u ^"/"^ Filename.basename entry.blog_source)
Expand All @@ -217,13 +203,15 @@ let make_menu ?srcurl entries =
| first_entry::entries, first_page::pages ->
let first =
[{ menu_source = first_entry.blog_source;
menu_link = link ~text:"Platform Blog" first_entry;
menu_link = blog_link first_entry;
menu_link_text = "Platform Blog";
menu_item = Internal (2, first_page);
menu_srcurl = srcurl first_entry; }] in
let others =
List.map2 (fun entry page ->
{ menu_source = entry.blog_source;
menu_link = link entry;
menu_link = blog_link entry;
menu_link_text = entry.blog_title;
menu_item = No_menu (2, page);
menu_srcurl = srcurl entry; })
entries pages
Expand All @@ -234,21 +222,18 @@ let make_news entries =
let oldest = Unix.time() -. 3600.*.24.*.365. in
let news = List.filter (fun e -> e.blog_date > oldest ) entries in
let mk entry =
let link = "blog/" ^ entry.blog_name ^ "/" in
<:html<
<p>
<i class="icon-ok"> </i>
<strong>$html_date entry.blog_date$</strong>
<a href="$str:link$">$str:entry.blog_title$</a>
<br/>
</p>
>>
let link = Uri.of_string("blog/" ^ entry.blog_name ^ "/") in
Html.p
(Html.i ~cls:"icon-ok" (Html.string " ")
@ Html.strong (html_date entry.blog_date)
@ Html.a ~href:link (Html.string entry.blog_title)
@ Html.br Html.empty)
in
List.fold_left (fun h e -> <:html< $h$ $mk e$ >>) <:html< >> news
List.fold_left (fun h e -> h @Html.string " " @ mk e) Html.empty news

let make_redirect ~root entries =
match entries with
| [] -> <:html<<p>No blog pages.</p>&>>
| [] -> Html.p (Html.string "No blog pages.")
| first_entry::_ ->
let blog_uri =
Uri.(resolve "http" root (of_string "blog/"))
Expand All @@ -257,12 +242,12 @@ let make_redirect ~root entries =
Uri.(resolve "http" blog_uri (of_string (first_entry.blog_name^"/")))
in
let redirect = Printf.sprintf "0;url=%s" (Uri.to_string post_uri) in
<:html<
<html><head>
<title>Latest blog entry (redirect)</title>
<meta http-equiv="refresh" content="$str:redirect$" />
</head><body></body></html>
>>
Html.html
(Html.head
(Html.title (Html.string "Latest blog entry (redirect)")
@ Html.meta ~attrs:["http-equiv", "refresh"; "content", redirect]
Html.empty)
@ Html.body Html.empty)

(*
let resolve_urls_in_attr base rewrites attrs =
Expand Down
94 changes: 40 additions & 54 deletions src/o2wDocumentation.ml
Expand Up @@ -14,7 +14,7 @@
(* *)
(**************************************************************************)

open Cow.Html
open Cow
open O2wTypes

type doc_kind =
Expand Down Expand Up @@ -48,7 +48,7 @@ let split_filename (file: string): (string * string) =
with
Not_found -> file, ""

(* Returns [(source_file, extension, link, menu_entry); ...] *)
(* Returns [(source_file, extension, link, link_text, menu_entry); ...] *)
let read_menu ~dir f =
let lines = OpamProcess.read_lines f in
let srcurl =
Expand All @@ -68,17 +68,17 @@ let read_menu ~dir f =
if String.length extension = 0 then
let empty_filename = OpamFilename.of_string "" in
if String.length title > 0 then
(empty_filename, "", { text=human_title; href="" }, Nav_header)
(empty_filename, "", (* href *) "", (* text *) human_title, Nav_header)
else
(empty_filename, "", { text=""; href="" }, Divider)
(empty_filename, "", (* href *) "", (* text *) "", Divider)
else
let source_file =
Printf.sprintf "%s/%s.%s" dir title extension
in
let source_filename = OpamFilename.of_string source_file in
let dest_file = Printf.sprintf "%s.html" title in
(source_filename, extension,
{ text=human_title; href=dest_file },
(* href *) dest_file, (* text *) human_title,
Internal (1, Template.serialize Cow.Html.nil)
)
in
Expand Down Expand Up @@ -133,7 +133,7 @@ let to_menu_aux ~content_dir ~subdir ?(header=Cow.Html.nil) ~menu_pages ~srcurl
let to_html doc_menu kind filename: Cow.Html.t =
if not (OpamFilename.exists filename) then (
OpamGlobals.warning "%s is not available." (OpamFilename.to_string filename);
<:html< >>
Html.empty
) else
let content = OpamFilename.read filename in
match kind with
Expand All @@ -142,57 +142,46 @@ let to_menu_aux ~content_dir ~subdir ?(header=Cow.Html.nil) ~menu_pages ~srcurl
let md_content = Omd.of_string content in
let md_toc = doc_toc md_content in
let html_toc = Cow.Html.of_string (Omd.to_html md_toc) in
<:html<
<div class="row">
<div class="span3">
<div class="bs-docs-menu hidden-phone">
$doc_menu$
<div class="bs-docs-toc">
$html_toc$
</div>
</div>
</div>
<div class="span9">
$header$
$Cow.Html.of_string (Omd.to_html md_content)$
</div>
</div>
>>
| _ -> <:html< >>
Html.div ~cls:"row"
(Html.div ~cls:"span3"
(Html.div ~cls:"bs-docs-menu hidden-phone"
(doc_menu
@ Html.div ~cls:"bs-docs-toc" html_toc))
@ Html.div ~cls:"span9"
(header
@ Html.string (Omd.to_html md_content)))
| _ -> Html.empty
in

let documentation_menu active_src =
let menu_items = List.map (fun (src, _, lnk, kind) -> match kind with
let menu_items =
List.map (fun (src, _, lnk, lnk_text, kind) -> match kind with
| Submenu _ | No_menu _ -> Cow.Html.nil
| Nav_header ->
<:html<
<li class="disabled">
<a href="#"><strong>$str: lnk.text$</strong></a>
</li>
>>
Html.li ~cls:"disabled"
(Html.a ~href:(Uri.of_string "#")
(Html.strong (Html.string lnk_text)))
| Divider ->
<:html<<li class="disabled divider"><a href="#"> </a></li>&>>
Html.li ~cls:"disabled divider"
(Html.a ~href:(Uri.of_string "#") (Html.string " "))
| External | Internal _ ->
let classes = if active_src = src then "active" else "" in
<:html<<li class="$str: classes$">
<a href="$str: lnk.href$"> $str: lnk.text$</a>
</li>&>>
let classes = if active_src = src then "active" else "" in
Html.li ~cls:classes
(Html.a ~href:(Uri.of_string lnk)
(Html.string (" " ^ lnk_text)))
) menu_pages
in
<:html<
<ul class="nav nav-pills nav-stacked">
$list: menu_items$
</ul>
>>
Html.ul ~cls:"nav nav-pills nav-stacked" menu_items
in

(* Pages creation *)
let aux_page (source_filename, extension, lnk, page) =
let aux_page (source_filename, extension, lnk, lnk_text, page) =
match page with
| Submenu _ | Nav_header | Divider | External ->
{
menu_source = OpamFilename.to_string source_filename;
menu_link = lnk;
menu_link_text = lnk_text;
menu_item = page;
menu_srcurl = None;
}
Expand All @@ -207,34 +196,31 @@ let to_menu_aux ~content_dir ~subdir ?(header=Cow.Html.nil) ~menu_pages ~srcurl
in
{
menu_source = OpamFilename.to_string source_filename;
menu_link = { lnk with href = subdir ^ "/" ^ lnk.href };
menu_link = subdir ^ "/" ^ lnk;
menu_link_text = lnk_text;
menu_item = Internal (level, Template.serialize html_page);
menu_srcurl = srcurl;
}
in

let menu = List.map aux_page menu_pages in
if List.exists (fun (src, _, _, _) ->
if List.exists (fun (src, _, _, _, _) ->
OpamFilename.(Base.to_string (basename (chop_extension src)))
= "index")
menu_pages
then menu
else
let doc_menu = documentation_menu (OpamFilename.of_string "index") in
let html_index =
<:html<
<div class="row"><div class="span4 offset4">
$doc_menu$
</div></div>
>>
in
let html_index = Html.div ~cls:"row"
(Html.div ~cls:"span4 offset4" doc_menu) in
let srcurl = match srcurl with
| None -> None
| Some src -> Some (src ^"/"^ "index.menu")
in
{
menu_source = "index.html";
menu_link = { text = "Documentation index"; href = subdir ^ "/" };
menu_link = subdir ^ "/";
menu_link_text = "Documentation index";
menu_item = No_menu (List.length (OpamMisc.split subdir '/'),
Template.serialize html_index);
menu_srcurl = srcurl;
Expand Down Expand Up @@ -289,8 +275,8 @@ let to_menu ~content_dir =
menu_12 @
[{
menu_source = "1.1";
menu_link = { text = "Archives (OPAM 1.1)";
href = "/doc/1.1/" };
menu_link = "/doc/1.1/";
menu_link_text = "Archives (OPAM 1.1)";
menu_item = External;
menu_srcurl =
match srcurl_11 with
Expand All @@ -299,8 +285,8 @@ let to_menu ~content_dir =
};
{
menu_source = "2.0";
menu_link = { text = "Development version (OPAM 2.0)";
href = "/doc/2.0/" };
menu_link = "/doc/2.0/";
menu_link_text = "Development version (OPAM 2.0)";
menu_item = External;
menu_srcurl =
match srcurl_20 with
Expand Down