1
0
Fork 0
mirror of https://code.sup39.dev/repos/Wqawg synced 2024-12-29 01:34:52 +09:00

Syntax-highlight the pretty JSON display of AP objects

This commit is contained in:
fr33domlover 2019-06-09 14:32:57 +00:00
parent 090c562553
commit 71ab1c4459
16 changed files with 48 additions and 40 deletions

View file

@ -16,7 +16,7 @@
-- | File content types and tools for detecting them. The focus is on content -- | File content types and tools for detecting them. The focus is on content
-- that gets special treatment in Vervis, and not general MIME type modeling or -- that gets special treatment in Vervis, and not general MIME type modeling or
-- detection (although that could be done in the future). -- detection (although that could be done in the future).
module Vervis.MediaType module Data.MediaType
( MediaType (..) ( MediaType (..)
, FileName , FileName
, FileBaseName , FileBaseName

View file

@ -61,7 +61,7 @@ import Vervis.Federation
import Vervis.Foundation import Vervis.Foundation
import Vervis.Model import Vervis.Model
import Vervis.Model.Ident import Vervis.Model.Ident
import Vervis.Render import Yesod.RenderSource
import Vervis.Settings import Vervis.Settings
import Vervis.Widget.Discussion import Vervis.Widget.Discussion

View file

@ -115,7 +115,7 @@ import Vervis.Model
import Vervis.Model.Ident import Vervis.Model.Ident
import Vervis.Paginate import Vervis.Paginate
import Vervis.RemoteActorStore import Vervis.RemoteActorStore
import Vervis.Render import Yesod.RenderSource
import Vervis.Settings import Vervis.Settings
getInboxR :: Handler Html getInboxR :: Handler Html

View file

@ -86,13 +86,13 @@ import Vervis.Foundation
import Vervis.Handler.Repo.Darcs import Vervis.Handler.Repo.Darcs
import Vervis.Handler.Repo.Git import Vervis.Handler.Repo.Git
import Vervis.Path import Vervis.Path
import Vervis.MediaType (chooseMediaType) import Data.MediaType
import Vervis.Model import Vervis.Model
import Vervis.Model.Ident import Vervis.Model.Ident
import Vervis.Model.Repo import Vervis.Model.Repo
import Vervis.Paginate import Vervis.Paginate
import Vervis.Readme import Vervis.Readme
import Vervis.Render import Yesod.RenderSource
import Vervis.Settings import Vervis.Settings
import Vervis.SourceTree import Vervis.SourceTree
import Vervis.Style import Vervis.Style

View file

@ -54,14 +54,14 @@ import Vervis.ChangeFeed (changeFeed)
import Vervis.Form.Repo import Vervis.Form.Repo
import Vervis.Foundation import Vervis.Foundation
import Vervis.Path import Vervis.Path
import Vervis.MediaType (chooseMediaType) import Data.MediaType
import Vervis.Model import Vervis.Model
import Vervis.Model.Ident import Vervis.Model.Ident
import Vervis.Model.Repo import Vervis.Model.Repo
import Vervis.Paginate import Vervis.Paginate
import Vervis.Patch import Vervis.Patch
import Vervis.Readme import Vervis.Readme
import Vervis.Render import Yesod.RenderSource
import Vervis.Settings import Vervis.Settings
import Vervis.SourceTree import Vervis.SourceTree
import Vervis.Style import Vervis.Style

View file

@ -64,14 +64,14 @@ import Vervis.ChangeFeed (changeFeed)
import Vervis.Form.Repo import Vervis.Form.Repo
import Vervis.Foundation import Vervis.Foundation
import Vervis.Path import Vervis.Path
import Vervis.MediaType (chooseMediaType) import Data.MediaType
import Vervis.Model import Vervis.Model
import Vervis.Model.Ident import Vervis.Model.Ident
import Vervis.Model.Repo import Vervis.Model.Repo
import Vervis.Paginate import Vervis.Paginate
import Vervis.Patch import Vervis.Patch
import Vervis.Readme import Vervis.Readme
import Vervis.Render import Yesod.RenderSource
import Vervis.Settings import Vervis.Settings
import Vervis.SourceTree import Vervis.SourceTree
import Vervis.Style import Vervis.Style

View file

@ -105,12 +105,12 @@ import Vervis.Form.Ticket
import Vervis.Foundation import Vervis.Foundation
import Vervis.Handler.Discussion import Vervis.Handler.Discussion
import Vervis.GraphProxy (ticketDepGraph) import Vervis.GraphProxy (ticketDepGraph)
import Vervis.MediaType (MediaType (Markdown)) import Data.MediaType
import Vervis.Model import Vervis.Model
import Vervis.Model.Ident import Vervis.Model.Ident
import Vervis.Model.Ticket import Vervis.Model.Ticket
import Vervis.Model.Workflow import Vervis.Model.Workflow
import Vervis.Render import Yesod.RenderSource
import Vervis.Settings import Vervis.Settings
import Vervis.Style import Vervis.Style
import Vervis.Ticket import Vervis.Ticket

View file

@ -33,12 +33,12 @@ import Yesod.Persist.Core (runDB, getBy404)
import Text.FilePath.Local (breakExt) import Text.FilePath.Local (breakExt)
import Vervis.Darcs import Vervis.Darcs
import Vervis.Foundation import Vervis.Foundation
import Vervis.MediaType import Data.MediaType
import Vervis.Model import Vervis.Model
import Vervis.Model.Ident import Vervis.Model.Ident
import Vervis.Model.Repo import Vervis.Model.Repo
import Vervis.Path (askRepoDir) import Vervis.Path (askRepoDir)
import Vervis.Render (renderSourceBL) import Yesod.RenderSource
import Vervis.Settings (widgetFile) import Vervis.Settings (widgetFile)
import Vervis.Wiki import Vervis.Wiki

View file

@ -66,7 +66,7 @@ import Database.Persist.Local
import Vervis.Model.Ident import Vervis.Model.Ident
import Vervis.Foundation (Route (..)) import Vervis.Foundation (Route (..))
import Vervis.Migration.Model import Vervis.Migration.Model
import Vervis.Render import Yesod.RenderSource
instance PersistDefault ByteString where instance PersistDefault ByteString where
pdef = def pdef = def

View file

@ -33,8 +33,8 @@ import System.FilePath (isExtSeparator)
import Data.Git.Local (TreeRows) import Data.Git.Local (TreeRows)
import Text.FilePath.Local (breakExt) import Text.FilePath.Local (breakExt)
import Vervis.Foundation (Widget) import Vervis.Foundation (Widget)
import Vervis.MediaType (chooseMediaType) import Data.MediaType
import Vervis.Render (renderSourceBL) import Yesod.RenderSource
-- | Check if the given filename should be considered as README file. Assumes -- | Check if the given filename should be considered as README file. Assumes
-- a flat filename which doesn't contain a directory part. -- a flat filename which doesn't contain a directory part.

View file

@ -34,9 +34,9 @@ import qualified Data.ByteString.Lazy as BL (ByteString)
import Text.FilePath.Local (breakExt) import Text.FilePath.Local (breakExt)
import Vervis.Foundation (Widget) import Vervis.Foundation (Widget)
import Vervis.MediaType (chooseMediaType) import Data.MediaType
import Vervis.Readme (renderReadme) import Vervis.Readme (renderReadme)
import Vervis.Render (renderSourceBL) import Yesod.RenderSource
data EntryType = TypeBlob | TypeTree data EntryType = TypeBlob | TypeTree

View file

@ -40,10 +40,10 @@ import Data.Time.Clock.Local ()
import Vervis.Discussion import Vervis.Discussion
import Vervis.Foundation import Vervis.Foundation
import Vervis.MediaType (MediaType (Markdown)) import Data.MediaType
import Vervis.Model import Vervis.Model
import Vervis.Model.Ident import Vervis.Model.Ident
import Vervis.Render (renderSourceT) import Yesod.RenderSource
import Vervis.Settings (widgetFile) import Vervis.Settings (widgetFile)
import Vervis.Widget.Sharer import Vervis.Widget.Sharer

View file

@ -26,9 +26,9 @@ import qualified Data.ByteString.Lazy as BL (ByteString)
import Text.FilePath.Local (breakExt) import Text.FilePath.Local (breakExt)
import Vervis.Foundation (Widget) import Vervis.Foundation (Widget)
import Vervis.MediaType (chooseMediaType) import Data.MediaType
import Vervis.Readme (renderReadme) import Vervis.Readme (renderReadme)
import Vervis.Render (renderSourceBL) import Yesod.RenderSource
data WikiView data WikiView
= WikiViewPage (Maybe Text) BL.ByteString = WikiViewPage (Maybe Text) BL.ByteString

View file

@ -39,10 +39,10 @@ import qualified Data.Text as T
import Network.HTTP.Signature import Network.HTTP.Signature
import Data.Aeson.Encode.Pretty.ToEncoding
import Network.FedURI import Network.FedURI
import Web.ActivityPub import Web.ActivityPub
import Yesod.MonadSite import Yesod.MonadSite
import Yesod.RenderSource
class Yesod site => YesodActivityPub site where class Yesod site => YesodActivityPub site where
siteInstanceHost :: site -> Text siteInstanceHost :: site -> Text
@ -134,10 +134,7 @@ provideHtmlAndAP' host object widget = selectRep $ do
mval <- lookupGetParam "prettyjson" mval <- lookupGetParam "prettyjson"
defaultLayout $ defaultLayout $
case mval of case mval of
Just "true" -> Just "true" -> renderPrettyJSON doc
[whamlet|
<div><pre>#{encodePrettyToLazyText doc}
|]
_ -> do _ -> do
widget widget
mroute <- getCurrentRoute mroute <- getCurrentRoute

View file

@ -36,10 +36,11 @@
-- (3) Not implemented -- (3) Not implemented
-- (4) Not implemented -- (4) Not implemented
-- (5) Not implmented -- (5) Not implmented
module Vervis.Render module Yesod.RenderSource
( renderSourceT ( renderSourceT
, renderSourceBL , renderSourceBL
, renderPandocMarkdown , renderPandocMarkdown
, renderPrettyJSON
) )
where where
@ -48,6 +49,7 @@ import Prelude
import Control.Exception import Control.Exception
import Control.Monad.Catch (throwM) import Control.Monad.Catch (throwM)
import Control.Monad.Logger (logDebug, logWarn) import Control.Monad.Logger (logDebug, logWarn)
import Data.Aeson
import Data.Foldable (for_) import Data.Foldable (for_)
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
import Data.Monoid ((<>)) import Data.Monoid ((<>))
@ -64,7 +66,7 @@ import Text.Pandoc.Highlighting
import Text.Pandoc.Options import Text.Pandoc.Options
import Text.Pandoc.Readers.Markdown import Text.Pandoc.Readers.Markdown
import Text.Pandoc.Writers.HTML import Text.Pandoc.Writers.HTML
import Yesod.Core.Widget (whamlet, toWidget) import Yesod.Core.Widget
import qualified Data.ByteString as B import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Lazy as BL
@ -74,9 +76,10 @@ import qualified Data.Text.Encoding.Error as TE
import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TLE import qualified Data.Text.Lazy.Encoding as TLE
import qualified Text.Highlighter.Lexers.Haskell as L.Haskell import qualified Text.Highlighter.Lexers.Haskell as L.Haskell
import qualified Text.Highlighter.Lexers.Javascript as L.JS
import Vervis.Foundation (Widget) import Data.Aeson.Encode.Pretty.ToEncoding
import Vervis.MediaType (MediaType (..)) import Data.MediaType
-- * File uploads and wiki attachments -- * File uploads and wiki attachments
-- * Wiki pages -- * Wiki pages
@ -116,20 +119,20 @@ import Vervis.MediaType (MediaType (..))
-- * [ ] asciidoc -- * [ ] asciidoc
-- * [ ] groff manpage -- * [ ] groff manpage
renderPlain :: TL.Text -> Widget renderPlain :: TL.Text -> WidgetFor site ()
renderPlain content = renderPlain content =
[whamlet| [whamlet|
<pre> <pre>
<code>#{content} <code>#{content}
|] |]
renderHighlight :: Lexer -> B.ByteString -> Maybe Widget renderHighlight :: Lexer -> B.ByteString -> Maybe (WidgetFor site ())
renderHighlight lexer content = renderHighlight lexer content =
case runLexer lexer content of case runLexer lexer content of
Left err -> Nothing Left err -> Nothing
Right tokens -> Just $ toWidget $ format True tokens Right tokens -> Just $ toWidget $ format True tokens
renderCode :: Lexer -> TL.Text -> B.ByteString -> Widget renderCode :: Lexer -> TL.Text -> B.ByteString -> WidgetFor site ()
renderCode lexer contentTL contentB = renderCode lexer contentTL contentB =
fromMaybe (renderPlain contentTL) $ renderHighlight lexer contentB fromMaybe (renderPlain contentTL) $ renderHighlight lexer contentB
@ -182,7 +185,7 @@ writerOptions = def
-- , writerSyntaxMap = defaultSyntaxMap -- , writerSyntaxMap = defaultSyntaxMap
} }
renderPandoc :: Pandoc -> Widget renderPandoc :: Pandoc -> WidgetFor site ()
renderPandoc renderPandoc
= either throwM toWidget = either throwM toWidget
. fmap . fmap
@ -194,20 +197,21 @@ renderPandoc
. runPure . runPure
. writeHtml5 writerOptions . writeHtml5 writerOptions
renderSourceT :: MediaType -> Text -> Widget renderSourceT :: MediaType -> Text -> WidgetFor site ()
renderSourceT mt contentT = renderSourceT mt contentT =
let contentB = TE.encodeUtf8 contentT let contentB = TE.encodeUtf8 contentT
contentTL = TL.fromStrict contentT contentTL = TL.fromStrict contentT
in renderSource mt contentB contentTL contentT in renderSource mt contentB contentTL contentT
renderSourceBL :: MediaType -> BL.ByteString -> Widget renderSourceBL :: MediaType -> BL.ByteString -> WidgetFor site ()
renderSourceBL mt contentBL = renderSourceBL mt contentBL =
let contentB = BL.toStrict contentBL let contentB = BL.toStrict contentBL
contentTL = TLE.decodeUtf8With TE.lenientDecode contentBL contentTL = TLE.decodeUtf8With TE.lenientDecode contentBL
contentT = TL.toStrict contentTL contentT = TL.toStrict contentTL
in renderSource mt contentB contentTL contentT in renderSource mt contentB contentTL contentT
renderSource :: MediaType -> B.ByteString -> TL.Text -> Text -> Widget renderSource
:: MediaType -> B.ByteString -> TL.Text -> Text -> WidgetFor site ()
renderSource mt contentB contentTL contentT = renderSource mt contentB contentTL contentT =
let mtName = T.pack $ show mt let mtName = T.pack $ show mt
@ -252,3 +256,10 @@ renderPandocMarkdown input =
= fmap (sanitizeBalance . TL.toStrict . renderHtml) = fmap (sanitizeBalance . TL.toStrict . renderHtml)
. runPure . runPure
. writeHtml5 writerOptions . writeHtml5 writerOptions
renderPrettyJSON :: ToJSON a => a -> WidgetFor site ()
renderPrettyJSON a =
let prettyBL = encodePretty a
prettyB = BL.toStrict prettyBL
prettyTL = TLE.decodeUtf8 prettyBL
in renderCode L.JS.lexer prettyTL prettyB

View file

@ -69,6 +69,7 @@ library
Data.List.Local Data.List.Local
Data.List.NonEmpty.Local Data.List.NonEmpty.Local
Data.Maybe.Local Data.Maybe.Local
Data.MediaType
Data.Paginate.Local Data.Paginate.Local
Data.Text.UTF8.Local Data.Text.UTF8.Local
Data.Text.Lazy.UTF8.Local Data.Text.Lazy.UTF8.Local
@ -106,6 +107,7 @@ library
Yesod.MonadSite Yesod.MonadSite
Yesod.Paginate.Local Yesod.Paginate.Local
Yesod.Persist.Local Yesod.Persist.Local
Yesod.RenderSource
Yesod.SessionEntity Yesod.SessionEntity
Vervis.Access Vervis.Access
@ -162,7 +164,6 @@ library
Vervis.Import Vervis.Import
Vervis.Import.NoFoundation Vervis.Import.NoFoundation
Vervis.KeyFile Vervis.KeyFile
Vervis.MediaType
Vervis.Migration Vervis.Migration
Vervis.Migration.Model Vervis.Migration.Model
Vervis.Migration.TH Vervis.Migration.TH
@ -182,7 +183,6 @@ library
Vervis.Query Vervis.Query
Vervis.Readme Vervis.Readme
Vervis.RemoteActorStore Vervis.RemoteActorStore
Vervis.Render
Vervis.Role Vervis.Role
Vervis.Secure Vervis.Secure
Vervis.Settings Vervis.Settings