2010-12-12 20:09:14 -08:00
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2007-11-03 23:27:58 +00:00
|
|
|
|
{-
|
2010-03-23 13:31:09 -07:00
|
|
|
|
Copyright (C) 2006-2010 John MacFarlane <jgm@berkeley.edu>
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
{- |
|
|
|
|
|
Module : Text.Pandoc.Writers.RST
|
2010-03-23 13:31:09 -07:00
|
|
|
|
Copyright : Copyright (C) 2006-2010 John MacFarlane
|
2007-11-03 23:27:58 +00:00
|
|
|
|
License : GNU GPL, version 2 or above
|
|
|
|
|
|
|
|
|
|
Maintainer : John MacFarlane <jgm@berkeley.edu>
|
|
|
|
|
Stability : alpha
|
|
|
|
|
Portability : portable
|
|
|
|
|
|
|
|
|
|
Conversion of 'Pandoc' documents to reStructuredText.
|
|
|
|
|
|
|
|
|
|
reStructuredText: <http://docutils.sourceforge.net/rst.html>
|
|
|
|
|
-}
|
|
|
|
|
module Text.Pandoc.Writers.RST ( writeRST) where
|
|
|
|
|
import Text.Pandoc.Definition
|
|
|
|
|
import Text.Pandoc.Shared
|
2009-12-31 01:14:57 +00:00
|
|
|
|
import Text.Pandoc.Templates (renderTemplate)
|
2010-12-12 20:09:14 -08:00
|
|
|
|
import Data.List ( isPrefixOf, intersperse, transpose )
|
|
|
|
|
import Text.Pandoc.Pretty
|
2007-11-03 23:27:58 +00:00
|
|
|
|
import Control.Monad.State
|
2008-12-02 22:43:25 +00:00
|
|
|
|
import Control.Applicative ( (<$>) )
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
2010-05-08 10:03:02 -07:00
|
|
|
|
type Refs = [([Inline], Target)]
|
|
|
|
|
|
2008-01-04 18:58:50 +00:00
|
|
|
|
data WriterState =
|
|
|
|
|
WriterState { stNotes :: [[Block]]
|
2010-05-08 10:03:02 -07:00
|
|
|
|
, stLinks :: Refs
|
|
|
|
|
, stImages :: Refs
|
2009-12-31 01:14:57 +00:00
|
|
|
|
, stHasMath :: Bool
|
2008-01-04 18:58:50 +00:00
|
|
|
|
, stOptions :: WriterOptions
|
|
|
|
|
}
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert Pandoc to RST.
|
|
|
|
|
writeRST :: WriterOptions -> Pandoc -> String
|
|
|
|
|
writeRST opts document =
|
2008-01-04 18:58:50 +00:00
|
|
|
|
let st = WriterState { stNotes = [], stLinks = [],
|
2009-12-31 01:14:57 +00:00
|
|
|
|
stImages = [], stHasMath = False,
|
2008-01-04 18:58:50 +00:00
|
|
|
|
stOptions = opts }
|
2009-12-31 01:14:57 +00:00
|
|
|
|
in evalState (pandocToRST document) st
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Return RST representation of document.
|
2009-12-31 01:14:57 +00:00
|
|
|
|
pandocToRST :: Pandoc -> State WriterState String
|
2009-12-31 01:16:19 +00:00
|
|
|
|
pandocToRST (Pandoc (Meta tit auth dat) blocks) = do
|
2009-12-31 01:14:57 +00:00
|
|
|
|
opts <- liftM stOptions get
|
2009-12-31 01:16:19 +00:00
|
|
|
|
title <- titleToRST tit
|
|
|
|
|
authors <- mapM inlineListToRST auth
|
|
|
|
|
date <- inlineListToRST dat
|
2009-12-31 01:14:57 +00:00
|
|
|
|
body <- blockListToRST blocks
|
|
|
|
|
notes <- liftM (reverse . stNotes) get >>= notesToRST
|
|
|
|
|
-- note that the notes may contain refs, so we do them first
|
2010-05-08 10:03:02 -07:00
|
|
|
|
refs <- liftM (reverse . stLinks) get >>= refsToRST
|
|
|
|
|
pics <- liftM (reverse . stImages) get >>= pictRefsToRST
|
2009-12-31 01:14:57 +00:00
|
|
|
|
hasMath <- liftM stHasMath get
|
2010-12-12 20:09:14 -08:00
|
|
|
|
let colwidth = if writerWrapText opts
|
|
|
|
|
then Just $ writerColumns opts
|
|
|
|
|
else Nothing
|
|
|
|
|
let main = render colwidth $ foldl ($+$) empty $ [body, notes, refs, pics]
|
2009-12-31 01:14:57 +00:00
|
|
|
|
let context = writerVariables opts ++
|
|
|
|
|
[ ("body", main)
|
2010-12-12 20:09:14 -08:00
|
|
|
|
, ("title", render Nothing title)
|
|
|
|
|
, ("date", render colwidth date) ] ++
|
2009-12-31 01:16:19 +00:00
|
|
|
|
[ ("math", "yes") | hasMath ] ++
|
2010-12-12 20:09:14 -08:00
|
|
|
|
[ ("author", render colwidth a) | a <- authors ]
|
2009-12-31 01:14:57 +00:00
|
|
|
|
if writerStandalone opts
|
|
|
|
|
then return $ renderTemplate context $ writerTemplate opts
|
|
|
|
|
else return main
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Return RST representation of reference key table.
|
2010-05-08 10:03:02 -07:00
|
|
|
|
refsToRST :: Refs -> State WriterState Doc
|
|
|
|
|
refsToRST refs = mapM keyToRST refs >>= return . vcat
|
2010-12-12 20:09:14 -08:00
|
|
|
|
|
2007-11-03 23:27:58 +00:00
|
|
|
|
-- | Return RST representation of a reference key.
|
2008-01-04 18:58:50 +00:00
|
|
|
|
keyToRST :: ([Inline], (String, String))
|
2007-11-03 23:27:58 +00:00
|
|
|
|
-> State WriterState Doc
|
2008-07-13 23:16:44 +00:00
|
|
|
|
keyToRST (label, (src, _)) = do
|
2008-01-04 18:58:50 +00:00
|
|
|
|
label' <- inlineListToRST label
|
2010-12-12 20:09:14 -08:00
|
|
|
|
let label'' = if ':' `elem` (render Nothing label')
|
2007-11-03 23:27:58 +00:00
|
|
|
|
then char '`' <> label' <> char '`'
|
|
|
|
|
else label'
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ ".. _" <> label'' <> ": " <> text src
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Return RST representation of notes.
|
2008-01-04 18:58:50 +00:00
|
|
|
|
notesToRST :: [[Block]] -> State WriterState Doc
|
|
|
|
|
notesToRST notes =
|
2010-12-12 20:09:14 -08:00
|
|
|
|
mapM (\(num, note) -> noteToRST num note) (zip [1..] notes) >>=
|
|
|
|
|
return . vsep
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Return RST representation of a note.
|
2008-01-04 18:58:50 +00:00
|
|
|
|
noteToRST :: Int -> [Block] -> State WriterState Doc
|
|
|
|
|
noteToRST num note = do
|
|
|
|
|
contents <- blockListToRST note
|
2010-12-12 20:09:14 -08:00
|
|
|
|
let marker = ".. [" <> text (show num) <> "]"
|
2008-12-17 15:34:25 +00:00
|
|
|
|
return $ marker $$ nest 3 contents
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Return RST representation of picture reference table.
|
2010-05-08 10:03:02 -07:00
|
|
|
|
pictRefsToRST :: Refs -> State WriterState Doc
|
|
|
|
|
pictRefsToRST refs = mapM pictToRST refs >>= return . vcat
|
2010-12-12 20:09:14 -08:00
|
|
|
|
|
2007-11-03 23:27:58 +00:00
|
|
|
|
-- | Return RST representation of a picture substitution reference.
|
2008-01-04 18:58:50 +00:00
|
|
|
|
pictToRST :: ([Inline], (String, String))
|
|
|
|
|
-> State WriterState Doc
|
|
|
|
|
pictToRST (label, (src, _)) = do
|
|
|
|
|
label' <- inlineListToRST label
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ ".. |" <> label' <> "| image:: " <> text src
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Escape special characters for RST.
|
|
|
|
|
escapeString :: String -> String
|
|
|
|
|
escapeString = escapeStringUsing (backslashEscapes "`\\|*_")
|
|
|
|
|
|
2008-01-04 18:58:50 +00:00
|
|
|
|
titleToRST :: [Inline] -> State WriterState Doc
|
|
|
|
|
titleToRST [] = return empty
|
|
|
|
|
titleToRST lst = do
|
|
|
|
|
contents <- inlineListToRST lst
|
2010-12-12 20:09:14 -08:00
|
|
|
|
let titleLength = length $ (render Nothing contents :: String)
|
2007-11-03 23:27:58 +00:00
|
|
|
|
let border = text (replicate titleLength '=')
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ border $$ contents $$ border
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert Pandoc block element to RST.
|
2008-01-04 18:58:50 +00:00
|
|
|
|
blockToRST :: Block -- ^ Block element
|
|
|
|
|
-> State WriterState Doc
|
|
|
|
|
blockToRST Null = return empty
|
2010-12-12 20:09:14 -08:00
|
|
|
|
blockToRST (Plain inlines) = inlineListToRST inlines
|
2010-03-18 02:38:58 +00:00
|
|
|
|
blockToRST (Para [Image txt (src,tit)]) = do
|
|
|
|
|
capt <- inlineListToRST txt
|
2010-12-12 20:09:14 -08:00
|
|
|
|
let fig = "figure:: " <> text src
|
|
|
|
|
let align = ":align: center"
|
|
|
|
|
let alt = ":alt: " <> if null tit then capt else text tit
|
|
|
|
|
return $ hang 3 ".. " $ fig $$ align $$ alt $+$ capt $$ blankline
|
2008-01-04 18:58:50 +00:00
|
|
|
|
blockToRST (Para inlines) = do
|
2010-12-12 20:09:14 -08:00
|
|
|
|
contents <- inlineListToRST inlines
|
|
|
|
|
return $ contents <> blankline
|
2011-01-23 10:55:56 -08:00
|
|
|
|
blockToRST (RawBlock f str) =
|
|
|
|
|
return $ blankline <> ".. raw:: " <> text f $+$
|
2011-01-06 21:03:08 -08:00
|
|
|
|
(nest 3 $ text str) $$ blankline
|
2010-12-12 20:09:14 -08:00
|
|
|
|
blockToRST HorizontalRule =
|
|
|
|
|
return $ blankline $$ "--------------" $$ blankline
|
2008-01-04 18:58:50 +00:00
|
|
|
|
blockToRST (Header level inlines) = do
|
|
|
|
|
contents <- inlineListToRST inlines
|
2007-11-03 23:27:58 +00:00
|
|
|
|
let headerChar = if level > 5 then ' ' else "=-~^'" !! (level - 1)
|
2010-12-12 20:09:14 -08:00
|
|
|
|
let border = text $ replicate (offset contents) headerChar
|
|
|
|
|
return $ contents $$ border $$ blankline
|
2008-12-02 22:43:25 +00:00
|
|
|
|
blockToRST (CodeBlock (_,classes,_) str) = do
|
|
|
|
|
opts <- stOptions <$> get
|
|
|
|
|
let tabstop = writerTabStop opts
|
2009-11-03 06:50:17 +00:00
|
|
|
|
if "haskell" `elem` classes && "literate" `elem` classes &&
|
|
|
|
|
writerLiterateHaskell opts
|
2011-01-06 21:03:08 -08:00
|
|
|
|
then return $ prefixed "> " (text str) $$ blankline
|
2010-12-12 20:09:14 -08:00
|
|
|
|
else return $ "::" $+$ nest tabstop (text str) $$ blankline
|
2008-01-04 18:58:50 +00:00
|
|
|
|
blockToRST (BlockQuote blocks) = do
|
|
|
|
|
tabstop <- get >>= (return . writerTabStop . stOptions)
|
|
|
|
|
contents <- blockListToRST blocks
|
2011-01-06 21:03:08 -08:00
|
|
|
|
return $ (nest tabstop contents) <> blankline
|
2008-07-13 23:16:44 +00:00
|
|
|
|
blockToRST (Table caption _ widths headers rows) = do
|
2008-01-04 18:58:50 +00:00
|
|
|
|
caption' <- inlineListToRST caption
|
2007-11-03 23:27:58 +00:00
|
|
|
|
let caption'' = if null caption
|
|
|
|
|
then empty
|
2010-12-12 20:09:14 -08:00
|
|
|
|
else blankline <> text "Table: " <> caption'
|
2008-01-04 18:58:50 +00:00
|
|
|
|
headers' <- mapM blockListToRST headers
|
2009-11-28 03:22:33 +00:00
|
|
|
|
rawRows <- mapM (mapM blockListToRST) rows
|
|
|
|
|
let isSimple = all (==0) widths && all (all (\bs -> length bs == 1)) rows
|
2010-12-12 20:09:14 -08:00
|
|
|
|
let numChars = maximum . map offset
|
|
|
|
|
opts <- get >>= return . stOptions
|
2009-11-28 03:22:33 +00:00
|
|
|
|
let widthsInChars =
|
|
|
|
|
if isSimple
|
|
|
|
|
then map ((+2) . numChars) $ transpose (headers' : rawRows)
|
2010-12-12 20:09:14 -08:00
|
|
|
|
else map (floor . (fromIntegral (writerColumns opts) *)) widths
|
|
|
|
|
let hpipeBlocks blocks = hcat [beg, middle, end]
|
|
|
|
|
where h = maximum (map height blocks)
|
|
|
|
|
sep' = lblock 3 $ vcat (map text $ replicate h " | ")
|
|
|
|
|
beg = lblock 2 $ vcat (map text $ replicate h "| ")
|
|
|
|
|
end = lblock 2 $ vcat (map text $ replicate h " |")
|
|
|
|
|
middle = hcat $ intersperse sep' blocks
|
|
|
|
|
let makeRow = hpipeBlocks . zipWith lblock widthsInChars
|
2008-07-13 23:16:44 +00:00
|
|
|
|
let head' = makeRow headers'
|
2008-01-04 18:58:50 +00:00
|
|
|
|
rows' <- mapM (\row -> do cols <- mapM blockListToRST row
|
2007-11-03 23:27:58 +00:00
|
|
|
|
return $ makeRow cols) rows
|
|
|
|
|
let border ch = char '+' <> char ch <>
|
|
|
|
|
(hcat $ intersperse (char ch <> char '+' <> char ch) $
|
|
|
|
|
map (\l -> text $ replicate l ch) widthsInChars) <>
|
|
|
|
|
char ch <> char '+'
|
2010-12-12 20:09:14 -08:00
|
|
|
|
let body = vcat $ intersperse (border '-') rows'
|
2010-03-11 03:01:27 +00:00
|
|
|
|
let head'' = if all null headers
|
|
|
|
|
then empty
|
2010-12-12 20:09:14 -08:00
|
|
|
|
else head' $$ border '='
|
|
|
|
|
return $ border '-' $$ head'' $$ body $$ border '-' $$ caption'' $$ blankline
|
2008-01-04 18:58:50 +00:00
|
|
|
|
blockToRST (BulletList items) = do
|
|
|
|
|
contents <- mapM bulletListItemToRST items
|
2007-11-03 23:27:58 +00:00
|
|
|
|
-- ensure that sublists have preceding blank line
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ blankline $$ vcat contents $$ blankline
|
2008-07-13 23:16:44 +00:00
|
|
|
|
blockToRST (OrderedList (start, style', delim) items) = do
|
|
|
|
|
let markers = if start == 1 && style' == DefaultStyle && delim == DefaultDelim
|
2007-11-03 23:27:58 +00:00
|
|
|
|
then take (length items) $ repeat "#."
|
|
|
|
|
else take (length items) $ orderedListMarkers
|
2008-07-13 23:16:44 +00:00
|
|
|
|
(start, style', delim)
|
2007-11-03 23:27:58 +00:00
|
|
|
|
let maxMarkerLength = maximum $ map length markers
|
|
|
|
|
let markers' = map (\m -> let s = maxMarkerLength - length m
|
|
|
|
|
in m ++ replicate s ' ') markers
|
2008-01-04 18:58:50 +00:00
|
|
|
|
contents <- mapM (\(item, num) -> orderedListItemToRST item num) $
|
2010-12-12 20:09:14 -08:00
|
|
|
|
zip markers' items
|
2007-11-03 23:27:58 +00:00
|
|
|
|
-- ensure that sublists have preceding blank line
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ blankline $$ vcat contents $$ blankline
|
2008-01-04 18:58:50 +00:00
|
|
|
|
blockToRST (DefinitionList items) = do
|
|
|
|
|
contents <- mapM definitionListItemToRST items
|
2010-12-12 20:09:14 -08:00
|
|
|
|
-- ensure that sublists have preceding blank line
|
|
|
|
|
return $ blankline $$ vcat contents $$ blankline
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert bullet list item (list of blocks) to RST.
|
2008-01-04 18:58:50 +00:00
|
|
|
|
bulletListItemToRST :: [Block] -> State WriterState Doc
|
|
|
|
|
bulletListItemToRST items = do
|
|
|
|
|
contents <- blockListToRST items
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ hang 3 "- " $ contents <> cr
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert ordered list item (a list of blocks) to RST.
|
2008-01-04 18:58:50 +00:00
|
|
|
|
orderedListItemToRST :: String -- ^ marker for list item
|
|
|
|
|
-> [Block] -- ^ list item (list of blocks)
|
|
|
|
|
-> State WriterState Doc
|
|
|
|
|
orderedListItemToRST marker items = do
|
|
|
|
|
contents <- blockListToRST items
|
2010-12-12 20:09:14 -08:00
|
|
|
|
let marker' = marker ++ " "
|
|
|
|
|
return $ hang (length marker') (text marker') $ contents <> cr
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert defintion list item (label, list of blocks) to RST.
|
2009-12-07 08:26:53 +00:00
|
|
|
|
definitionListItemToRST :: ([Inline], [[Block]]) -> State WriterState Doc
|
|
|
|
|
definitionListItemToRST (label, defs) = do
|
2008-07-13 23:16:44 +00:00
|
|
|
|
label' <- inlineListToRST label
|
2009-12-07 08:26:53 +00:00
|
|
|
|
contents <- liftM vcat $ mapM blockListToRST defs
|
2008-01-04 18:58:50 +00:00
|
|
|
|
tabstop <- get >>= (return . writerTabStop . stOptions)
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ label' $$ nest tabstop (contents <> cr)
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert list of Pandoc block elements to RST.
|
2008-01-04 18:58:50 +00:00
|
|
|
|
blockListToRST :: [Block] -- ^ List of block elements
|
|
|
|
|
-> State WriterState Doc
|
|
|
|
|
blockListToRST blocks = mapM blockToRST blocks >>= return . vcat
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert list of Pandoc inline elements to RST.
|
2008-01-04 18:58:50 +00:00
|
|
|
|
inlineListToRST :: [Inline] -> State WriterState Doc
|
|
|
|
|
inlineListToRST lst = mapM inlineToRST lst >>= return . hcat
|
2007-11-03 23:27:58 +00:00
|
|
|
|
|
|
|
|
|
-- | Convert Pandoc inline element to RST.
|
2008-01-04 18:58:50 +00:00
|
|
|
|
inlineToRST :: Inline -> State WriterState Doc
|
|
|
|
|
inlineToRST (Emph lst) = do
|
|
|
|
|
contents <- inlineListToRST lst
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ "*" <> contents <> "*"
|
2008-01-04 18:58:50 +00:00
|
|
|
|
inlineToRST (Strong lst) = do
|
|
|
|
|
contents <- inlineListToRST lst
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ "**" <> contents <> "**"
|
2008-01-04 18:58:50 +00:00
|
|
|
|
inlineToRST (Strikeout lst) = do
|
|
|
|
|
contents <- inlineListToRST lst
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ "[STRIKEOUT:" <> contents <> "]"
|
2008-01-04 18:58:50 +00:00
|
|
|
|
inlineToRST (Superscript lst) = do
|
|
|
|
|
contents <- inlineListToRST lst
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ "\\ :sup:`" <> contents <> "`\\ "
|
2008-01-04 18:58:50 +00:00
|
|
|
|
inlineToRST (Subscript lst) = do
|
|
|
|
|
contents <- inlineListToRST lst
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ "\\ :sub:`" <> contents <> "`\\ "
|
2008-07-15 23:26:06 +00:00
|
|
|
|
inlineToRST (SmallCaps lst) = inlineListToRST lst
|
2008-01-04 18:58:50 +00:00
|
|
|
|
inlineToRST (Quoted SingleQuote lst) = do
|
|
|
|
|
contents <- inlineListToRST lst
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ "‘" <> contents <> "’"
|
2008-01-04 18:58:50 +00:00
|
|
|
|
inlineToRST (Quoted DoubleQuote lst) = do
|
|
|
|
|
contents <- inlineListToRST lst
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ "“" <> contents <> "”"
|
2008-08-04 03:15:12 +00:00
|
|
|
|
inlineToRST (Cite _ lst) =
|
|
|
|
|
inlineListToRST lst
|
2010-06-30 18:53:35 -07:00
|
|
|
|
inlineToRST EmDash = return $ char '\8212'
|
|
|
|
|
inlineToRST EnDash = return $ char '\8211'
|
|
|
|
|
inlineToRST Apostrophe = return $ char '\8217'
|
|
|
|
|
inlineToRST Ellipses = return $ char '\8230'
|
2010-12-12 20:09:14 -08:00
|
|
|
|
inlineToRST (Code str) = return $ "``" <> text str <> "``"
|
2008-01-04 18:58:50 +00:00
|
|
|
|
inlineToRST (Str str) = return $ text $ escapeString str
|
2008-08-13 03:02:42 +00:00
|
|
|
|
inlineToRST (Math t str) = do
|
2009-12-31 01:14:57 +00:00
|
|
|
|
modify $ \st -> st{ stHasMath = True }
|
2008-08-13 03:02:42 +00:00
|
|
|
|
return $ if t == InlineMath
|
2010-12-12 20:09:14 -08:00
|
|
|
|
then ":math:`$" <> text str <> "$`"
|
|
|
|
|
else ":math:`$$" <> text str <> "$$`"
|
2011-01-23 10:55:56 -08:00
|
|
|
|
inlineToRST (RawInline _ _) = return empty
|
2010-12-12 20:09:14 -08:00
|
|
|
|
inlineToRST (LineBreak) = return cr -- there's no line break in RST
|
|
|
|
|
inlineToRST Space = return space
|
2008-07-13 23:16:44 +00:00
|
|
|
|
inlineToRST (Link [Code str] (src, _)) | src == str ||
|
|
|
|
|
src == "mailto:" ++ str = do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
let srcSuffix = if isPrefixOf "mailto:" src then drop 7 src else src
|
2010-03-23 15:35:19 -07:00
|
|
|
|
return $ text $ unescapeURI srcSuffix
|
|
|
|
|
inlineToRST (Link txt (src', tit)) = do
|
|
|
|
|
let src = unescapeURI src'
|
2010-12-12 20:09:14 -08:00
|
|
|
|
useReferenceLinks <- get >>= return . writerReferenceLinks . stOptions
|
2008-01-04 18:58:50 +00:00
|
|
|
|
linktext <- inlineListToRST $ normalizeSpaces txt
|
2007-11-03 23:27:58 +00:00
|
|
|
|
if useReferenceLinks
|
2010-12-12 20:09:14 -08:00
|
|
|
|
then do refs <- get >>= return . stLinks
|
2007-11-03 23:27:58 +00:00
|
|
|
|
let refs' = if (txt, (src, tit)) `elem` refs
|
|
|
|
|
then refs
|
|
|
|
|
else (txt, (src, tit)):refs
|
2008-01-04 18:58:50 +00:00
|
|
|
|
modify $ \st -> st { stLinks = refs' }
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ "`" <> linktext <> "`_"
|
|
|
|
|
else return $ "`" <> linktext <> " <" <> text src <> ">`_"
|
2010-03-23 15:35:19 -07:00
|
|
|
|
inlineToRST (Image alternate (source', tit)) = do
|
|
|
|
|
let source = unescapeURI source'
|
2010-12-12 20:09:14 -08:00
|
|
|
|
pics <- get >>= return . stImages
|
2007-11-03 23:27:58 +00:00
|
|
|
|
let labelsUsed = map fst pics
|
2010-12-12 20:09:14 -08:00
|
|
|
|
let txt = if null alternate || alternate == [Str ""] ||
|
2007-11-03 23:27:58 +00:00
|
|
|
|
alternate `elem` labelsUsed
|
2008-01-04 18:58:50 +00:00
|
|
|
|
then [Str $ "image" ++ show (length pics)]
|
2007-11-03 23:27:58 +00:00
|
|
|
|
else alternate
|
|
|
|
|
let pics' = if (txt, (source, tit)) `elem` pics
|
|
|
|
|
then pics
|
|
|
|
|
else (txt, (source, tit)):pics
|
2008-01-04 18:58:50 +00:00
|
|
|
|
modify $ \st -> st { stImages = pics' }
|
|
|
|
|
label <- inlineListToRST txt
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ "|" <> label <> "|"
|
2008-01-04 18:58:50 +00:00
|
|
|
|
inlineToRST (Note contents) = do
|
2007-11-03 23:27:58 +00:00
|
|
|
|
-- add to notes in state
|
2010-12-12 20:09:14 -08:00
|
|
|
|
notes <- get >>= return . stNotes
|
2008-01-04 18:58:50 +00:00
|
|
|
|
modify $ \st -> st { stNotes = contents:notes }
|
|
|
|
|
let ref = show $ (length notes) + 1
|
2010-12-12 20:09:14 -08:00
|
|
|
|
return $ " [" <> text ref <> "]_"
|