From be01f75e192b1a4780aa2b6f53c0192057d2d1af Mon Sep 17 00:00:00 2001
From: Virgile Prevosto <virgile.prevosto@m4x.org>
Date: Tue, 5 Dec 2017 13:46:01 +0100
Subject: [PATCH] better handling of Raw material

---
 .../markdown-report/Report_markdown.mli       |  4 +++-
 src/plugins/markdown-report/markdown.ml       |  9 +++++++--
 src/plugins/markdown-report/markdown.mli      |  4 +++-
 src/plugins/markdown-report/md_gen.ml         |  8 ++++----
 src/plugins/markdown-report/parse_remarks.ml  | 20 +++++++++----------
 5 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/src/plugins/markdown-report/Report_markdown.mli b/src/plugins/markdown-report/Report_markdown.mli
index 5edadb17f3d..24a5713e2e2 100644
--- a/src/plugins/markdown-report/Report_markdown.mli
+++ b/src/plugins/markdown-report/Report_markdown.mli
@@ -51,7 +51,9 @@ and block = block_element list
 
 and element =
   | Block of block
-  | Raw of string (** non-markdown element, printed as-is. *)
+  | Raw of string list
+   (** non-markdown. Each element of the list is printed as-is on its own line.
+       A blank line separates the [Raw] node from the next one. *)
   | Comment of string (** markdown comment, printed <!-- like this --> *)
   | H1 of text * string option (** optional label. *)
   | H2 of text * string option
diff --git a/src/plugins/markdown-report/markdown.ml b/src/plugins/markdown-report/markdown.ml
index 6996182b542..b26a07fa8fa 100644
--- a/src/plugins/markdown-report/markdown.ml
+++ b/src/plugins/markdown-report/markdown.ml
@@ -23,7 +23,9 @@ and block = block_element list
 
 and element =
   | Block of block
-  | Raw of string (** non-markdown element, printed as-is. *)
+  | Raw of string list
+   (** non-markdown. Each element of the list is printed as-is on its own line.
+       A blank line separates the [Raw] node from the next one. *)
   | Comment of string (** markdown comment, printed <!-- like this --> *)
   | H1 of text * string option (** optional label. *)
   | H2 of text * string option
@@ -169,7 +171,10 @@ and pp_quote fmt l =
 
 and pp_element fmt = function
   | Block b -> Format.fprintf fmt "@[<v>%a@]@\n" pp_block b
-  | Raw s -> Format.pp_print_string fmt s
+  | Raw l ->
+    Format.(
+      fprintf fmt "%a@\n"
+        (pp_print_list ~pp_sep:pp_force_newline pp_print_string) l)
   | Comment s ->
     Format.fprintf fmt
       "@[<hv>@[<hov 5><!-- %a@]@ -->@]@\n" Format.pp_print_text s
diff --git a/src/plugins/markdown-report/markdown.mli b/src/plugins/markdown-report/markdown.mli
index 1cedad28699..dc5199989ac 100644
--- a/src/plugins/markdown-report/markdown.mli
+++ b/src/plugins/markdown-report/markdown.mli
@@ -23,7 +23,9 @@ and block = block_element list
 
 and element =
   | Block of block
-  | Raw of string (** non-markdown element, printed as-is. *)
+  | Raw of string list
+   (** non-markdown. Each element of the list is printed as-is on its own line.
+       A blank line separates the [Raw] node from the next one. *)
   | Comment of string (** markdown comment, printed <!-- like this --> *)
   | H1 of text * string option (** optional label. *)
   | H2 of text * string option
diff --git a/src/plugins/markdown-report/md_gen.ml b/src/plugins/markdown-report/md_gen.ml
index d3157522cc2..701e0e2d555 100644
--- a/src/plugins/markdown-report/md_gen.ml
+++ b/src/plugins/markdown-report/md_gen.ml
@@ -602,9 +602,9 @@ let gen_report is_draft =
     else elements
   in
   let elements =
-   Raw "\\let\\underscore\\_" ::
-   Raw "\\renewcommand{\\_}{\\discretionary{\\underscore}{}{\\underscore}}" ::
-   elements
+   Raw [ "\\let\\underscore\\_" ;
+         "\\renewcommand{\\_}{\\discretionary{\\underscore}{}{\\underscore}}"]
+   :: elements
   in
   let doc = { title; authors; date; elements;} in
   try
@@ -621,7 +621,7 @@ let main () =
   if Mdr_params.Gen_draft.get () then begin
     if Mdr_params.Generate.get () then
       Mdr_params.warning
-        "-mdr-gen and -mdr-gen-draft can be activated at the \
+        "-mdr-gen and -mdr-gen-draft cannot be activated at the \
          same time. Only draft will be generated";
     gen_report true
   end
diff --git a/src/plugins/markdown-report/parse_remarks.ml b/src/plugins/markdown-report/parse_remarks.ml
index 3e5780cf4c1..56b2331ccbc 100644
--- a/src/plugins/markdown-report/parse_remarks.ml
+++ b/src/plugins/markdown-report/parse_remarks.ml
@@ -1,21 +1,21 @@
 type env =
   { mutable current_section: string;
     mutable is_markdown: bool;
-    current_markdown: Buffer.t;
+    mutable current_markdown: string list;
+    (* markdown lines of current element, in reverse order. *)
     mutable remarks: Markdown.element list Datatype.String.Map.t }
 
 let empty_env () =
   { current_section = "";
     is_markdown = false;
-    current_markdown = Buffer.create 40;
+    current_markdown = [];
     remarks = Datatype.String.Map.empty }
 
-let add_channel buf chan =
+let add_channel env chan =
   try
     while true do
       let s = input_line chan in
-      Buffer.add_string buf s;
-      Buffer.add_char buf '\n'
+      env.current_markdown <- s :: env.current_markdown;
     done;
   with End_of_file -> ()
 
@@ -33,20 +33,20 @@ let parse_line env line =
       env.remarks <-
         Datatype.String.Map.add
           env.current_section
-          [ Markdown.Raw (Buffer.contents env.current_markdown)]
-          env.remarks
+          [ Markdown.Raw (List.rev env.current_markdown)]
+          env.remarks;
+      env.current_markdown <- []
     end else if Str.string_match include_markdown line 0 then begin
       let f = Str.matched_group 1 line in
       try
         let chan = open_in f in
-        add_channel env.current_markdown chan;
+        add_channel env chan;
         close_in chan
       with Sys_error err ->
         Mdr_params.error
           "Unable to open included remarks file %s (%s), Ignoring." f err
     end else begin
-      Buffer.add_string env.current_markdown line;
-      Buffer.add_char env.current_markdown '\n'
+      env.current_markdown <- line :: env.current_markdown;
     end
   end else if Str.string_match beg_markdown line 0 then begin
     env.is_markdown <- true
-- 
GitLab