diff options
author | stuebinm | 2021-11-20 02:30:20 +0100 |
---|---|---|
committer | stuebinm | 2021-11-20 02:30:20 +0100 |
commit | 2511c52d9452f60c533871ac111ba9473065310c (patch) | |
tree | 9b4fd6a3670841bbfc3f91cc0b2f55b4fc800605 /lib/Paths.hs | |
parent | 321f4d5fa118515dcde522e1ad01ddd65741828b (diff) |
make link adjustments configurable
this allows for creating custom URI "schemas" in the linter's config,
which may be either allowed, prefixed, or translated according to
some (domain-based) substitution.
Diffstat (limited to 'lib/Paths.hs')
-rw-r--r-- | lib/Paths.hs | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/lib/Paths.hs b/lib/Paths.hs index 4082268..b628ee8 100644 --- a/lib/Paths.hs +++ b/lib/Paths.hs @@ -1,10 +1,11 @@ +{-# LANGUAGE MultiWayIf #-} {-# LANGUAGE OverloadedStrings #-} -- | Paths are horrible, so they have their own module now. -- I just hope you are running this on some kind of Unix module Paths where -import Data.Text (Text) +import Data.Text (Text, isPrefixOf) import qualified Data.Text as T import System.FilePath (splitPath) import System.FilePath.Posix ((</>)) @@ -18,21 +19,17 @@ data RelPath = Path Int Text (Maybe Text) deriving (Show, Eq, Ord) -extractDomain :: Text -> Maybe Text -extractDomain url = - let (_,_,_,matches) = url =~ ("^https://([^/]+)/?.*$" :: Text) :: (Text,Text,Text,[Text]) - in case matches of - [domain] -> Just domain - _ -> Nothing - +data PathResult = OkRelPath RelPath | AbsolutePath | NotAPath | UnderscoreMapLink | AtMapLink -- | horrible regex parsing for filepaths that is hopefully kinda safe -parsePath :: Text -> Maybe RelPath +parsePath :: Text -> PathResult parsePath text = - if rest =~ ("^([^/]*[^\\./]/)*[^/]*[^\\./]$" :: Text) :: Bool - then Just $ Path up path fragment - else Nothing + if | rest =~ ("^([^/]*[^\\./]/)*[^/]*[^\\./]$" :: Text) -> OkRelPath (Path up path fragment) + | "/_/" `isPrefixOf` text -> UnderscoreMapLink + | "/@/" `isPrefixOf` text -> AtMapLink + | "/" `isPrefixOf` text -> AbsolutePath + | otherwise -> NotAPath where (_, prefix, rest, _) = text =~ ("^((\\.|\\.\\.)/)*" :: Text) :: (Text, Text, Text, [Text]) |