{-# LANGUAGE GHC2021 #-}
module Utils
    ( wordCount
    , readingTime
    , escapeHtml
    ) where

-- | Count the number of words in a string (split on whitespace).
wordCount :: String -> Int
wordCount = length . words

-- | Estimate reading time in minutes (assumes 200 words per minute).
-- Minimum is 1 minute.
readingTime :: String -> Int
readingTime s = max 1 (wordCount s `div` 200)

-- | Escape HTML special characters: <, >, &, ", '.
escapeHtml :: String -> String
escapeHtml = concatMap escChar
  where
    escChar '<'  = "&lt;"
    escChar '>'  = "&gt;"
    escChar '&'  = "&amp;"
    escChar '"'  = "&quot;"
    escChar '\'' = "&#39;"
    escChar c    = [c]
