1
0
Fork 0
mirror of https://code.sup39.dev/repos/Wqawg synced 2025-01-09 04:16:47 +09:00
vervis/src/Vervis/Render.hs

55 lines
1.8 KiB
Haskell
Raw Normal View History

{- 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 repository file contents and other source files.
module Vervis.Render
( renderSource
)
where
import Prelude
import Control.Monad.Logger (logWarn)
import Data.ByteString.Lazy (ByteString, toStrict)
import Data.Text (pack)
import Data.Text.Encoding.Error (lenientDecode)
import Data.Text.Lazy.Encoding (decodeUtf8With)
import Text.Highlighter (lexerFromFilename, runLexer, Lexer (lName))
import Text.Highlighter.Formatters.Html (format)
import Yesod.Core.Widget (whamlet, toWidget)
import Vervis.Foundation (Widget)
renderSource :: FilePath -> ByteString -> Widget
renderSource name content =
let raw =
[whamlet|
<pre>
<code>#{decodeUtf8With lenientDecode content}
|]
in case lexerFromFilename name of
Nothing -> raw
Just lexer ->
case runLexer lexer $ toStrict content of
Left err -> do
$logWarn $ mconcat
[ "Failed to highlight "
, pack name
, " with lexer "
, pack $ lName lexer
]
raw
Right tokens -> toWidget $ format True tokens