aboutsummaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Readers/Docx/Reducible.hs
blob: 39a93d98827b54fc3c958b96a8a40e9019acd951 (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
{-# LANGUAGE OverloadedStrings, PatternGuards #-}

{-
Copyright (C) 2014 Jesse Rosenthal <jrosenthal@jhu.edu>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-}

{- |
   Module      : Text.Pandoc.Readers.Docx.Reducible
   Copyright   : Copyright (C) 2014 Jesse Rosenthal
   License     : GNU GPL, version 2 or above

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

Typeclass for combining adjacent blocks and inlines correctly.
-}


module Text.Pandoc.Readers.Docx.Reducible ((<++>),
                                           (<+++>),
                                           Reducible,
                                           Container(..),
                                           container,
                                           innards,
                                           reduceList,
                                           reduceListB,
                                           concatR,
                                           rebuild)
       where

import Text.Pandoc.Builder
import Data.List ((\\), intersect)

data Container a = Container ([a] -> a) | NullContainer

instance (Eq a) => Eq (Container a) where
  (Container x) == (Container y) = ((x []) == (y []))
  NullContainer == NullContainer = True
  _ == _ = False

instance (Show a) => Show (Container a) where
  show (Container x) = "Container {" ++
                       (reverse $ drop 3 $ reverse $ show $ x []) ++
                       "}"
  show (NullContainer) = "NullContainer"

class Reducible a where
  (<++>) :: a -> a -> [a]
  container :: a -> Container a
  innards :: a -> [a]
  isSpace :: a -> Bool

(<+++>) :: (Reducible a) => Many a -> Many a -> Many a
mr <+++> ms = fromList $ reduceList $ toList mr ++ toList ms

reduceListB :: (Reducible a) => Many a -> Many a
reduceListB = fromList . reduceList . toList

reduceList' :: (Reducible a) => [a] -> [a] -> [a]
reduceList' acc [] = acc
reduceList' [] (x:xs) = reduceList' [x] xs
reduceList' as (x:xs) = reduceList' (init as ++ (last as <++> x) ) xs

reduceList :: (Reducible a) => [a] -> [a]
reduceList = reduceList' []

concatR :: (Reducible a) => [a] -> [a] -> [a]
concatR [] [] = []
concatR [] ss = ss
concatR rs [] = rs
concatR rs ss = let (x:xs) = reverse rs
                    (y:ys) = ss
                in
                 reverse xs ++ ( x <++> y ) ++ ys

combineReducibles :: (Reducible a, Eq a) => a -> a -> [a]
combineReducibles r s =
  let (conts, rs) = topLevelContainers r
      (conts', ss) = topLevelContainers s
      shared = conts `intersect` conts'
      remaining = conts \\ shared
      remaining' = conts' \\ shared
  in
   case null shared of
     True | (x : xs) <- reverse rs
          , isSpace x -> case xs of
                          [] -> [x, s]
                          _  -> rebuild conts (reverse xs) ++ [x, s]
          | (x : xs) <- ss
          , isSpace x -> case xs of
                          [] -> [r, x]
                          _  -> [r, x] ++ rebuild conts' (xs)
     True  -> [r,s]
     False -> rebuild
              shared $
              reduceList $
              (rebuild remaining rs) ++ (rebuild remaining' ss)

instance Reducible Inline where
  s1@(Span (id1, classes1, kvs1) ils1) <++> s2@(Span (id2, classes2, kvs2) ils2) =
    let classes'  = classes1 `intersect` classes2
        kvs'      = kvs1 `intersect` kvs2
        classes1' = classes1 \\ classes'
        kvs1'     = kvs1 \\ kvs'
        classes2' = classes2 \\ classes'
        kvs2'     = kvs2 \\ kvs'
    in
     case null classes' && null kvs' of
       True -> [s1,s2]
       False -> let attr'  = ("", classes', kvs')
                    attr1' = (id1, classes1', kvs1')
                    attr2' = (id2, classes2', kvs2')
                    s1' = case null classes1' && null kvs1' of
                      True -> ils1
                      False -> [Span attr1' ils1]
                    s2' = case null classes2' && null kvs2' of
                      True -> ils2
                      False -> [Span attr2' ils2]
                in
                 [Span attr' $ reduceList $ s1' ++ s2']
  (Str x) <++> (Str y) = [Str (x++y)]
  il <++> il' = combineReducibles il il'

  container (Emph _) = Container Emph
  container (Strong _) = Container Strong
  container (Strikeout _) = Container Strikeout
  container (Subscript _) = Container Subscript
  container (Superscript _) = Container Superscript
  container (Quoted qt _) = Container $ Quoted qt
  container (Cite cs _) = Container $ Cite cs
  container (Span attr _) = Container $ Span attr
  container _ = NullContainer

  innards (Emph ils) = ils
  innards (Strong ils) = ils
  innards (Strikeout ils) = ils
  innards (Subscript ils) = ils
  innards (Superscript ils) = ils
  innards (Quoted _ ils) = ils
  innards (Cite _ ils) = ils
  innards (Span _ ils) = ils
  innards _ = []

  isSpace Space = True
  isSpace _     = False

instance Reducible Block where
  (Div (ident, classes, kvs) blks) <++> blk | "list-item" `elem` classes =
    [Div (ident, classes, kvs) (reduceList blks), blk]

  blk <++> blk' = combineReducibles blk blk'

  container (BlockQuote _) = Container BlockQuote
  container (Div attr _) = Container $ Div attr
  container _            = NullContainer

  innards (BlockQuote bs) = bs
  innards (Div _ bs) = bs
  innards _          = []

  isSpace _          = False


topLevelContainers' :: (Reducible a) => [a] -> ([Container a], [a])
topLevelContainers' (r : []) = case container r of
  NullContainer -> ([], [r])
  _             ->
    let (conts, inns) = topLevelContainers' (innards r)
    in
    ((container r) : conts, inns)
topLevelContainers' rs = ([], rs)

topLevelContainers :: (Reducible a) => a -> ([Container a], [a])
topLevelContainers il = topLevelContainers' [il]

rebuild :: [Container a] -> [a] -> [a]
rebuild [] xs = xs
rebuild ((Container f) : cs) xs = rebuild cs $ [f xs]
rebuild (NullContainer : cs) xs = rebuild cs $ xs