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

Commit

Permalink
Use common types for date/time (#76)
Browse files Browse the repository at this point in the history
* Use common types for date/time

* Restore `T` in DateTime pretty printing
  • Loading branch information
garyb authored Mar 13, 2017
1 parent 1778943 commit d89cc23
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 276 deletions.
21 changes: 11 additions & 10 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
],
"dependencies": {
"purescript-const": "^2.0.0",
"purescript-functors": "^1.0.0",
"purescript-lists": "^3.2.0",
"purescript-parsing": "^3.0.0",
"purescript-partial": "^1.1.2",
"purescript-precise": "^1.0.0",
"purescript-prelude": "^2.1.0",
"purescript-sets": "^2.0.0",
"purescript-strings": "^2.0.2",
"purescript-strongcheck": "^2.0.0",
"purescript-validation": "^2.0.0"
"purescript-functors": "^1.1.0",
"purescript-lists": "^3.4.0",
"purescript-parsing": "^3.2.1",
"purescript-partial": "^1.2.0",
"purescript-precise": "^1.1.0",
"purescript-prelude": "^2.5.0",
"purescript-sets": "^2.0.1",
"purescript-strings": "^2.1.0",
"purescript-strongcheck": "^2.1.0",
"purescript-validation": "^2.0.0",
"purescript-datetime": "^2.2.0"
}
}
39 changes: 24 additions & 15 deletions src/Text/Markdown/SlamDown/Parser/Inline.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import Control.Lazy as Lazy
import Data.Array as A
import Data.Bifunctor (lmap)
import Data.Const (Const(..))
import Data.DateTime as DT
import Data.Either (Either(..))
import Data.Enum (toEnum)
import Data.Foldable (elem)
import Data.Functor.Compose (Compose(..))
import Data.HugeNum as HN
Expand Down Expand Up @@ -291,19 +293,19 @@ inlines = L.many inline2 <* PS.eof
M.Nothing
pure $ Left case template of
SD.DateTime SD.Minutes _ →
"Incorrect datetime default value, please use \"YYYY-MM-DD HH:mm\" or \"YYYY-MM-DDTHH:mm\" format"
"Invalid datetime default value, please use \"YYYY-MM-DD HH:mm\" format"
SD.DateTime SD.Seconds _ →
"Incorrect datetime default value, please use \"YYYY-MM-DD HH:mm:ss\" or \"YYYY-MM-DDTHH:mm:ss\" format"
"Invalid datetime default value, please use \"YYYY-MM-DD HH:mm:ss\" format"
SD.Date _ →
"Incorrect date default value, please use \"YYYY-MM-DD\" format"
"Invalid date default value, please use \"YYYY-MM-DD\" format"
SD.Time SD.Minutes _ →
"Incorrect time default value, please use \"HH:mm\" format"
"Invalid time default value, please use \"HH:mm\" format"
SD.Time SD.Seconds _ →
"Incorrect time default value, please use \"HH:mm:ss\" format"
"Invalid time default value, please use \"HH:mm:ss\" format"
SD.Numeric _ →
"Incorrect numeric default value"
"Invalid numeric default value"
SD.PlainText _ →
"Incorrect default value"
"Invalid default value"

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

where
parseDateTimeValue SD.TimePrecision P.Parser String DT.DateTime
parseDateTimeValue prec = do
date ← parseDateValue
(PC.try $ void $ PS.string "T") <|> PU.skipSpaces
time ← parseTimeValue prec
pure { date, time }
pure $ DT.DateTime date time

parseDateValue P.Parser String DT.Date
parseDateValue = do
year ← parseYear
PU.skipSpaces *> dash *> PU.skipSpaces
month ← natural
when (month > 12) $ P.fail "Incorrect month"
when (month > 12) $ P.fail "Invalid month"
PU.skipSpaces *> dash *> PU.skipSpaces
day ← natural
when (day > 31) $ P.fail "Incorrect day"
pure { month, day, year }
when (day > 31) $ P.fail "Invalid day"
case DT.canonicalDate <$> toEnum year <*> toEnum month <*> toEnum day of
M.NothingP.fail "Invalid date"
M.Just dt → pure dt

parseTimeValue SD.TimePrecision P.Parser String DT.Time
parseTimeValue prec = do
hours ← natural
when (hours > 23) $ P.fail "Incorrect hours"
when (hours > 23) $ P.fail "Invalid hours"
PU.skipSpaces *> colon *> PU.skipSpaces
minutes ← natural
when (minutes > 59) $ P.fail "Incorrect minutes"
when (minutes > 59) $ P.fail "Invalid minutes"
seconds ← case prec of
SD.Minutes -> do
scolon ← PC.try $ PC.optionMaybe $ PU.skipSpaces *> colon
Expand All @@ -449,7 +456,7 @@ parseTextBox isPlainText eta template =
SD.Seconds -> do
PU.skipSpaces *> colon *> PU.skipSpaces
secs ← natural
when (secs > 59) $ P.fail "Incorrect seconds"
when (secs > 59) $ P.fail "Invalid seconds"
PU.skipSpaces
pure $ M.Just secs
PU.skipSpaces
Expand All @@ -466,7 +473,9 @@ parseTextBox isPlainText eta template =
else if isAM && hours == 12
then 0
else hours
pure { hours : hours', minutes, seconds }
case DT.Time <$> toEnum hours' <*> toEnum minutes <*> toEnum (M.fromMaybe 0 seconds) <*> pure bottom of
M.NothingP.fail "Invalid time"
M.Just t → pure t

parseNumericValue = do
sign ← PC.try (-1 <$ PS.char '-') <|> pure 1
Expand Down
32 changes: 17 additions & 15 deletions src/Text/Markdown/SlamDown/Pretty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ module Text.Markdown.SlamDown.Pretty
import Prelude

import Data.Array as A
import Data.DateTime as DT
import Data.Foldable (fold, elem)
import Data.Functor.Compose (Compose)
import Data.HugeNum as HN
import Data.Identity (Identity(..))
import Data.List as L
import Data.Maybe as M
import Data.Enum (fromEnum)
import Data.Monoid (mempty)
import Data.String as S
import Data.Newtype (unwrap)
import Data.String as S
import Data.Unfoldable as U

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

prettyPrintDate SD.DateValue String
prettyPrintDate { day, month, year } =
printIntPadded 4 year
prettyPrintDate DT.Date String
prettyPrintDate d =
printIntPadded 4 (fromEnum $ DT.year d)
<> "-"
<> printIntPadded 2 month
<> printIntPadded 2 (fromEnum $ DT.month d)
<> "-"
<> printIntPadded 2 day
<> printIntPadded 2 (fromEnum $ DT.day d)

prettyPrintTime SD.TimePrecision SD.TimeValue String
prettyPrintTime prec { hours, minutes, seconds }=
printIntPadded 2 hours
prettyPrintTime SD.TimePrecision DT.Time String
prettyPrintTime prec t =
printIntPadded 2 (fromEnum $ DT.hour t)
<> ":"
<> printIntPadded 2 minutes
<> printIntPadded 2 (fromEnum $ DT.minute t)
<> case prec of
SD.Seconds -> ":" <> printIntPadded 2 (M.fromMaybe 0 seconds)
SD.Seconds -> ":" <> printIntPadded 2 (fromEnum $ DT.second t)
_ -> ""

prettyPrintDateTime SD.TimePrecision SD.DateTimeValue String
prettyPrintDateTime prec { date, time } =
prettyPrintDate date
prettyPrintDateTime SD.TimePrecision DT.DateTime String
prettyPrintDateTime prec dt =
prettyPrintDate (DT.date dt)
<> "T"
<> prettyPrintTime prec time
<> prettyPrintTime prec (DT.time dt)

printIntPadded Int Int String
printIntPadded l i =
Expand Down
3 changes: 1 addition & 2 deletions src/Text/Markdown/SlamDown/Syntax/Inline.purs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ instance showInline ∷ (Show a) ⇒ Show (Inline a) where
show (FormField l r f) = "(FormField " <> show l <> " " <> show r <> " " <> show f <> ")"

derive instance eqInline ∷ (Eq a, Ord a) Eq (Inline a)
derive instance ordInline(Ord a) Ord (Inline a)
derive instance ordInlineOrd a Ord (Inline a)

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


data LinkTarget
= InlineLink String
| ReferenceLink (M.Maybe String)
Expand Down
Loading

0 comments on commit d89cc23

Please sign in to comment.