git.iko.soy/typeable/pandoc
on master help sign in
Moved extractSpaces to Shared.hs

Generalised and move the extractSpaces function from `HTML.hs` to `Shared.hs` so that the docx reader can also use it.

commit7807564d4493af1462d61138c63d8ec365abc792 authormpickering <matthewtpickering@gmail.com> dateMon 16 Jun 2014, 20:45 (12 years ago) parent4b6e279
2 files changed+20−13
src/Text/Pandoc/Readers/HTML.hs+4−13 src/Text/Pandoc/Shared.hs+16
src/Text/Pandoc/Readers/HTML.hs+4−13
@@ -50,7 +50,6 @@ import Data.Char ( isDigit )
5050import Control.Monad ( liftM, guard, when, mzero )
5151import Control.Applicative ( (<$>), (<$), (<*) )
5252import Data.Monoid
53−import Data.Sequence (ViewL(..), ViewR(..), viewr, viewl)
5453
5554isSpace :: Char -> Bool
5655isSpace ' ' = True
@@ -369,9 +368,9 @@ pQ = do
369368 then InSingleQuote
370369 else InDoubleQuote
371370 let constructor = case quoteType of
372− SingleQuote -> B.singleQuoted
371+ SingleQuote -> B.singleQuoted
373372 DoubleQuote -> B.doubleQuoted
374− withQuoteContext innerQuoteContext $
373+ withQuoteContext innerQuoteContext $
375374 pInlinesInTags "q" constructor
376375
377376pEmph :: TagParser Inlines
@@ -406,7 +405,7 @@ pLink = try $ do
406405 let url = fromAttrib "href" tag
407406 let title = fromAttrib "title" tag
408407 lab <- trimInlines . mconcat <$> manyTill inline (pCloses "a")
409− return $ B.link (escapeURI url) title lab
408+ return $ B.link (escapeURI url) title lab
410409
411410pImage :: TagParser Inlines
412411pImage = do
@@ -439,15 +438,7 @@ pRawHtmlInline = do
439438
440439pInlinesInTags :: String -> (Inlines -> Inlines)
441440 -> TagParser Inlines
442−pInlinesInTags tagtype f = do
443− contents <- B.unMany <$> pInTags tagtype inline
444− let left = case viewl contents of
445− (Space :< _) -> B.space
446− _ -> mempty
447− let right = case viewr contents of
448− (_ :> Space) -> B.space
449− _ -> mempty
450− return (left <> f (trimInlines . B.Many $ contents) <> right)
441+pInlinesInTags tagtype f = extractSpaces f <$> pInTags tagtype inline
451442
452443pInTags :: (Monoid a) => String -> TagParser a
453444 -> TagParser a
src/Text/Pandoc/Shared.hs+16
@@ -53,6 +53,7 @@ module Text.Pandoc.Shared (
5353 -- * Pandoc block and inline list processing
5454 orderedListMarkers,
5555 normalizeSpaces,
56+ extractSpaces,
5657 normalize,
5758 stringify,
5859 compactify,
@@ -113,6 +114,7 @@ import qualified Data.ByteString as BS
113114import qualified Data.ByteString.Char8 as B8
114115import Text.Pandoc.Compat.Monoid
115116import Data.ByteString.Base64 (decodeLenient)
117+import Data.Sequence (ViewR(..), ViewL(..), viewl, viewr)
116118
117119#ifdef EMBED_DATA_FILES
118120import Text.Pandoc.Data (dataFiles)
@@ -331,6 +333,20 @@ isSpaceOrEmpty Space = True
331333isSpaceOrEmpty (Str "") = True
332334isSpaceOrEmpty _ = False
333335
336+-- | Extract the leading and trailing spaces from inside an inline element
337+-- and place them outside the element.
338+
339+extractSpaces :: (Inlines -> Inlines) -> Inlines -> Inlines
340+extractSpaces f is =
341+ let contents = B.unMany is
342+ left = case viewl contents of
343+ (Space :< _) -> B.space
344+ _ -> mempty
345+ right = case viewr contents of
346+ (_ :> Space) -> B.space
347+ _ -> mempty in
348+ (left <> f (B.trimInlines . B.Many $ contents) <> right)
349+
334350-- | Normalize @Pandoc@ document, consolidating doubled 'Space's,
335351-- combining adjacent 'Str's and 'Emph's, remove 'Null's and
336352-- empty elements, etc.