diff options
| author | fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> | 2007-08-08 02:43:15 +0000 |
|---|---|---|
| committer | fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> | 2007-08-08 02:43:15 +0000 |
| commit | e814a3f6d23f640b1aed5b7cb949459d514a3e33 (patch) | |
| tree | 4c9f89c85d5e050f27b4a732c7bad0542b5c9928 /src/Text/Pandoc/Writers | |
| parent | 22a65385571737b6232debac884184d6504222fc (diff) | |
| download | pandoc-e814a3f6d23f640b1aed5b7cb949459d514a3e33.tar.gz | |
Major change in the way ordered lists are handled:
+ The changes are documented in README, under Lists.
+ The OrderedList block element now stores information
about list number style, list number delimiter, and
starting number.
+ The readers parse this information, when possible.
+ The writers use this information to style ordered
lists.
+ Test suites have been changed accordingly.
Motivation: It's often useful to start lists with
numbers other than 1, and to have control over the
style of the list.
Added to Text.Pandoc.Shared:
+ camelCaseToHyphenated
+ toRomanNumeral
+ anyOrderedListMarker
+ orderedListMarker
+ orderedListMarkers
Added to Text.Pandoc.ParserCombinators:
+ charsInBalanced'
+ withHorizDisplacement
+ romanNumeral
RST writer:
+ Force blank line before lists, so that sublists will be handled
correctly.
LaTeX reader:
+ Fixed bug in parsing of footnotes containing multiple paragraphs,
introduced by use of charsInBalanced. Fix: use charsInBalanced'
instead.
LaTeX header:
+ use mathletters option in ucs package, so that basic unicode Greek
letters will work properly.
git-svn-id: https://pandoc.googlecode.com/svn/trunk@834 788f1e2b-df1e-0410-8736-df70ead52e1b
Diffstat (limited to 'src/Text/Pandoc/Writers')
| -rw-r--r-- | src/Text/Pandoc/Writers/ConTeXt.hs | 21 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/Docbook.hs | 17 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/HTML.hs | 78 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/LaTeX.hs | 24 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/Man.hs | 33 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/Markdown.hs | 31 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/RST.hs | 56 | ||||
| -rw-r--r-- | src/Text/Pandoc/Writers/RTF.hs | 16 |
8 files changed, 179 insertions, 97 deletions
diff --git a/src/Text/Pandoc/Writers/ConTeXt.hs b/src/Text/Pandoc/Writers/ConTeXt.hs index d5f0ba1d0..1f93787b0 100644 --- a/src/Text/Pandoc/Writers/ConTeXt.hs +++ b/src/Text/Pandoc/Writers/ConTeXt.hs @@ -122,9 +122,20 @@ blockToConTeXt (RawHtml str) = return "" blockToConTeXt (BulletList lst) = do contents <- mapM listItemToConTeXt lst return $ "\\startltxitem\n" ++ concat contents ++ "\\stopltxitem\n" -blockToConTeXt (OrderedList lst) = do - contents <- mapM listItemToConTeXt lst - return $ "\\startltxenum\n" ++ concat contents ++ "\\stopltxenum\n" +blockToConTeXt (OrderedList attribs lst) = case attribs of + (1, DefaultStyle, DefaultDelim) -> do + contents <- mapM listItemToConTeXt lst + return $ "\\startltxenum\n" ++ concat contents ++ "\\stopltxenum\n" + _ -> do + let markers = take (length lst) $ orderedListMarkers attribs + contents <- zipWithM orderedListItemToConTeXt markers lst + let markerWidth = maximum $ map length markers + let markerWidth' = if markerWidth < 3 + then "" + else "[width=" ++ + show ((markerWidth + 2) `div` 2) ++ "em]" + return $ "\\startitemize" ++ markerWidth' ++ "\n" ++ concat contents ++ + "\\stopitemize\n" blockToConTeXt (DefinitionList lst) = mapM defListItemToConTeXt lst >>= (return . (++ "\n") . concat) blockToConTeXt HorizontalRule = return "\\thinrule\n\n" @@ -163,6 +174,10 @@ listItemToConTeXt list = do contents <- blockListToConTeXt list return $ "\\item " ++ contents +orderedListItemToConTeXt marker list = do + contents <- blockListToConTeXt list + return $ "\\sym{" ++ marker ++ "} " ++ contents + defListItemToConTeXt (term, def) = do term' <- inlineListToConTeXt term def' <- blockListToConTeXt def diff --git a/src/Text/Pandoc/Writers/Docbook.hs b/src/Text/Pandoc/Writers/Docbook.hs index 4824f81da..ecd27ee0c 100644 --- a/src/Text/Pandoc/Writers/Docbook.hs +++ b/src/Text/Pandoc/Writers/Docbook.hs @@ -173,8 +173,21 @@ blockToDocbook opts (CodeBlock str) = text "<screen>\n" <> text (escapeStringForXML str) <> text "\n</screen>" blockToDocbook opts (BulletList lst) = inTagsIndented "itemizedlist" $ listItemsToDocbook opts lst -blockToDocbook opts (OrderedList lst) = - inTagsIndented "orderedlist" $ listItemsToDocbook opts lst +blockToDocbook opts (OrderedList _ []) = empty +blockToDocbook opts (OrderedList (start, numstyle, numdelim) (first:rest)) = + let attribs = case numstyle of + DefaultStyle -> [] + Decimal -> [("numeration", "arabic")] + UpperAlpha -> [("numeration", "upperalpha")] + LowerAlpha -> [("numeration", "loweralpha")] + UpperRoman -> [("numeration", "upperroman")] + LowerRoman -> [("numeration", "lowerroman")] + items = if start == 1 + then listItemsToDocbook opts (first:rest) + else (inTags True "listitem" [("override",show start)] + (blocksToDocbook opts $ map plainToPara first)) $$ + listItemsToDocbook opts rest + in inTags True "orderedlist" attribs items blockToDocbook opts (DefinitionList lst) = inTagsIndented "variablelist" $ deflistItemsToDocbook opts lst blockToDocbook opts (RawHtml str) = text str -- raw XML block diff --git a/src/Text/Pandoc/Writers/HTML.hs b/src/Text/Pandoc/Writers/HTML.hs index 3d46ba1c9..34c59f334 100644 --- a/src/Text/Pandoc/Writers/HTML.hs +++ b/src/Text/Pandoc/Writers/HTML.hs @@ -36,15 +36,21 @@ import Text.Regex ( mkRegex, matchRegex ) import Numeric ( showHex ) import Data.Char ( ord, toLower ) import Data.List ( isPrefixOf, partition, intersperse ) +import qualified Data.Set as S import Control.Monad.State -import Text.XHtml.Strict +import Text.XHtml.Transitional data WriterState = WriterState - { stNotes :: [Html] -- ^ List of notes - , stIds :: [String] -- ^ List of header identifiers - , stHead :: [Html] -- ^ Html to include in header + { stNotes :: [Html] -- ^ List of notes + , stIds :: [String] -- ^ List of header identifiers + , stMath :: Bool -- ^ Math is used in document + , stCSS :: S.Set String -- ^ CSS to include in header } deriving Show +defaultWriterState :: WriterState +defaultWriterState = WriterState {stNotes= [], stIds = [], + stMath = False, stCSS = S.empty} + -- | Convert Pandoc document to Html string. writeHtmlString :: WriterOptions -> Pandoc -> String writeHtmlString opts = @@ -56,8 +62,7 @@ writeHtmlString opts = writeHtml :: WriterOptions -> Pandoc -> Html writeHtml opts (Pandoc (Meta tit authors date) blocks) = let titlePrefix = writerTitlePrefix opts - topTitle = evalState (inlineListToHtml opts tit) - (WriterState {stNotes = [], stIds = [], stHead = []}) + topTitle = evalState (inlineListToHtml opts tit) defaultWriterState topTitle' = if null titlePrefix then topTitle else titlePrefix +++ " - " +++ topTitle @@ -81,8 +86,19 @@ writeHtml opts (Pandoc (Meta tit authors date) blocks) = else noHtml (blocks', newstate) = runState (blockListToHtml opts blocks) - (WriterState {stNotes = [], stIds = ids, stHead = []}) - head = header $ metadata +++ toHtmlFromList (stHead newstate) +++ + (defaultWriterState {stIds = ids}) + cssLines = stCSS newstate + css = if S.null cssLines + then noHtml + else style ! [thetype "text/css"] $ primHtml $ + '\n':(unlines $ S.toList cssLines) + math = if stMath newstate + then case writerASCIIMathMLURL opts of + Just path -> script ! [src path, + thetype "text/javascript"] $ noHtml + Nothing -> primHtml asciiMathMLScript + else noHtml + head = header $ metadata +++ math +++ css +++ primHtml (writerHeader opts) notes = reverse (stNotes newstate) before = primHtml $ writerIncludeBefore opts @@ -100,7 +116,7 @@ tableOfContents opts headers ids = let opts' = opts { writerIgnoreNotes = True } contentsTree = hierarchicalize headers contents = evalState (mapM (elementToListItem opts') contentsTree) - (WriterState {stNotes= [], stIds = ids, stHead = []}) + (defaultWriterState {stIds = ids}) in thediv ! [identifier "toc"] $ unordList contents -- | Converts an Element to a list item for a table of contents, @@ -177,12 +193,12 @@ isPunctuation c = then True else False --- | Add Html to document header. -addToHeader :: Html -> State WriterState () -addToHeader item = do +-- | Add CSS for document header. +addToCSS :: String -> State WriterState () +addToCSS item = do st <- get - let current = stHead st - put $ st {stHead = (item:current)} + let current = stCSS st + put $ st {stCSS = (S.insert item current)} -- | Convert Pandoc inline list to plain text identifier. inlineListToIdentifier :: [Inline] -> String @@ -241,8 +257,9 @@ blockToHtml opts block = case blocks of [BulletList lst] -> blockToHtml (opts {writerIncremental = inc}) (BulletList lst) - [OrderedList lst] -> blockToHtml (opts {writerIncremental = inc}) - (OrderedList lst) + [OrderedList attribs lst] -> + blockToHtml (opts {writerIncremental = inc}) + (OrderedList attribs lst) otherwise -> blockListToHtml opts blocks >>= (return . blockquote) else blockListToHtml opts blocks >>= (return . blockquote) @@ -272,10 +289,23 @@ blockToHtml opts block = then [theclass "incremental"] else [] return $ unordList ! attribs $ contents - (OrderedList lst) -> do contents <- mapM (blockListToHtml opts) lst - let attribs = if writerIncremental opts + (OrderedList (startnum, numstyle, _) lst) -> do + contents <- mapM (blockListToHtml opts) lst + let numstyle' = camelCaseToHyphenated $ show numstyle + let attribs = (if writerIncremental opts then [theclass "incremental"] - else [] + else []) ++ + (if startnum /= 1 + then [start startnum] + else []) ++ + (if numstyle /= DefaultStyle + then [theclass numstyle'] + else []) + if numstyle /= DefaultStyle + then addToCSS $ "ol." ++ numstyle' ++ + " { list-style-type: " ++ + numstyle' ++ "; }" + else return () return $ ordList ! attribs $ contents (DefinitionList lst) -> do contents <- mapM (\(term, def) -> do term' <- inlineListToHtml opts term @@ -342,8 +372,7 @@ inlineToHtml opts inline = (Emph lst) -> inlineListToHtml opts lst >>= (return . emphasize) (Strong lst) -> inlineListToHtml opts lst >>= (return . strong) (Code str) -> return $ thecode << str - (Strikeout lst) -> addToHeader (style ! [thetype "text/css"] $ (stringToHtml - ".strikeout { text-decoration: line-through; }")) >> + (Strikeout lst) -> addToCSS ".strikeout { text-decoration: line-through; }" >> inlineListToHtml opts lst >>= (return . (thespan ! [theclass "strikeout"])) (Superscript lst) -> inlineListToHtml opts lst >>= (return . sup) @@ -357,12 +386,7 @@ inlineToHtml opts inline = do contents <- inlineListToHtml opts lst return $ leftQuote +++ contents +++ rightQuote (TeX str) -> do if writerUseASCIIMathML opts - then addToHeader $ - case writerASCIIMathMLURL opts of - Just path -> script ! [src path, - thetype "text/javascript"] $ - noHtml - Nothing -> primHtml asciiMathMLScript + then modify (\st -> st {stMath = True}) else return () return $ stringToHtml str (HtmlInline str) -> return $ primHtml str diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs index d907e8b88..3d0c66e45 100644 --- a/src/Text/Pandoc/Writers/LaTeX.hs +++ b/src/Text/Pandoc/Writers/LaTeX.hs @@ -36,10 +36,12 @@ import Text.Printf ( printf ) import Data.List ( (\\), isInfixOf ) import qualified Data.Set as S import Control.Monad.State +import Data.Char ( toLower ) data WriterState = WriterState { stIncludes :: S.Set String -- strings to include in header - , stInNote :: Bool } -- @True@ if we're in a note + , stInNote :: Bool -- @True@ if we're in a note + , stOLLevel :: Int } -- level of ordered list nesting -- | Add line to header. addToHeader :: String -> State WriterState () @@ -52,7 +54,7 @@ addToHeader str = do writeLaTeX :: WriterOptions -> Pandoc -> String writeLaTeX options document = evalState (pandocToLaTeX options document) $ - WriterState { stIncludes = S.empty, stInNote = False } + WriterState { stIncludes = S.empty, stInNote = False, stOLLevel = 1 } pandocToLaTeX :: WriterOptions -> Pandoc -> State WriterState String pandocToLaTeX options (Pandoc meta blocks) = do @@ -137,9 +139,23 @@ blockToLaTeX (RawHtml str) = return "" blockToLaTeX (BulletList lst) = do items <- mapM listItemToLaTeX lst return $ "\\begin{itemize}\n" ++ concat items ++ "\\end{itemize}\n" -blockToLaTeX (OrderedList lst) = do +blockToLaTeX (OrderedList (start, numstyle, numdelim) lst) = do + st <- get + let oldlevel = stOLLevel st + put $ st {stOLLevel = oldlevel + 1} items <- mapM listItemToLaTeX lst - return $ "\\begin{enumerate}\n" ++ concat items ++ "\\end{enumerate}\n" + put $ st {stOLLevel = oldlevel} + exemplar <- if numstyle /= DefaultStyle || numdelim /= DefaultDelim + then do addToHeader "\\usepackage{enumerate}" + return $ "[" ++ head (orderedListMarkers (1, numstyle, numdelim)) ++ "]" + else return "" + let resetcounter = if start /= 1 && oldlevel <= 4 + then "\\setcounter{enum" ++ + map toLower (toRomanNumeral oldlevel) ++ + "}{" ++ show (start - 1) ++ "}\n" + else "" + return $ "\\begin{enumerate}" ++ exemplar ++ "\n" ++ + resetcounter ++ concat items ++ "\\end{enumerate}\n" blockToLaTeX (DefinitionList lst) = do items <- mapM defListItemToLaTeX lst return $ "\\begin{description}\n" ++ concat items ++ "\\end{description}\n" diff --git a/src/Text/Pandoc/Writers/Man.hs b/src/Text/Pandoc/Writers/Man.hs index 8c0f6e1b3..3232a454a 100644 --- a/src/Text/Pandoc/Writers/Man.hs +++ b/src/Text/Pandoc/Writers/Man.hs @@ -173,9 +173,11 @@ blockToMan opts (Table caption alignments widths headers rows) = blockToMan opts (BulletList items) = do contents <- mapM (bulletListItemToMan opts) items return (vcat contents) -blockToMan opts (OrderedList items) = do - contents <- mapM (\(item, num) -> orderedListItemToMan opts item num) $ - zip [1..] items +blockToMan opts (OrderedList attribs items) = do + let markers = take (length items) $ orderedListMarkers attribs + let indent = 1 + (maximum $ map length markers) + contents <- mapM (\(num, item) -> orderedListItemToMan opts num indent item) $ + zip markers items return (vcat contents) blockToMan opts (DefinitionList items) = do contents <- mapM (definitionListItemToMan opts) items @@ -201,25 +203,22 @@ bulletListItemToMan opts (first:rest) = do -- | Convert ordered list item (a list of blocks) to man. orderedListItemToMan :: WriterOptions -- ^ options - -> Int -- ^ ordinal number of list item - -> [Block] -- ^ list item (list of blocks) + -> String -- ^ order marker for list item + -> Int -- ^ number of spaces to indent + -> [Block] -- ^ list item (list of blocks) -> State WriterState Doc -orderedListItemToMan _ _ [] = return empty -orderedListItemToMan opts num ((Para first):rest) = - orderedListItemToMan opts num ((Plain first):rest) -orderedListItemToMan opts num ((Plain first):rest) = do - first' <- blockToMan opts (Plain first) +orderedListItemToMan _ _ _ [] = return empty +orderedListItemToMan opts num indent ((Para first):rest) = + orderedListItemToMan opts num indent ((Plain first):rest) +orderedListItemToMan opts num indent (first:rest) = do + first' <- blockToMan opts first rest' <- blockListToMan opts rest - let first'' = text (".IP " ++ show num ++ "." ++ " 4") $$ first' + let num' = printf ("%" ++ show (indent - 1) ++ "s") num + let first'' = text (".IP \"" ++ num' ++ "\" " ++ show indent) $$ first' let rest'' = if null rest then empty else text ".RS 4" $$ rest' $$ text ".RE" - return (first'' $$ rest'') -orderedListItemToMan opts num (first:rest) = do - first' <- blockToMan opts first - rest' <- blockListToMan opts rest - return $ text (".IP " ++ show num ++ "." ++ " 4") $$ first' $$ - rest' $$ text ".RE" + return $ first'' $$ rest'' -- | Convert definition list item (label, list of blocks) to man. definitionListItemToMan :: WriterOptions diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs index c6c3f3156..eb633166d 100644 --- a/src/Text/Pandoc/Writers/Markdown.hs +++ b/src/Text/Pandoc/Writers/Markdown.hs @@ -57,7 +57,7 @@ pandocToMarkdown opts (Pandoc meta blocks) = do after' = if null after then empty else text after metaBlock <- metaToMarkdown opts meta let head = if (writerStandalone opts) - then metaBlock $$ text (writerHeader opts) + then metaBlock $+$ text (writerHeader opts) else empty let headerBlocks = filter isHeaderBlock blocks let toc = if writerTableOfContents opts @@ -68,8 +68,8 @@ pandocToMarkdown opts (Pandoc meta blocks) = do notes' <- notesToMarkdown opts (reverse notes) (_, refs) <- get -- note that the notes may contain refs refs' <- keyTableToMarkdown opts (reverse refs) - return $ head $$ before' $$ toc $$ body $$ text "" $$ - notes' $$ text "" $$ refs' $$ after' + return $ head $+$ before' $+$ toc $+$ body $+$ text "" $+$ + notes' $+$ text "" $+$ refs' $+$ after' -- | Return markdown representation of reference key table. keyTableToMarkdown :: WriterOptions -> KeyTable -> State WriterState Doc @@ -116,7 +116,7 @@ metaToMarkdown opts (Meta title authors date) = do title' <- titleToMarkdown opts title authors' <- authorsToMarkdown authors date' <- dateToMarkdown date - return $ title' $$ authors' $$ date' + return $ title' $+$ authors' $+$ date' titleToMarkdown :: WriterOptions -> [Inline] -> State WriterState Doc titleToMarkdown opts [] = return empty @@ -173,7 +173,7 @@ blockToMarkdown opts (Table caption aligns widths headers rows) = do caption' <- inlineListToMarkdown opts caption let caption'' = if null caption then empty - else text "" $$ (text "Table: " <> caption') + else text "" $+$ (text "Table: " <> caption') headers' <- mapM (blockListToMarkdown opts) headers let widthsInChars = map (floor . (78 *)) widths let alignHeader alignment = case alignment of @@ -199,14 +199,19 @@ blockToMarkdown opts (Table caption aligns widths headers rows) = do then text "" else empty let body = vcat $ intersperse spacer $ map blockToDoc rows' - return $ (nest 2 $ border $$ (blockToDoc head) $$ underline $$ body $$ - border $$ caption'') $$ text "" + return $ (nest 2 $ border $+$ (blockToDoc head) $+$ underline $+$ body $+$ + border $+$ caption'') <> text "\n" blockToMarkdown opts (BulletList items) = do contents <- mapM (bulletListItemToMarkdown opts) items return $ (vcat contents) <> text "\n" -blockToMarkdown opts (OrderedList items) = do +blockToMarkdown opts (OrderedList attribs items) = do + let markers = orderedListMarkers attribs + let markers' = map (\m -> if length m < 3 + then m ++ replicate (3 - length m) ' ' + else m) + markers contents <- mapM (\(item, num) -> orderedListItemToMarkdown opts item num) $ - zip [1..] items + zip markers' items return $ (vcat contents) <> text "\n" blockToMarkdown opts (DefinitionList items) = do contents <- mapM (definitionListItemToMarkdown opts) items @@ -220,14 +225,12 @@ bulletListItemToMarkdown opts items = do -- | Convert ordered list item (a list of blocks) to markdown. orderedListItemToMarkdown :: WriterOptions -- ^ options - -> Int -- ^ ordinal number of list item + -> String -- ^ list item marker -> [Block] -- ^ list item (list of blocks) -> State WriterState Doc -orderedListItemToMarkdown opts num items = do +orderedListItemToMarkdown opts marker items = do contents <- blockListToMarkdown opts items - let spacer = if (num < 10) then " " else "" - return $ hang (text ((show num) ++ "." ++ spacer)) (writerTabStop opts) - contents + return $ hang (text marker) (writerTabStop opts) contents -- | Convert definition list item (label, list of blocks) to markdown. definitionListItemToMarkdown :: WriterOptions diff --git a/src/Text/Pandoc/Writers/RST.hs b/src/Text/Pandoc/Writers/RST.hs index 5c486480c..c39f7bdab 100644 --- a/src/Text/Pandoc/Writers/RST.hs +++ b/src/Text/Pandoc/Writers/RST.hs @@ -57,7 +57,7 @@ pandocToRST opts (Pandoc meta blocks) = do after' = if null after then empty else text after metaBlock <- metaToRST opts meta let head = if (writerStandalone opts) - then metaBlock $$ text (writerHeader opts) + then metaBlock $+$ text (writerHeader opts) else empty body <- blockListToRST opts blocks (notes, _, _) <- get @@ -65,8 +65,8 @@ pandocToRST opts (Pandoc meta blocks) = do (_, refs, pics) <- get -- note that the notes may contain refs refs' <- keyTableToRST opts (reverse refs) pics' <- pictTableToRST opts (reverse pics) - return $ head <> (before' $$ body $$ notes' <> text "\n" $$ refs' $$ - pics' $$ after') + return $ head $+$ before' $+$ body $+$ notes' $+$ text "" $+$ refs' $+$ + pics' $+$ after' -- | Return RST representation of reference key table. keyTableToRST :: WriterOptions -> KeyTable -> State WriterState Doc @@ -131,9 +131,9 @@ metaToRST opts (Meta title authors date) = do authors' <- authorsToRST authors date' <- dateToRST date let toc = if writerTableOfContents opts - then text "" $$ text ".. contents::" + then text "" $+$ text ".. contents::" else empty - return $ title' $$ authors' $$ date' $$ toc $$ text "" + return $ title' $+$ authors' $+$ date' $+$ toc titleToRST :: WriterOptions -> [Inline] -> State WriterState Doc titleToRST opts [] = return empty @@ -141,13 +141,13 @@ titleToRST opts lst = do contents <- inlineListToRST opts lst let titleLength = length $ render contents let border = text (replicate titleLength '=') - return $ border <> char '\n' <> contents <> char '\n' <> border <> text "\n" + return $ border $+$ contents $+$ border <> text "\n" authorsToRST :: [String] -> State WriterState Doc authorsToRST [] = return empty authorsToRST (first:rest) = do rest' <- authorsToRST rest - return $ (text ":Author: " <> text first) $$ rest' + return $ (text ":Author: " <> text first) $+$ rest' dateToRST :: String -> State WriterState Doc dateToRST [] = return empty @@ -161,21 +161,23 @@ blockToRST opts Null = return empty blockToRST opts (Plain inlines) = wrappedRST opts inlines blockToRST opts (Para [TeX str]) = let str' = if (endsWith '\n' str) then (str ++ "\n") else (str ++ "\n\n") in - return $ hang (text "\n.. raw:: latex\n") 3 (vcat $ map text (lines str')) + return $ hang (text "\n.. raw:: latex\n") 3 + (vcat $ map text (lines str')) blockToRST opts (Para inlines) = do contents <- wrappedRST opts inlines return $ contents <> text "\n" blockToRST opts (RawHtml str) = let str' = if (endsWith '\n' str) then (str ++ "\n") else (str ++ "\n\n") in - return $ hang (text "\n.. raw:: html\n") 3 (vcat $ map text (lines str')) + return $ hang (text "\n.. raw:: html\n") 3 + (vcat $ map text (lines str')) blockToRST opts HorizontalRule = return $ text "--------------\n" blockToRST opts (Header level inlines) = do contents <- inlineListToRST opts inlines let headerLength = length $ render contents let headerChar = if (level > 5) then ' ' else "=-~^'" !! (level - 1) let border = text $ replicate headerLength headerChar - return $ contents <> char '\n' <> border <> char '\n' -blockToRST opts (CodeBlock str) = return $ (text "::\n") $$ text "" $$ + return $ contents $+$ border <> text "\n" +blockToRST opts (CodeBlock str) = return $ (text "::\n") $+$ (nest (writerTabStop opts) $ vcat $ map text (lines str)) <> text "\n" blockToRST opts (BlockQuote blocks) = do contents <- blockListToRST opts blocks @@ -184,7 +186,7 @@ blockToRST opts (Table caption aligns widths headers rows) = do caption' <- inlineListToRST opts caption let caption'' = if null caption then empty - else text "" $$ (text "Table: " <> caption') + else text "" $+$ (text "Table: " <> caption') headers' <- mapM (blockListToRST opts) headers let widthsInChars = map (floor . (78 *)) widths let alignHeader alignment = case alignment of @@ -210,15 +212,25 @@ blockToRST opts (Table caption aligns widths headers rows) = do map (\l -> text $ replicate l ch) widthsInChars) <> char ch <> char '+' let body = vcat $ intersperse (border '-') $ map blockToDoc rows' - return $ border '-' $$ blockToDoc head $$ border '=' $$ body $$ + return $ border '-' $+$ blockToDoc head $+$ border '=' $+$ body $+$ border '-' $$ caption'' $$ text "" blockToRST opts (BulletList items) = do contents <- mapM (bulletListItemToRST opts) items - return $ (vcat contents) <> text "\n" -blockToRST opts (OrderedList items) = do + -- ensure that sublists have preceding blank line + return $ text "" $+$ vcat contents <> text "\n" +blockToRST opts (OrderedList (start, style, delim) items) = do + let markers = if start == 1 && style == DefaultStyle && delim == DefaultDelim + then take (length items) $ repeat "#." + else take (length items) $ orderedListMarkers + (start, style, delim) + let maxMarkerLength = maximum $ map length markers + let markers' = map (\m -> let s = maxMarkerLength - length m + in m ++ replicate s ' ') + markers contents <- mapM (\(item, num) -> orderedListItemToRST opts item num) $ - zip [1..] items - return $ (vcat contents) <> text "\n" + zip markers' items + -- ensure that sublists have preceding blank line + return $ text "" $+$ vcat contents <> text "\n" blockToRST opts (DefinitionList items) = do contents <- mapM (definitionListItemToRST opts) items return $ (vcat contents) <> text "\n" @@ -231,14 +243,12 @@ bulletListItemToRST opts items = do -- | Convert ordered list item (a list of blocks) to RST. orderedListItemToRST :: WriterOptions -- ^ options - -> Int -- ^ ordinal number of list item - -> [Block] -- ^ list item (list of blocks) + -> String -- ^ marker for list item + -> [Block] -- ^ list item (list of blocks) -> State WriterState Doc -orderedListItemToRST opts num items = do +orderedListItemToRST opts marker items = do contents <- blockListToRST opts items - let spacer = if (num < 10) then " " else "" - return $ hang (text ((show num) ++ "." ++ spacer)) (writerTabStop opts) - contents + return $ hang (text marker) (writerTabStop opts) contents -- | Convert defintion list item (label, list of blocks) to RST. definitionListItemToRST :: WriterOptions -> ([Inline], [Block]) -> State WriterState Doc diff --git a/src/Text/Pandoc/Writers/RTF.hs b/src/Text/Pandoc/Writers/RTF.hs index b1e401fed..9b3d6662c 100644 --- a/src/Text/Pandoc/Writers/RTF.hs +++ b/src/Text/Pandoc/Writers/RTF.hs @@ -132,11 +132,13 @@ bulletMarker indent = case (indent `mod` 720) of otherwise -> "\\endash " -- | Returns appropriate (list of) ordered list markers for indent level. -orderedMarkers :: Int -> [String] -orderedMarkers indent = - case (indent `mod` 720) of - 0 -> map (\x -> show x ++ ".") [1..] - otherwise -> map (\x -> show x ++ ".") $ cycle ['a'..'z'] +orderedMarkers :: Int -> ListAttributes -> [String] +orderedMarkers indent (start, style, delim) = + if style == DefaultStyle && delim == DefaultDelim + then case (indent `mod` 720) of + 0 -> orderedListMarkers (start, Decimal, Period) + otherwise -> orderedListMarkers (start, LowerAlpha, Period) + else orderedListMarkers (start, style, delim) -- | Returns RTF header. rtfHeader :: String -- ^ header text @@ -177,9 +179,9 @@ blockToRTF _ _ (RawHtml str) = "" blockToRTF indent alignment (BulletList lst) = spaceAtEnd $ concatMap (listItemToRTF alignment indent (bulletMarker indent)) lst -blockToRTF indent alignment (OrderedList lst) = +blockToRTF indent alignment (OrderedList attribs lst) = spaceAtEnd $ concat $ - zipWith (listItemToRTF alignment indent) (orderedMarkers indent) lst + zipWith (listItemToRTF alignment indent) (orderedMarkers indent attribs) lst blockToRTF indent alignment (DefinitionList lst) = spaceAtEnd $ concatMap (definitionListItemToRTF alignment indent) lst |
