git.iko.soy/typeable/pandoc
on master help sign in
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.

commit2b701f9389844e0a4d0a1a77c49e06fc5ae08ef3 authorJesse Rosenthal <jrosenthal@jhu.edu> dateWed 22 Jun 2016, 13:04 (10 years ago) parent319a56a
1 file changed+37
src/Text/Pandoc/Shared.hs+37
src/Text/Pandoc/Shared.hs+37
@@ -89,6 +89,8 @@ module Text.Pandoc.Shared (
8989 warn,
9090 mapLeft,
9191 hush,
92+ -- * for squashing blocks
93+ blocksToInlines,
9294 -- * Safe read
9395 safeRead,
9496 -- * Temp directory
@@ -1020,6 +1022,41 @@ collapseFilePath = Posix.joinPath . reverse . foldl go [] . splitDirectories
10201022 isSingleton _ = Nothing
10211023 checkPathSeperator = fmap isPathSeparator . isSingleton
10221024
1025+---
1026+--- Squash blocks into inlines
1027+---
1028+
1029+blockToInlines :: Block -> [Inline]
1030+blockToInlines (Plain ils) = ils
1031+blockToInlines (Para ils) = ils
1032+blockToInlines (CodeBlock attr str) = [Code attr str]
1033+blockToInlines (RawBlock fmt str) = [RawInline fmt str]
1034+blockToInlines (OrderedList _ blkslst) =
1035+ concatMap blocksToInlines blkslst
1036+blockToInlines (BulletList blkslst) =
1037+ concatMap blocksToInlines blkslst
1038+blockToInlines (DefinitionList pairslst) =
1039+ concatMap f pairslst
1040+ where
1041+ f (ils, blkslst) = ils ++
1042+ [Str ":", Space] ++
1043+ (concatMap blocksToInlines blkslst)
1044+blockToInlines (Header _ _ ils) = ils
1045+blockToInlines (HorizontalRule) = []
1046+blockToInlines (Table _ _ _ headers rows) =
1047+ intercalate [LineBreak] $ map (concatMap blocksToInlines) tbl
1048+ where
1049+ tbl = headers : rows
1050+blockToInlines (Div _ blks) = blocksToInlines blks
1051+blockToInlines Null = []
1052+
1053+blocksToInlinesWithSep :: [Inline] -> [Block] -> [Inline]
1054+blocksToInlinesWithSep sep blks = intercalate sep $ map blockToInlines blks
1055+
1056+blocksToInlines :: [Block] -> [Inline]
1057+blocksToInlines = blocksToInlinesWithSep [Space, Str "¶", Space]
1058+
1059+
10231060--
10241061-- Safe read
10251062--