2016-04-14 01:17:34 +09:00
|
|
|
{- This file is part of Vervis.
|
|
|
|
-
|
|
|
|
- Written in 2016 by fr33domlover <fr33domlover@riseup.net>.
|
|
|
|
-
|
|
|
|
- ♡ Copying is an act of love. Please copy, reuse and share.
|
|
|
|
-
|
|
|
|
- The author(s) have dedicated all copyright and related and neighboring
|
|
|
|
- rights to this software to the public domain worldwide. This software is
|
|
|
|
- distributed without any warranty.
|
|
|
|
-
|
|
|
|
- You should have received a copy of the CC0 Public Domain Dedication along
|
|
|
|
- with this software. If not, see
|
|
|
|
- <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
-}
|
|
|
|
|
|
|
|
-- | Tools for rendering README files in repository tree view.
|
|
|
|
module Vervis.Readme
|
|
|
|
( findReadme
|
|
|
|
, renderReadme
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2016-04-14 09:01:56 +09:00
|
|
|
import Prelude hiding (takeWhile)
|
2016-04-14 01:17:34 +09:00
|
|
|
|
|
|
|
import Data.Byteable (toBytes)
|
|
|
|
import Data.ByteString.Lazy (ByteString)
|
|
|
|
import Data.Git.Storage (Git, getObject_)
|
|
|
|
import Data.Git.Storage.Object (Object (..))
|
|
|
|
import Data.Git.Types (Blob (..), Tree (..))
|
2016-04-14 09:01:56 +09:00
|
|
|
import Data.Text (Text, toCaseFold, takeWhile, unpack)
|
2016-04-14 01:17:34 +09:00
|
|
|
import Data.Text.Encoding (decodeUtf8With)
|
|
|
|
import Data.Text.Encoding.Error (strictDecode)
|
2016-04-14 09:01:56 +09:00
|
|
|
import System.FilePath (isExtSeparator)
|
2016-04-14 01:17:34 +09:00
|
|
|
|
|
|
|
import Vervis.Foundation (Widget)
|
2016-04-18 02:55:23 +09:00
|
|
|
import Vervis.MediaType (chooseMediaType)
|
2016-05-03 06:20:25 +09:00
|
|
|
import Vervis.Render (renderSourceBL)
|
2016-04-18 02:55:23 +09:00
|
|
|
import Text.FilePath.Local (breakExt)
|
2016-04-14 01:17:34 +09:00
|
|
|
|
2016-04-14 09:15:27 +09:00
|
|
|
-- | Check if the given filename should be considered as README file. Assumes
|
|
|
|
-- a flat filename which doesn't contain a directory part.
|
|
|
|
isReadme :: Text -> Bool
|
|
|
|
isReadme file =
|
|
|
|
let basename = takeWhile (not . isExtSeparator) file
|
|
|
|
in toCaseFold "readme" == toCaseFold basename
|
|
|
|
|
2016-04-14 01:17:34 +09:00
|
|
|
-- | Find a README file in a directory. Return the filename and the file
|
|
|
|
-- content.
|
|
|
|
findReadme :: Git -> Tree -> IO (Maybe (Text, ByteString))
|
|
|
|
findReadme git tree = go $ treeGetEnts tree
|
|
|
|
where
|
|
|
|
go [] = return Nothing
|
|
|
|
go ((_perm, name, ref) : es) =
|
|
|
|
let nameT = decodeUtf8With strictDecode $ toBytes name
|
2016-04-14 09:15:27 +09:00
|
|
|
in if isReadme nameT
|
2016-04-14 01:17:34 +09:00
|
|
|
then do
|
|
|
|
obj <- getObject_ git ref True
|
|
|
|
case obj of
|
|
|
|
ObjBlob b -> return $ Just (nameT, blobGetContent b)
|
|
|
|
_ -> go es
|
|
|
|
else go es
|
|
|
|
|
|
|
|
-- | Render README content into a widget for inclusion in a page.
|
2016-04-18 02:55:23 +09:00
|
|
|
renderReadme :: [Text] -> Text -> ByteString -> Widget
|
|
|
|
renderReadme dir name content =
|
|
|
|
let (base, ext) = breakExt name
|
|
|
|
mediaType = chooseMediaType dir base ext () ()
|
2016-05-03 06:20:25 +09:00
|
|
|
in renderSourceBL mediaType content
|