diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-11-19 18:37:05 +0100 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-11-19 18:37:05 +0100 |
commit | ed9055bd9ab4b88554128b7aea293f641bbde17e (patch) | |
tree | 4414e15eb606789eeb4d9e2176ff75301c761b6c | |
parent | Fix backlinks. (diff) | |
download | vimwiki-scripts-ed9055bd9ab4b88554128b7aea293f641bbde17e.tar.gz vimwiki-scripts-ed9055bd9ab4b88554128b7aea293f641bbde17e.tar.xz |
Backlinks now work through paths containing '..'.
-rw-r--r-- | hs/src/Links.hs | 11 | ||||
-rw-r--r-- | hs/src/System/FilePath/Normalize.hs | 17 | ||||
-rw-r--r-- | hs/vimwiki.cabal | 3 |
3 files changed, 27 insertions, 4 deletions
diff --git a/hs/src/Links.hs b/hs/src/Links.hs index c8f0222..f50258f 100644 --- a/hs/src/Links.hs +++ b/hs/src/Links.hs @@ -25,6 +25,7 @@ import System.FilePath , dropExtension , takeDirectory , (</>)) +import System.FilePath.Normalize (normalizeDir) import Text.Pandoc (PandocIO, readVimwiki) import Text.Pandoc.Definition (Pandoc, Inline (Link)) @@ -39,6 +40,7 @@ extractLinks = query extractLink extractLink _ = [] + findLinks :: FilePath -> [FilePath] -> PandocIO (FilePath, [Text]) findLinks wiki_root parts = do let item_path = joinPath parts @@ -48,8 +50,11 @@ findLinks wiki_root parts = do pandoc <- readVimwiki def text let links = extractLinks pandoc - -- TODO item paths with .. in them - let absolute_links = pack . ((takeDirectory $ "/" <> item_path) </>) . unpack <$> links + let absolute_links = pack + . normalizeDir + . ((takeDirectory $ "/" <> item_path) </>) + . unpack + <$> links return ("/" <> dropExtension item_path, absolute_links) @@ -58,6 +63,6 @@ findLinks wiki_root parts = do buildBacklinkSet :: FilePath -> [Text] -> M.Map Text (Set FilePath) -buildBacklinkSet source targets = foldr (M.alter f) M.empty targets +buildBacklinkSet source = foldr (M.alter f) M.empty where f Nothing = Just $ S.singleton source f (Just lst) = Just $ S.insert source lst diff --git a/hs/src/System/FilePath/Normalize.hs b/hs/src/System/FilePath/Normalize.hs new file mode 100644 index 0000000..3a4462a --- /dev/null +++ b/hs/src/System/FilePath/Normalize.hs @@ -0,0 +1,17 @@ +module System.FilePath.Normalize +( normalizeDir +) where + +import System.FilePath + ( splitPath + , joinPath + ) + +-- Expand .. components. May give wrong result if symlinks are +-- included. +normalizeDir :: FilePath -> FilePath +normalizeDir dir = joinPath . reverse . foldl f [] $ splitPath dir + where f :: [FilePath] -> FilePath -> [FilePath] + f [] "../" = [".."] + f (_:xs) "../" = xs + f xs x = x:xs diff --git a/hs/vimwiki.cabal b/hs/vimwiki.cabal index 1d6ebc3..5488f04 100644 --- a/hs/vimwiki.cabal +++ b/hs/vimwiki.cabal @@ -35,7 +35,8 @@ executable Main Vimwiki.Man, System.Home, Util, - Data.Text.Compat + Data.Text.Compat, + System.FilePath.Normalize build-depends: base >= 4.8, blaze-html >= 0.9, |