我如何组织本地haskell包文件?

时间:2014-08-02 04:03:58

标签: haskell haddock hoogle

我有很多haskell包,我启用了一些标志,允许它们生成haddock文件。现在这些文档位于/usr/share/doc/{package-name}-{version}/html/

之类的目录下

是否有工具来组织它们?我想要hackage中的all packages by name页面, 这样就可以在一个页面中找到所有这些已安装软件包的本地链接。

如果可以告诉hoogle使用这些文件,那会更好。到目前为止,我的hoogle搜索结果都指向hackage中的相应页面。

1 个答案:

答案 0 :(得分:1)

由于我的问题尚未得到解答,我写了一个快速而肮脏的程序来回答我的第一个问题:

import System.Directory
import System.IO
import System.Environment
import System.Exit
import System.Path
import System.FilePath.Posix

import Control.Applicative
import Control.Monad
import Data.Maybe
import Data.List
import Text.Printf

-- | make markdown table row
makeTableRow :: String -> FilePath -> String
makeTableRow dirName htmlPath = intercalate "|" [ dirName
                                                , link "frames"
                                                , link "index"
                                                , link "doc-index"]
    where
        link s = printf "[%s](%s)" s $ htmlPath </> s ++ ".html"

scanAndMakeTable :: String -> IO [String]
scanAndMakeTable relDocPath = do
    (Just docPath) <- absNormPath' <$> getCurrentDirectory <*> pure relDocPath
    dirs <- getDirectoryContents docPath
    items <- liftM catMaybes
           . mapM (asHaskellPackage docPath)
           . sort $ dirs
    return $ headers1:headers2:map (uncurry makeTableRow) items
    where
        headers1 = "| " ++  intercalate " | " (words "Package Frames Contents Index") ++ " |"
        headers2 = intercalate " --- " $ replicate 5 "|"
        absNormPath' a p = addMissingRoot  <$> absNormPath a p
        -- sometimes the leading '/' is missing in absNormPath results
        addMissingRoot s@('/':_) = s
        addMissingRoot s = '/' : s
        asHaskellPackage :: String -> String -> IO (Maybe (String,FilePath))
        asHaskellPackage docPath dirName = do
            -- a valid haskell package has a "haddock dir"
            -- in which we can at least find a file with ".haddock" as extension name
            b1 <- doesDirectoryExist haddockFileDir
            if b1
               then do
                   b2 <- any ((== ".haddock") . takeExtension)
                             <$> getDirectoryContents haddockFileDir
                   return $ if b2 then Just (dirName,haddockFileDir) else Nothing
               else return Nothing
            where
                -- guess haddock dir
                haddockFileDir = docPath </> dirName </> "html"

main :: IO ()
main = do
    args <- getArgs
    case args of
      [docPath'] -> scanAndMakeTable docPath' >>= putStrLn . unlines
      _ -> help
    where
        help = hPutStrLn stderr "Usage: <program> <path-to-packages>"
            >> exitFailure

通过观察这些haddock目录的结构,我通过测试来识别haddock目录:

  • 如果有一个名为html的子目录。
  • 如果在子目录html中,则会有一个.haddock作为扩展名的文件。

使用runghc <source-file> /usr/share/doc/ >document-nav.md运行程序应生成包含文档链接的markdown文件。然后将它传递给pandoc或其他一些markdown2html转换器,并在浏览器中使用生成的HTML文件来浏览包文档。