7 Commits
Author SHA1 Message Date
paul 9a4c1f17b7 Bump version to 0.5 2023-03-24 13:08:11 +01:00
paul 3d104ef50c Add allNames function 2023-03-24 13:06:17 +01:00
paul 976fbada7e Add text/plain rendering for /text 2023-03-24 12:59:34 +01:00
paul 0d59ded2ec Add newline after text table 2023-03-24 12:54:55 +01:00
paul ab667742da Bump version to 0.4 2023-03-24 12:53:38 +01:00
paul 647b5c2ad1 Rename /bytes to /utf8 2023-03-17 02:22:13 +01:00
paul 57173d14dd Limit number of returned codepoints 2023-03-17 02:19:15 +01:00
5 changed files with 46 additions and 18 deletions
+42 -14
View File
@@ -61,17 +61,17 @@ app :: Application
app = serve (Proxy :: Proxy API) server
type API =
"bytes" :> Capture "bytes" Text :> Get '[PlainText, HTML] BytesModel
"utf8" :> Capture "bytes" Text :> Get '[PlainText, HTML] Utf8Model
:<|> "codepoints" :> Capture "codepoints" Text :> Get '[PlainText, HTML] CodepointsModel
:<|> "text" :> Capture "text" Text :> Get '[HTML] TextModel
:<|> "text" :> Capture "text" Text :> Get '[PlainText, HTML] TextModel
server :: Server API
server =
bytesR :<|> codepointsR :<|> textR
utf8R :<|> codepointsR :<|> textR
where
bytesR bytesP = do
utf8R bytesP = do
bytes <- Parsers.parseHexBytes bytesP `orThrow` const err400
pure $ BytesModel $ Decode.decodeUtf8 bytes
pure $ mkUtf8Model bytes
codepointsR codepointsP = do
codepoints' <- Parsers.parseCodepoints codepointsP `orThrow` const err400
@@ -82,11 +82,14 @@ server =
-- /bytes/<bytes>
newtype BytesModel = BytesModel
newtype Utf8Model = Utf8Model
{ codepoints :: [([Word8], Either String Char)]
}
instance MimeRender PlainText BytesModel where
mkUtf8Model :: [Word8] -> Utf8Model
mkUtf8Model = Utf8Model . Decode.decodeUtf8
instance MimeRender PlainText Utf8Model where
mimeRender _ model = renderText $
Table.render " " $ concat
[ [ [ Table.cl $ Text.pack $ unwords $ map showByteHex bytes
@@ -99,7 +102,7 @@ instance MimeRender PlainText BytesModel where
Right c ->
[ Text.pack [c]
, Text.pack $ printf "U+%04X" c
, Text.pack $ intercalate ", " $ maybeToList (UnicodeNames.name c) ++ map (++ "*") (UnicodeNames.nameAliases c)
, Text.pack $ intercalate ", " $ allNames c
, Text.pack $ fromMaybe "" $ blockName c
]
)
@@ -107,7 +110,7 @@ instance MimeRender PlainText BytesModel where
| (bytes, eiC) <- model.codepoints
]
instance MimeRender HTML BytesModel where
instance MimeRender HTML Utf8Model where
mimeRender _ model = renderHtml $ documentWithBody $ do
H.table $ for_ model.codepoints $ \(bytes, eiC) -> do
H.tr $ do
@@ -118,7 +121,7 @@ instance MimeRender HTML BytesModel where
Right c -> do
H.td $ H.input ! A.class_ "charbox" ! A.value (H.toValue [c])
H.td $ H.code $ printfHtml "U+%04X" c
H.td $ H.code $ H.toHtml $ intercalate ", " $ maybeToList (UnicodeNames.name c) ++ map (++ "*") (UnicodeNames.nameAliases c)
H.td $ H.code $ H.toHtml $ intercalate ", " $ allNames c
H.td $ H.code $ H.toHtml $ fromMaybe "" $ blockName c
-- /codepoints/<codepoints>
@@ -128,7 +131,14 @@ newtype CodepointsModel = CodepointsModel
}
mkCodepointsModel :: [(Word, Word)] -> CodepointsModel
mkCodepointsModel = CodepointsModel . map go . concatMap (uncurry enumFromTo)
mkCodepointsModel =
CodepointsModel
-- Limit number of returned codepoints. Otherwise it's
-- too easy to provoke massive response bodies with requests like
-- /codepoints/0-99999999
. take 100000
. map go
. concatMap (uncurry enumFromTo)
where
go codepoint = (codepoint, toChar codepoint)
@@ -149,7 +159,7 @@ instance MimeRender PlainText CodepointsModel where
Right c ->
[ Text.pack [c]
, Text.pack $ printf "U+%04X" c
, Text.pack $ intercalate ", " $ maybeToList (UnicodeNames.name c) ++ map (++ "*") (UnicodeNames.nameAliases c)
, Text.pack $ intercalate ", " $ allNames c
, Text.pack $ fromMaybe "" $ blockName c
]
)
@@ -167,7 +177,7 @@ instance MimeRender HTML CodepointsModel where
Right c -> do
H.td $ H.input ! A.class_ "charbox" ! A.value (H.toValue [c])
H.td $ H.code $ printfHtml "U+%04X" c
H.td $ H.code $ H.toHtml $ intercalate ", " $ maybeToList (UnicodeNames.name c) ++ map (++ "*") (UnicodeNames.nameAliases c)
H.td $ H.code $ H.toHtml $ intercalate ", " $ allNames c
H.td $ H.code $ H.toHtml $ fromMaybe "" $ blockName c
-- /text/<text>
@@ -182,9 +192,21 @@ instance MimeRender HTML TextModel where
H.tr $ do
H.td $ H.input ! A.class_ "charbox" ! A.value (H.toValue [c])
H.td $ H.code $ printfHtml "U+%04X" c
H.td $ H.code $ H.toHtml $ intercalate ", " $ maybeToList (UnicodeNames.name c) ++ map (++ "*") (UnicodeNames.nameAliases c)
H.td $ H.code $ H.toHtml $ intercalate ", " $ allNames c
H.td $ H.code $ H.toHtml $ fromMaybe "" $ blockName c
instance MimeRender PlainText TextModel where
mimeRender _ model = renderText $ Table.render " "
[ map (Table.cl)
[ Text.pack [c]
, Text.pack $ printf "U+%04X" c
, Text.pack $ intercalate ", " $ allNames c
, Text.pack $ fromMaybe "" $ blockName c
]
| c <- Text.unpack model.text
]
-- Utilities
renderText :: Text -> BL.ByteString
@@ -196,6 +218,12 @@ showByteHex = printf " %02X"
showByteBin :: Word8 -> String
showByteBin = printf "%08b"
-- | Retrieve name and aliases (suffixed with @*@) of a 'Char'.
allNames :: Char -> [String]
allNames c =
maybeToList (UnicodeNames.name c)
++ map (++ "*") (UnicodeNames.nameAliases c)
blockName :: Char -> Maybe String
blockName c = UnicodeBlocks.blockName . UnicodeBlocks.blockDefinition <$> UnicodeBlocks.block c
+1 -1
View File
@@ -1,7 +1,7 @@
# Adapted from new-template.hsfiles
name: utoy
version: 0.3
version: 0.5
git: "https://git.pbrinkmeier.de/paul/utoy"
license: MIT
author: "Paul Brinkmeier"
+1 -1
View File
@@ -17,7 +17,7 @@ cr :: Text -> Cell
cr = C AlignRight
render :: Text -> [[Cell]] -> Text
render delim cells = Text.intercalate "\n" $ map showRow cells
render delim cells = Text.unlines $ map showRow cells
where
showRow = Text.intercalate delim . map showCell . zipLongest columnWidths
+1 -1
View File
@@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack
name: utoy
version: 0.3
version: 0.5
author: Paul Brinkmeier
maintainer: hallo@pbrinkmeier.de
copyright: 2023 Paul Brinkmeier
+1 -1
View File
@@ -8,7 +8,7 @@ let
utoy =
{ mkDerivation }:
mkDerivation {
version = "0.3";
version = "0.5";
pname = "utoy";
license = pkgs.lib.licenses.mit;
src =