{- This file is part of Vervis. - - Written in 2016 by fr33domlover . - - ♡ 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 - . -} -- | 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|
                #{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