Skip to content
Snippets Groups Projects
Select Git revision
  • b843aa0c4fdcc997dc74ff1a6ae633f139550866
  • main default protected
  • renovate/solid_queue-1.x-lockfile
  • renovate/selenium-webdriver-4.x-lockfile
  • renovate/icalendar-2.x-lockfile
  • renovate/debug-1.x-lockfile
  • renovate/turbo-rails-2.x-lockfile
  • renovate/gcr.io-kaniko-project-executor-1.x
  • renovate/ruby
  • eh22 protected
  • update-rubocop
11 results

sessions_controller.rb

Blame
  • Paths.hs 2.03 KiB
    {-# 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 qualified Data.Text             as T
    import           System.FilePath       (splitPath)
    import           System.FilePath.Posix ((</>))
    import           Text.Regex.TDFA
    import           Util                  (PrettyPrint (prettyprint))
    
    -- | a normalised path: a number of "upwards" steps, and
    -- a path without any . or .. in it. Also possibly a
    -- fragment, mostly for map links.
    data RelPath = Path Int Text (Maybe Text)
      deriving (Show, Eq, Ord)
    
    -- | horrible regex parsing for filepaths that is hopefully kinda safe
    parsePath :: Text -> Maybe RelPath
    parsePath text =
      if rest =~ ("^([^/]*[^\\./]/)*[^/]*[^\\./]$" :: Text) :: Bool
      then Just $ Path up path fragment
      else Nothing
      where
        (_, prefix, rest, _) =
          text =~ ("^((\\.|\\.\\.)/)*" :: Text) :: (Text, Text, Text, [Text])
        -- how many steps upwards in the tree?
        up = length . filter (".." ==) . T.splitOn  "/" $ prefix
        parts = T.splitOn "#" rest
        path = head parts
        fragment = if length parts >= 2
          then Just $ T.concat $ tail parts -- TODO!
          else Nothing
    
    instance PrettyPrint RelPath where
      prettyprint (Path up rest frag) = ups <> rest <> fragment
        where ups = T.concat $ replicate up "../"
              fragment = maybe mempty ("#" <>) frag
    
    -- | Normalises a path.
    --
    -- It takes a `prefix`, and will "truncate" the .. operator
    -- at the end of the prefix, i.e. it will never return paths
    -- that lie (naïvely) outside of the prefix.
    normalise :: FilePath -> RelPath ->  FilePath
    normalise prefix (Path 0 path _) = prefix </> T.unpack path
    normalise prefix (Path i path _) =
      concat (take (length dirs - i) dirs) </> T.unpack path
      where dirs = splitPath prefix
    
    normaliseWithFrag :: FilePath -> RelPath -> FilePath
    normaliseWithFrag prefix (Path i path frag) =
      normalise prefix (Path (i+1) path frag) <> T.unpack (maybe mempty ("#" <>) frag)