From 3b9be92492cca7573f9ce79dff099b4d2fa3c56c Mon Sep 17 00:00:00 2001
From: fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b>
Date: Sat, 13 Mar 2010 04:11:24 +0000
Subject: [PATCH] Use template variables for include-before/after.

* These options now imply -s; previously they worked also
  in fragment mode.
* Users can now adjust position of include-before and
  include-after text in the templates.
* Default position of include-before moved back (as it
  originally was) before table of contents.
* Resolves Issue #217.

git-svn-id: https://pandoc.googlecode.com/svn/trunk@1883 788f1e2b-df1e-0410-8736-df70ead52e1b
---
 README                                  | 10 ++++++++--
 man/man1/pandoc.1.md                    |  8 ++++++++
 src/Text/Pandoc/Writers/ConTeXt.hs      |  8 +-------
 src/Text/Pandoc/Writers/Docbook.hs      |  7 +------
 src/Text/Pandoc/Writers/HTML.hs         |  4 +---
 src/Text/Pandoc/Writers/LaTeX.hs        |  8 +-------
 src/Text/Pandoc/Writers/Man.hs          |  6 +-----
 src/Text/Pandoc/Writers/Markdown.hs     |  8 +-------
 src/Text/Pandoc/Writers/MediaWiki.hs    |  4 +---
 src/Text/Pandoc/Writers/OpenDocument.hs |  7 +------
 src/Text/Pandoc/Writers/RST.hs          |  7 +------
 src/Text/Pandoc/Writers/RTF.hs          |  4 +---
 src/Text/Pandoc/Writers/Texinfo.hs      |  8 +-------
 src/pandoc.hs                           | 14 ++++++++------
 templates/context.template              |  6 ++++++
 templates/docbook.template              |  6 ++++++
 templates/html.template                 |  6 ++++++
 templates/latex.template                |  8 ++++++++
 templates/man.template                  |  6 ++++++
 templates/markdown.template             |  8 ++++++++
 templates/mediawiki.template            |  8 ++++++++
 templates/opendocument.template         |  6 ++++++
 templates/rst.template                  |  8 ++++++++
 templates/rtf.template                  |  6 ++++++
 templates/texinfo.template              |  8 ++++++++
 tests/s5.inserts.html                   |  3 ++-
 26 files changed, 113 insertions(+), 69 deletions(-)

diff --git a/README b/README
index ac86f2997..57fd2dea5 100644
--- a/README
+++ b/README
@@ -276,14 +276,14 @@ For further documentation, see the `pandoc(1)` man page.
     `\begin{document}` command in LaTeX). This can be used to include
     navigation bars or banners in HTML documents. This option can be
     used repeatedly to include multiple files. They will be included in
-    the order specified.
+    the order specified.  Implies `--standalone`.
 
 `-A` or `--include-after-body` *filename*
 :   includes the contents of *filename* (verbatim) at the end of
     the document body (before the `</body>` tag in HTML, or the
     `\end{document}` command in LaTeX). This option can be be used
     repeatedly to include multiple files. They will be included in the
-    order specified.
+    order specified.  Implies `--standalone`.
 
 `--reference-odt` *filename*
 :   uses the specified file as a style reference in producing an ODT.
@@ -475,6 +475,12 @@ depending on the output format, but include:
     values)
 `toc`
 :   non-null value if `--toc/--table-of-contents` was specified
+`include-before`
+:   contents specified by `-B/--include-before-body` (may have
+    multiple values)
+`include-after`
+:   contents specified by `-A/--include-after-body` (may have
+    multiple values)
 `body`
 :   body of document
 `title`
diff --git a/man/man1/pandoc.1.md b/man/man1/pandoc.1.md
index 49f645140..3e1005caa 100644
--- a/man/man1/pandoc.1.md
+++ b/man/man1/pandoc.1.md
@@ -195,9 +195,11 @@ should pipe input and output through `iconv`:
 
 -B *FILE*, \--include-before-body=*FILE*
 :   Include contents of *FILE* at the beginning of the document body.
+    Implies `-s`.
 
 -A *FILE*, \--include-after-body=*FILE*
 :   Include contents of *FILE* at the end of the document body.
+    Implies `-s`.
 
 -C *FILE*, \--custom-header=*FILE*
 :   Use contents of *FILE* as the document header. *Note: This option is
@@ -295,6 +297,12 @@ depending on the output format, but include:
     values)
 `toc`
 :   non-null value if `--toc/--table-of-contents` was specified
+`include-before`
+:   contents specified by `-B/--include-before-body` (may have
+    multiple values)
+`include-after`
+:   contents specified by `-A/--include-after-body` (may have
+    multiple values)
 `body`
 :   body of document
 `title`
diff --git a/src/Text/Pandoc/Writers/ConTeXt.hs b/src/Text/Pandoc/Writers/ConTeXt.hs
index cf8c3cc91..b1e3acb8f 100644
--- a/src/Text/Pandoc/Writers/ConTeXt.hs
+++ b/src/Text/Pandoc/Writers/ConTeXt.hs
@@ -64,13 +64,7 @@ pandocToConTeXt options (Pandoc (Meta title authors date) blocks) = do
                   then return ""
                   else liftM render $ inlineListToConTeXt date
   body <- blockListToConTeXt blocks 
-  let before = if null (writerIncludeBefore options)
-                  then empty
-                  else text $ writerIncludeBefore options
-  let after  = if null (writerIncludeAfter options)
-                  then empty
-                  else text $ writerIncludeAfter options
-  let main = render $ before $$ body $$ after
+  let main = render body
   let context  = writerVariables options ++
                  [ ("toc", if writerTableOfContents options then "yes" else "")
                  , ("body", main)
diff --git a/src/Text/Pandoc/Writers/Docbook.hs b/src/Text/Pandoc/Writers/Docbook.hs
index 153f93391..63647e3aa 100644
--- a/src/Text/Pandoc/Writers/Docbook.hs
+++ b/src/Text/Pandoc/Writers/Docbook.hs
@@ -65,12 +65,7 @@ writeDocbook opts (Pandoc (Meta tit auths dat) blocks) =
       authors = map (authorToDocbook opts) auths
       date = inlinesToDocbook opts dat
       elements = hierarchicalize blocks
-      before   = writerIncludeBefore opts
-      after    = writerIncludeAfter opts
-      main     = render $
-                 (if null before then empty else text before) $$
-                 vcat (map (elementToDocbook opts) elements) $$
-                 (if null after then empty else text after)
+      main     = render $ vcat (map (elementToDocbook opts) elements)
       context = writerVariables opts ++
                 [ ("body", main)
                 , ("title", render title)
diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs
index 2f58c7614..a32d63eaf 100644
--- a/src/Text/Pandoc/Writers/HTML.hs
+++ b/src/Text/Pandoc/Writers/HTML.hs
@@ -105,9 +105,7 @@ pandocToHtml opts (Pandoc (Meta title' authors' date') blocks) = do
   blocks' <- liftM toHtmlFromList $ mapM (elementToHtml opts) sects
   st <- get
   let notes = reverse (stNotes st)
-  let before = primHtml $ writerIncludeBefore opts
-  let after = primHtml $ writerIncludeAfter opts
-  let thebody = before +++ blocks' +++ footnoteSection notes +++ after
+  let thebody = blocks' +++ footnoteSection notes
   let  math = if stMath st
                 then case writerHTMLMathMethod opts of
                            LaTeXMathML (Just url) ->
diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs
index f4375ad8c..3362c1d25 100644
--- a/src/Text/Pandoc/Writers/LaTeX.hs
+++ b/src/Text/Pandoc/Writers/LaTeX.hs
@@ -74,13 +74,7 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
   authorsText <- mapM (liftM render . inlineListToLaTeX) authors
   dateText <- liftM render $ inlineListToLaTeX date
   body <- blockListToLaTeX blocks
-  let before = if null (writerIncludeBefore options)
-                 then empty
-                 else text $ writerIncludeBefore options
-  let after = if null (writerIncludeAfter options)
-                 then empty
-                 else text $ writerIncludeAfter options
-  let main = render $ before $$ body $$ after
+  let main = render body
   st <- get
   let context  = writerVariables options ++
                  [ ("toc", if writerTableOfContents options then "yes" else "")
diff --git a/src/Text/Pandoc/Writers/Man.hs b/src/Text/Pandoc/Writers/Man.hs
index df66e17ba..62bb90f8e 100644
--- a/src/Text/Pandoc/Writers/Man.hs
+++ b/src/Text/Pandoc/Writers/Man.hs
@@ -48,10 +48,6 @@ writeMan opts document = evalState (pandocToMan opts document) (WriterState [] F
 -- | Return groff man representation of document.
 pandocToMan :: WriterOptions -> Pandoc -> State WriterState String
 pandocToMan opts (Pandoc (Meta title authors date) blocks) = do
-  let before  = writerIncludeBefore opts
-  let after   = writerIncludeAfter opts
-  let before' = if null before then empty else text before
-  let after'  = if null after then empty else text after
   titleText <- inlineListToMan opts title
   authors' <- mapM (inlineListToMan opts) authors
   date' <- inlineListToMan opts date 
@@ -66,7 +62,7 @@ pandocToMan opts (Pandoc (Meta title authors date) blocks) = do
   body <- blockListToMan opts blocks
   notes <- liftM stNotes get
   notes' <- notesToMan opts (reverse notes)
-  let main = render $ before' $$ body $$ notes' $$ after'
+  let main = render $ body $$ notes'
   hasTables <- liftM stHasTables get
   let context  = writerVariables opts ++
                  [ ("body", main)
diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index 96c4fd15d..d5f750bd6 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -60,17 +60,11 @@ pandocToMarkdown opts (Pandoc (Meta title authors date) blocks) = do
                then tableOfContents opts headerBlocks
                else empty
   body <- blockListToMarkdown opts blocks
-  let before = if null (writerIncludeBefore opts)
-                  then empty
-                  else text $ writerIncludeBefore opts
-  let after = if null (writerIncludeAfter opts)
-                  then empty
-                  else text $ writerIncludeAfter opts
   (notes, _) <- get
   notes' <- notesToMarkdown opts (reverse notes)
   (_, refs) <- get  -- note that the notes may contain refs
   refs' <- keyTableToMarkdown opts (reverse refs)
-  let main = render $ before $+$ body $+$ text "" $+$ notes' $+$ text "" $+$ refs' $+$ after
+  let main = render $ body $+$ text "" $+$ notes' $+$ text "" $+$ refs'
   let context  = writerVariables opts ++
                  [ ("toc", render toc)
                  , ("body", main)
diff --git a/src/Text/Pandoc/Writers/MediaWiki.hs b/src/Text/Pandoc/Writers/MediaWiki.hs
index f22172505..07fddcfb4 100644
--- a/src/Text/Pandoc/Writers/MediaWiki.hs
+++ b/src/Text/Pandoc/Writers/MediaWiki.hs
@@ -53,14 +53,12 @@ writeMediaWiki opts document =
 -- | Return MediaWiki representation of document.
 pandocToMediaWiki :: WriterOptions -> Pandoc -> State WriterState String
 pandocToMediaWiki opts (Pandoc _ blocks) = do
-  let before  = writerIncludeBefore opts
-  let after   = writerIncludeAfter opts
   body <- blockListToMediaWiki opts blocks
   notesExist <- get >>= return . stNotes
   let notes = if notesExist
                  then "\n<references />"
                  else "" 
-  let main = before ++ body ++ after ++ notes
+  let main = body ++ notes
   let context = writerVariables opts ++
                 [ ("body", main) ] ++
                 [ ("toc", "yes") | writerTableOfContents opts ]
diff --git a/src/Text/Pandoc/Writers/OpenDocument.hs b/src/Text/Pandoc/Writers/OpenDocument.hs
index e3ed913d4..347072cf1 100644
--- a/src/Text/Pandoc/Writers/OpenDocument.hs
+++ b/src/Text/Pandoc/Writers/OpenDocument.hs
@@ -166,12 +166,7 @@ writeOpenDocument opts (Pandoc (Meta title authors date) blocks) =
            date'' <- inlinesToOpenDocument opts date
            doc'' <- blocksToOpenDocument opts blocks
            return (doc'', title'', authors'', date'')
-      before   = writerIncludeBefore opts
-      after    = writerIncludeAfter opts
-      body     = (if null before then empty else text before) $$
-                 doc $$
-                 (if null after then empty else text after)
-      body'    = render body
+      body'    = render doc
       styles   = stTableStyles s ++ stParaStyles s ++ stTextStyles s
       listStyle (n,l) = inTags True "text:list-style"
                           [("style:name", "L" ++ show n)] (vcat l)
diff --git a/src/Text/Pandoc/Writers/RST.hs b/src/Text/Pandoc/Writers/RST.hs
index e046c5a81..bcffb0693 100644
--- a/src/Text/Pandoc/Writers/RST.hs
+++ b/src/Text/Pandoc/Writers/RST.hs
@@ -59,10 +59,6 @@ writeRST opts document =
 pandocToRST :: Pandoc -> State WriterState String
 pandocToRST (Pandoc (Meta tit auth dat) blocks) = do
   opts <- liftM stOptions get
-  let before  = writerIncludeBefore opts
-      after   = writerIncludeAfter opts
-      before' = if null before then empty else text before
-      after'  = if null after then empty else text after
   title <- titleToRST tit
   authors <- mapM inlineListToRST auth
   date <- inlineListToRST dat
@@ -72,8 +68,7 @@ pandocToRST (Pandoc (Meta tit auth dat) blocks) = do
   refs <- liftM (reverse . stLinks) get >>= keyTableToRST
   pics <- liftM (reverse . stImages) get >>= pictTableToRST
   hasMath <- liftM stHasMath get
-  let main = render $ before' $+$ body $+$ notes $+$
-                      text "" $+$ refs $+$ pics $+$ after'
+  let main = render $ body $+$ notes $+$ text "" $+$ refs $+$ pics
   let context = writerVariables opts ++
                 [ ("body", main)
                 , ("title", render title)
diff --git a/src/Text/Pandoc/Writers/RTF.hs b/src/Text/Pandoc/Writers/RTF.hs
index b9ef2a29a..c0c3d0536 100644
--- a/src/Text/Pandoc/Writers/RTF.hs
+++ b/src/Text/Pandoc/Writers/RTF.hs
@@ -42,9 +42,7 @@ writeRTF options (Pandoc (Meta title authors date) blocks) =
       authorstext = map inlineListToRTF authors
       datetext = inlineListToRTF date
       spacer = not $ all null $ titletext : datetext : authorstext
-      body = writerIncludeBefore options ++ 
-             concatMap (blockToRTF 0 AlignDefault) blocks ++ 
-             writerIncludeAfter options
+      body = concatMap (blockToRTF 0 AlignDefault) blocks
       context = writerVariables options ++
                 [ ("body", body)
                 , ("title", titletext)
diff --git a/src/Text/Pandoc/Writers/Texinfo.hs b/src/Text/Pandoc/Writers/Texinfo.hs
index db32e5a62..bd77c57bb 100644
--- a/src/Text/Pandoc/Writers/Texinfo.hs
+++ b/src/Text/Pandoc/Writers/Texinfo.hs
@@ -69,13 +69,7 @@ pandocToTexinfo options (Pandoc (Meta title authors date) blocks) = do
   let titlePage = not $ all null $ title : date : authors
   main <- blockListToTexinfo blocks
   st <- get
-  let before = if null (writerIncludeBefore options)
-                  then empty
-                  else text (writerIncludeBefore options)
-  let after  = if null (writerIncludeAfter options)
-                  then empty
-                  else text (writerIncludeAfter options)
-  let body = render $ before $$ main $$ after
+  let body = render main
   let context = writerVariables options ++
                 [ ("body", body)
                 , ("title", render titleText)
diff --git a/src/pandoc.hs b/src/pandoc.hs
index 659b091c8..5e2eec08a 100644
--- a/src/pandoc.hs
+++ b/src/pandoc.hs
@@ -399,9 +399,10 @@ options =
                  (ReqArg
                   (\arg opt -> do
                      text <- readFile arg
-                     let oldBefore = optBefore opt
-                     -- add new text to end, so it is included in proper order
-                     return opt { optBefore =  oldBefore ++ [text] })
+                     -- add new ones to end, so they're included in order specified
+                     let newvars = optVariables opt ++ [("include-before",text)]
+                     return opt { optVariables = newvars,
+                                  optStandalone = True })
                   "FILENAME")
                  "" -- "File to include before document body"
 
@@ -409,9 +410,10 @@ options =
                  (ReqArg
                   (\arg opt -> do
                      text <- readFile arg
-                     let oldAfter = optAfter opt
-                     -- add new text to end, so it is included in proper order
-                     return opt { optAfter = oldAfter ++ [text]})
+                     -- add new ones to end, so they're included in order specified
+                     let newvars = optVariables opt ++ [("include-after",text)]
+                     return opt { optVariables = newvars,
+                                  optStandalone = True })
                   "FILENAME")
                  "" -- "File to include after document body"
 
diff --git a/templates/context.template b/templates/context.template
index adfaec494..6d7e842a2 100644
--- a/templates/context.template
+++ b/templates/context.template
@@ -73,10 +73,16 @@ $endif$
   \blank[3*medium]
 \stopalignment
 $endif$
+$for(include-before)$
+$include-before$
+$endfor$
 $if(toc)$
 \placecontent
 $endif$
 
 $body$
 
+$for(include-after)$
+$include-after$
+$endfor$
 \stoptext
diff --git a/templates/docbook.template b/templates/docbook.template
index 2f0de73da..374a8fbf2 100644
--- a/templates/docbook.template
+++ b/templates/docbook.template
@@ -17,6 +17,12 @@ $if(date)$
     <date>$date$</date>
 $endif$
   </articleinfo>
+$for(include-before)$
+$include-before$
+$endfor$
 $body$
+$for(include-after)$
+$include-after$
+$endfor$
 </article>
 
diff --git a/templates/html.template b/templates/html.template
index 7f1bd17db..8d1fafd36 100644
--- a/templates/html.template
+++ b/templates/html.template
@@ -44,9 +44,15 @@ $endif$
 $if(title)$
 <h1 class="title">$title$</h1>
 $endif$
+$for(include-before)$
+$include-before$
+$endfor$
 $if(toc)$
 $toc$
 $endif$
 $body$
+$for(include-after)$
+$include-after$
+$endfor$
 </body>
 </html>
diff --git a/templates/latex.template b/templates/latex.template
index bca71f091..02e570a76 100644
--- a/templates/latex.template
+++ b/templates/latex.template
@@ -80,10 +80,18 @@ $if(title)$
 \maketitle
 $endif$
 
+$for(include-before)$
+$include-before$
+
+$endfor$
 $if(toc)$
 \tableofcontents
 
 $endif$
 $body$
+$for(include-after)$
+
+$include-after$
+$endfor$
 
 \end{document}
diff --git a/templates/man.template b/templates/man.template
index 586724395..ff86c8ca6 100644
--- a/templates/man.template
+++ b/templates/man.template
@@ -5,7 +5,13 @@ $endif$
 $for(header-includes)$
 $header-includes$
 $endfor$
+$for(include-before)$
+$include-before$
+$endfor$
 $body$
+$for(include-after)$
+$include-after$
+$endfor$
 $if(author)$
 .SH AUTHORS
 $for(author)$$author$$sep$; $endfor$.
diff --git a/templates/markdown.template b/templates/markdown.template
index 6124462ba..d500d3384 100644
--- a/templates/markdown.template
+++ b/templates/markdown.template
@@ -7,9 +7,17 @@ $endif$
 $for(header-includes)$
 $header-includes$
 
+$endfor$
+$for(include-before)$
+$include-before$
+
 $endfor$
 $if(toc)$
 $toc$
 
 $endif$
 $body$
+$for(include-after)$
+
+$include-after$
+$endfor$
diff --git a/templates/mediawiki.template b/templates/mediawiki.template
index b32808d65..673e2fc8d 100644
--- a/templates/mediawiki.template
+++ b/templates/mediawiki.template
@@ -1,8 +1,16 @@
 $if(legacy-header)$
 $legacy-header$
 $endif$
+$for(include-before)$
+$include-before$
+
+$endfor$
 $if(toc)$
 __TOC__
 
 $endif$
 $body$
+$for(include-after)$
+
+$include-after$
+$endfor$
diff --git a/templates/opendocument.template b/templates/opendocument.template
index 782c2ad53..ca49782f0 100644
--- a/templates/opendocument.template
+++ b/templates/opendocument.template
@@ -15,7 +15,13 @@ $endfor$
 $if(date)$
 <text:p text:style-name="Date">$date$</text:p>
 $endif$
+$for(include-before)$
+$include-before$
+$endfor$
 $body$
+$for(include-after)$
+$include-after$
+$endfor$
 </office:text>
 </office:body>
 </office:document-content>
diff --git a/templates/rst.template b/templates/rst.template
index b4c3e2733..f09bdd8b9 100644
--- a/templates/rst.template
+++ b/templates/rst.template
@@ -20,6 +20,10 @@ $if(math)$
    :format: html latex
 
 $endif$
+$for(include-before)$
+$include-before$
+
+$endfor$
 $if(toc)$
 .. contents::
 
@@ -29,3 +33,7 @@ $header-includes$
 
 $endfor$
 $body$
+$for(include-after)$
+
+$include-after$
+$endfor$
diff --git a/templates/rtf.template b/templates/rtf.template
index 40030077b..c5493cd34 100644
--- a/templates/rtf.template
+++ b/templates/rtf.template
@@ -21,5 +21,11 @@ $endif$
 $if(spacer)$
 {\pard \ql \f0 \sa180 \li0 \fi0 \par}
 $endif$
+$for(include-before)$
+$include-before$
+$endfor$
 $body$
+$for(include-after)$
+$include-after$
+$endfor$
 }
diff --git a/templates/texinfo.template b/templates/texinfo.template
index c910badfb..58948a068 100644
--- a/templates/texinfo.template
+++ b/templates/texinfo.template
@@ -51,10 +51,18 @@ $endif$
 @end titlepage
 
 $endif$
+$for(include-before)$
+$include-before$
+
+$endfor$
 $if(toc)$
 @contents
 
 $endif$
 $body$
+$for(include-after)$
+
+$include-after$
+$endfor$
 
 @bye
diff --git a/tests/s5.inserts.html b/tests/s5.inserts.html
index c223f0f45..efce03868 100644
--- a/tests/s5.inserts.html
+++ b/tests/s5.inserts.html
@@ -57,7 +57,8 @@ STUFF INSERTED
       ></li
     ></ul
   ></div
->STUFF INSERTED
+>
+STUFF INSERTED
 </body>
 </html>