2019-07-27 16:24:13 +02:00
|
|
|
module Parse exposing (parseUrl)
|
|
|
|
|
|
|
|
import Data exposing (Provider(..), Url, pathSeparator)
|
|
|
|
|
|
|
|
|
|
|
|
parseUrl : String -> Maybe Url
|
|
|
|
parseUrl url =
|
|
|
|
stripProtocol url
|
|
|
|
|> splitProvider
|
2019-07-28 17:32:50 +02:00
|
|
|
|> Maybe.andThen splitOffHead
|
|
|
|
|> Maybe.andThen splitOffHead
|
|
|
|
|> Maybe.andThen splitOffHead
|
|
|
|
|> Maybe.andThen splitOffHead
|
2019-07-27 16:24:13 +02:00
|
|
|
|> Maybe.andThen
|
|
|
|
(\( ( ( ( ( prov, user ), repo ), separator ), gitref ), file ) ->
|
|
|
|
if List.isEmpty file || (separator /= pathSeparator prov) then
|
|
|
|
Nothing
|
|
|
|
|
|
|
|
else
|
|
|
|
Just
|
|
|
|
{ prov = prov
|
|
|
|
, user = user
|
|
|
|
, repo = repo
|
|
|
|
, gitref = gitref
|
|
|
|
, file = String.join "/" file
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-07-28 17:32:50 +02:00
|
|
|
splitOffHead : ( a, List b ) -> Maybe ( ( a, b ), List b )
|
|
|
|
splitOffHead ( head, tail ) =
|
2019-07-27 16:24:13 +02:00
|
|
|
splitPart tail
|
|
|
|
|> Maybe.map (\( h, t ) -> ( ( head, h ), t ))
|
|
|
|
|
|
|
|
|
|
|
|
stripProtocol : String -> String
|
|
|
|
stripProtocol url =
|
|
|
|
let
|
|
|
|
index =
|
|
|
|
String.indexes "://" url
|
|
|
|
|> List.head
|
|
|
|
|> Maybe.withDefault -3
|
|
|
|
in
|
|
|
|
String.dropLeft (index + 3) url
|
|
|
|
|
|
|
|
|
|
|
|
parseProvider : String -> Maybe Provider
|
|
|
|
parseProvider prov =
|
|
|
|
case String.toLower prov of
|
|
|
|
"github.com" ->
|
|
|
|
Just GitHub
|
|
|
|
|
|
|
|
"bitbucket.org" ->
|
|
|
|
Just Bitbucket
|
|
|
|
|
2019-07-28 17:32:50 +02:00
|
|
|
"gitlab.com" ->
|
|
|
|
Just GitLab
|
|
|
|
|
2019-07-27 16:24:13 +02:00
|
|
|
_ ->
|
|
|
|
Nothing
|
|
|
|
|
|
|
|
|
|
|
|
splitProvider : String -> Maybe ( Provider, List String )
|
|
|
|
splitProvider url =
|
|
|
|
let
|
|
|
|
split =
|
|
|
|
String.split "/" url
|
|
|
|
|
|
|
|
parts =
|
|
|
|
splitPart split
|
|
|
|
in
|
|
|
|
parts
|
|
|
|
|> Maybe.andThen
|
|
|
|
(\( head, tail ) ->
|
|
|
|
parseProvider head
|
|
|
|
|> Maybe.map (\prov -> ( prov, tail ))
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
splitPart : List a -> Maybe ( a, List a )
|
|
|
|
splitPart parts =
|
|
|
|
let
|
|
|
|
head =
|
|
|
|
List.head parts
|
|
|
|
|
|
|
|
tail =
|
|
|
|
List.tail parts
|
|
|
|
in
|
|
|
|
head
|
|
|
|
|> Maybe.andThen (\h -> Maybe.map (\t -> ( h, t )) tail)
|