2015-12-01 22:18:13 +03:00
|
|
|
module Split where
|
2015-12-01 19:33:16 +03:00
|
|
|
|
|
|
|
import Diff
|
2015-12-01 22:18:13 +03:00
|
|
|
import Patch
|
|
|
|
import Syntax
|
|
|
|
import Term
|
|
|
|
import Unified
|
2015-12-01 22:35:23 +03:00
|
|
|
import Control.Comonad.Cofree
|
2015-12-01 23:31:02 +03:00
|
|
|
import qualified Data.Map as Map
|
2015-12-01 23:39:45 +03:00
|
|
|
import qualified Data.Set as Set
|
2015-12-01 19:33:16 +03:00
|
|
|
import Rainbow
|
|
|
|
|
2015-12-01 22:35:23 +03:00
|
|
|
type ClassName = String
|
|
|
|
type Element a = Cofree (Syntax a) (Maybe ClassName, String)
|
2015-12-01 22:18:13 +03:00
|
|
|
|
2015-12-01 23:31:02 +03:00
|
|
|
data HTML =
|
|
|
|
Text String
|
2015-12-01 23:39:21 +03:00
|
|
|
| Span (Maybe ClassName) String
|
|
|
|
| Ul (Maybe ClassName) [HTML]
|
2015-12-02 00:18:19 +03:00
|
|
|
| Dl (Maybe ClassName) [HTML]
|
2015-12-02 00:18:08 +03:00
|
|
|
| Dt String
|
2015-12-01 23:31:02 +03:00
|
|
|
deriving (Eq, Show)
|
|
|
|
|
2015-12-01 19:33:16 +03:00
|
|
|
split :: Diff a Info -> String -> String -> IO ByteString
|
2015-12-01 22:18:13 +03:00
|
|
|
split _ _ _ = return mempty
|
|
|
|
|
|
|
|
splitDiff :: Diff a Info -> String -> String -> [(String, String)]
|
|
|
|
splitDiff _ _ _ = []
|
|
|
|
|
2015-12-01 23:39:55 +03:00
|
|
|
splitPatch :: String -> String -> Patch (Term a Info) -> (Maybe HTML, Maybe HTML)
|
2015-12-01 22:35:41 +03:00
|
|
|
splitPatch before after patch = (fmap (splitTerm before) $ Patch.before patch, fmap (splitTerm after) $ Patch.after patch)
|
2015-12-01 22:18:13 +03:00
|
|
|
|
2015-12-01 23:39:55 +03:00
|
|
|
splitTerm :: String -> Term a Info -> HTML
|
2015-12-01 23:56:15 +03:00
|
|
|
splitTerm source term = fst $ cata toElement term where
|
|
|
|
toElement (Info range lineRange categories) (Leaf _) = (Span (classify categories) $ substring range source, range)
|
2015-12-02 00:07:45 +03:00
|
|
|
toElement (Info range lineRange categories) (Indexed i) = makeList i range categories
|
2015-12-02 00:07:53 +03:00
|
|
|
toElement (Info range lineRange categories) (Fixed i) = makeList i range categories
|
2015-12-02 00:07:45 +03:00
|
|
|
|
2015-12-01 23:57:44 +03:00
|
|
|
accumulate (children, previous) (child, range) = (children ++ [ subtext previous $ start range, child ], end range)
|
2015-12-02 00:18:36 +03:00
|
|
|
accumulateFromMap (children, previous) (key, (child, range)) = (children ++ [ subtext previous $ start range, Dt key, child ], end range)
|
2015-12-02 00:07:45 +03:00
|
|
|
|
|
|
|
makeList i range categories = (Ul (classify categories) items, range)
|
|
|
|
where
|
|
|
|
(children, previous) = foldl accumulate ([], start range) i
|
|
|
|
items = children ++ [ subtext previous $ end range ]
|
|
|
|
|
2015-12-01 23:57:37 +03:00
|
|
|
subtext :: Int -> Int -> HTML
|
|
|
|
subtext start end = Text $ substring (Range start end) source
|
2015-12-01 23:39:45 +03:00
|
|
|
|
|
|
|
classify :: Set.Set Category -> Maybe ClassName
|
|
|
|
classify categories = foldr (const . Just) Nothing categories
|