1
0
Fork 0
mirror of https://code.sup39.dev/repos/Wqawg synced 2025-01-06 06:26:45 +09:00
vervis/src/Vervis
fr33domlover 69229fb80e Enable Darcs repo creation using the executable
Darcs does export most of its module tree, but there's a problem: Darcs
relies on the current directory. It changes the current directory of the
process to the repo, and then proceeds using paths relative to the repo
dir. This is bad for my case here. If some other thread uses a relative
path (e.g. currently any repo path is relative by default) in parallel,
it will fail.

For now, the quick path around this problem is to use the `darcs`
program.
2016-05-04 11:44:06 +00:00
..
Field Add Ticket to persistent model 2016-04-30 20:40:33 +00:00
Form Let user choose VCS and fail to create repo if Darcs is chosen 2016-05-03 00:33:49 +00:00
Handler Enable Darcs repo creation using the executable 2016-05-04 11:44:06 +00:00
Import Build with LTS-5.13 2016-04-19 06:38:52 +00:00
Model Repos specify their VCS (i.e. Git or Darcs) in the DB 2016-05-02 23:51:53 +00:00
Settings Put all modules under a new Vervis module 2016-02-23 08:45:03 +00:00
Application.hs New ticket post form 2016-04-30 22:32:22 +00:00
BinaryBody.hs Binary request body decoder 2016-04-24 18:48:07 +00:00
Content.hs Send raw pack as git-upload-pack-result 2016-04-29 04:32:32 +00:00
Foundation.hs Breadcrumbs widget in default layout 2016-05-02 14:16:51 +00:00
Git.hs Implement git history log in repo page 2016-03-03 08:15:54 +00:00
Import.hs Put all modules under a new Vervis module 2016-02-23 08:45:03 +00:00
MediaType.hs Use Pandoc for document rendering, for now just Markdown 2016-04-17 17:55:23 +00:00
Model.hs Repos specify their VCS (i.e. Git or Darcs) in the DB 2016-05-02 23:51:53 +00:00
Path.hs Repos right under users, not under projects 2016-04-12 17:37:31 +00:00
Readme.hs Render ticket description as Markdown 2016-05-02 21:20:25 +00:00
Render.hs Render ticket description as Markdown 2016-05-02 21:20:25 +00:00
Settings.hs Add settings option to disable registration 2016-04-19 16:03:27 +00:00
Ssh.hs In Darcs pull over SSH, support specifying just repo name 2016-05-04 11:10:23 +00:00
Style.hs Put all modules under a new Vervis module 2016-02-23 08:45:03 +00:00
Widget.hs Breadcrumbs widget in default layout 2016-05-02 14:16:51 +00: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

import Prelude hiding (takeWhile)

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 (..))
import Data.Text (Text, toCaseFold, takeWhile, unpack)
import Data.Text.Encoding (decodeUtf8With)
import Data.Text.Encoding.Error (strictDecode)
import System.FilePath (isExtSeparator)

import Vervis.Foundation (Widget)
import Vervis.MediaType (chooseMediaType)
import Vervis.Render (renderSourceBL)
import Text.FilePath.Local (breakExt)

-- | 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

-- | 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
        in  if isReadme nameT
                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.
renderReadme :: [Text] -> Text -> ByteString -> Widget
renderReadme dir name content =
    let (base, ext) = breakExt name
        mediaType = chooseMediaType dir base ext () ()
    in  renderSourceBL mediaType content