diff options
author | John MacFarlane <jgm@berkeley.edu> | 2017-12-02 16:26:26 -0800 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2017-12-02 16:28:20 -0800 |
commit | f4b86a1bc2c77d50057399f9d93163c2bbc053bd (patch) | |
tree | 2b4b4be6e44dd0bc159ea419119e295ad45191cd /src/Text | |
parent | 7b8c2b6691e3816ba52ee07ee7f63573d4ae7253 (diff) | |
download | pandoc-f4b86a1bc2c77d50057399f9d93163c2bbc053bd.tar.gz |
Shared.blocksToInlines: rewrote using builder.
This gives us automatic normalization, so we don't get
for example two consecutive Spaces.
Diffstat (limited to 'src/Text')
-rw-r--r-- | src/Text/Pandoc/Shared.hs | 51 |
1 files changed, 27 insertions, 24 deletions
diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs index 1c3a25cc7..975847de4 100644 --- a/src/Text/Pandoc/Shared.hs +++ b/src/Text/Pandoc/Shared.hs @@ -107,7 +107,7 @@ import qualified Data.ByteString.Lazy as BL import Data.Char (isAlpha, isDigit, isLetter, isLower, isSpace, isUpper, toLower) import Data.Data (Data, Typeable) -import Data.List (find, intercalate, stripPrefix) +import Data.List (find, intercalate, intersperse, stripPrefix) import qualified Data.Map as M import Data.Maybe (mapMaybe) import Data.Monoid ((<>)) @@ -717,37 +717,40 @@ isURI = maybe False hasKnownScheme . parseURI --- Squash blocks into inlines --- -blockToInlines :: Block -> [Inline] -blockToInlines (Plain ils) = ils -blockToInlines (Para ils) = ils -blockToInlines (LineBlock lns) = combineLines lns -blockToInlines (CodeBlock attr str) = [Code attr str] -blockToInlines (RawBlock fmt str) = [RawInline fmt str] -blockToInlines (BlockQuote blks) = blocksToInlines blks +blockToInlines :: Block -> Inlines +blockToInlines (Plain ils) = B.fromList ils +blockToInlines (Para ils) = B.fromList ils +blockToInlines (LineBlock lns) = B.fromList $ combineLines lns +blockToInlines (CodeBlock attr str) = B.codeWith attr str +blockToInlines (RawBlock (Format fmt) str) = B.rawInline fmt str +blockToInlines (BlockQuote blks) = blocksToInlines' blks blockToInlines (OrderedList _ blkslst) = - concatMap blocksToInlines blkslst + mconcat $ map blocksToInlines' blkslst blockToInlines (BulletList blkslst) = - concatMap blocksToInlines blkslst + mconcat $ map blocksToInlines' blkslst blockToInlines (DefinitionList pairslst) = - concatMap f pairslst + mconcat $ map f pairslst where - f (ils, blkslst) = ils ++ - [Str ":", Space] ++ - concatMap blocksToInlines blkslst -blockToInlines (Header _ _ ils) = ils -blockToInlines HorizontalRule = [] + f (ils, blkslst) = B.fromList ils <> B.str ":" <> B.space <> + mconcat (map blocksToInlines' blkslst) +blockToInlines (Header _ _ ils) = B.fromList ils +blockToInlines HorizontalRule = mempty blockToInlines (Table _ _ _ headers rows) = - intercalate [LineBreak] $ map (concatMap blocksToInlines) tbl - where - tbl = headers : rows -blockToInlines (Div _ blks) = blocksToInlines blks -blockToInlines Null = [] + mconcat $ intersperse B.linebreak $ + map (mconcat . map blocksToInlines') (headers:rows) +blockToInlines (Div _ blks) = blocksToInlines' blks +blockToInlines Null = mempty + +blocksToInlinesWithSep :: Inlines -> [Block] -> Inlines +blocksToInlinesWithSep sep = + mconcat . intersperse sep . map blockToInlines -blocksToInlinesWithSep :: [Inline] -> [Block] -> [Inline] -blocksToInlinesWithSep sep blks = intercalate sep $ map blockToInlines blks +blocksToInlines' :: [Block] -> Inlines +blocksToInlines' = blocksToInlinesWithSep parSep + where parSep = B.space <> B.str "¶" <> B.space blocksToInlines :: [Block] -> [Inline] -blocksToInlines = blocksToInlinesWithSep [Space, Str "¶", Space] +blocksToInlines = B.toList . blocksToInlines' -- |