@@ -34,7 +34,7 @@ Conversion of 'Pandoc' documents to Emacs Org-Mode.
35Org-Mode: <http://orgmode.org>
35Org-Mode: <http://orgmode.org>
37−module Text.Pandoc.Writers.Org ( writeOrg) where
37+module Text.Pandoc.Writers.Org (writeOrg) where
38import Control.Monad.State
38import Control.Monad.State
39import Data.Char (isAlphaNum, toLower)
39import Data.Char (isAlphaNum, toLower)
40import Data.List (intersect, intersperse, isPrefixOf, partition, transpose)
40import Data.List (intersect, intersperse, isPrefixOf, partition, transpose)
@@ -77,9 +77,9 @@ pandocToOrg (Pandoc meta blocks) = do
77 body <- blockListToOrg blocks
77 body <- blockListToOrg blocks
78 notes <- gets (reverse . stNotes) >>= notesToOrg
78 notes <- gets (reverse . stNotes) >>= notesToOrg
79 hasMath <- gets stHasMath
79 hasMath <- gets stHasMath
80− let main = render colwidth $ foldl ($+$) empty $ [body, notes]
80+ let main = render colwidth . foldl ($+$) empty $ [body, notes]
81 let context = defField "body" main
81 let context = defField "body" main
82− $ defField "math" hasMath
82+ . defField "math" hasMath
83 $ metadata
83 $ metadata
84 case writerTemplate opts of
84 case writerTemplate opts of
85 Nothing -> return main
85 Nothing -> return main
@@ -88,8 +88,7 @@ pandocToOrg (Pandoc meta blocks) = do
88-- | Return Org representation of notes.
88-- | Return Org representation of notes.
89notesToOrg :: PandocMonad m => [[Block]] -> Org m Doc
89notesToOrg :: PandocMonad m => [[Block]] -> Org m Doc
90notesToOrg notes =
90notesToOrg notes =
91− mapM (\(num, note) -> noteToOrg num note) (zip [1..] notes) >>=
91+ vsep <$> zipWithM noteToOrg [1..] notes
94-- | Return Org representation of a note.
93-- | Return Org representation of a note.
95noteToOrg :: PandocMonad m => Int -> [Block] -> Org m Doc
94noteToOrg :: PandocMonad m => Int -> [Block] -> Org m Doc
@@ -221,16 +220,16 @@ blockToOrg (Table caption' _ _ headers rows) = do
221 -- FIXME: Org doesn't allow blocks with height more than 1.
220 -- FIXME: Org doesn't allow blocks with height more than 1.
222 let hpipeBlocks blocks = hcat [beg, middle, end]
221 let hpipeBlocks blocks = hcat [beg, middle, end]
223 where h = maximum (1 : map height blocks)
222 where h = maximum (1 : map height blocks)
224− sep' = lblock 3 $ vcat (map text $ replicate h " | ")
223+ sep' = lblock 3 $ vcat (replicate h (text " | "))
225− beg = lblock 2 $ vcat (map text $ replicate h "| ")
224+ beg = lblock 2 $ vcat (replicate h (text "| "))
226− end = lblock 2 $ vcat (map text $ replicate h " |")
225+ end = lblock 2 $ vcat (replicate h (text " |"))
227 middle = hcat $ intersperse sep' blocks
226 middle = hcat $ intersperse sep' blocks
228 let makeRow = hpipeBlocks . zipWith lblock widthsInChars
227 let makeRow = hpipeBlocks . zipWith lblock widthsInChars
229 let head' = makeRow headers'
228 let head' = makeRow headers'
230 rows' <- mapM (\row -> do cols <- mapM blockListToOrg row
229 rows' <- mapM (\row -> do cols <- mapM blockListToOrg row
231 return $ makeRow cols) rows
230 return $ makeRow cols) rows
232 let border ch = char '|' <> char ch <>
231 let border ch = char '|' <> char ch <>
233− (hcat $ intersperse (char ch <> char '+' <> char ch) $
232+ (hcat . intersperse (char ch <> char '+' <> char ch) $
234 map (\l -> text $ replicate l ch) widthsInChars) <>
233 map (\l -> text $ replicate l ch) widthsInChars) <>
235 char ch <> char '|'
234 char ch <> char '|'
236 let body = vcat rows'
235 let body = vcat rows'
@@ -251,8 +250,7 @@ blockToOrg (OrderedList (start, _, delim) items) = do
251 let maxMarkerLength = maximum $ map length markers
250 let maxMarkerLength = maximum $ map length markers
252 let markers' = map (\m -> let s = maxMarkerLength - length m
251 let markers' = map (\m -> let s = maxMarkerLength - length m
253 in m ++ replicate s ' ') markers
252 in m ++ replicate s ' ') markers
254− contents <- mapM (\(item, num) -> orderedListItemToOrg item num) $
253+ contents <- zipWithM orderedListItemToOrg markers' items
256 -- ensure that sublists have preceding blank line
254 -- ensure that sublists have preceding blank line
257 return $ blankline $$ vcat contents $$ blankline
255 return $ blankline $$ vcat contents $$ blankline
258blockToOrg (DefinitionList items) = do
256blockToOrg (DefinitionList items) = do
@@ -279,8 +277,8 @@ definitionListItemToOrg :: PandocMonad m
279 => ([Inline], [[Block]]) -> Org m Doc
277 => ([Inline], [[Block]]) -> Org m Doc
280definitionListItemToOrg (label, defs) = do
278definitionListItemToOrg (label, defs) = do
281 label' <- inlineListToOrg label
279 label' <- inlineListToOrg label
282− contents <- liftM vcat $ mapM blockListToOrg defs
280+ contents <- vcat <$> mapM blockListToOrg defs
283− return $ hang 2 "- " $ label' <> " :: " <> (contents <> cr)
281+ return . hang 2 "- " $ label' <> " :: " <> (contents <> cr)
285-- | Convert list of key/value pairs to Org :PROPERTIES: drawer.
283-- | Convert list of key/value pairs to Org :PROPERTIES: drawer.
286propertiesDrawer :: Attr -> Doc
284propertiesDrawer :: Attr -> Doc
@@ -312,13 +310,13 @@ attrHtml (ident, classes, kvs) =
312blockListToOrg :: PandocMonad m
310blockListToOrg :: PandocMonad m
313 => [Block] -- ^ List of block elements
311 => [Block] -- ^ List of block elements
314 -> Org m Doc
312 -> Org m Doc
315−blockListToOrg blocks = mapM blockToOrg blocks >>= return . vcat
313+blockListToOrg blocks = vcat <$> mapM blockToOrg blocks
317-- | Convert list of Pandoc inline elements to Org.
315-- | Convert list of Pandoc inline elements to Org.
318inlineListToOrg :: PandocMonad m
316inlineListToOrg :: PandocMonad m
319 => [Inline]
317 => [Inline]
320 -> Org m Doc
318 -> Org m Doc
321−inlineListToOrg lst = mapM inlineToOrg lst >>= return . hcat
319+inlineListToOrg lst = hcat <$> mapM inlineToOrg lst
323-- | Convert Pandoc inline element to Org.
321-- | Convert Pandoc inline element to Org.
324inlineToOrg :: PandocMonad m => Inline -> Org m Doc
322inlineToOrg :: PandocMonad m => Inline -> Org m Doc
@@ -350,7 +348,7 @@ inlineToOrg (Quoted DoubleQuote lst) = do
350 return $ "\"" <> contents <> "\""
348 return $ "\"" <> contents <> "\""
351inlineToOrg (Cite _ lst) = inlineListToOrg lst
349inlineToOrg (Cite _ lst) = inlineListToOrg lst
352inlineToOrg (Code _ str) = return $ "=" <> text str <> "="
350inlineToOrg (Code _ str) = return $ "=" <> text str <> "="
353−inlineToOrg (Str str) = return $ text $ escapeString str
351+inlineToOrg (Str str) = return . text $ escapeString str
354inlineToOrg (Math t str) = do
352inlineToOrg (Math t str) = do
355 modify $ \st -> st{ stHasMath = True }
353 modify $ \st -> st{ stHasMath = True }
356 return $ if t == InlineMath
354 return $ if t == InlineMath