aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Readers/Docx/Combine.hs
blob: 54736cd0e7b97c7e2b78d16ef522c3bb39d9f025 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
{-# LANGUAGE FlexibleInstances    #-}
{-# LANGUAGE PatternGuards        #-}
{-# LANGUAGE OverloadedStrings    #-}
{- |
   Module      : Text.Pandoc.Readers.Docx.Combine
   Copyright   : © 2014-2020 Jesse Rosenthal <jrosenthal@jhu.edu>,
                   2014-2020 John MacFarlane <jgm@berkeley.edu>
   License     : GNU GPL, version 2 or above

   Maintainer  : Jesse Rosenthal <jrosenthal@jhu.edu>
   Stability   : alpha
   Portability : portable

Flatten sequences of elements.
-}

{-
The purpose of this module is to combine the formatting of separate
runs, which have *non-nesting* formatting. Because the formatting
doesn't nest, you can't actually tell the nesting order until you
combine with the runs that follow.

For example, say you have a something like `<em><strong>foo</strong>
bar</em>`. Then in ooxml, you'll get these two runs:

~~~
<w:r>
 <w:rPr>
  <w:b />
  <w:i />
 </w:rPr>
 <w:t>Foo</w:t>
</w:r>
<w:r>
 <w:rPr>
  <w:i />
 </w:rPr>
 <w:t> Bar</w:t>
</w:r>
~~~

Note that this is an ideal situation. In practice, it will probably be
more---if, for example, the user turned italics
off and then on.

So, when you get the first run, which is marked as both bold and italic,
you have no idea whether it's `Strong [Emph [Str "Foo"]]` or `Emph
[Strong [Str "Foo"]]`.

We combine two runs, then, by taking off the formatting that modifies an
inline, seeing what is shared between them, and rebuilding an inline. We
fold this to combine the inlines.

-}

module Text.Pandoc.Readers.Docx.Combine ( smushInlines
                                        , smushBlocks
                                        )
       where

import Data.List
import Data.Sequence (ViewL (..), ViewR (..), viewl, viewr, (><), (|>))
import qualified Data.Sequence as Seq (null)
import Text.Pandoc.Builder

data Modifier a = Modifier (a -> a)
                | AttrModifier (Attr -> a -> a) Attr
                | NullModifier

spaceOutInlinesL :: Inlines -> (Inlines, Inlines)
spaceOutInlinesL ms = (l, stackInlines fs (m' <> r))
  where (l, m, r) = spaceOutInlines ms
        (fs, m')  = unstackInlines m

spaceOutInlinesR :: Inlines -> (Inlines, Inlines)
spaceOutInlinesR ms = (stackInlines fs (l <> m'), r)
  where (l, m, r) = spaceOutInlines ms
        (fs, m')  = unstackInlines m

spaceOutInlines :: Inlines -> (Inlines, Inlines, Inlines)
spaceOutInlines ils =
  let (fs, ils') = unstackInlines ils
      contents = unMany ils'
      left  = case viewl contents of
        (Space :< _) -> space
        _            -> mempty
      right = case viewr contents of
        (_ :> Space) -> space
        _            -> mempty in
  (left, stackInlines fs $ trimInlines . Many $ contents, right)

stackInlines :: [Modifier Inlines] -> Inlines -> Inlines
stackInlines [] ms = ms
stackInlines (NullModifier : fs) ms = stackInlines fs ms
stackInlines (Modifier f : fs) ms =
  if isEmpty ms
  then stackInlines fs ms
  else f $ stackInlines fs ms
stackInlines (AttrModifier f attr : fs) ms = f attr $ stackInlines fs ms

unstackInlines :: Inlines -> ([Modifier Inlines], Inlines)
unstackInlines ms = case ilModifier ms of
  NullModifier -> ([], ms)
  _            -> (f : fs, ms') where
    f = ilModifier ms
    (fs, ms') = unstackInlines $ ilInnards ms

ilModifier :: Inlines -> Modifier Inlines
ilModifier ils = case viewl (unMany ils) of
  (x :< xs) | Seq.null xs -> case x of
    (Emph _)          -> Modifier emph
    (Strong _)        -> Modifier strong
    (SmallCaps _)     -> Modifier smallcaps
    (Strikeout _)     -> Modifier strikeout
    (Superscript _)   -> Modifier superscript
    (Subscript _)     -> Modifier subscript
    (Link attr _ tgt) -> Modifier $ linkWith attr (fst tgt) (snd tgt)
    (Span attr _)     -> AttrModifier spanWith attr
    _                 -> NullModifier
  _ -> NullModifier

ilInnards :: Inlines -> Inlines
ilInnards ils = case viewl (unMany ils) of
  (x :< xs) | Seq.null xs -> case x of
    (Emph lst)        -> fromList lst
    (Strong lst)      -> fromList lst
    (SmallCaps lst)   -> fromList lst
    (Strikeout lst)   -> fromList lst
    (Superscript lst) -> fromList lst
    (Subscript lst)   -> fromList lst
    (Link _ lst _)    -> fromList lst
    (Span _ lst)      -> fromList lst
    _                 -> ils
  _          -> ils

inlinesL :: Inlines -> (Inlines, Inlines)
inlinesL ils = case viewl $ unMany ils of
  (s :< sq) -> (singleton s, Many sq)
  _         -> (mempty, ils)

inlinesR :: Inlines -> (Inlines, Inlines)
inlinesR ils = case viewr $ unMany ils of
  (sq :> s) -> (Many sq, singleton s)
  _         -> (ils, mempty)

combineInlines :: Inlines -> Inlines -> Inlines
combineInlines x y =
  let (xs', x') = inlinesR x
      (y', ys') = inlinesL y
  in
   xs' <> combineSingletonInlines x' y' <> ys'

combineSingletonInlines :: Inlines -> Inlines -> Inlines
combineSingletonInlines x y =
  let (xfs, xs) = unstackInlines x
      (yfs, ys) = unstackInlines y
      shared = xfs `intersect` yfs
      x_remaining = xfs \\ shared
      y_remaining = yfs \\ shared
      x_rem_attr = filter isAttrModifier x_remaining
      y_rem_attr = filter isAttrModifier y_remaining
  in
   case null shared of
     True | isEmpty xs && isEmpty ys ->
            stackInlines (x_rem_attr ++ y_rem_attr) mempty
          | isEmpty xs ->
            let (sp, y') = spaceOutInlinesL y in
            stackInlines x_rem_attr mempty <> sp <> y'
          | isEmpty ys ->
            let (x', sp) = spaceOutInlinesR x in
            x' <> sp <> stackInlines y_rem_attr mempty
          | otherwise ->
              let (x', xsp) = spaceOutInlinesR x
                  (ysp, y') = spaceOutInlinesL y
              in
               x' <> xsp <> ysp <> y'
     False -> stackInlines shared $
              combineInlines
              (stackInlines x_remaining xs)
              (stackInlines y_remaining ys)

combineBlocks :: Blocks -> Blocks -> Blocks
combineBlocks bs cs
  | bs' :> BlockQuote bs'' <- viewr (unMany bs)
  , BlockQuote cs'' :< cs' <- viewl (unMany cs) =
      Many $ (bs' |> BlockQuote (bs'' <> cs'')) >< cs'
  | bs' :> CodeBlock attr codeStr <- viewr (unMany bs)
  , CodeBlock attr' codeStr' :< cs' <- viewl (unMany cs)
  , attr == attr' =
      Many $ (bs' |> CodeBlock attr (codeStr <> "\n" <> codeStr')) >< cs'
combineBlocks bs cs = bs <> cs

instance (Monoid a, Eq a) => Eq (Modifier a) where
  (Modifier f) == (Modifier g) = f mempty == g mempty
  (AttrModifier f attr) == (AttrModifier g attr') = f attr mempty == g attr' mempty
  NullModifier == NullModifier = True
  _ == _ = False

isEmpty :: (Monoid a, Eq a) => a -> Bool
isEmpty x = x == mempty

isAttrModifier :: Modifier a -> Bool
isAttrModifier (AttrModifier _ _) = True
isAttrModifier _                  = False

smushInlines :: [Inlines] -> Inlines
smushInlines xs = combineInlines xs' mempty
  where xs' = foldl combineInlines mempty xs

smushBlocks :: [Blocks] -> Blocks
smushBlocks xs = foldl combineBlocks mempty xs