diff options
Diffstat (limited to 'src/Text/Pandoc/Writers/RST.hs')
-rw-r--r-- | src/Text/Pandoc/Writers/RST.hs | 62 |
1 files changed, 37 insertions, 25 deletions
diff --git a/src/Text/Pandoc/Writers/RST.hs b/src/Text/Pandoc/Writers/RST.hs index 72afb1f21..fc9b69983 100644 --- a/src/Text/Pandoc/Writers/RST.hs +++ b/src/Text/Pandoc/Writers/RST.hs @@ -30,11 +30,12 @@ Conversion of 'Pandoc' documents to reStructuredText. reStructuredText: <http://docutils.sourceforge.net/rst.html> -} -module Text.Pandoc.Writers.RST ( writeRST) where +module Text.Pandoc.Writers.RST ( writeRST ) where import Text.Pandoc.Definition import Text.Pandoc.Options import Text.Pandoc.Shared -import Text.Pandoc.Templates (renderTemplate) +import Text.Pandoc.Templates (renderTemplate') +import Text.Pandoc.Builder (deleteMeta) import Data.List ( isPrefixOf, intersperse, transpose ) import Network.URI (isAbsoluteURI) import Text.Pandoc.Pretty @@ -62,31 +63,35 @@ writeRST opts document = -- | Return RST representation of document. pandocToRST :: Pandoc -> State WriterState String -pandocToRST (Pandoc (Meta tit auth dat) blocks) = do +pandocToRST (Pandoc meta blocks) = do opts <- liftM stOptions get - title <- titleToRST tit - authors <- mapM inlineListToRST auth - date <- inlineListToRST dat + let colwidth = if writerWrapText opts + then Just $ writerColumns opts + else Nothing + let subtit = case lookupMeta "subtitle" meta of + Just (MetaBlocks [Plain xs]) -> xs + _ -> [] + title <- titleToRST (docTitle meta) subtit + metadata <- metaToJSON (fmap (render colwidth) . blockListToRST) + (fmap (trimr . render colwidth) . inlineListToRST) + $ deleteMeta "title" $ deleteMeta "subtitle" meta body <- blockListToRST blocks notes <- liftM (reverse . stNotes) get >>= notesToRST -- note that the notes may contain refs, so we do them first refs <- liftM (reverse . stLinks) get >>= refsToRST pics <- liftM (reverse . stImages) get >>= pictRefsToRST hasMath <- liftM stHasMath get - let colwidth = if writerWrapText opts - then Just $ writerColumns opts - else Nothing let main = render colwidth $ foldl ($+$) empty $ [body, notes, refs, pics] - let context = writerVariables opts ++ - [ ("body", main) - , ("title", render Nothing title) - , ("date", render colwidth date) - , ("toc", if writerTableOfContents opts then "yes" else "") - , ("toc-depth", show (writerTOCDepth opts)) ] ++ - [ ("math", "yes") | hasMath ] ++ - [ ("author", render colwidth a) | a <- authors ] + let context = setField "body" main + $ setField "toc" (writerTableOfContents opts) + $ setField "toc-depth" (writerTOCDepth opts) + $ setField "math" hasMath + $ setField "title" (render Nothing title :: String) + $ setField "math" hasMath + $ foldl (\acc (x,y) -> setField x y acc) + metadata (writerVariables opts) if writerStandalone opts - then return $ renderTemplate context $ writerTemplate opts + then return $ renderTemplate' (writerTemplate opts) context else return main -- | Return RST representation of reference key table. @@ -136,13 +141,20 @@ pictToRST (label, (src, _, mbtarget)) = do escapeString :: String -> String escapeString = escapeStringUsing (backslashEscapes "`\\|*_") -titleToRST :: [Inline] -> State WriterState Doc -titleToRST [] = return empty -titleToRST lst = do - contents <- inlineListToRST lst - let titleLength = length $ (render Nothing contents :: String) - let border = text (replicate titleLength '=') - return $ border $$ contents $$ border +titleToRST :: [Inline] -> [Inline] -> State WriterState Doc +titleToRST [] _ = return empty +titleToRST tit subtit = do + title <- inlineListToRST tit + subtitle <- inlineListToRST subtit + return $ bordered title '=' $$ bordered subtitle '-' + +bordered :: Doc -> Char -> Doc +bordered contents c = + if len > 0 + then border $$ contents $$ border + else empty + where len = offset contents + border = text (replicate len c) -- | Convert Pandoc block element to RST. blockToRST :: Block -- ^ Block element |