Commit a4977c65 authored by Philippe Veber's avatar Philippe Veber
Browse files

new tk/Note module

parent f2d78011
(library
(name codepitk)
(public_name codepi.tk)
(libraries biotk biocaml.ez ocaml-r.graphics ocaml-r.grDevices phylogenetics)
(libraries biotk biocaml.ez bos ocaml-r.graphics ocaml-r.grDevices phylogenetics)
(inline_tests
(deps ../../tests/data/gemma_output.tsv
../../tests/data/gemma_alignment.fa
......
open Core_kernel
type elt =
| Text of string
| Table of (string * string list) list
| Png of string
| Svg of string
| Vg of Vg.image
type t = {
title : string ;
toc : bool ;
contents : elt list ;
}
let make ?(toc = false) ~title contents = {
title ; toc ; contents
}
let text x = Text x
let table cols = Table cols
let png fn = Png fn
let svg fn = Svg fn
let vg i = Vg i
let render_picture format data buf =
let format = match format with
| `svg -> "svg+xml"
| `png -> "png"
in
let contents = Base64.encode_exn data in
Printf.bprintf buf {|<img src="data:image/%s;base64,%s"/>|} format contents
let render_picture_on_disk format fn buf =
render_picture format (In_channel.read_all fn) buf
let header doc =
sprintf {|---
title: %s
---
|} doc.title
let render_doc doc =
let buf = Buffer.create 253 in
Buffer.add_string buf (header doc) ;
List.iter doc.contents ~f:(fun elt ->
(
match elt with
| Text t -> Buffer.add_string buf t
| Png fn -> render_picture_on_disk `png fn buf
| Svg fn -> render_picture_on_disk `svg fn buf
| _ -> failwith "not implemented"
) ;
Buffer.add_string buf "\n\n"
) ;
Buffer.contents buf
let html_template = {|<!DOCTYPE html>
<html $if(lang)$ lang="$lang$" $endif$ dir="ltr">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>$if(title)$$title$$endif$</title>
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">
<link rel="apple-touch-icon-precomposed" href="images/apple-touch-icon.png">
$if(template_css)$
<link rel="stylesheet" href="$template_css$">
$else$
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/uikit/2.26.4/css/uikit.gradient.css">
$endif$
<!-- <link rel="stylesheet" href="style.css"> -->
<link rel="stylesheet" href="https://cdn.rawgit.com/diversen/pandoc-uikit/master/style.css">
<link href="https://vjs.zencdn.net/5.4.4/video-js.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
<!-- <script src="uikit.js"></script> -->
<script src="https://cdn.rawgit.com/diversen/pandoc-uikit/master/uikit.js"></script>
<!-- <script src="scripts.js"></script> -->
<script src="https://cdn.rawgit.com/diversen/pandoc-uikit/master/scripts.js"></script>
<!-- <script src="jquery.sticky-kit.js "></script> -->
<script src="https://cdn.rawgit.com/diversen/pandoc-uikit/master/jquery.sticky-kit.js"></script>
<meta name="generator" content="pandoc-uikit" />
$for(author-meta)$
<meta name="author" content="$author-meta$" />
$endfor$
$if(date-meta)$
<meta name="date" content="$date-meta$" />
$endif$
<title>$if(title-prefix)$$title-prefix$ - $endif$$pagetitle$</title>
<style type="text/css">code{white-space: pre;}</style>
$if(quotes)$
<style type="text/css">q { quotes: "“" "”" "‘" "’"; }</style>
$endif$
$if(highlighting-css)$
<style type="text/css">
$highlighting-css$
</style>
$endif$
$for(css)$
<link rel="stylesheet" href="$css$" $if(html5)$$else$type="text/css" $endif$/>
$endfor$
$if(math)$
$math$
$endif$
$for(header-includes)$
$header-includes$
$endfor$
</head>
<body>
<div class="uk-container uk-container-center uk-margin-top uk-margin-large-bottom">
$if(title)$
<div class="uk-grid" data-uk-grid-margin>
<div class="uk-width-1-1">
<h1 class="uk-heading-large">$title$</h1>
$if(date)$
<h3 class="uk-heading-large">$date$</p></h3>
$endif$
$for(author)$
<p class="uk-text-large">$author$</p>
$endfor$
</div>
</div>
$endif$
<div class="uk-grid" data-uk-grid-margin >
<div class="uk-width-medium-1-4">
<div class="uk-overflow-container" data-uk-sticky="{top:25,media: 768}">
<div class="uk-panel uk-panel-box menu-begin" >
$if(toc)$
$toc$
$endif$
</div>
</div>
</div>
<div class="uk-width-medium-3-4">
$body$
</div>
</div>
$if(analytics)$
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', '$analytics$', 'auto');
ga('send', 'pageview');
$endif$
<script src="https://vjs.zencdn.net/5.4.4/video.js"></script>
</div>
</body>
</html>
|}
let pandoc ?(toc = false) ~md ~template ~html =
Bos.Cmd.(
v "pandoc"
% "--from=markdown+tex_math_single_backslash+tex_math_dollars"
% "--to=html5"
% "--katex"
% ("--template=" ^ p template)
% ("--output=" ^ html)
% (if toc then "--toc" else "")
% p md
)
|> Bos.OS.Cmd.run
let to_html doc dest =
let open Bos.OS in
let with_dir d () =
let md = Fpath.(d / "doc.md") in
let template = Fpath.(d / "template.html5") in
Out_channel.write_all (Bos.Cmd.p template) ~data:html_template ;
Out_channel.write_all (Bos.Cmd.p md) ~data:(render_doc doc) ;
pandoc ~toc:doc.toc ~md ~template ~html:dest
in
Dir.with_tmp "codepi%s" with_dir ()
|> Result.join
type t
type elt
val text : string -> elt
val table : (string * string list) list -> elt
val png : string -> elt
val svg : string -> elt
val vg : Vg.image -> elt
val make :
?toc:bool ->
title:string ->
elt list ->
t
val to_html :
t ->
string ->
(unit, [> `Msg of string]) result
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment