mirror of
https://github.com/urbit/shrub.git
synced 2024-12-29 07:03:37 +03:00
king: add helper for escaping Text into @ta format
Behavior should match (scot %t the-text).
This commit is contained in:
parent
2e7b2ceedb
commit
418b9fda7e
@ -13,6 +13,7 @@ module Urbit.Noun.Conversions
|
||||
, Mug(..), Path(..), EvilPath(..), Ship(..)
|
||||
, Lenient(..), pathToFilePath, filePathToPath
|
||||
, showUD, tshowUD
|
||||
, textAsTa
|
||||
) where
|
||||
|
||||
import ClassyPrelude hiding (hash)
|
||||
@ -40,7 +41,9 @@ import Urbit.Noun.Jam (jam)
|
||||
import Urbit.Ob (patp)
|
||||
|
||||
import qualified Data.Char as C
|
||||
import qualified Data.Text as T
|
||||
import qualified Data.Text.Encoding as T
|
||||
import qualified Numeric as N
|
||||
|
||||
|
||||
-- Noun ------------------------------------------------------------------------
|
||||
@ -551,6 +554,18 @@ instance FromNoun Knot where
|
||||
then pure (MkKnot txt)
|
||||
else fail ("Non-ASCII chars in knot: " <> unpack txt)
|
||||
|
||||
-- equivalent of (cury scot %t)
|
||||
textAsTa :: Text -> Text
|
||||
textAsTa = ("~~" <>) . concatMap \case
|
||||
' ' -> "."
|
||||
'.' -> "~."
|
||||
'~' -> "~~"
|
||||
c ->
|
||||
if C.isAlphaNum c || (c == '-') then
|
||||
T.singleton c
|
||||
else
|
||||
if C.ord c < 0x10 then "~0" else "~"
|
||||
<> (pack $ N.showHex (C.ord c) ".")
|
||||
|
||||
-- Term ------------------------------------------------------------------------
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user