@@ -39,7 +39,7 @@ import Text.Pandoc.Writers.Shared
3939import Text.Pandoc.Options
4040import Text.Pandoc.Parsing hiding (blankline, char, space)
4141import Data.List ( group, isPrefixOf, find, intersperse, transpose, sortBy )
42−import Data.Char ( isSpace, isPunctuation )
42+import Data.Char ( isSpace, isPunctuation, toUpper )
4343import Data.Ord ( comparing )
4444import Text.Pandoc.Pretty
4545import Control.Monad.State
@@ -77,27 +77,10 @@ writePlain :: WriterOptions -> Pandoc -> String
7777writePlain opts document =
7878 evalState (pandocToMarkdown opts{
7979 writerExtensions = Set.delete Ext_escaped_line_breaks $
80+ Set.delete Ext_pipe_tables $
81+ Set.delete Ext_raw_html $
8082 writerExtensions opts }
81− document') def{ stPlain = True }
82− where document' = plainify document
83−
84−plainify :: Pandoc -> Pandoc
85−plainify = walk go
86− where go :: Inline -> Inline
87− go (Emph xs) = Span ("",[],[]) xs
88− go (Strong xs) = Span ("",[],[]) xs
89− go (Strikeout xs) = Span ("",[],[]) xs
90− go (Superscript xs) = Span ("",[],[]) xs
91− go (Subscript xs) = Span ("",[],[]) xs
92− go (SmallCaps xs) = Span ("",[],[]) xs
93− go (Span _ xs) = Span ("",[],[]) xs
94− go (Code _ s) = Str s
95− go (Math _ s) = Str s
96− go (RawInline _ _) = Str ""
97− go (Link xs _) = Span ("",[],[]) xs
98− go (Image xs _) = Span ("",[],[]) $ [Str "["] ++ xs ++ [Str "]"]
99− go (Cite _ cits) = Span ("",[],[]) cits
100− go x = x
83+ document) def{ stPlain = True }
10184
10285pandocTitleBlock :: Doc -> [Doc] -> Doc -> Doc
10386pandocTitleBlock tit auths dat =
@@ -309,9 +292,9 @@ blockToMarkdown :: WriterOptions -- ^ Options
309292 -> State WriterState Doc
310293blockToMarkdown _ Null = return empty
311294blockToMarkdown opts (Div attrs ils) = do
312− isPlain <- gets stPlain
295+ plain <- gets stPlain
313296 contents <- blockListToMarkdown opts ils
314− return $ if isPlain || not (isEnabled Ext_markdown_in_html_blocks opts)
297+ return $ if plain || not (isEnabled Ext_markdown_in_html_blocks opts)
315298 then contents <> blankline
316299 else tagWithAttrs "div" attrs <> blankline <>
317300 contents <> blankline <> "</div>" <> blankline
@@ -338,21 +321,22 @@ blockToMarkdown opts (Para inlines) =
338321 (<> blankline) `fmap` blockToMarkdown opts (Plain inlines)
339322blockToMarkdown opts (RawBlock f str)
340323 | f == "html" = do
341− st <- get
342− if stPlain st
343− then return empty
344− else return $ if isEnabled Ext_markdown_attribute opts
324+ plain <- gets stPlain
325+ return $ if plain
326+ then empty
327+ else if isEnabled Ext_markdown_attribute opts
345328 then text (addMarkdownAttribute str) <> text "\n"
346329 else text str <> text "\n"
347330 | f `elem` ["latex", "tex", "markdown"] = do
348− st <- get
349− if stPlain st
350− then return empty
351− else return $ text str <> text "\n"
331+ plain <- gets stPlain
332+ return $ if plain
333+ then empty
334+ else text str <> text "\n"
352335blockToMarkdown _ (RawBlock _ _) = return empty
353−blockToMarkdown _ HorizontalRule =
354− return $ blankline <> text "* * * * *" <> blankline
336+blockToMarkdown opts HorizontalRule = do
337+ return $ blankline <> text (replicate (writerColumns opts) '-') <> blankline
355338blockToMarkdown opts (Header level attr inlines) = do
339+ plain <- gets stPlain
356340 -- we calculate the id that would be used by auto_identifiers
357341 -- so we know whether to print an explicit identifier
358342 ids <- gets stIds
@@ -368,18 +352,19 @@ blockToMarkdown opts (Header level attr inlines) = do
368352 space <> attrsToMarkdown attr
369353 | otherwise -> empty
370354 contents <- inlineListToMarkdown opts inlines
371− st <- get
372355 let setext = writerSetextHeaders opts
373356 return $ nowrap
374357 $ case level of
375− 1 | setext ->
358+ 1 | plain -> blankline <> text "\n\n" <> contents <> blankline <> text "\n"
359+ | setext ->
376360 contents <> attr' <> cr <> text (replicate (offset contents) '=') <>
377361 blankline
378− 2 | setext ->
362+ 2 | plain -> blankline <> text "\n" <> contents <> blankline
363+ | setext ->
379364 contents <> attr' <> cr <> text (replicate (offset contents) '-') <>
380365 blankline
381366 -- ghc interprets '#' characters in column 1 as linenum specifiers.
382− _ | stPlain st || isEnabled Ext_literate_haskell opts ->
367+ _ | plain || isEnabled Ext_literate_haskell opts ->
383368 contents <> blankline
384369 _ -> text (replicate level '#') <> space <> contents <> attr' <> blankline
385370blockToMarkdown opts (CodeBlock (_,classes,_) str)
@@ -409,14 +394,12 @@ blockToMarkdown opts (CodeBlock attribs str) = return $
409394 (_,(cls:_),_) -> " " <> text cls
410395 _ -> empty
411396blockToMarkdown opts (BlockQuote blocks) = do
412− st <- get
397+ plain <- gets stPlain
413398 -- if we're writing literate haskell, put a space before the bird tracks
414399 -- so they won't be interpreted as lhs...
415400 let leader = if isEnabled Ext_literate_haskell opts
416401 then " > "
417− else if stPlain st
418− then " "
419− else "> "
402+ else if plain then " " else "> "
420403 contents <- blockListToMarkdown opts blocks
421404 return $ (prefixed leader contents) <> blankline
422405blockToMarkdown opts t@(Table caption aligns widths headers rows) = do
@@ -680,62 +663,84 @@ escapeSpaces (Str s) = Str $ substitute " " "\\ " s
680663escapeSpaces Space = Str "\\ "
681664escapeSpaces x = x
682665
666+toCaps :: Inline -> Inline
667+toCaps (Str s) = Str (map toUpper s)
668+toCaps x = x
669+
683670-- | Convert Pandoc inline element to markdown.
684671inlineToMarkdown :: WriterOptions -> Inline -> State WriterState Doc
685672inlineToMarkdown opts (Span attrs ils) = do
686− st <- get
673+ plain <- gets stPlain
687674 contents <- inlineListToMarkdown opts ils
688− return $ if stPlain st
675+ return $ if plain
689676 then contents
690677 else tagWithAttrs "span" attrs <> contents <> text "</span>"
691678inlineToMarkdown opts (Emph lst) = do
679+ plain <- gets stPlain
692680 contents <- inlineListToMarkdown opts lst
693− return $ "*" <> contents <> "*"
681+ return $ if plain
682+ then "_" <> contents <> "_"
683+ else "*" <> contents <> "*"
694684inlineToMarkdown opts (Strong lst) = do
695− contents <- inlineListToMarkdown opts lst
696− return $ "**" <> contents <> "**"
685+ plain <- gets stPlain
686+ if plain
687+ then inlineListToMarkdown opts $ walk toCaps lst
688+ else do
689+ contents <- inlineListToMarkdown opts lst
690+ return $ "**" <> contents <> "**"
697691inlineToMarkdown opts (Strikeout lst) = do
698692 contents <- inlineListToMarkdown opts lst
699− return $ if isEnabled Ext_strikeout opts
693+ plain <- gets stPlain
694+ return $ if plain || isEnabled Ext_strikeout opts
700695 then "~~" <> contents <> "~~"
701696 else "<s>" <> contents <> "</s>"
702697inlineToMarkdown opts (Superscript lst) = do
703− let lst' = walk escapeSpaces lst
704− contents <- inlineListToMarkdown opts lst'
705− return $ if isEnabled Ext_superscript opts
706− then "^" <> contents <> "^"
707− else "<sup>" <> contents <> "</sup>"
698+ plain <- gets stPlain
699+ if plain
700+ then inlineListToMarkdown opts lst
701+ else do
702+ contents <- inlineListToMarkdown opts $ walk escapeSpaces lst
703+ return $ if isEnabled Ext_superscript opts
704+ then "^" <> contents <> "^"
705+ else "<sup>" <> contents <> "</sup>"
708706inlineToMarkdown opts (Subscript lst) = do
709− let lst' = walk escapeSpaces lst
710− contents <- inlineListToMarkdown opts lst'
711− return $ if isEnabled Ext_subscript opts
712− then "~" <> contents <> "~"
713− else "<sub>" <> contents <> "</sub>"
707+ plain <- gets stPlain
708+ if plain
709+ then inlineListToMarkdown opts lst
710+ else do
711+ contents <- inlineListToMarkdown opts $ walk escapeSpaces lst
712+ return $ if isEnabled Ext_subscript opts
713+ then "~" <> contents <> "~"
714+ else "<sub>" <> contents <> "</sub>"
714715inlineToMarkdown opts (SmallCaps lst) = do
715− contents <- inlineListToMarkdown opts lst
716− st <- get
717− return $ if stPlain st
718− then contents
719− else tagWithAttrs "span"
720− ("",[],[("style","font-variant:small-caps;")])
721− <> contents <> text "</span>"
716+ plain <- gets stPlain
717+ if plain
718+ then inlineListToMarkdown opts $ walk toCaps lst
719+ else do
720+ contents <- inlineListToMarkdown opts lst
721+ return $ tagWithAttrs "span"
722+ ("",[],[("style","font-variant:small-caps;")])
723+ <> contents <> text "</span>"
722724inlineToMarkdown opts (Quoted SingleQuote lst) = do
723725 contents <- inlineListToMarkdown opts lst
724726 return $ "‘" <> contents <> "’"
725727inlineToMarkdown opts (Quoted DoubleQuote lst) = do
726728 contents <- inlineListToMarkdown opts lst
727729 return $ "“" <> contents <> "”"
728−inlineToMarkdown opts (Code attr str) =
730+inlineToMarkdown opts (Code attr str) = do
729731 let tickGroups = filter (\s -> '`' `elem` s) $ group str
730− longest = if null tickGroups
732+ let longest = if null tickGroups
731733 then 0
732734 else maximum $ map length tickGroups
733− marker = replicate (longest + 1) '`'
734− spacer = if (longest == 0) then "" else " "
735− attrs = if isEnabled Ext_inline_code_attributes opts && attr /= nullAttr
735+ let marker = replicate (longest + 1) '`'
736+ let spacer = if (longest == 0) then "" else " "
737+ let attrs = if isEnabled Ext_inline_code_attributes opts && attr /= nullAttr
736738 then attrsToMarkdown attr
737739 else empty
738− in return $ text (marker ++ spacer ++ str ++ spacer ++ marker) <> attrs
740+ plain <- gets stPlain
741+ if plain
742+ then return $ text str
743+ else return $ text (marker ++ spacer ++ str ++ spacer ++ marker) <> attrs
739744inlineToMarkdown _ (Str str) = do
740745 st <- get
741746 if stPlain st
@@ -758,15 +763,20 @@ inlineToMarkdown opts (Math DisplayMath str)
758763 return $ "\\\\[" <> text str <> "\\\\]"
759764 | otherwise = (\x -> cr <> x <> cr) `fmap`
760765 inlineListToMarkdown opts (texMathToInlines DisplayMath str)
761−inlineToMarkdown opts (RawInline f str)
762− | f == "html" || f == "markdown" ||
763− (isEnabled Ext_raw_tex opts && (f == "latex" || f == "tex")) =
764− return $ text str
765−inlineToMarkdown _ (RawInline _ _) = return empty
766−inlineToMarkdown opts (LineBreak)
767− | isEnabled Ext_hard_line_breaks opts = return cr
768− | isEnabled Ext_escaped_line_breaks opts = return $ "\\" <> cr
769− | otherwise = return $ " " <> cr
766+inlineToMarkdown opts (RawInline f str) = do
767+ plain <- gets stPlain
768+ if not plain && f == "html" || f == "markdown" ||
769+ (isEnabled Ext_raw_tex opts && (f == "latex" || f == "tex"))
770+ then return $ text str
771+ else return empty
772+inlineToMarkdown opts (LineBreak) = do
773+ plain <- gets stPlain
774+ if plain || isEnabled Ext_hard_line_breaks opts
775+ then return cr
776+ else return $
777+ if isEnabled Ext_escaped_line_breaks opts
778+ then "\\" <> cr
779+ else " " <> cr
770780inlineToMarkdown _ Space = return space
771781inlineToMarkdown opts (Cite [] lst) = inlineListToMarkdown opts lst
772782inlineToMarkdown opts (Cite (c:cs) lst)
@@ -799,6 +809,7 @@ inlineToMarkdown opts (Cite (c:cs) lst)
799809 modekey SuppressAuthor = "-"
800810 modekey _ = ""
801811inlineToMarkdown opts (Link txt (src, tit)) = do
812+ plain <- gets stPlain
802813 linktext <- inlineListToMarkdown opts txt
803814 let linktitle = if null tit
804815 then empty
@@ -812,22 +823,29 @@ inlineToMarkdown opts (Link txt (src, tit)) = do
812823 ref <- if useRefLinks then getReference txt (src, tit) else return []
813824 reftext <- inlineListToMarkdown opts ref
814825 return $ if useAuto
815− then "<" <> text srcSuffix <> ">"
826+ then if plain
827+ then text srcSuffix
828+ else "<" <> text srcSuffix <> ">"
816829 else if useRefLinks
817830 then let first = "[" <> linktext <> "]"
818831 second = if txt == ref
819832 then "[]"
820833 else "[" <> reftext <> "]"
821834 in first <> second
822− else "[" <> linktext <> "](" <>
823− text src <> linktitle <> ")"
835+ else if plain
836+ then linktext
837+ else "[" <> linktext <> "](" <>
838+ text src <> linktitle <> ")"
824839inlineToMarkdown opts (Image alternate (source, tit)) = do
840+ plain <- gets stPlain
825841 let txt = if null alternate || alternate == [Str source]
826842 -- to prevent autolinks
827843 then [Str ""]
828844 else alternate
829845 linkPart <- inlineToMarkdown opts (Link txt (source, tit))
830− return $ "!" <> linkPart
846+ return $ if plain
847+ then "[" <> linkPart <> "]"
848+ else "!" <> linkPart
831849inlineToMarkdown opts (Note contents) = do
832850 modify (\st -> st{ stNotes = contents : stNotes st })
833851 st <- get