From bcfb77e2ab832d97b66e0bd06c35a9a13be437da Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Wed, 8 Mar 2017 12:47:39 +0100
Subject: [PATCH] Markdown writer: Avoid spurious blanklines at end of
 document...

after tables and list, for example.
---
 src/Text/Pandoc/Writers/Markdown.hs |  2 +-
 test/command/2834.md                |  3 ---
 test/command/3337.md                |  3 ---
 test/command/3487.md                |  2 --
 test/tables.markdown                |  2 --
 test/tables.plain                   |  2 --
 test/writer.opml                    | 10 +++++-----
 7 files changed, 6 insertions(+), 18 deletions(-)

diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index 782d0d085..ab986208d 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -214,7 +214,7 @@ pandocToMarkdown opts (Pandoc meta blocks) = do
   body <- blockListToMarkdown opts blocks'
   notesAndRefs' <- notesAndRefs opts
   let render' :: Doc -> String
-      render' = render colwidth
+      render' = render colwidth . chomp
   let main = render' $ body <> notesAndRefs'
   let context  = defField "toc" (render' toc)
                $ defField "body" main
diff --git a/test/command/2834.md b/test/command/2834.md
index e4c2b3da5..850c39254 100644
--- a/test/command/2834.md
+++ b/test/command/2834.md
@@ -26,7 +26,4 @@ Nested grid tables.
 | |   -----------                                                    |  |
 | +------------------------------------------------------------------+  |
 +-----------------------------------------------------------------------+
-
-
-
 ```
diff --git a/test/command/3337.md b/test/command/3337.md
index 8f6735f0f..7ba14dcbb 100644
--- a/test/command/3337.md
+++ b/test/command/3337.md
@@ -9,8 +9,5 @@
   a   
   1   2
   --- ---
-
-
-
 ```
 
diff --git a/test/command/3487.md b/test/command/3487.md
index 2f3e2f24a..1d475676f 100644
--- a/test/command/3487.md
+++ b/test/command/3487.md
@@ -8,6 +8,4 @@ Some text
 Some text
 
 -   element
-
-
 ```
diff --git a/test/tables.markdown b/test/tables.markdown
index 4b5754cf9..7f89bfc08 100644
--- a/test/tables.markdown
+++ b/test/tables.markdown
@@ -74,5 +74,3 @@ Multiline table without column headers:
                                       the blank line between
                                       rows.
   ----------- ---------- ------------ --------------------------
-
-
diff --git a/test/tables.plain b/test/tables.plain
index 4c7ebbf82..e46317a6f 100644
--- a/test/tables.plain
+++ b/test/tables.plain
@@ -74,5 +74,3 @@ Multiline table without column headers:
                                       the blank line between
                                       rows.
   ----------- ---------- ------------ --------------------------
-
-
diff --git a/test/writer.opml b/test/writer.opml
index 261f83426..0b621823e 100644
--- a/test/writer.opml
+++ b/test/writer.opml
@@ -31,18 +31,18 @@
 <outline text="Code Blocks" _note="Code:&#10;&#10;    ---- (should be four hyphens)&#10;&#10;    sub status {&#10;        print &quot;working&quot;;&#10;    }&#10;&#10;    this code block is indented by one tab&#10;&#10;And:&#10;&#10;        this code block is indented by two tabs&#10;&#10;    These should not be escaped:  \$ \\ \&gt; \[ \{&#10;&#10;------------------------------------------------------------------------">
 </outline>
 <outline text="Lists">
-  <outline text="Unordered" _note="Asterisks tight:&#10;&#10;-   asterisk 1&#10;-   asterisk 2&#10;-   asterisk 3&#10;&#10;Asterisks loose:&#10;&#10;-   asterisk 1&#10;&#10;-   asterisk 2&#10;&#10;-   asterisk 3&#10;&#10;Pluses tight:&#10;&#10;-   Plus 1&#10;-   Plus 2&#10;-   Plus 3&#10;&#10;Pluses loose:&#10;&#10;-   Plus 1&#10;&#10;-   Plus 2&#10;&#10;-   Plus 3&#10;&#10;Minuses tight:&#10;&#10;-   Minus 1&#10;-   Minus 2&#10;-   Minus 3&#10;&#10;Minuses loose:&#10;&#10;-   Minus 1&#10;&#10;-   Minus 2&#10;&#10;-   Minus 3&#10;&#10;">
+  <outline text="Unordered" _note="Asterisks tight:&#10;&#10;-   asterisk 1&#10;-   asterisk 2&#10;-   asterisk 3&#10;&#10;Asterisks loose:&#10;&#10;-   asterisk 1&#10;&#10;-   asterisk 2&#10;&#10;-   asterisk 3&#10;&#10;Pluses tight:&#10;&#10;-   Plus 1&#10;-   Plus 2&#10;-   Plus 3&#10;&#10;Pluses loose:&#10;&#10;-   Plus 1&#10;&#10;-   Plus 2&#10;&#10;-   Plus 3&#10;&#10;Minuses tight:&#10;&#10;-   Minus 1&#10;-   Minus 2&#10;-   Minus 3&#10;&#10;Minuses loose:&#10;&#10;-   Minus 1&#10;&#10;-   Minus 2&#10;&#10;-   Minus 3">
   </outline>
-  <outline text="Ordered" _note="Tight:&#10;&#10;1.  First&#10;2.  Second&#10;3.  Third&#10;&#10;and:&#10;&#10;1.  One&#10;2.  Two&#10;3.  Three&#10;&#10;Loose using tabs:&#10;&#10;1.  First&#10;&#10;2.  Second&#10;&#10;3.  Third&#10;&#10;and using spaces:&#10;&#10;1.  One&#10;&#10;2.  Two&#10;&#10;3.  Three&#10;&#10;Multiple paragraphs:&#10;&#10;1.  Item 1, graf one.&#10;&#10;    Item 1. graf two. The quick brown fox jumped over the lazy dog’s&#10;    back.&#10;&#10;2.  Item 2.&#10;&#10;3.  Item 3.&#10;&#10;">
+  <outline text="Ordered" _note="Tight:&#10;&#10;1.  First&#10;2.  Second&#10;3.  Third&#10;&#10;and:&#10;&#10;1.  One&#10;2.  Two&#10;3.  Three&#10;&#10;Loose using tabs:&#10;&#10;1.  First&#10;&#10;2.  Second&#10;&#10;3.  Third&#10;&#10;and using spaces:&#10;&#10;1.  One&#10;&#10;2.  Two&#10;&#10;3.  Three&#10;&#10;Multiple paragraphs:&#10;&#10;1.  Item 1, graf one.&#10;&#10;    Item 1. graf two. The quick brown fox jumped over the lazy dog’s&#10;    back.&#10;&#10;2.  Item 2.&#10;&#10;3.  Item 3.">
   </outline>
-  <outline text="Nested" _note="-   Tab&#10;    -   Tab&#10;        -   Tab&#10;&#10;Here’s another:&#10;&#10;1.  First&#10;2.  Second:&#10;    -   Fee&#10;    -   Fie&#10;    -   Foe&#10;3.  Third&#10;&#10;Same thing but with paragraphs:&#10;&#10;1.  First&#10;&#10;2.  Second:&#10;&#10;    -   Fee&#10;    -   Fie&#10;    -   Foe&#10;&#10;3.  Third&#10;&#10;">
+  <outline text="Nested" _note="-   Tab&#10;    -   Tab&#10;        -   Tab&#10;&#10;Here’s another:&#10;&#10;1.  First&#10;2.  Second:&#10;    -   Fee&#10;    -   Fie&#10;    -   Foe&#10;3.  Third&#10;&#10;Same thing but with paragraphs:&#10;&#10;1.  First&#10;&#10;2.  Second:&#10;&#10;    -   Fee&#10;    -   Fie&#10;    -   Foe&#10;&#10;3.  Third">
   </outline>
-  <outline text="Tabs and spaces" _note="-   this is a list item indented with tabs&#10;&#10;-   this is a list item indented with spaces&#10;&#10;    -   this is an example list item indented with tabs&#10;&#10;    -   this is an example list item indented with spaces&#10;&#10;">
+  <outline text="Tabs and spaces" _note="-   this is a list item indented with tabs&#10;&#10;-   this is a list item indented with spaces&#10;&#10;    -   this is an example list item indented with tabs&#10;&#10;    -   this is an example list item indented with spaces">
   </outline>
   <outline text="Fancy list markers" _note="1.  begins with 2&#10;2.  and now 3&#10;&#10;    with a continuation&#10;&#10;    1.  sublist with roman numerals, starting with 4&#10;    2.  more items&#10;        1.  a subsublist&#10;        2.  a subsublist&#10;&#10;Nesting:&#10;&#10;1.  Upper Alpha&#10;    1.  Upper Roman.&#10;        1.  Decimal start with 6&#10;            1.  Lower alpha with paren&#10;&#10;Autonumbering:&#10;&#10;1.  Autonumber.&#10;2.  More.&#10;    1.  Nested.&#10;&#10;Should not be a list item:&#10;&#10;M.A. 2007&#10;&#10;B. Williams&#10;&#10;------------------------------------------------------------------------">
   </outline>
 </outline>
-<outline text="Definition Lists" _note="Tight using spaces:&#10;&#10;apple  &#10;red fruit&#10;&#10;orange  &#10;orange fruit&#10;&#10;banana  &#10;yellow fruit&#10;&#10;Tight using tabs:&#10;&#10;apple  &#10;red fruit&#10;&#10;orange  &#10;orange fruit&#10;&#10;banana  &#10;yellow fruit&#10;&#10;Loose:&#10;&#10;apple  &#10;red fruit&#10;&#10;orange  &#10;orange fruit&#10;&#10;banana  &#10;yellow fruit&#10;&#10;Multiple blocks with italics:&#10;&#10;*apple*  &#10;red fruit&#10;&#10;contains seeds, crisp, pleasant to taste&#10;&#10;*orange*  &#10;orange fruit&#10;&#10;    { orange code block }&#10;&#10;&gt; orange block quote&#10;&#10;Multiple definitions, tight:&#10;&#10;apple  &#10;red fruit&#10;&#10;computer&#10;&#10;orange  &#10;orange fruit&#10;&#10;bank&#10;&#10;Multiple definitions, loose:&#10;&#10;apple  &#10;red fruit&#10;&#10;computer&#10;&#10;orange  &#10;orange fruit&#10;&#10;bank&#10;&#10;Blank line after term, indented marker, alternate markers:&#10;&#10;apple  &#10;red fruit&#10;&#10;computer&#10;&#10;orange  &#10;orange fruit&#10;&#10;1.  sublist&#10;2.  sublist&#10;">
+<outline text="Definition Lists" _note="Tight using spaces:&#10;&#10;apple  &#10;red fruit&#10;&#10;orange  &#10;orange fruit&#10;&#10;banana  &#10;yellow fruit&#10;&#10;Tight using tabs:&#10;&#10;apple  &#10;red fruit&#10;&#10;orange  &#10;orange fruit&#10;&#10;banana  &#10;yellow fruit&#10;&#10;Loose:&#10;&#10;apple  &#10;red fruit&#10;&#10;orange  &#10;orange fruit&#10;&#10;banana  &#10;yellow fruit&#10;&#10;Multiple blocks with italics:&#10;&#10;*apple*  &#10;red fruit&#10;&#10;contains seeds, crisp, pleasant to taste&#10;&#10;*orange*  &#10;orange fruit&#10;&#10;    { orange code block }&#10;&#10;&gt; orange block quote&#10;&#10;Multiple definitions, tight:&#10;&#10;apple  &#10;red fruit&#10;&#10;computer&#10;&#10;orange  &#10;orange fruit&#10;&#10;bank&#10;&#10;Multiple definitions, loose:&#10;&#10;apple  &#10;red fruit&#10;&#10;computer&#10;&#10;orange  &#10;orange fruit&#10;&#10;bank&#10;&#10;Blank line after term, indented marker, alternate markers:&#10;&#10;apple  &#10;red fruit&#10;&#10;computer&#10;&#10;orange  &#10;orange fruit&#10;&#10;1.  sublist&#10;2.  sublist">
 </outline>
 <outline text="HTML Blocks" _note="Simple block on one line:&#10;&#10;foo&#10;&#10;And nested without indentation:&#10;&#10;foo&#10;&#10;bar&#10;&#10;Interpreted markdown in a table:&#10;&#10;This is *emphasized*&#10;And this is **strong**&#10;Here’s a simple block:&#10;&#10;foo&#10;&#10;This should be a code block, though:&#10;&#10;    &lt;div&gt;&#10;        foo&#10;    &lt;/div&gt;&#10;&#10;As should this:&#10;&#10;    &lt;div&gt;foo&lt;/div&gt;&#10;&#10;Now, nested:&#10;&#10;foo&#10;&#10;This should just be an HTML comment:&#10;&#10;Multiline:&#10;&#10;Code block:&#10;&#10;    &lt;!-- Comment --&gt;&#10;&#10;Just plain comment, with trailing spaces on the line:&#10;&#10;Code:&#10;&#10;    &lt;hr /&gt;&#10;&#10;Hr’s:&#10;&#10;------------------------------------------------------------------------">
 </outline>