1 | module Text.Html.Values
4 | import Language.Reflection
5 | import Language.Reflection.Pretty
6 | import Language.Reflection.Syntax
7 | import Language.Reflection.Syntax.Ops
8 | import Language.Reflection.Util
12 | import Derive.Prelude
13 | import Derive.Refined
15 | import Data.Refined.Integer
17 | import Text.Html.Utils
20 | %language ElabReflection
38 | mkEnum : (name : String) -> (enum : List String) -> Elab ()
40 | declare [simpleDataPublic (UN $
Basic name) (map mkCon enum)]
42 | mkCon : String -> ITy
44 | (UN $
Basic $
upperFirstStr x)
63 | mkShowImpl : (name : String) -> (enum : List String) -> Elab ()
64 | mkShowImpl enumName cons =
67 | mkShow := UN $
Basic "MkShow"
68 | showName := UN $
Basic "show"
69 | functionName := UN . Basic $
"implShow" ++ enumName
72 | show := var showName
73 | function := var functionName
74 | enum := arg $
varStr $
enumName
76 | mkC := \x => show .$ (varStr $
upperFirstStr x)
77 | .= primVal (Str $
replaceChar '_' '-' x)
79 | showPrec := `(\_,v => show v)
83 | [ private' showName $
enum .-> `(String)
84 | , def showName $
map mkC cons
85 | ] (var mkShow .$ show .$ showPrec)
89 | [ interfaceHint Public functionName (var "Show" .$ type enum)
90 | , def functionName [function .= impl]
110 | mkValueFunc : (name : String) -> (enum : List String) -> Elab ()
111 | mkValueFunc name enum =
113 | argStr := arg $
primVal (PrT StringType)
114 | output := var "Either" .$ primVal (PrT StringType) .$ var (UN . Basic $
name)
117 | [ public' functionName $
argStr .-> output
118 | , def functionName $
map mkCase enum ++ [errorCase]
122 | functionName : Name
123 | functionName = UN . Basic $
lowerFirstStr name
125 | mkCase : String -> Clause
128 | .$ (var "fromString" .$ primVal (Str $
replaceChar '_' '-' c))
129 | .= var "Right" .$ var (UN . Basic $
upperFirstStr c)
134 | "ERROR: The accepted values for \{name} are: " ++ joinBy ", " enum ++ "."
138 | var functionName .$ var "str"
139 | .= var "Left" .$ (var "fromString" .$ primVal (Str msg))
154 | mkFromStringFunc : (name : String) -> Elab ()
155 | mkFromStringFunc name =
157 | functionName := "fromString"
158 | callName := UN . Basic $
lowerFirstStr name
159 | strArg := MkArg MW ExplicitArg (Just "str") `(String)
164 | (var "IsRight" .$ (var callName .$ bindVar "str"))
165 | output := var (UN . Basic $
name)
168 | [ public' functionName $
strArg .-> prfArg .-> output
170 | [ var functionName .$ bindVar "str"
171 | .= var "fromRight" .$ (var callName .$ var "str")
185 | mkValueFuncs : (name : String) -> (enum : List String) -> Elab ()
186 | mkValueFuncs name enum = do
188 | mkShowImpl name enum
189 | mkValueFunc name enum
190 | mkFromStringFunc name
192 | namespace Identifier
196 | data Identifier = MkIdentifier String
199 | Show Identifier where
200 | show (MkIdentifier x) = x
213 | identifier : String -> Either String Identifier
214 | identifier str = case idHelper $
unpack str of
215 | Right _ => Right (MkIdentifier str)
218 | idHelperTail : List Char -> Either String (List Char)
219 | idHelperTail [] = Right []
220 | idHelperTail (x :: xs) =
221 | if isAlphaNum x || x == '_' || x == '-'
222 | then idHelperTail xs
223 | else Left ("ERROR: \{show x} is not an authorised character for the "
224 | ++ "start of an identifier.")
227 | idHelper : List Char -> Either String (List Char)
228 | idHelper [] = Left "ERROR: an identifier cannot be empty."
229 | idHelper (x :: xs) =
230 | if isAlpha x || x == '_'
231 | then idHelperTail xs
232 | else Left ("ERROR: \{show x} is not an authorised character for the "
233 | ++ "start of an identifier.")
235 | %runElab mkFromStringFunc "Identifier"
237 | namespace Identifiers
241 | data Identifiers = MkIdentifiers (List1 Identifier)
244 | Show Identifiers where
245 | show (MkIdentifiers xs) = unwords $
map show $
toList xs
251 | identifiers : String -> Either String Identifiers
253 | case !(mapM identifier $
words str) of
254 | [] => Left "ERROR: a list of identifiers cannot be empty."
255 | (x :: xs) => Right $
MkIdentifiers $
toList1 (x :: xs)
257 | %runElab mkFromStringFunc "Identifiers"
262 | %runElab mkValueFuncs
280 | namespace Autocomplete
283 | data AutocompleteValue =
286 | | TokenList Identifiers
289 | Show AutocompleteValue where
292 | show (TokenList xs) = show xs
295 | autocompleteValue : String -> Either String AutocompleteValue
296 | autocompleteValue "on" = Right On
297 | autocompleteValue "off" = Right Off
298 | autocompleteValue str = pure $
TokenList !(identifiers str)
300 | %runElab mkFromStringFunc "AutocompleteValue"
303 | namespace Autocorrect
305 | %runElab mkValueFuncs "AutocorrectValue" ["on" , "off"]
311 | data Base64 : Nat -> Type where
312 | MkBase64 : (n : Nat) -> (chars : Vect n Char) -> Base64 n
315 | base64Digits : List Char -> Either String (List Char)
316 | base64Digits [] = Right []
317 | base64Digits ('=' :: xs) = pure ('=' :: !(b64tail xs))
319 | b64tail : List Char -> Either String (List Char)
320 | b64tail [] = Right []
321 | b64tail ('=' :: xs) = pure ('=' :: !(b64tail xs))
323 | Left "ERROR: \{show x} is not a valid base64 padding character."
324 | base64Digits (x :: xs) =
325 | if isAlphaNum x || x == '+' || x == '/'
326 | then pure (x :: !(base64Digits xs))
327 | else Left "ERROR: \{show x} is not a valid base64 character."
330 | Show (Base64 n) where
331 | show (MkBase64 _ xs) = pack (toList xs)
334 | base64Value' : (n : Nat) -> List Char -> Either String (Base64 n)
335 | base64Value' n xs =
336 | case toVect n !(base64Digits xs) of
337 | Nothing => Left "ERROR: \{pack xs} is not of length \{show n}."
338 | Just v => Right (MkBase64 n v)
341 | base64Value : (n : Nat) -> String -> Either String (Base64 n)
342 | base64Value n str = base64Value' n (unpack str)
345 | fromString : (str : String)
346 | -> {auto 0 prf : IsRight (base64Value (length str) str)}
347 | -> Base64 (length str)
348 | fromString str = fromRight $
base64Value (length str) str
351 | namespace Contenteditable
353 | %runElab mkValueFuncs
354 | "ContenteditableValue"
364 | data CoordsValue : Type where
365 | Rect : (x1 : Double)
370 | Circle : (x : Double)
374 | Poly : (x1 : Double)
378 | -> List (Double, Double)
382 | Show CoordsValue where
383 | show (Rect x1 y1 x2 y2) = "\{show x1},\{show y1},\{show x2},\{show y2}"
384 | show (Circle x y r) = "\{show x},\{show y},\{show r}"
385 | show (Poly x1 y1 x2 y2 xs) =
386 | "\{show x1},\{show y1},\{show x2},\{show y2}" ++ showList xs
388 | showList : List (Double, Double) -> String
390 | showList ((xn, yn) :: xs) = ",\{show xn},\{show yn}" ++ showList xs
393 | namespace Crossorigin
395 | %runElab mkValueFuncs "CrossoriginValue" ["anonymous", "use_credentials"]
447 | fromMonth : Month -> Nat
462 | toMonth : Integer -> Maybe Month
463 | toMonth 1 = Just Jan
464 | toMonth 2 = Just Feb
465 | toMonth 3 = Just Mar
466 | toMonth 4 = Just Apr
467 | toMonth 5 = Just May
468 | toMonth 6 = Just Jun
469 | toMonth 7 = Just Jul
470 | toMonth 8 = Just Aug
471 | toMonth 9 = Just Sep
472 | toMonth 10 = Just Oct
473 | toMonth 11 = Just Nov
474 | toMonth 12 = Just Dec
475 | toMonth _ = Nothing
479 | x < y = fromMonth x < fromMonth y
487 | isLeap : (year : Integer) -> Bool
490 | nthYear : Integer -> Bool
491 | nthYear n = mod year n == 0
493 | (nthYear 4) && (not (nthYear 100) || (nthYear 400))
497 | daysInMonth : (year : Integer) -> Month -> Integer
498 | daysInMonth year Feb = if isLeap year then 29 else 28
499 | daysInMonth _ Apr = 30
500 | daysInMonth _ Jun = 30
501 | daysInMonth _ Sep = 30
502 | daysInMonth _ Nov = 30
503 | daysInMonth _ _ = 31
507 | isValidDay : (year : Integer) -> Month -> (day : Integer) -> Bool
508 | isValidDay year month day = day > 0 && day <= daysInMonth year month
512 | isValidYear : (year : Integer) -> Bool
513 | isValidYear year = year > -
10000 && year < 10000
517 | isValidDate : (year : Integer) -> Month -> (day : Integer) -> Bool
518 | isValidDate year month day = isValidYear year && isValidDay year month day
527 | {auto 0 prf : Holds (isValidDate year month) day}
531 | showYear : (year : Integer) -> String
533 | let yStr := show year in
534 | replicate (minus 4 $
length yStr) '0' ++ yStr
538 | showMonth : Month -> String
540 | let mStr := show (fromMonth month) in
541 | replicate (minus 2 $
length mStr) '0' ++ mStr
546 | show2Digits : Integer -> String
548 | let str := show x in
549 | replicate (minus 2 $
length str) '0' ++ str
554 | show (MkDate year month day) =
555 | "\{showYear year}-\{showMonth month}-\{show2Digits day}"
557 | %runElab derive "Date" [Eq, Ord]
561 | maybe_date : Integer -> Month -> Integer -> Maybe Date
562 | maybe_date year month day =
563 | case hdec0 {p = Holds (isValidDate year month)} day of
564 | Just0 _ => Just (MkDate year month day)
565 | Nothing0 => Nothing
568 | parseNeg : String -> Int
569 | parseNeg str = if strSubstr 0 1 str == "-" then 1 else 0
572 | extractPart : (start : Int) -> (length : Int) -> String -> String
573 | extractPart start l str = strSubstr (start + parseNeg str) l str
576 | parseYear : String -> Either String Integer
578 | let yyyy := extractPart 0 4 str in
579 | case parseInteger yyyy of
580 | Just x => Right (if parseNeg str == 1 then -x else x)
581 | Nothing => Left "ERROR: '\{yyyy}' is not a valid year value."
584 | parseMonth : String -> Either String Month
586 | let mm := extractPart 5 2 str in
587 | case the (Maybe Integer) (parsePositive mm) >>= toMonth of
589 | Nothing => Left "ERROR: '\{mm}' is not a valid month value."
592 | parseDay : String -> Either String Integer
594 | let dd := extractPart 8 2 str in
595 | case parsePositive dd of
597 | Nothing => Left "ERROR: '\{dd}' is not a valid day value."
600 | parseSep : (pos : Int) -> String -> Either String String
602 | let sep := extractPart pos 1 str in
605 | else Left "ERROR: '\{sep}' is not a valid date separator."
612 | dateValue : String -> Either String Date
614 | _ <- checkLength str
615 | _ <- parseSep 4 str
616 | _ <- parseSep 7 str
617 | case maybe_date !(parseYear str) !(parseMonth str) !(parseDay str) of
619 | Nothing => Left "ERROR: '\{str}' is not a valid date."
621 | checkLength : String -> Either String String
623 | if length str == 10
625 | else Left "ERROR: \{str} has not a correct length for a date."
628 | fromString : (str : String) -> {auto 0 prf : IsRight (dateValue str)} -> Date
629 | fromString str = fromRight (dateValue str)
631 | namespace YearMonth
635 | record YearMonth where
636 | constructor MkYearMonth
639 | {auto 0 prf : Holds Date.isValidYear year}
642 | Show YearMonth where
643 | show (MkYearMonth year month) = "\{showYear year}-\{showMonth month}"
647 | maybe_yearMonth : (year : Integer) -> Month -> Maybe YearMonth
648 | maybe_yearMonth year month =
649 | case hdec0 {p = Holds Date.isValidYear} year of
650 | Just0 _ => Just (MkYearMonth year month)
651 | Nothing0 => Nothing
654 | yearMonthValue : String -> Either String YearMonth
655 | yearMonthValue str = do
656 | _ <- checkLength str
657 | _ <- parseSep 4 str
658 | case maybe_yearMonth !(parseYear str) !(parseMonth str) of
660 | Nothing => Left "ERROR: '\{str}' is not a valid year-month."
662 | checkLength : String -> Either String String
666 | else Left "ERROR: \{str} has not a correct length for a year-month."
669 | fromString : (str : String)
670 | -> {auto 0 prf : IsRight (yearMonthValue str)}
672 | fromString str = fromRight (yearMonthValue str)
682 | dayOn31Dec : (year : Integer) -> Integer
688 | cast (year' + (year' / 4) - (year' / 100) + (year' / 400)) `mod` 7
696 | nbWeeks : (year : Integer) -> Integer
698 | if dayOn31Dec year == 4 || dayOn31Dec (year - 1) == 3
704 | isValidYearWeek : (year : Integer) -> (week : Integer) -> Bool
705 | isValidYearWeek year week =
706 | isValidYear year && week > 0 && week <= nbWeeks year
710 | record YearWeek where
711 | constructor MkYearWeek
714 | {auto 0 prf : Holds (isValidYearWeek year) week}
717 | Show YearWeek where
718 | show (MkYearWeek year week) = "\{showYear year}-W\{show2Digits week}"
724 | maybe_yearWeek : (year : Integer) -> (week : Integer) -> Maybe YearWeek
725 | maybe_yearWeek year week =
726 | case hdec0 {p = Holds (isValidYearWeek year)} week of
727 | Just0 _ => Just (MkYearWeek year week)
728 | Nothing0 => Nothing
731 | yearWeekValue : String -> Either String YearWeek
732 | yearWeekValue str = do
733 | _ <- checkLength str
734 | _ <- parseSep 4 str
736 | case maybe_yearWeek !(parseYear str) !(parseWeek str) of
738 | Nothing => Left "ERROR: '\{str}' is not a valid year-week"
740 | checkLength : String -> Either String String
744 | else Left "ERROR: \{str} has not a correct length for a year-week."
746 | parseW : String -> Either String String
748 | let w := extractPart 5 1 str in
751 | else Left ("ERROR: `\{w}' is not a valid week symbol. The only accepted "
752 | ++ "symbol is 'W'.")
754 | parseWeek : String -> Either String Integer
756 | let w := extractPart 6 2 str in
757 | case parsePositive w of
759 | Nothing => Left "ERROR: `\{w} is not a valid week number."
762 | fromString : (str : String)
763 | -> {auto 0 prf : IsRight (yearWeekValue str)}
765 | fromString str = fromRight (yearWeekValue str)
770 | isValidTime : (hour : Int) -> (minute : Int) -> (second : Maybe Int) -> Bool
771 | isValidTime hour minute second =
772 | hour >= 0 && hour < 24
773 | && minute >= 0 && minute < 60
775 | Just s => s >= 0 && s < 60
785 | {auto 0 prf : Holds (isValidTime hour minute) second}
789 | show2Digits : Int -> String
791 | let str := show x in
792 | replicate (minus 2 $
length str) '0' ++ str
796 | show (MkTime hour minute second) =
797 | "\{show2Digits hour}:\{show2Digits minute}"
799 | Just s => ":\{show2Digits s}"
806 | maybe_time : (hour : Int)
808 | -> (second : Maybe Int)
810 | maybe_time hour minute second =
811 | case hdec0 {p = Holds (isValidTime hour minute)} second of
812 | Just0 _ => Just (MkTime hour minute second)
813 | Nothing0 => Nothing
816 | timeValue : String -> Either String Time
818 | _ <- checkLength str
819 | _ <- parseTimeSep1 str
820 | _ <- parseTimeSep2 str
821 | case maybe_time !(parsePart 0 str) !(parsePart 3 str) !(parseSecond str) of
823 | Nothing => Left "ERROR: '\{str}' is not a valid time."
825 | checkLength : String -> Either String String
827 | if length str == 5 || length str == 8
829 | else Left "ERROR: \{str} has not a correct length for a time."
831 | parseTimeSep1 : String -> Either String String
832 | parseTimeSep1 str =
833 | let sep := extractPart 2 1 str in
836 | else Left ("ERROR: `\{sep}' is not a valid time separator. The only "
837 | ++ "accepted one is ':'.")
839 | parseTimeSep2 : String -> Either String String
840 | parseTimeSep2 str =
841 | let sep := extractPart 5 1 str in
842 | if sep == ":" || length str < 6
844 | else Left ("ERROR: `\{sep}' is not a valid time separator. The only "
845 | ++ "accepted one is ':'.")
847 | parsePart : Int -> String -> Either String Int
848 | parsePart pos str =
850 | part := extractPart pos 2 str
851 | text := if pos == 0 then "hour" else "minute"
853 | case parsePositive part of
855 | Nothing => Left "ERROR: `\{part} is not a valid \{text}."
857 | parseSecond : String -> Either String (Maybe Int)
860 | part := extractPart 6 2 str
864 | else case parsePositive part of
865 | Just x => Right (Just x)
866 | Nothing => Left "ERROR: `\{part} is not a valid second."
869 | fromString : (str : String)
870 | -> {auto 0 prf : IsRight (timeValue str)}
872 | fromString str = fromRight (timeValue str)
878 | data Datetime : Type where
880 | MkDatetime : (string : String) -> Datetime
883 | Show Datetime where
884 | show (MkDatetime str) = str
888 | datetimeValue : String -> Either String Datetime
889 | datetimeValue = Right . MkDatetime
893 | fromString : (value : String) -> Datetime
894 | fromString = MkDatetime
899 | %runElab mkValueFuncs "DecodingValue" ["sync", "async", "auto"]
904 | %runElab mkValueFuncs "DirValue" ["ltr", "rtl", "auto"]
909 | %runElab mkValueFuncs "DirnameValue" ["ltr", "rtl"]
911 | namespace DocumentFragment
915 | record DocumentFragment where
916 | constructor MkDocumentFragment
923 | documentFragment : String -> Either String DocumentFragment
924 | documentFragment str =
926 | ('#' :: _) => Right (MkDocumentFragment str)
928 | "ERROR: a document fragment must start with '#', **\{pack v}** provided"
931 | FromString DocumentFragment where
932 | fromString str = MkDocumentFragment str
935 | Show DocumentFragment where
936 | show (MkDocumentFragment str) = str
940 | %runElab mkEnum "EnctypeValue" ["urlencoded", "formData", "plain"]
942 | %runElab mkShowImpl "EnctypeValue" ["urlencoded", "formData", "plain"]
944 | enctypeValue : String -> Either String EnctypeValue
945 | enctypeValue "application/x-www-form-urlencoded" = Right Urlencoded
946 | enctypeValue "multipart/form-data" = Right FormData
947 | enctypeValue "text/plain" = Right Plain
948 | enctypeValue str = Left
949 | "ERROR: \{str} is not an allowed value for the **enctype** argument."
951 | %runElab mkFromStringFunc "EnctypeValue"
954 | namespace Enterkeyhint
956 | %runElab mkValueFuncs
957 | "EnterkeyhintValue"
967 | namespace Exportpart
971 | data ExportpartValue : Type where
972 | Value : Identifier -> ExportpartValue
973 | KeyValue : (key : Identifier) -> (value : Identifier) -> ExportpartValue
976 | Show ExportpartValue where
977 | show (Value v) = show v
978 | show (KeyValue k v) = "\{show k}:\{show v}"
981 | exportpartValue : String -> Either String ExportpartValue
982 | exportpartValue str =
984 | parts := break (== ':') str
986 | if strLength (snd parts) == 0
987 | then map Value (identifier str)
990 | !(identifier $
fst parts)
991 | !(identifier $
strSubstr 1 (strLength $
snd parts) (snd parts))
993 | %runElab mkFromStringFunc "ExportpartValue"
995 | namespace Exportparts
999 | data ExportpartsValue = MkExportpartsValue (List1 ExportpartValue)
1002 | Show ExportpartsValue where
1003 | show (MkExportpartsValue xs) = joinBy ", " $
map show $
toList xs
1006 | exportpartsValue : String -> Either String ExportpartsValue
1008 | case !(mapM exportpartValue $
map trim $
toList $
split (== ',') str) of
1009 | [] => Left "ERROR: **exportparts** attribute cannot be empty."
1010 | (x :: xs) => Right $
MkExportpartsValue $
toList1 (x :: xs)
1012 | %runElab mkFromStringFunc "ExportpartsValue"
1015 | namespace Fetchpriority
1017 | %runElab mkValueFuncs "FetchpriorityValue" ["high" , "low" , "auto"]
1023 | data Filename = MkFilename String
1027 | show (MkFilename name) = name
1031 | filename : String -> Either String Filename
1032 | filename name = Right (MkFilename name)
1036 | fromString : (str : String) -> Filename
1037 | fromString name = MkFilename name
1042 | %runElab mkValueFuncs "FormmethodValue" ["post" , "get" , "dialog"]
1047 | %runElab mkEnum "FormtargetValue" ["self", "blank", "parent", "top"]
1049 | %runElab mkShowImpl "FormtargetValue" ["self", "blank", "parent", "top"]
1052 | formtargetValue : String -> Either String FormtargetValue
1053 | formtargetValue "_self" = Right Self
1054 | formtargetValue "_blank" = Right Blank
1055 | formtargetValue "_parent" = Right Parent
1056 | formtargetValue "_top" = Right Top
1058 | Left ("ERROR: \{str} provided. For the **formtarget** attribute, "
1059 | ++ "the allowed values are: _self, _blank, _parent or _top.")
1061 | %runElab mkFromStringFunc "FormtargetValue"
1065 | %runElab mkEnum "HiddenValue" ["hidden" , "untilFound" , "empty"]
1067 | %runElab mkShowImpl "HiddenValue" ["hidden" , "untilFound" , "empty"]
1070 | hiddenValue : String -> Either String HiddenValue
1071 | hiddenValue "hidden" = Right Hidden
1072 | hiddenValue "until-found" = Right UntilFound
1073 | hiddenValue "" = Right Empty
1075 | Left ("ERROR: \{str} provided. For the **hidden** attribute, "
1076 | ++ "the allowed values are: hidden, until-found or an empty string.")
1078 | %runElab mkFromStringFunc "HiddenValue"
1086 | , "contentSecurityPolicy"
1094 | , "contentSecurityPolicy"
1099 | httpEquivValue : String -> Either String HttpEquivValue
1100 | httpEquivValue "content-type" = Right ContentType
1101 | httpEquivValue "content-security-policy" = Right ContentSecurityPolicy
1102 | httpEquivValue "default-style" = Right DefaultStyle
1103 | httpEquivValue "refresh" = Right Refresh
1105 | Left ("ERROR: \{str} provided. For the **http-equiv** attribute, "
1106 | ++ "the allowed values are: content-type, content-security-policy, "
1107 | ++ "default-style or refresh.")
1109 | %runElab mkFromStringFunc "HttpEquivValue"
1115 | data IntegrityValue : Type where
1116 | Sha256 : Base64 44 -> IntegrityValue
1117 | Sha384 : Base64 64 -> IntegrityValue
1118 | Sha512 : Base64 88 -> IntegrityValue
1121 | Show IntegrityValue where
1122 | show (Sha256 b64) = "sha256-" ++ show b64
1123 | show (Sha384 b64) = "sha384-" ++ show b64
1124 | show (Sha512 b64) = "sha512-" ++ show b64
1127 | integrityValue' : List Char -> Either String IntegrityValue
1128 | integrityValue' ('s' :: 'h' :: 'a' :: '2' :: '5' :: '6' :: '-' :: xs)
1130 | _ | 43 = pure $
Sha256 !(base64Value' 44 $
snoc xs '=')
1131 | _ | 44 = pure $
Sha256 !(base64Value' 44 xs)
1132 | _ | n = Left "ERROR: \{show n} isn't a valid length for sha256 in base64."
1133 | integrityValue' ('s' :: 'h' :: 'a' :: '3' :: '8' :: '4' :: '-' :: xs)
1135 | _ | 64 = pure $
Sha384 !(base64Value' 64 xs)
1136 | _ | n = Left "ERROR: \{show n} isn't a valid length for sha384 in base64."
1137 | integrityValue' ('s' :: 'h' :: 'a' :: '5' :: '1' :: '2' :: '-' :: xs)
1139 | _ | 86 = pure $
Sha512 !(base64Value' 88 $
xs ++ ['=', '='])
1140 | _ | 87 = pure $
Sha512 !(base64Value' 88 $
snoc xs '=')
1141 | _ | 88 = pure $
Sha512 !(base64Value' 88 xs)
1142 | _ | n = Left "ERROR: \{show n} isn't a valid length for sha512 in base64."
1144 | Left "Error: \{pack xs} is not a valid **integrity** attribute value."
1147 | integrityValue : String -> Either String IntegrityValue
1148 | integrityValue = integrityValue' . unpack
1150 | %runElab mkFromStringFunc "IntegrityValue"
1155 | %runElab mkValueFuncs "InputmodeValue"
1169 | %runElab mkValueFuncs "KindValue"
1180 | %runElab mkValueFuncs "LoadingValue" ["eager" , "lazy"]
1188 | data MediaQuery = MkMediaQuery String
1192 | show (MkMediaQuery str) = str
1195 | mediaQueryValue : String -> Either String MediaQuery
1196 | mediaQueryValue = Right . MkMediaQuery
1199 | fromString : String -> MediaQuery
1200 | fromString = MkMediaQuery
1205 | %runElab mkValueFuncs "MethodValue" ["post" , "get", "dialog"]
1210 | %runElab mkValueFuncs "PopoverValue" ["auto" , "hint", "manual"]
1212 | namespace Referrerpolicy
1214 | %runElab mkValueFuncs "ReferrerpolicyValue"
1216 | , "no_referrer_when-downgrade"
1218 | , "origin_when_cross_origin"
1221 | , "strict_origin_when_cross_origin"
1231 | data RegExp = MkRegExp String
1235 | show (MkRegExp str) = str
1238 | regExpValue : String -> Either String RegExp
1239 | regExpValue = Right . MkRegExp
1242 | fromString : String -> RegExp
1248 | %runElab mkValueFuncs "SandboxValue"
1252 | , "allow_orientation_lock"
1255 | , "allow_popups_to_escape_sandbox"
1259 | , "allow_top_navigation"
1260 | , "allow_top_navigation_by_user_activation"
1261 | , "allow_top_navigation_to_custom_protocols"
1267 | %runElab mkValueFuncs "ScopeValue" ["row" , "col" , "rowgroup" , "colgroup"]
1272 | %runElab mkValueFuncs "ShapeValue" ["rect" , "circle" , "poly" , "default"]
1285 | show size = "\{show size.width}x\{show size.height}"
1288 | sizeValue : String -> Either String SizeValue
1291 | parts := break (== 'x') $
toLower str
1293 | height := strSubstr 1 (strLength $
snd parts) (snd parts)
1295 | x <- case parsePositive width of
1297 | "ERROR: '\{width}' in '\{str}' is not a valid positive integer."
1299 | y <- case parsePositive height of
1301 | "ERROR: '\{height}' in '\{str}' is not a valid positive integer."
1306 | data SizesValue : Type where
1308 | ListSizes : List1 SizeValue -> SizesValue
1313 | show (ListSizes xs) = unwords $
map show $
toList xs
1316 | sizesValue : String -> Either String SizesValue
1317 | sizesValue "any" = Right Any
1319 | case !(mapM sizeValue $
words str) of
1320 | [] => Left "ERROR: a list of sizes cannot be empty."
1321 | (x :: xs) => Right $
ListSizes $
toList1 (x :: xs)
1323 | %runElab mkFromStringFunc "SizesValue"
1328 | %runElab mkValueFuncs "TypeValue"
1357 | record MimeTypeValue where
1358 | constructor MkMimeTypeValue
1363 | Show MimeTypeValue where
1364 | show mimeType = "\{mimeType.content}/\{mimeType.fileType}"
1367 | mimeTypeValue : String -> Either String MimeTypeValue
1370 | parts := break (== '/') $
str
1372 | fileType := strSubstr 1 (strLength $
snd parts) (snd parts)
1375 | then Right (MkMimeTypeValue content fileType)
1376 | else Left "ERROR: '\{str}' is not a valid MIME type."
1378 | %runElab mkFromStringFunc "MimeTypeValue"
1381 | namespace NumberingType
1384 | data NumberingTypeValue = ALower | AUpper | ILower | IUpper | One
1387 | Show NumberingTypeValue where
1395 | numberingTypeValue : String -> Either String NumberingTypeValue
1396 | numberingTypeValue "a" = Right ALower
1397 | numberingTypeValue "A" = Right AUpper
1398 | numberingTypeValue "i" = Right ILower
1399 | numberingTypeValue "I" = Right IUpper
1400 | numberingTypeValue "1" = Right One
1401 | numberingTypeValue str =
1402 | Left $
"ERROR: '\{str}' is not a valid numbering type value. "
1403 | ++ "The possible values are 'a', 'A', 'i', 'I' or '1'."
1405 | %runElab mkFromStringFunc "NumberingTypeValue"
1411 | data ScriptTypeValue = Importmap | Module | Other MimeTypeValue
1414 | Show ScriptTypeValue where
1415 | show Importmap = "importmap"
1417 | show (Other mime) = show mime
1420 | scriptTypeValue : String -> Either String ScriptTypeValue
1421 | scriptTypeValue "importmap" = Right Importmap
1422 | scriptTypeValue "module" = Right Module
1423 | scriptTypeValue str = pure $
Other !(mimeTypeValue str)
1425 | %runElab mkFromStringFunc "ScriptTypeValue"
1430 | %runElab mkValueFuncs "WrapValue" ["hard" , "soft"]
1442 | uriValue : String -> Either String Uri
1443 | uriValue = Right . MkUri
1448 | -> {auto 0 prf : IsRight (uriValue str)}
1450 | fromString str = MkUri str
1466 | urlValue : String -> Either String Url
1467 | urlValue = Right . MkUrl
1472 | -> {auto 0 prf : IsRight (urlValue str)}
1474 | fromString str = MkUrl str
1486 | data Urls = MkUrls (List1 Url)
1490 | show (MkUrls xs) = unwords $
map show $
toList xs
1494 | urls : String -> Either String Urls
1496 | case !(mapM urlValue $
words str) of
1497 | [] => Left "ERROR: a list of URLs cannot be empty."
1498 | (x :: xs) => Right $
MkUrls $
toList1 (x :: xs)
1500 | %runElab mkFromStringFunc "Urls"
1506 | data Urn = MkUrn String
1514 | urnValue : String -> Either String Urn
1515 | urnValue = Right . MkUrn
1520 | -> {auto 0 prf : IsRight (urnValue str)}
1522 | fromString str = MkUrn str
1528 | data PartialUrl = MkPartialUrl String
1532 | show (MkPartialUrl str) = str
1536 | partialUrl : String -> Either String PartialUrl
1538 | case strSubstr 0 1 str of
1539 | "#" => Right (MkPartialUrl str)
1541 | $
"ERROR: \{str} is not a valid partial URL, "
1542 | ++ "which must start with `#`."
1544 | %runElab mkFromStringFunc "PartialUrl"
1549 | %runElab mkValueFuncs "PreloadValue" ["none" , "metadata", "auto"]
1556 | data ItemidValue = UrlValue Url | UrnValue Urn
1560 | show (UrlValue url) = show url
1561 | show (UrnValue urn) = show urn
1565 | itemidValue : String -> Either String ItemidValue
1566 | itemidValue str = pure $
UrlValue !(urlValue str)
1568 | %runElab mkFromStringFunc "ItemidValue"
1578 | %runElab mkValueFuncs "RoleValue"
1628 | data SrcdocValue = MkSrcdocValue String
1632 | show (MkSrcdocValue str) = str
1635 | fromString : String -> SrcdocValue
1636 | fromString = MkSrcdocValue
1642 | data TabindexValue = MinusOne | Zero
1645 | Show TabindexValue where
1655 | tabindexValue : String -> Either String TabindexValue
1656 | tabindexValue "-1" = Right MinusOne
1657 | tabindexValue "0" = Right Zero
1658 | tabindexValue str = Left $
1659 | "ERROR: `\{str}` isn't a valid **tabindex** attribute value. "
1660 | ++ "Only `-1` or `0` should be used."
1662 | %runElab mkFromStringFunc "TabindexValue"
1670 | data CssValue = MkCssValue String
1674 | show (MkCssValue str) = str
1677 | fromString : String -> CssValue
1678 | fromString = MkCssValue
1691 | HrefDocumentFragment : DocumentFragment -> Href
1700 | hrefValue : String -> Either String Href
1702 | case documentFragment str of
1703 | Right docF => Right $
HrefDocumentFragment docF
1704 | Left _ => Right $
HrefUrl (MkUrl str)
1709 | -> {auto prf : IsRight (hrefValue value)}
1711 | fromString str = fromRight (hrefValue str)
1715 | show (HrefUrl url) = show url
1716 | show (HrefDocumentFragment fragment) = show fragment
1726 | %runElab mkValueFuncs "Charset" ["utf_8"]
1733 | constructor MkFileTypes
1737 | FromString FileTypes where
1738 | fromString = MkFileTypes . split (== ',') . trim
1748 | namespace NonWhitespace
1752 | data NonWhitespace : Type where
1753 | MkNonWhitespace : (string : String) -> NonWhitespace
1756 | nonWhiteSpace : String -> Either String NonWhitespace
1759 | then Left "ERROR: this string cannot be empty or formed of spaces only."
1760 | else Right (MkNonWhitespace str)
1765 | -> {auto 0 prf : IsRight (nonWhiteSpace value)}
1767 | fromString str = MkNonWhitespace str
1770 | Show NonWhitespace where
1771 | show (MkNonWhitespace str) = str
1778 | %runElab mkValueFuncs "RelValue" ["icon", "stylesheet"]
1784 | record LanguageTag where
1785 | constructor MkLanguageTag
1792 | show (MkLanguageTag language Nothing Nothing) = language
1793 | show (MkLanguageTag language Nothing (Just region)) =
1794 | language <+> "-" <+> region
1795 | show (MkLanguageTag language (Just script) Nothing) =
1796 | language <+> "-" <+> script
1797 | show (MkLanguageTag language (Just script) (Just region)) =
1798 | language <+> "-" <+> script <+> "-" <+> region
1804 | splitList : (a -> Bool) -> List a -> List (List a)
1808 | (chunk, []) => singleton chunk
1809 | (chunk, (c :: rest)) => chunk :: splitList p (assert_smaller xs rest)
1815 | splitStr : (Char -> Bool) -> String -> List String
1816 | splitStr p xs = map pack (splitList p (unpack xs))
1823 | bcpValue : String -> Either String LanguageTag
1825 | let parts = splitStr (== '-') str in
1827 | (x :: []) => Right (MkLanguageTag x Nothing Nothing)
1828 | (x :: y :: []) => if String.length y == 4
1829 | then Right $
MkLanguageTag x Nothing (Just y)
1830 | else Right $
MkLanguageTag x (Just y) Nothing
1831 | (x :: y :: z :: []) => Right $
MkLanguageTag x (Just y) (Just z)
1833 | ( "ERROR: **\{str}** is not a valid BCP 47 "
1840 | -> {auto 0 prf : IsRight (bcpValue str)}
1842 | fromString str = fromRight (bcpValue str)
1847 | data Descriptor = Width Nat | Density Double
1851 | show (Width n) = show n ++ "w"
1852 | show (Density d) = show d ++ "x"
1855 | descriptorValue : String -> Either String Descriptor
1859 | start = strSubstr 0 (len - 1) val
1860 | end = strSubstr (len - 1) len val
1863 | "w" => case parsePositive start of
1864 | Just 0 => Left (eMsgW "0")
1865 | Just w => Right $
Width w
1866 | Nothing => Left (eMsgW start)
1867 | "x" => case parseDouble start of
1868 | Just 0 => Left (eMsgX "0")
1871 | else Left $
eMsgX $
show x
1872 | Nothing => Left (eMsgX start)
1874 | "ERROR: a descriptor must end with 'w' or 'x'.\"\{e}\" provided."
1876 | eMsgW : String -> String
1877 | eMsgW w = "ERROR: width descriptor must be a positive integer; "
1878 | ++ "\"\{w}\" provided."
1880 | eMsgX : String -> String
1881 | eMsgX x = "ERROR: pixel density descriptor must be a positive double; "
1882 | ++ "\"\{x}\" provided."
1886 | record SrcsetValue where
1889 | descriptor : Maybe Descriptor
1893 | show (MkSrcset url (Just descriptor)) = show url ++ " " ++ show descriptor
1894 | show (MkSrcset url Nothing) = show url
1897 | srcsetValue : String -> Either String SrcsetValue
1899 | let srcset = words val in
1901 | (url :: []) => pure $
MkSrcset !(urlValue url) Nothing
1902 | (url :: descriptor :: []) => pure $
MkSrcset
1903 | !(urlValue url) (Just $
1904 | !(descriptorValue descriptor))
1906 | Left "ERROR: \"\{val}\" is not a valid **srcset** value."
1909 | srcsetValues : String -> Either String (List1 SrcsetValue)
1911 | let vals = map trim $
split (== ',') val in
1917 | -> {auto 0 prf : IsRight (srcsetValues str)}
1919 | fromString str = fromRight (srcsetValues str)
1922 | toString : (List1 SrcsetValue) -> String
1923 | toString (x ::: []) = show x
1924 | toString (x ::: xs) = show x ++ ", " ++ toString' xs
1926 | toString' : List SrcsetValue -> String
1928 | toString' (x :: []) = show x
1929 | toString' (x :: xs) = show x ++ ", " ++ toString' xs
1944 | show Parent = "_parent"
1947 | show (XmlName str) = str
1950 | targetValue : String -> Either String TargetValue
1951 | targetValue "_self" = Right Self
1952 | targetValue "_parent" = Right Parent
1953 | targetValue "_top" = Right Top
1954 | targetValue "_blank" = Right Blank
1956 | targetValue str = if strSubstr 0 0 str /= "_"
1957 | then Right (XmlName str)
1959 | "ERROR: \"\{str}\" provided. The accepted "
1960 | <+> "values for the **target** attribute are "
1961 | <+> "\"_self\", \"_parent\", \"_top\", \"_blank\", "
1962 | <+> "or a valid XML name not starting with \"_\"."
1964 | %runElab mkFromStringFunc "TargetValue"
1968 | namespace PositiveFloat
1971 | PositiveDouble : Double -> Bool
1972 | PositiveDouble x = x > 0
1976 | record PositiveFloat where
1977 | constructor MkPositiveFloat
1981 | {auto 0 prf : Holds PositiveDouble value}
1984 | Show PositiveFloat where
1985 | show (MkPositiveFloat value) = show value
1989 | maybe_positiveFloat : Double -> Maybe PositiveFloat
1990 | maybe_positiveFloat x =
1991 | case hdec0 {p = Holds PositiveDouble} x of
1992 | Just0 _ => Just (MkPositiveFloat x)
1996 | Num PositiveFloat where
1997 | x + y = case maybe_positiveFloat (x.value + y.value) of
1999 | Nothing => MkPositiveFloat 1.0
2001 | x * y = case maybe_positiveFloat (x.value * y.value) of
2003 | Nothing => MkPositiveFloat 1.0
2005 | fromInteger x = case maybe_positiveFloat (fromInteger x) of
2007 | Nothing => MkPositiveFloat 1.0
2010 | positiveFloatValue : Double -> Either String PositiveFloat
2012 | case maybe_positiveFloat x of
2014 | Nothing => Left "ERROR: \{show x} is not a positive value."
2017 | fromDouble : (x : Double)
2018 | -> {auto 0 prf : IsRight (positiveFloatValue x)}
2020 | fromDouble x = fromRight (positiveFloatValue x)
2031 | {auto 0 prf : GreaterThan 0 value}
2035 | show (MkPositive value) = show value
2039 | maybe_positive : Integer -> Maybe Positive
2041 | case hdec0 {p = GreaterThan 0} x of
2042 | Just0 _ => Just (MkPositive x)
2046 | positiveValue : Integer -> Either String Positive
2048 | case maybe_positive x of
2050 | Nothing => Left "ERROR: \{show x} is not a positive value."
2053 | fromInteger : (x : Integer)
2054 | -> {auto 0 prf : IsRight (positiveValue x)}
2056 | fromInteger x = fromRight (positiveValue x)