Compare commits

..

No commits in common. "61355405d5d6e3354029a71c2c4feddb8a8fbb46" and "b0451300a553a2a481305524b68a4c90e1a56f87" have entirely different histories.

2 changed files with 14 additions and 43 deletions

View File

@ -66,14 +66,13 @@ type API =
:<|> "utf8" :> Capture "bytes" Text :> Get '[PlainText, HTML] Utf8Model :<|> "utf8" :> Capture "bytes" Text :> Get '[PlainText, HTML] Utf8Model
:<|> "codepoints" :> Capture "codepoints" Text :> Get '[PlainText, HTML] CodepointsModel :<|> "codepoints" :> Capture "codepoints" Text :> Get '[PlainText, HTML] CodepointsModel
:<|> "text" :> Capture "text" Text :> Get '[PlainText, HTML] TextModel :<|> "text" :> Capture "text" Text :> Get '[PlainText, HTML] TextModel
:<|> "search" :> Capture "search" Text :> Get '[PlainText, HTML] SearchModel
server :: Server API server :: Server API
server = server =
rootR :<|> utf8R :<|> codepointsR :<|> textR :<|> searchR rootR :<|> utf8R :<|> codepointsR :<|> textR
where where
rootR host' = do rootR host = do
pure $ RootModel $ fromMaybe "" host' pure $ RootModel $ fromMaybe "" host
utf8R bytesP = do utf8R bytesP = do
bytes <- Parsers.parseHexBytes bytesP `orThrow` const err400 bytes <- Parsers.parseHexBytes bytesP `orThrow` const err400
@ -86,9 +85,6 @@ server =
textR textP = do textR textP = do
pure $ TextModel textP pure $ TextModel textP
searchR searchP = do
pure $ mkSearchModel searchP
-- / -- /
newtype RootModel = RootModel newtype RootModel = RootModel
@ -228,53 +224,28 @@ newtype TextModel = TextModel
} }
instance MimeRender HTML TextModel where instance MimeRender HTML TextModel where
mimeRender _ model = charTableHtml $ Text.unpack model.text mimeRender _ model = renderHtml $ documentWithBody $ do
instance MimeRender PlainText TextModel where H.table $ for_ (Text.unpack model.text) $ \c -> do
mimeRender _ model = charTableText $ Text.unpack model.text
-- /search/<search>
newtype SearchModel = SearchModel
{ results :: [Char]
}
mkSearchModel :: Text -> SearchModel
mkSearchModel search = SearchModel $ searchAllChars search
searchAllChars :: Text -> [Char]
searchAllChars search = [c | c <- [minBound..maxBound], any (\name -> Text.toLower search `Text.isInfixOf` Text.toLower (Text.pack name)) (allNames c)]
instance MimeRender HTML SearchModel where
mimeRender _ model = charTableHtml model.results
instance MimeRender PlainText SearchModel where
mimeRender _ model = charTableText model.results
-- Utilities
charTableHtml :: [Char] -> BL.ByteString
charTableHtml chars =
renderHtml $ documentWithBody $ do
H.table $ for_ chars $ \c -> do
H.tr $ do H.tr $ do
H.td $ H.input ! A.class_ "charbox" ! A.value (H.toValue [c]) H.td $ H.input ! A.class_ "charbox" ! A.value (H.toValue [c])
H.td $ H.code $ printfHtml "U+%04X" c H.td $ H.code $ printfHtml "U+%04X" c
H.td $ H.code $ H.toHtml $ intercalate ", " $ allNames c H.td $ H.code $ H.toHtml $ intercalate ", " $ allNames c
H.td $ H.code $ H.toHtml $ fromMaybe "" $ blockName c H.td $ H.code $ H.toHtml $ fromMaybe "" $ blockName c
instance MimeRender PlainText TextModel where
charTableText :: [Char] -> BL.ByteString mimeRender _ model = renderText $ Table.render " "
charTableText chars =
renderText $ Table.render " "
[ map Table.cl [ map Table.cl
[ Text.pack [c] [ Text.pack [c]
, Text.pack $ printf "U+%04X" c , Text.pack $ printf "U+%04X" c
, Text.pack $ intercalate ", " $ allNames c , Text.pack $ intercalate ", " $ allNames c
, Text.pack $ fromMaybe "" $ blockName c , Text.pack $ fromMaybe "" $ blockName c
] ]
| c <- chars | c <- Text.unpack model.text
] ]
-- Utilities
renderText :: Text -> BL.ByteString renderText :: Text -> BL.ByteString
renderText = BL.fromStrict . Encoding.encodeUtf8 renderText = BL.fromStrict . Encoding.encodeUtf8

6
flake.lock generated
View File

@ -2,11 +2,11 @@
"nodes": { "nodes": {
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1703693486, "lastModified": 1703588687,
"narHash": "sha256-tuzNTOs+1zR2BEVKKrRRGdpR/n095AXIcT8Me1px2bI=", "narHash": "sha256-yj/AFxJjW/aE0lmHz1wlTk3jScZqVjQQEeBOnhyroRc=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "671c2d3e1506a7ee1583515ca80cb3474fdc9c95", "rev": "ad9ca03be8aaf8d6e458102e7d77370b7fe71ccf",
"type": "github" "type": "github"
}, },
"original": { "original": {