1 -- This file is part of Diohsc
2 -- Copyright (C) 2020 Martin Bays <mbays@sdf.org>
4 -- This program is free software: you can redistribute it and/or modify
5 -- it under the terms of version 3 of the GNU General Public License as
6 -- published by the Free Software Foundation, or any later version.
8 -- You should have received a copy of the GNU General Public License
9 -- along with this program. If not, see http://www.gnu.org/licenses/.
11 {-# LANGUAGE OverloadedStrings #-}
13 {-# LANGUAGE TupleSections #-}
17 import Control
.Exception
(handle
)
18 import Control
.Monad
(guard, msum)
19 import Data
.Bifunctor
(second
)
20 import Data
.Char (isAlphaNum)
21 import Data
.Either (partitionEithers
)
22 import Data
.List
(isPrefixOf)
23 import System
.Directory
24 import System
.FilePath
26 import qualified Data
.ByteString
as BS
27 import qualified Data
.Map
as Map
28 import qualified Data
.Text
as TS
29 import qualified Data
.Text
.Encoding
as TS
35 data URIWithIdName
= URIWithIdName
{ uriIdUri
:: URI
, uriIdId
:: Maybe String }
38 showUriWithId
:: URIWithIdName
-> String
39 showUriWithId
(URIWithIdName uri Nothing
) = show uri
40 showUriWithId
(URIWithIdName uri
(Just idName
)) = show uri
++ "[" ++ idName
++ "]"
42 readUriWithId
:: TS
.Text
-> Maybe URIWithIdName
43 readUriWithId s
= msum [ do
44 s
' <- TS
.stripSuffix
"]" s
45 let (u
,i
) = TS
.breakOn
"[" s
'
47 guard . not $ TS
.null idName
48 uri
<- parseUriAsAbsolute
. escapeIRI
$ TS
.unpack u
49 return . URIWithIdName uri
. Just
$ TS
.unpack idName
50 , (`URIWithIdName` Nothing
) <$> (parseUriAsAbsolute
. escapeIRI
$ TS
.unpack s
) ]
52 type Marks
= Map
.Map
String URIWithIdName
55 emptyMarks
= Map
.empty
57 lookupMark
:: String -> Marks
-> Maybe URIWithIdName
58 lookupMark s marks
= do
59 (s
',uriId
) <- Map
.lookupGE s marks
60 guard $ s `
isPrefixOf` s
'
63 insertMark
:: String -> URIWithIdName
-> Marks
-> Marks
64 insertMark
= Map
.insert
66 loadMarks
:: FilePath -> IO ([String], Marks
)
68 second Map
.fromList
. partitionEithers
<$> (mapM loadMark
=<< ignoreIOErr
(listDirectory path
))
70 loadMark
:: FilePath -> IO (Either String (String, URIWithIdName
))
72 let filepath
= path
</> filename
73 onIOErr
:: IOError -> IO (Either String a
)
74 onIOErr e
= return . Left
$
75 "Error loading mark from " ++ path
++ ": " ++ show e
77 in handle onIOErr
$ maybe (Left
$
78 "Failed to decode uri in:" ++ show filepath
)
79 (Right
. (filename
,)) . readUriWithId
. TS
.strip
. TS
.decodeUtf8
<$> BS
.readFile filepath
81 markNameValid
:: String -> Bool
82 markNameValid
= all isAlphaNum
84 saveMark
:: FilePath -> String -> URIWithIdName
-> IO ()
85 saveMark path mark uriId | markNameValid mark
=
86 let filepath
= path
</> mark
87 in isSubPath path filepath
>>? mkdirhierto filepath
>> writeFile filepath
(showUriWithId uriId
)
88 saveMark _ _ _
= pure
()
90 marksWithUri
:: URI
-> Marks
-> [(String,URIWithIdName
)]
91 marksWithUri uri
= Map
.toList
. Map
.filter ((==uri
) . uriIdUri
)
94 tempMarks
= (:[]) <$> ['0'..'9']