From 2b701f9389844e0a4d0a1a77c49e06fc5ae08ef3 Mon Sep 17 00:00:00 2001 From: Jesse Rosenthal Date: Wed, 22 Jun 2016 13:04:25 -0400 Subject: Shared: introduce blocksToInlines function This is a lossy function for converting `[Block] -> [Inline]`. Its main use, at the moment, is for docx comments, which can contain arbitrary blocks (except for footnotes), but which will be converted to spans. This is, at the moment, pretty useless for everything but the basic `Para` and `Plain` comments. It can be improved, but the docx reader should probably emit a warning if the comment contains more than this. --- src/Text/Pandoc/Shared.hs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src') diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs index d6b088338..9153ec7e9 100644 --- a/src/Text/Pandoc/Shared.hs +++ b/src/Text/Pandoc/Shared.hs @@ -89,6 +89,8 @@ module Text.Pandoc.Shared ( warn, mapLeft, hush, + -- * for squashing blocks + blocksToInlines, -- * Safe read safeRead, -- * Temp directory @@ -1020,6 +1022,41 @@ collapseFilePath = Posix.joinPath . reverse . foldl go [] . splitDirectories isSingleton _ = Nothing checkPathSeperator = fmap isPathSeparator . isSingleton +--- +--- Squash blocks into inlines +--- + +blockToInlines :: Block -> [Inline] +blockToInlines (Plain ils) = ils +blockToInlines (Para ils) = ils +blockToInlines (CodeBlock attr str) = [Code attr str] +blockToInlines (RawBlock fmt str) = [RawInline fmt str] +blockToInlines (OrderedList _ blkslst) = + concatMap blocksToInlines blkslst +blockToInlines (BulletList blkslst) = + concatMap blocksToInlines blkslst +blockToInlines (DefinitionList pairslst) = + concatMap f pairslst + where + f (ils, blkslst) = ils ++ + [Str ":", Space] ++ + (concatMap blocksToInlines blkslst) +blockToInlines (Header _ _ ils) = ils +blockToInlines (HorizontalRule) = [] +blockToInlines (Table _ _ _ headers rows) = + intercalate [LineBreak] $ map (concatMap blocksToInlines) tbl + where + tbl = headers : rows +blockToInlines (Div _ blks) = blocksToInlines blks +blockToInlines Null = [] + +blocksToInlinesWithSep :: [Inline] -> [Block] -> [Inline] +blocksToInlinesWithSep sep blks = intercalate sep $ map blockToInlines blks + +blocksToInlines :: [Block] -> [Inline] +blocksToInlines = blocksToInlinesWithSep [Space, Str "ΒΆ", Space] + + -- -- Safe read -- -- cgit v1.2.3