From 4806628c8d8ef9e696a981e6f47ede2b3a6f4d48 Mon Sep 17 00:00:00 2001
From: fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b>
Date: Thu, 31 Dec 2009 01:12:14 +0000
Subject: [PATCH] Added 'standalone' variable, use in templates.

git-svn-id: https://pandoc.googlecode.com/svn/trunk@1697 788f1e2b-df1e-0410-8736-df70ead52e1b
---
 README                              | 2 ++
 man/man1/pandoc.1.md                | 2 ++
 src/Text/Pandoc/Writers/LaTeX.hs    | 7 +------
 src/Text/Pandoc/Writers/Markdown.hs | 7 +------
 src/pandoc.hs                       | 5 ++++-
 templates/latex.template            | 4 ++++
 templates/markdown.template         | 2 ++
 7 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/README b/README
index 8b5d869a4..8140087b1 100644
--- a/README
+++ b/README
@@ -475,6 +475,8 @@ will be replaced by the document title.
 Some variables are set automatically by pandoc.  These vary somewhat
 depending on the output format, but include:
 
+`standalone`
+:   "yes" if the `-s/--standalone` option was specified
 `before`
 :   contents specified by `-B/--include-before-body` 
 `after`
diff --git a/man/man1/pandoc.1.md b/man/man1/pandoc.1.md
index d8209d2c1..067c435ba 100644
--- a/man/man1/pandoc.1.md
+++ b/man/man1/pandoc.1.md
@@ -256,6 +256,8 @@ will be replaced by the document title.
 Some variables are set automatically by pandoc.  These vary somewhat
 depending on the output format, but include:
 
+`standalone`
+:   "yes" if the `-s/--standalone` option was specified
 `before`
 :   contents specified by `-B/--include-before-body` 
 `after`
diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs
index fadde0760..92bf65b0a 100644
--- a/src/Text/Pandoc/Writers/LaTeX.hs
+++ b/src/Text/Pandoc/Writers/LaTeX.hs
@@ -62,12 +62,7 @@ pandocToLaTeX options (Pandoc (Meta title authors date) blocks) = do
                  , ("title", titletext)
                  , ("authors", intercalate "\\\\" $ map stringToLaTeX authors)
                  , ("date", stringToLaTeX date) ]
-  let templ = if writerStandalone options
-                 then writerTemplate options
-                 else "$if(toc)$\\tableofcontents\n$endif$" ++
-                      "$if(before)$$before$\n$endif$" ++
-                      "$body$$if(after)$$after$\n$endif$"
-  return $ renderTemplate context templ
+  return $ renderTemplate context $ writerTemplate options
 
 -- escape things as needed for LaTeX
 
diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index 305b71310..a72116376 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -70,12 +70,7 @@ pandocToMarkdown opts (Pandoc meta blocks) = do
                                      text "" $+$ refs')
                  , ("titleblock", render head')
                  ]
-  let templ = if writerStandalone opts
-                 then writerTemplate opts
-                 else "$if(toc)$$toc$\n$endif$" ++
-                      "$if(before)$$before$\n$endif$" ++
-                      "$body$$if(after)$$after$\n$endif$"
-  return $ renderTemplate context templ
+  return $ renderTemplate context $ writerTemplate opts
 
 -- | Return markdown representation of reference key table.
 keyTableToMarkdown :: WriterOptions -> KeyTable -> State WriterState Doc
diff --git a/src/pandoc.hs b/src/pandoc.hs
index 26bf0abb8..a18e069cc 100644
--- a/src/pandoc.hs
+++ b/src/pandoc.hs
@@ -212,7 +212,10 @@ options =
 
     , Option "s" ["standalone"]
                  (NoArg
-                  (\opt -> return opt { optStandalone = True }))
+                  (\opt -> do
+                      let newvars = ("standalone", "yes") : optVariables opt
+                      return opt { optStandalone = True,
+                                   optVariables = newvars }))
                  "" -- "Include needed header and footer on output"
 
     , Option "o" ["output"]
diff --git a/templates/latex.template b/templates/latex.template
index 7c235f8e4..6af0a59cc 100644
--- a/templates/latex.template
+++ b/templates/latex.template
@@ -1,3 +1,4 @@
+$if(standalone)$
 $if(legacy-header)$
 $legacy-header$
 $else$
@@ -48,6 +49,7 @@ $endif$
 \begin{document}
 \maketitle
 
+$endif$
 $if(toc)$
 \tableofcontents
 $endif$
@@ -58,4 +60,6 @@ $body$
 $if(after)$
 $after$
 $endif$
+$if(standalone)$
 \end{document}
+$endif$
diff --git a/templates/markdown.template b/templates/markdown.template
index d5df8a606..5c8a27674 100644
--- a/templates/markdown.template
+++ b/templates/markdown.template
@@ -1,9 +1,11 @@
+$if(standalone)$
 $if(titleblock)$
 $titleblock$
 $endif$
 $if(header-includes)$
 $header-includes$
 $endif$
+$endif$
 $if(before)$
 $before$
 $endif$