Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit d89cc23

Browse files
authored
Use common types for date/time (#76)
* Use common types for date/time * Restore `T` in DateTime pretty printing
1 parent 1778943 commit d89cc23

File tree

6 files changed

+115
-276
lines changed

6 files changed

+115
-276
lines changed

bower.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
],
2424
"dependencies": {
2525
"purescript-const": "^2.0.0",
26-
"purescript-functors": "^1.0.0",
27-
"purescript-lists": "^3.2.0",
28-
"purescript-parsing": "^3.0.0",
29-
"purescript-partial": "^1.1.2",
30-
"purescript-precise": "^1.0.0",
31-
"purescript-prelude": "^2.1.0",
32-
"purescript-sets": "^2.0.0",
33-
"purescript-strings": "^2.0.2",
34-
"purescript-strongcheck": "^2.0.0",
35-
"purescript-validation": "^2.0.0"
26+
"purescript-functors": "^1.1.0",
27+
"purescript-lists": "^3.4.0",
28+
"purescript-parsing": "^3.2.1",
29+
"purescript-partial": "^1.2.0",
30+
"purescript-precise": "^1.1.0",
31+
"purescript-prelude": "^2.5.0",
32+
"purescript-sets": "^2.0.1",
33+
"purescript-strings": "^2.1.0",
34+
"purescript-strongcheck": "^2.1.0",
35+
"purescript-validation": "^2.0.0",
36+
"purescript-datetime": "^2.2.0"
3637
}
3738
}

src/Text/Markdown/SlamDown/Parser/Inline.purs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ import Control.Lazy as Lazy
1313
import Data.Array as A
1414
import Data.Bifunctor (lmap)
1515
import Data.Const (Const(..))
16+
import Data.DateTime as DT
1617
import Data.Either (Either(..))
18+
import Data.Enum (toEnum)
1719
import Data.Foldable (elem)
1820
import Data.Functor.Compose (Compose(..))
1921
import Data.HugeNum as HN
@@ -291,19 +293,19 @@ inlines = L.many inline2 <* PS.eof
291293
M.Nothing
292294
pure $ Left case template of
293295
SD.DateTime SD.Minutes _ →
294-
"Incorrect datetime default value, please use \"YYYY-MM-DD HH:mm\" or \"YYYY-MM-DDTHH:mm\" format"
296+
"Invalid datetime default value, please use \"YYYY-MM-DD HH:mm\" format"
295297
SD.DateTime SD.Seconds _ →
296-
"Incorrect datetime default value, please use \"YYYY-MM-DD HH:mm:ss\" or \"YYYY-MM-DDTHH:mm:ss\" format"
298+
"Invalid datetime default value, please use \"YYYY-MM-DD HH:mm:ss\" format"
297299
SD.Date _ →
298-
"Incorrect date default value, please use \"YYYY-MM-DD\" format"
300+
"Invalid date default value, please use \"YYYY-MM-DD\" format"
299301
SD.Time SD.Minutes _ →
300-
"Incorrect time default value, please use \"HH:mm\" format"
302+
"Invalid time default value, please use \"HH:mm\" format"
301303
SD.Time SD.Seconds _ →
302-
"Incorrect time default value, please use \"HH:mm:ss\" format"
304+
"Invalid time default value, please use \"HH:mm:ss\" format"
303305
SD.Numeric _ →
304-
"Incorrect numeric default value"
306+
"Invalid numeric default value"
305307
SD.PlainText _ →
306-
"Incorrect default value"
308+
"Invalid default value"
307309

308310
parseTextBoxTemplate P.Parser String (SD.TextBox (Const Unit))
309311
parseTextBoxTemplate =
@@ -419,28 +421,33 @@ parseTextBox isPlainText eta template =
419421
SD.PlainText _ → SD.PlainText <$> eta parsePlainTextValue
420422

421423
where
424+
parseDateTimeValue SD.TimePrecision P.Parser String DT.DateTime
422425
parseDateTimeValue prec = do
423426
date ← parseDateValue
424427
(PC.try $ void $ PS.string "T") <|> PU.skipSpaces
425428
time ← parseTimeValue prec
426-
pure { date, time }
429+
pure $ DT.DateTime date time
427430

431+
parseDateValue P.Parser String DT.Date
428432
parseDateValue = do
429433
year ← parseYear
430434
PU.skipSpaces *> dash *> PU.skipSpaces
431435
month ← natural
432-
when (month > 12) $ P.fail "Incorrect month"
436+
when (month > 12) $ P.fail "Invalid month"
433437
PU.skipSpaces *> dash *> PU.skipSpaces
434438
day ← natural
435-
when (day > 31) $ P.fail "Incorrect day"
436-
pure { month, day, year }
439+
when (day > 31) $ P.fail "Invalid day"
440+
case DT.canonicalDate <$> toEnum year <*> toEnum month <*> toEnum day of
441+
M.NothingP.fail "Invalid date"
442+
M.Just dt → pure dt
437443

444+
parseTimeValue SD.TimePrecision P.Parser String DT.Time
438445
parseTimeValue prec = do
439446
hours ← natural
440-
when (hours > 23) $ P.fail "Incorrect hours"
447+
when (hours > 23) $ P.fail "Invalid hours"
441448
PU.skipSpaces *> colon *> PU.skipSpaces
442449
minutes ← natural
443-
when (minutes > 59) $ P.fail "Incorrect minutes"
450+
when (minutes > 59) $ P.fail "Invalid minutes"
444451
seconds ← case prec of
445452
SD.Minutes -> do
446453
scolon ← PC.try $ PC.optionMaybe $ PU.skipSpaces *> colon
@@ -449,7 +456,7 @@ parseTextBox isPlainText eta template =
449456
SD.Seconds -> do
450457
PU.skipSpaces *> colon *> PU.skipSpaces
451458
secs ← natural
452-
when (secs > 59) $ P.fail "Incorrect seconds"
459+
when (secs > 59) $ P.fail "Invalid seconds"
453460
PU.skipSpaces
454461
pure $ M.Just secs
455462
PU.skipSpaces
@@ -466,7 +473,9 @@ parseTextBox isPlainText eta template =
466473
else if isAM && hours == 12
467474
then 0
468475
else hours
469-
pure { hours : hours', minutes, seconds }
476+
case DT.Time <$> toEnum hours' <*> toEnum minutes <*> toEnum (M.fromMaybe 0 seconds) <*> pure bottom of
477+
M.NothingP.fail "Invalid time"
478+
M.Just t → pure t
470479

471480
parseNumericValue = do
472481
sign ← PC.try (-1 <$ PS.char '-') <|> pure 1

src/Text/Markdown/SlamDown/Pretty.purs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ module Text.Markdown.SlamDown.Pretty
66
import Prelude
77

88
import Data.Array as A
9+
import Data.DateTime as DT
910
import Data.Foldable (fold, elem)
1011
import Data.Functor.Compose (Compose)
1112
import Data.HugeNum as HN
1213
import Data.Identity (Identity(..))
1314
import Data.List as L
1415
import Data.Maybe as M
16+
import Data.Enum (fromEnum)
1517
import Data.Monoid (mempty)
16-
import Data.String as S
1718
import Data.Newtype (unwrap)
19+
import Data.String as S
1820
import Data.Unfoldable as U
1921

2022
import Text.Markdown.SlamDown.Syntax as SD
@@ -121,28 +123,28 @@ prettyPrintTextBoxValue t =
121123
SD.Time prec (Identity def) → prettyPrintTime prec def
122124
SD.DateTime prec (Identity def) → prettyPrintDateTime prec def
123125

124-
prettyPrintDate SD.DateValue String
125-
prettyPrintDate { day, month, year } =
126-
printIntPadded 4 year
126+
prettyPrintDate DT.Date String
127+
prettyPrintDate d =
128+
printIntPadded 4 (fromEnum $ DT.year d)
127129
<> "-"
128-
<> printIntPadded 2 month
130+
<> printIntPadded 2 (fromEnum $ DT.month d)
129131
<> "-"
130-
<> printIntPadded 2 day
132+
<> printIntPadded 2 (fromEnum $ DT.day d)
131133

132-
prettyPrintTime SD.TimePrecision SD.TimeValue String
133-
prettyPrintTime prec { hours, minutes, seconds }=
134-
printIntPadded 2 hours
134+
prettyPrintTime SD.TimePrecision DT.Time String
135+
prettyPrintTime prec t =
136+
printIntPadded 2 (fromEnum $ DT.hour t)
135137
<> ":"
136-
<> printIntPadded 2 minutes
138+
<> printIntPadded 2 (fromEnum $ DT.minute t)
137139
<> case prec of
138-
SD.Seconds -> ":" <> printIntPadded 2 (M.fromMaybe 0 seconds)
140+
SD.Seconds -> ":" <> printIntPadded 2 (fromEnum $ DT.second t)
139141
_ -> ""
140142

141-
prettyPrintDateTime SD.TimePrecision SD.DateTimeValue String
142-
prettyPrintDateTime prec { date, time } =
143-
prettyPrintDate date
143+
prettyPrintDateTime SD.TimePrecision DT.DateTime String
144+
prettyPrintDateTime prec dt =
145+
prettyPrintDate (DT.date dt)
144146
<> "T"
145-
<> prettyPrintTime prec time
147+
<> prettyPrintTime prec (DT.time dt)
146148

147149
printIntPadded Int Int String
148150
printIntPadded l i =

src/Text/Markdown/SlamDown/Syntax/Inline.purs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ instance showInline ∷ (Show a) ⇒ Show (Inline a) where
5555
show (FormField l r f) = "(FormField " <> show l <> " " <> show r <> " " <> show f <> ")"
5656

5757
derive instance eqInline ∷ (Eq a, Ord a) Eq (Inline a)
58-
derive instance ordInline(Ord a) Ord (Inline a)
58+
derive instance ordInlineOrd a Ord (Inline a)
5959

6060
-- | Nota bene: this does not generate any recursive structure
6161
instance arbitraryInline ∷ (SCA.Arbitrary a, Eq a) SCA.Arbitrary (Inline a) where
@@ -74,7 +74,6 @@ instance arbitraryInline ∷ (SCA.Arbitrary a, Eq a) ⇒ SCA.Arbitrary (Inline a
7474
9Link L.Nil <$> SCA.arbitrary
7575
_ → Image L.Nil <$> SCA.arbitrary
7676

77-
7877
data LinkTarget
7978
= InlineLink String
8079
| ReferenceLink (M.Maybe String)

0 commit comments

Comments
 (0)