diff --git a/src/plugins/markdown-report/Makefile b/src/plugins/markdown-report/Makefile index da5163246719d6a4011676b0660c2d7ebffb2c3d..d9b339efd9ac419ee0a4e5e772ba14a64a02e1b5 100644 --- a/src/plugins/markdown-report/Makefile +++ b/src/plugins/markdown-report/Makefile @@ -24,15 +24,11 @@ $(Report_markdown_DIR)/mdr_version.ml: $(Report_markdown_DIR)/Makefile $(Report_markdown_DIR)/Report_markdown.mli: \ $(Report_markdown_DIR)/mdr_params.mli \ - $(Report_markdown_DIR)/markdown.mli \ $(Report_markdown_DIR)/md_gen.mli \ $(Report_markdown_DIR)/Makefile echo "module Mdr_params: sig" > $@ cat $(Report_markdown_DIR)/mdr_params.mli >> $@ echo "end" >> $@ - echo "module Markdown: sig" >> $@ - cat $(Report_markdown_DIR)/markdown.mli >> $@ - echo "end" >> $@ echo "module Md_gen: sig" >> $@ cat $(Report_markdown_DIR)/md_gen.mli >> $@ echo "end" >> $@ diff --git a/src/plugins/markdown-report/markdown.ml b/src/plugins/markdown-report/markdown.ml index 8e91d501c0a394925f203799c088ca9bba116408..7ba2e768fe1742008c87f6a94c73b0f552b7cd15 100644 --- a/src/plugins/markdown-report/markdown.ml +++ b/src/plugins/markdown-report/markdown.ml @@ -1,11 +1,17 @@ type align = Left | Center | Right +type href = + | URL of string + | Page of string + | Name of string + | Section of string * string + type inline = | Plain of string | Emph of string | Bold of string | Inline_code of string - | Link of text * string (** [Link(text,url)] *) + | Link of text * href (** [Link(text,url)] *) | Image of string * string (** [Image(alt,location)] *) and text = inline list @@ -47,20 +53,50 @@ let plain s = [ Plain s] let plain_format txt = Format.kasprintf plain txt -let plain_link s = Link ([Inline_code s],s) +let plain_link h = + let s = match h with + | URL url -> url + | Page p -> p + | Section (_,s) -> s + | Name a -> a + in + Link ([Inline_code s], URL s) let codelines lang pp code = let s = Format.asprintf "@[%a@]" pp code in let lines = String.split_on_char '\n' s in Code_block (lang, lines) +let id m = + let buffer = Buffer.create (String.length m) in + let lowercase = Char.lowercase_ascii in + let dash = ref false in + let emit c = + if !dash then (Buffer.add_char buffer '-' ; dash := false) ; + Buffer.add_char buffer c in + String.iter + (function + | '0'..'9' as c -> emit c + | 'a'..'z' as c -> emit c + | 'A'..'Z' as c -> emit (lowercase c) + | '.' | '_' as c -> emit c + | ' ' | '\t' | '\n' | '-' -> dash := (Buffer.length buffer > 0) + | _ -> ()) m ; + Buffer.contents buffer + +let pp_href fmt = function + | URL s | Page s -> Format.pp_print_string fmt s + | Section (p,s) -> Format.fprintf fmt "%s#%s" p (id s) + | Name a -> Format.fprintf fmt "#%s" (id a) + let rec pp_inline fmt = function | Plain s -> Format.pp_print_string fmt s | Emph s -> Format.fprintf fmt "_%s_" (String.trim s) | Bold s -> Format.fprintf fmt "**%s**" (String.trim s) | Inline_code s -> Format.fprintf fmt "`%s`" (String.trim s) - | Link (text,url) -> Format.fprintf fmt "@[<h>[%a](%s)@]@ " pp_text text url + | Link (text,url) -> + Format.fprintf fmt "@[<h>[%a](%a)@]@ " pp_text text pp_href url | Image (alt,url) -> Format.fprintf fmt "@[<h>@]@ " alt url and pp_text fmt l = diff --git a/src/plugins/markdown-report/markdown.mli b/src/plugins/markdown-report/markdown.mli index 08b1f67fd0e686030d91c6ecbd82b9d4e51211f2..5580883687bda1d784ca95c2bc7a35f36013e298 100644 --- a/src/plugins/markdown-report/markdown.mli +++ b/src/plugins/markdown-report/markdown.mli @@ -1,11 +1,17 @@ type align = Left | Center | Right +type href = + | URL of string + | Page of string + | Name of string + | Section of string * string + type inline = | Plain of string | Emph of string | Bold of string | Inline_code of string - | Link of text * string (** [Link(text,url)] *) + | Link of text * href | Image of string * string (** [Image(alt,location)] *) and text = inline list