git.iko.soy/typeable/pandoc
on master help sign in
Modified templates to respect indentation.

git-svn-id: https://pandoc.googlecode.com/svn/trunk@1708 788f1e2b-df1e-0410-8736-df70ead52e1b

commit213895f033e60707123b42f8a780f1a3a9ef6196 authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateThu 31 Dec 2009, 01:14 (17 years ago) parent3d8cccc
2 files changed+77−57
src/Text/Pandoc/Templates.hs+48−30 templates/html.template+29−27
src/Text/Pandoc/Templates.hs+48−30
@@ -46,10 +46,11 @@ is used.
4646module Text.Pandoc.Templates (renderTemplate, getDefaultTemplate) where
4747
4848import Text.ParserCombinators.Parsec
49−import Control.Monad (liftM)
49+import Control.Monad (liftM, when)
5050import qualified Control.Exception as E (try, IOException)
5151import System.FilePath
5252import Text.Pandoc.Shared (readDataFile)
53+import Data.List (intercalate)
5354
5455-- | Get the default template, either from the application's user data
5556-- directory (~/.pandoc on unix) or from the cabal data directory.
@@ -60,51 +61,67 @@ getDefaultTemplate "odt" = getDefaultTemplate "opendocument"
6061getDefaultTemplate format = do
6162 let format' = takeWhile (/='+') format -- strip off "+lhs" if present
6263 E.try $ readDataFile $ "templates" </> format' <.> "template"
63−
64+
65+data TemplateState = TemplateState Int [(String,String)]
66+
67+adjustPosition :: String -> GenParser Char TemplateState String
68+adjustPosition str = do
69+ let lastline = takeWhile (/= '\n') $ reverse str
70+ updateState $ \(TemplateState pos x) ->
71+ if str == lastline
72+ then TemplateState (pos + length lastline) x
73+ else TemplateState (length lastline) x
74+ return str
75+
6476-- | Renders a template
6577renderTemplate :: [(String,String)] -- ^ Assoc. list of values for variables
6678 -> String -- ^ Template
6779 -> String
6880renderTemplate vals templ =
69− case runParser (do x <- parseTemplate; eof; return x) vals "template" templ of
81+ case runParser (do x <- parseTemplate; eof; return x) (TemplateState 0 vals) "template" templ of
7082 Left e -> error $ show e
7183 Right r -> concat r
7284
7385reservedWords :: [String]
7486reservedWords = ["else","endif"]
7587
76−parseTemplate :: GenParser Char [(String,String)] [String]
88+parseTemplate :: GenParser Char TemplateState [String]
7789parseTemplate =
78− many $ plaintext <|> escapedDollar <|> conditional <|> variable
90+ many $ (plaintext <|> escapedDollar <|> conditional <|> variable)
91+ >>= adjustPosition
7992
80−plaintext :: GenParser Char [(String,String)] String
81−plaintext = many1 $ satisfy (/='$')
93+plaintext :: GenParser Char TemplateState String
94+plaintext = many1 $ noneOf "$"
8295
83−escapedDollar :: GenParser Char [(String,String)] String
96+escapedDollar :: GenParser Char TemplateState String
8497escapedDollar = try $ string "$$" >> return "$"
8598
86−conditional :: GenParser Char [(String,String)] String
99+conditional :: GenParser Char TemplateState String
87100conditional = try $ do
101+ TemplateState pos vars <- getState
88102 string "$if("
89103 id' <- ident
90104 string ")$"
91− skipMany (oneOf " \t")
92− optional newline
93− ifContents <- liftM concat parseTemplate
94− elseContents <- option "" $ do try (string "$else$")
95− skipMany (oneOf " \t")
96− optional newline
97− liftM concat parseTemplate
105+ -- if newline after the "if", then a newline after "endif" will be swallowed
106+ multiline <- option False $ try $
107+ newline >> count pos (char ' ') >> return True
108+ let conditionSatisfied = case lookup id' vars of
109+ Nothing -> False
110+ Just "" -> False
111+ Just _ -> True
112+ contents <- if conditionSatisfied
113+ then liftM concat parseTemplate
114+ else do
115+ parseTemplate -- skip if part, then reset position
116+ setState $ TemplateState pos vars
117+ option "" $ do try (string "$else$")
118+ optional newline
119+ liftM concat parseTemplate
98120 string "$endif$"
99− skipMany (oneOf " \t")
100− optional newline
101− st <- getState
102− return $ case lookup id' st of
103− Just "" -> elseContents
104− Just _ -> ifContents
105− Nothing -> elseContents
106−
107−ident :: GenParser Char [(String,String)] String
121+ when multiline $ optional $ newline
122+ return contents
123+
124+ident :: GenParser Char TemplateState String
108125ident = do
109126 first <- letter
110127 rest <- many (alphaNum <|> oneOf "_-")
@@ -113,12 +130,13 @@ ident = do
113130 then pzero
114131 else return id'
115132
116−variable :: GenParser Char [(String,String)] String
133+variable :: GenParser Char TemplateState String
117134variable = try $ do
118135 char '$'
119136 id' <- ident
120137 char '$'
121− st <- getState
122− return $ case lookup id' st of
123− Just val -> val
124− Nothing -> ""
138+ TemplateState pos vars <- getState
139+ let indent = replicate pos ' '
140+ return $ case lookup id' vars of
141+ Just val -> intercalate ('\n' : indent) $ lines val
142+ Nothing -> ""
templates/html.template+29−27
@@ -1,47 +1,49 @@
11<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
22<html xmlns="http://www.w3.org/1999/xhtml">
33<head>
4− <title>$if(title-prefix)$$title-prefix$ - $endif$$if(pagetitle)$$pagetitle$$endif$</title>
4+ <title>
5+ $if(title-prefix)$$title-prefix$ - $endif$$if(pagetitle)$$pagetitle$$endif$
6+ </title>
57 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
68 <meta name="generator" content="pandoc" />
79 <meta name="author" content="$authors$" />
810 <meta name="date" content="$date$" />
911 $if(highlighting)$
1012 <style type="text/css">
11− table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode, table.sourceCode pre { margin: 0; padding: 0; border: 0; vertical-align: baseline; border: none; }
12− td.lineNumbers { border-right: 1px solid #AAAAAA; text-align: right; color: #AAAAAA; padding-right: 5px; padding-left: 5px; }
13− td.sourceCode { padding-left: 5px; }
14− pre.sourceCode { }
15− pre.sourceCode span.Normal { }
16− pre.sourceCode span.Keyword { color: #007020; font-weight: bold; }
17− pre.sourceCode span.DataType { color: #902000; }
18− pre.sourceCode span.DecVal { color: #40a070; }
19− pre.sourceCode span.BaseN { color: #40a070; }
20− pre.sourceCode span.Float { color: #40a070; }
21− pre.sourceCode span.Char { color: #4070a0; }
22− pre.sourceCode span.String { color: #4070a0; }
23− pre.sourceCode span.Comment { color: #60a0b0; font-style: italic; }
24− pre.sourceCode span.Others { color: #007020; }
25− pre.sourceCode span.Alert { color: red; font-weight: bold; }
26− pre.sourceCode span.Function { color: #06287e; }
27− pre.sourceCode span.RegionMarker { }
28− pre.sourceCode span.Error { color: red; font-weight: bold; }
13+ table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode, table.sourceCode pre { margin: 0; padding: 0; border: 0; vertical-align: baseline; border: none; }
14+ td.lineNumbers { border-right: 1px solid #AAAAAA; text-align: right; color: #AAAAAA; padding-right: 5px; padding-left: 5px; }
15+ td.sourceCode { padding-left: 5px; }
16+ pre.sourceCode { }
17+ pre.sourceCode span.Normal { }
18+ pre.sourceCode span.Keyword { color: #007020; font-weight: bold; }
19+ pre.sourceCode span.DataType { color: #902000; }
20+ pre.sourceCode span.DecVal { color: #40a070; }
21+ pre.sourceCode span.BaseN { color: #40a070; }
22+ pre.sourceCode span.Float { color: #40a070; }
23+ pre.sourceCode span.Char { color: #4070a0; }
24+ pre.sourceCode span.String { color: #4070a0; }
25+ pre.sourceCode span.Comment { color: #60a0b0; font-style: italic; }
26+ pre.sourceCode span.Others { color: #007020; }
27+ pre.sourceCode span.Alert { color: red; font-weight: bold; }
28+ pre.sourceCode span.Function { color: #06287e; }
29+ pre.sourceCode span.RegionMarker { }
30+ pre.sourceCode span.Error { color: red; font-weight: bold; }
2931 </style>
3032 $endif$
3133 $if(header-includes)$
32− $header-includes$
34+ $header-includes$
3335 $endif$
3436 $if(latexmathml-script)$
35− $latexmathml-script$
37+ $latexmathml-script$
3638 $endif$
3739</head>
3840<body>
39−$if(title)$
41+ $if(title)$
4042 <h1 class="title">$title$</h1>
41−$endif$
42−$if(toc)$
43−$toc$
44−$endif$
45−$body$
43+ $endif$
44+ $if(toc)$
45+ $toc$
46+ $endif$
47+ $body$
4648</body>
4749</html>