Skip to content

Commit

Permalink
[#164] Use only '/' as path separator
Browse files Browse the repository at this point in the history
Problem: in markdown links, '/' should always be used as path separator ,
e.g. GitHub renderer is not threating link `[x](a\b.md)` as a link
to file in dir `a`
We use OS-dependent file separators everywhere, so
e.g. `canonizeLocalRef "./a.md"="./a.md`"` on Windows

Solution: replace '\' to '/' while constructing repo tree if needed
and then use only '/', preferring functions from `System.FilePath.Posix`.
We can do this, since 'a/b' and `a\b` are equivalent paths on Windows
  • Loading branch information
Sorokin-Anton committed Oct 18, 2022
1 parent eef5235 commit d096274
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Xrefcheck/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Data.Map qualified as M
import Data.Reflection (Given)
import Data.Text qualified as T
import Fmt (Buildable (..), blockListF, blockListF', nameF, (+|), (|+))
import System.FilePath (isPathSeparator, pathSeparator)
import System.FilePath.Posix (isPathSeparator)
import Time (Second, Time)

import Data.DList (DList)
Expand Down Expand Up @@ -303,7 +303,7 @@ canonizeLocalRef :: Text -> Text
canonizeLocalRef ref =
maybe ref canonizeLocalRef (T.stripPrefix localPrefix ref)
where
localPrefix = toText ['.', pathSeparator]
localPrefix = "./"

-----------------------------------------------------------
-- Visualisation
Expand Down
10 changes: 7 additions & 3 deletions src/Xrefcheck/Scan.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import Data.Reflection (Given)
import Fmt (Buildable (..), nameF, (+|), (|+))
import System.Directory (doesDirectoryExist)
import System.Directory.Tree qualified as Tree
import System.FilePath (dropTrailingPathSeparator, equalFilePath, takeDirectory, takeExtension)
import System.FilePath qualified as OsDependentFilePath
import System.FilePath.Posix
(dropTrailingPathSeparator, equalFilePath, takeDirectory, takeExtension)

import Xrefcheck.Core
import Xrefcheck.Progress
Expand Down Expand Up @@ -118,15 +120,17 @@ scanRepo rw formatsSupport config root = do

_ Tree.:/ repoTree <- liftIO $ Tree.readDirectoryWithL processFile root
let (errs, fileInfos) = gatherScanErrs &&& gatherFileInfos
$ dropSndMaybes . F.toList
$ map (first useLinuxPathSep) . dropSndMaybes . F.toList
$ Tree.zipPaths $ location Tree.:/ repoTree
return . ScanResult errs $ RepoInfo (M.fromList fileInfos)
where
isDirectory = readingSystem . doesDirectoryExist
gatherScanErrs = foldMap (snd . snd)
gatherFileInfos = map (bimap normaliseWithNoTrailing fst)
useLinuxPathSep | OsDependentFilePath.pathSeparator == '/' = id
| otherwise = intercalate "/" . OsDependentFilePath.splitDirectories

processFile file = do
processFile (useLinuxPathSep -> file) = do
let ext = takeExtension file
let mscanner = formatsSupport ext
if isIgnored file
Expand Down
3 changes: 2 additions & 1 deletion src/Xrefcheck/System.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import Data.Coerce (coerce)
import GHC.IO.Unsafe (unsafePerformIO)
import System.Directory (canonicalizePath)
import System.Environment (lookupEnv)
import System.FilePath ((</>))
import System.FilePath.Glob qualified as Glob
import System.FilePath.Posix ((</>))

import Xrefcheck.Util (normaliseWithNoTrailing)

-- | We can quite safely treat surrounding filesystem as frozen,
Expand Down
2 changes: 1 addition & 1 deletion src/Xrefcheck/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Data.Time (UTCTime)
import Data.Time.Clock (nominalDiffTimeToSeconds)
import Data.Time.Clock.POSIX (POSIXTime, utcTimeToPOSIXSeconds)
import Fmt (Builder)
import System.FilePath (dropTrailingPathSeparator, normalise)
import System.FilePath.Posix (dropTrailingPathSeparator, normalise)
import Time (Second, Time (..), sec)

import Xrefcheck.Util.Colorize
Expand Down
2 changes: 1 addition & 1 deletion src/Xrefcheck/Verify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import Network.HTTP.Req
import Network.HTTP.Types.Header (hRetryAfter)
import Network.HTTP.Types.Status (Status, statusCode, statusMessage)
import System.Directory (doesDirectoryExist, doesFileExist)
import System.FilePath (makeRelative, normalise, splitDirectories, takeDirectory, (</>))
import System.FilePath.Posix (makeRelative, normalise, splitDirectories, takeDirectory, (</>))
import Text.ParserCombinators.ReadPrec qualified as ReadPrec (lift)
import Text.Regex.TDFA.Text (Regex, regexec)
import Text.URI (Authority (..), ParseExceptionBs, URI (..), mkURIBs)
Expand Down

0 comments on commit d096274

Please sign in to comment.