Improved RST writer.

Updated test suite.

git-svn-id: https://pandoc.googlecode.com/svn/trunk@1725 788f1e2b-df1e-0410-8736-df70ead52e1b
This commit is contained in:
fiddlosopher 2009-12-31 01:16:19 +00:00
parent f4e738a6a9
commit 312d9337b9
7 changed files with 28 additions and 44 deletions

View file

@ -58,16 +58,15 @@ writeRST opts document =
-- | Return RST representation of document.
pandocToRST :: Pandoc -> State WriterState String
pandocToRST (Pandoc meta blocks) = do
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
metaBlock <- metaToRST opts meta
let head' = if writerStandalone opts
then render metaBlock
else ""
title <- titleToRST tit
authors <- mapM inlineListToRST auth
date <- inlineListToRST dat
body <- blockListToRST blocks
notes <- liftM (reverse . stNotes) get >>= notesToRST
-- note that the notes may contain refs, so we do them first
@ -78,9 +77,10 @@ pandocToRST (Pandoc meta blocks) = do
text "" $+$ refs $+$ pics $+$ after'
let context = writerVariables opts ++
[ ("body", main)
, ("titleblock", head')
] ++
[ ("math", "yes") | hasMath ]
, ("title", render title)
, ("date", render date) ] ++
[ ("math", "yes") | hasMath ] ++
[ ("author", render a) | a <- authors ]
if writerStandalone opts
then return $ renderTemplate context $ writerTemplate opts
else return main
@ -136,38 +136,13 @@ wrappedRST opts inlines = do
escapeString :: String -> String
escapeString = escapeStringUsing (backslashEscapes "`\\|*_")
-- | Convert bibliographic information into RST header.
metaToRST :: WriterOptions -> Meta -> State WriterState Doc
metaToRST _ (Meta [] [] []) = return empty
metaToRST opts (Meta title authors date) = do
title' <- titleToRST title
authors' <- authorsToRST authors
date' <- dateToRST date
let toc = if writerTableOfContents opts
then text "" $+$ text ".. contents::"
else empty
return $ title' $+$ authors' $+$ date' $+$ toc $+$ text ""
titleToRST :: [Inline] -> State WriterState Doc
titleToRST [] = return empty
titleToRST lst = do
contents <- inlineListToRST lst
let titleLength = length $ render contents
let border = text (replicate titleLength '=')
return $ border $+$ contents $+$ border <> text "\n"
authorsToRST :: [[Inline]] -> State WriterState Doc
authorsToRST [] = return empty
authorsToRST (first:rest) = do
rest' <- authorsToRST rest
first' <- inlineListToRST first
return $ (text ":Author: " <> first') $+$ rest'
dateToRST :: [Inline] -> State WriterState Doc
dateToRST [] = return empty
dateToRST str = do
date <- inlineListToRST str
return $ text ":Date: " <> date
return $ border $+$ contents $+$ border
-- | Convert Pandoc block element to RST.
blockToRST :: Block -- ^ Block element

View file

@ -1,15 +1,19 @@
$if(titleblock)$
$if(title)$
$title$
$endif$
$if(authors)$
$authors$
$endif$
$for(author)$
:Author: $author$
$endfor$
$if(date)$
Date: $date$
:Date: $date$
$endif$
$if(author)$
$else$
$if(date)$
$endif$
$endif$
$if(math)$
.. role:: math(raw)

View file

@ -4,7 +4,8 @@
<title>My S5 Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="author" content="Sam Smith; Jen Jones" />
<meta name="author" content="Sam Smith" />
<meta name="author" content="Jen Jones" />
<meta name="date" content="July 15, 2006" />
<!-- configuration parameters -->
<meta name="defaultView" content="slideshow" />

View file

@ -4,7 +4,8 @@
<title>My S5 Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="author" content="Sam Smith; Jen Jones" />
<meta name="author" content="Sam Smith" />
<meta name="author" content="Jen Jones" />
<meta name="date" content="July 15, 2006" />
<!-- configuration parameters -->
<meta name="defaultView" content="slideshow" />

View file

@ -4,8 +4,10 @@
<title>My S5 Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="author" content="Sam Smith; Jen Jones" />
<meta name="author" content="Sam Smith" />
<meta name="author" content="Jen Jones" />
<meta name="date" content="July 15, 2006" />
<link rel="stylesheet" href="main.css" type="text/css" />
STUFF INSERTED
</head>
<body>

View file

@ -4,7 +4,8 @@
<title>Pandoc Test Suite</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="author" content="John MacFarlane; Anonymous" />
<meta name="author" content="John MacFarlane" />
<meta name="author" content="Anonymous" />
<meta name="date" content="July 17, 2006" />
</head>
<body>

View file

@ -4,7 +4,7 @@ Pandoc Test Suite
:Author: John MacFarlane
:Author: Anonymous
:Date: July 17, 2006
:Date: July 17, 2006
.. role:: math(raw)
:format: html latex