| 1 |
{- |
| 2 |
- Language/Ding/Parser/Header.hs - parse the Ding header |
| 3 |
- |
| 4 |
- Copyright 2020,2021 Einhard Leichtfuß |
| 5 |
- |
| 6 |
- This file is part of ding2tei-haskell. |
| 7 |
- |
| 8 |
- ding2tei-haskell is free software: you can redistribute it and/or modify |
| 9 |
- it under the terms of the GNU Affero General Public License as published |
| 10 |
- by the Free Software Foundation, either version 3 of the License, or |
| 11 |
- (at your option) any later version. |
| 12 |
- |
| 13 |
- ding2tei-haskell is distributed in the hope that it will be useful, |
| 14 |
- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
- GNU Affero General Public License for more details. |
| 17 |
- |
| 18 |
- You should have received a copy of the GNU Affero General Public License |
| 19 |
- along with ding2tei-haskell. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
-} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
module Language.Ding.Parser.Header (parseHeader) where |
| 26 |
|
| 27 |
import Data.List (stripPrefix) |
| 28 |
|
| 29 |
import Language.Ding.Syntax (Header(..)) |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
parseHeader :: [String] -> Either String Header |
| 36 |
parseHeader (versionL : copyrightL : yearsL : licenseL : urlL : []) = |
| 37 |
maybe (Left "Failed parsing header.") Right $ |
| 38 |
do |
| 39 |
versionInfo <- stripPrefix "# Version :: " versionL |
| 40 |
(version, date) <- case words versionInfo of |
| 41 |
[a, b] -> Just (a, b) |
| 42 |
_ -> Nothing |
| 43 |
|
| 44 |
copyrightHolder <- stripPrefix "# Copyright (c) :: " copyrightL >>= |
| 45 |
stripSuffixChar ',' |
| 46 |
|
| 47 |
years <- stripPrefix "# " yearsL |
| 48 |
|
| 49 |
license <- stripPrefix "# License :: " licenseL |
| 50 |
|
| 51 |
url <- stripPrefix "# URL :: " urlL |
| 52 |
|
| 53 |
return $ Header |
| 54 |
{ headerVersion = version |
| 55 |
, headerVersionDate = date |
| 56 |
, headerCopyrightHolder = copyrightHolder |
| 57 |
, headerCopyrightPeriod = years |
| 58 |
, headerLicense = license |
| 59 |
, headerURL = url |
| 60 |
} |
| 61 |
|
| 62 |
parseHeader _ = Left "Unexpected number of header lines." |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
stripSuffixChar :: Char -> String -> Maybe String |
| 69 |
stripSuffixChar _ [] = Nothing |
| 70 |
stripSuffixChar k [c] = if k == c then Just [] else Nothing |
| 71 |
stripSuffixChar k (c:cs) = fmap (c:) $ stripSuffixChar k cs |
| 72 |
|
| 73 |
|
| 74 |
-- vi: ft=haskell ts=2 sw=2 et |