1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
----------------------------------------------------------------------
--- Some auxiliary operations to lookup Curry source files.
---
--- @author Michael Hanus
--- @version December 2018
----------------------------------------------------------------------

module CurryDoc.Files ( generateModuleDocMapping )
 where

import Directory    ( doesDirectoryExist, getDirectoryContents )
import FilePath     ( (</>), takeExtension )

import System.CurryPath ( stripCurrySuffix )

--- Constructs a mapping from module names into locations where
--- the documentation is stored. The argument is a list of pairs
--- (root dir of Curry sources / documentation dir for these sources).
generateModuleDocMapping :: [(String,String)] -> IO [(String,String)]
generateModuleDocMapping pkglocs =
  mapIO genPkgMapping pkglocs >>= return . concat
 where
  genPkgMapping (srcroot,docroot) = do
    mods <- curryModulesInDir srcroot
    return $ map (\m -> (m,docroot)) mods

--- Gets the names of all Curry modules contained in a directory.
--- Modules in subdirectories are returned as hierarchical modules.
curryModulesInDir :: String -> IO [String]
curryModulesInDir dir = getModules "" dir
 where
  getModules p d = do
    exdir <- doesDirectoryExist d
    entries <- if exdir then getDirectoryContents d else return []
    let realentries = filter (\f -> length f >= 1 && head f /= '.') entries
        newprogs    = filter (\f -> takeExtension f == ".curry") realentries
    subdirs <- mapIO (\e -> doesDirectoryExist (d </> e) >>=
                            \b -> return $ if b then [e] else []) realentries
               >>= return . concat
    subdirentries <- mapIO (\s -> getModules (p ++ s ++ ".") (d </> s)) subdirs
    return $ map ((p ++) . stripCurrySuffix) newprogs ++ concat subdirentries