@@ -1,6 +1,6 @@
1{-# LANGUAGE RelaxedPolyRec #-} -- needed for inlinesBetween on GHC < 7
1{-# LANGUAGE RelaxedPolyRec #-} -- needed for inlinesBetween on GHC < 7
3−Copyright (C) 2012 John MacFarlane <jgm@berkeley.edu>
3+ Copyright (C) 2012 John MacFarlane <jgm@berkeley.edu>
5This program is free software; you can redistribute it and/or modify
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
6it under the terms of the GNU General Public License as published by
@@ -33,8 +33,6 @@ TODO:
33_ support HTML lists
33_ support HTML lists
34_ support list style attributes and start values in ol lists, also
34_ support list style attributes and start values in ol lists, also
35 value attribute on li
35 value attribute on li
36−_ support preformatted text (lines starting with space)
37−_ support preformatted text blocks
38_ support internal links http://www.mediawiki.org/wiki/Help:Links
36_ support internal links http://www.mediawiki.org/wiki/Help:Links
39_ support external links (partially implemented)
37_ support external links (partially implemented)
40_ support images http://www.mediawiki.org/wiki/Help:Images
38_ support images http://www.mediawiki.org/wiki/Help:Images
@@ -50,13 +48,13 @@ import Text.Pandoc.Definition
50import qualified Text.Pandoc.Builder as B
48import qualified Text.Pandoc.Builder as B
51import Text.Pandoc.Builder (Inlines, Blocks, trimInlines, (<>))
49import Text.Pandoc.Builder (Inlines, Blocks, trimInlines, (<>))
52import Text.Pandoc.Options
50import Text.Pandoc.Options
53−import Text.Pandoc.Readers.HTML ( htmlTag, htmlInBalanced,
51+import Text.Pandoc.Readers.HTML ( htmlTag, isInlineTag,
54− isInlineTag, isBlockTag, isTextTag, isCommentTag )
52+ isBlockTag, isCommentTag )
55import Text.Pandoc.XML ( fromEntities )
53import Text.Pandoc.XML ( fromEntities )
56import Text.Pandoc.Parsing
54import Text.Pandoc.Parsing
55+import Text.Pandoc.Generic ( bottomUp )
57import Text.Pandoc.Shared ( stripTrailingNewlines )
56import Text.Pandoc.Shared ( stripTrailingNewlines )
58import Data.Monoid (mconcat, mempty)
57import Data.Monoid (mconcat, mempty)
59−import qualified Data.Foldable as F
60import Control.Applicative ((<$>), (<*), (*>), (<$))
58import Control.Applicative ((<$>), (<*), (*>), (<$))
61import Control.Monad
59import Control.Monad
62import Data.List (intersperse)
60import Data.List (intersperse)
@@ -124,12 +122,13 @@ block = header
124 <|> bulletList
122 <|> bulletList
125 <|> orderedList
123 <|> orderedList
126 <|> definitionList
124 <|> definitionList
125+ <|> mempty <$ try (spaces *> htmlComment)
127 <|> blockquote
127 <|> blockquote
128 <|> codeblock
128 <|> codeblock
129 <|> syntaxhighlight
129 <|> syntaxhighlight
130 <|> haskell
130 <|> haskell
131 <|> mempty <$ skipMany1 blankline
131 <|> mempty <$ skipMany1 blankline
132− <|> mempty <$ try (spaces *> htmlComment)
134 <|> blockHtml
133 <|> blockHtml
@@ -147,6 +146,22 @@ blockHtml = (B.rawBlock "html" . snd <$> htmlTag isBlockTag)
147hrule :: MWParser Blocks
146hrule :: MWParser Blocks
148hrule = B.horizontalRule <$ try (string "----" *> many (char '-') *> newline)
147hrule = B.horizontalRule <$ try (string "----" *> many (char '-') *> newline)
149+preformatted :: MWParser Blocks
152+ let endline' = B.linebreak <$ (try $ newline <* char ' ')
153+ let whitespace' = B.str <$> many1 ('\160' <$ spaceChar)
154+ let spToNbsp ' ' = '\160'
156+ let nowiki' = mconcat . intersperse B.linebreak . map B.str .
157+ lines . fromEntities . map spToNbsp <$> try
158+ (htmlTag (~== TagOpen "nowiki" []) *>
159+ manyTill anyChar (htmlTag (~== TagClose "nowiki")))
160+ let inline' = whitespace' <|> endline' <|> nowiki' <|> inline
161+ let strToCode (Str s) = Code ("",[],[]) s
163+ B.para . bottomUp strToCode . mconcat <$> many1 inline'
150blockquote :: MWParser Blocks
165blockquote :: MWParser Blocks
151blockquote = B.blockQuote <$> blocksInTags "blockquote"
166blockquote = B.blockQuote <$> blocksInTags "blockquote"
@@ -159,8 +174,8 @@ trimCode xs = stripTrailingNewlines xs
160syntaxhighlight :: MWParser Blocks
175syntaxhighlight :: MWParser Blocks
161syntaxhighlight = try $ do
176syntaxhighlight = try $ do
162− (tag@(TagOpen _ attrs), _) <- lookAhead
177+ (TagOpen _ attrs, _) <- lookAhead
163− $ htmlTag (~== TagOpen "syntaxhighlight" [])
178+ $ htmlTag (~== TagOpen "syntaxhighlight" [])
164 let mblang = lookup "lang" attrs
179 let mblang = lookup "lang" attrs
165 let mbstart = lookup "start" attrs
180 let mbstart = lookup "start" attrs
166 let mbline = lookup "line" attrs
181 let mbline = lookup "line" attrs
@@ -220,6 +235,7 @@ listItem c = try $ do
220 if null extras
235 if null extras
221 then listItem' c
236 then listItem' c
223 first <- manyTill anyChar newline
239 first <- manyTill anyChar newline
224 rest <- many (try $ string extras *> manyTill anyChar newline)
240 rest <- many (try $ string extras *> manyTill anyChar newline)
225 contents <- parseFromString (many1 $ listItem' c)
241 contents <- parseFromString (many1 $ listItem' c)
@@ -233,14 +249,15 @@ listItem c = try $ do
233listItem' :: Char -> MWParser Blocks
249listItem' :: Char -> MWParser Blocks
234listItem' c = try $ do
250listItem' c = try $ do
235 listStart c
251 listStart c
236 first <- manyTill anyChar newline
253 first <- manyTill anyChar newline
237 rest <- many (try $ char c *> lookAhead listStartChar *>
254 rest <- many (try $ char c *> lookAhead listStartChar *>
238 manyTill anyChar newline)
255 manyTill anyChar newline)
239 contents <- parseFromString (mconcat <$> many1 block)
256 contents <- parseFromString (mconcat <$> many1 block)
240 $ unlines $ first : rest
257 $ unlines $ first : rest
241 case viewl (B.unMany contents) of
258 case viewl (B.unMany contents) of
242− (Para xs) :< rest -> return $ B.Many $ (Plain xs) <| rest
259+ (Para xs) :< ys -> return $ B.Many $ (Plain xs) <| ys
243− _ -> return contents
260+ _ -> return contents
246-- inline parsers
263-- inline parsers