@@ -4,7 +4,8 @@
5 Module : Text.Pandoc.Readers.Docx.Combine
5 Module : Text.Pandoc.Readers.Docx.Combine
6 Copyright : © 2014-2020 Jesse Rosenthal <jrosenthal@jhu.edu>,
6 Copyright : © 2014-2020 Jesse Rosenthal <jrosenthal@jhu.edu>,
7− 2014-2020 John MacFarlane <jgm@berkeley.edu>
7+ 2014-2020 John MacFarlane <jgm@berkeley.edu>,
8+ 2020 Nikolay Yakimov <root@livid.pp.ru>
8 License : GNU GPL, version 2 or above
9 License : GNU GPL, version 2 or above
10 Maintainer : Jesse Rosenthal <jrosenthal@jhu.edu>
11 Maintainer : Jesse Rosenthal <jrosenthal@jhu.edu>
@@ -59,7 +60,9 @@ module Text.Pandoc.Readers.Docx.Combine ( smushInlines
61import Data.List
62import Data.List
62−import Data.Sequence (ViewL (..), ViewR (..), viewl, viewr, (><), (|>))
64+import Data.Sequence ( ViewL (..), ViewR (..), viewl, viewr, spanr, spanl
63import qualified Data.Sequence as Seq (null)
66import qualified Data.Sequence as Seq (null)
64import Text.Pandoc.Builder
67import Text.Pandoc.Builder
@@ -80,14 +83,14 @@ spaceOutInlinesR ms = (stackInlines fs (l <> m'), r)
80spaceOutInlines :: Inlines -> (Inlines, Inlines, Inlines)
83spaceOutInlines :: Inlines -> (Inlines, Inlines, Inlines)
81spaceOutInlines ils =
84spaceOutInlines ils =
82 let (fs, ils') = unstackInlines ils
85 let (fs, ils') = unstackInlines ils
83− contents = unMany ils'
86+ (left, (right, contents')) = second (spanr isSpace) $ spanl isSpace $ unMany ils'
84− left = case viewl contents of
87+ -- NOTE: spanr counterintuitively returns suffix as the FIRST tuple element
85− (Space :< _) -> space
87− right = case viewr contents of
88− (_ :> Space) -> space
90− (left, stackInlines fs $ trimInlines . Many $ contents, right)
88+ in (Many left, stackInlines fs $ Many contents', Many right)
90+isSpace :: Inline -> Bool
92+isSpace SoftBreak = True
92stackInlines :: [Modifier Inlines] -> Inlines -> Inlines
95stackInlines :: [Modifier Inlines] -> Inlines -> Inlines
93stackInlines [] ms = ms
96stackInlines [] ms = ms
@@ -99,39 +102,24 @@ stackInlines (Modifier f : fs) ms =
99stackInlines (AttrModifier f attr : fs) ms = f attr $ stackInlines fs ms
102stackInlines (AttrModifier f attr : fs) ms = f attr $ stackInlines fs ms
101unstackInlines :: Inlines -> ([Modifier Inlines], Inlines)
104unstackInlines :: Inlines -> ([Modifier Inlines], Inlines)
102−unstackInlines ms = case ilModifier ms of
105+unstackInlines ms = case ilModifierAndInnards ms of
103− NullModifier -> ([], ms)
106+ (NullModifier, _) -> ([], ms)
104− _ -> (f : fs, ms') where
106− (fs, ms') = unstackInlines $ ilInnards ms
107+ (f, innards ) -> first (f :) $ unstackInlines innards
108−ilModifier :: Inlines -> Modifier Inlines
109+ilModifierAndInnards :: Inlines -> (Modifier Inlines, Inlines)
109−ilModifier ils = case viewl (unMany ils) of
110+ilModifierAndInnards ils = case viewl $ unMany ils of
110− (x :< xs) | Seq.null xs -> case x of
111+ x :< xs | Seq.null xs -> case x of
111− (Emph _) -> Modifier emph
112+ Emph lst -> (Modifier emph, fromList lst)
112− (Strong _) -> Modifier strong
113+ Strong lst -> (Modifier strong, fromList lst)
113− (SmallCaps _) -> Modifier smallcaps
114+ SmallCaps lst -> (Modifier smallcaps, fromList lst)
114− (Strikeout _) -> Modifier strikeout
115+ Strikeout lst -> (Modifier strikeout, fromList lst)
116+ Underline lst -> (Modifier underline, fromList lst)
115− (Superscript _) -> Modifier superscript
117+ Superscript lst -> (Modifier superscript, fromList lst)
116− (Subscript _) -> Modifier subscript
118+ Subscript lst -> (Modifier subscript, fromList lst)
117− (Link attr _ tgt) -> Modifier $ linkWith attr (fst tgt) (snd tgt)
119+ Link attr lst tgt -> (Modifier $ linkWith attr (fst tgt) (snd tgt), fromList lst)
118− (Span attr _) -> AttrModifier spanWith attr
120+ Span attr lst -> (AttrModifier spanWith attr, fromList lst)
119− _ -> NullModifier
121+ _ -> (NullModifier, ils)
120− _ -> NullModifier
122+ _ -> (NullModifier, ils)
122−ilInnards :: Inlines -> Inlines
123−ilInnards ils = case viewl (unMany ils) of
124− (x :< xs) | Seq.null xs -> case x of
125− (Emph lst) -> fromList lst
126− (Strong lst) -> fromList lst
127− (SmallCaps lst) -> fromList lst
128− (Strikeout lst) -> fromList lst
129− (Superscript lst) -> fromList lst
130− (Subscript lst) -> fromList lst
131− (Link _ lst _) -> fromList lst
132− (Span _ lst) -> fromList lst
136inlinesL :: Inlines -> (Inlines, Inlines)
124inlinesL :: Inlines -> (Inlines, Inlines)
137inlinesL ils = case viewl $ unMany ils of
125inlinesL ils = case viewl $ unMany ils of
@@ -162,7 +150,7 @@ combineSingletonInlines x y =
163 case null shared of
151 case null shared of
164 True | isEmpty xs && isEmpty ys ->
152 True | isEmpty xs && isEmpty ys ->
165− stackInlines (x_rem_attr ++ y_rem_attr) mempty
153+ stackInlines (x_rem_attr <> y_rem_attr) mempty
166 | isEmpty xs ->
154 | isEmpty xs ->
167 let (sp, y') = spaceOutInlinesL y in
155 let (sp, y') = spaceOutInlinesL y in
168 stackInlines x_rem_attr mempty <> sp <> y'
156 stackInlines x_rem_attr mempty <> sp <> y'