A static site generator written in Haskell
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. module Utils
  2. ( basename
  3. , errorsAndResults
  4. , escapeHTML
  5. , lookupMeta'
  6. , makeFullFilePath
  7. , sortByDate
  8. )
  9. where
  10. import Data.Aeson ( toJSON
  11. , Value
  12. )
  13. import Data.List
  14. import Data.Ord
  15. import qualified Data.Text as T
  16. import System.Directory
  17. import System.FilePath
  18. import Text.Pandoc
  19. basename :: FilePath -> String
  20. basename = dropExtension . takeFileName
  21. errorsAndResults
  22. :: [(FilePath, Either PandocError a)] -> ([PandocError], [(FilePath, a)])
  23. errorsAndResults xs =
  24. let errors = [ error | result@(_, Left error) <- xs ]
  25. results = [ (path, a) | result@(path, Right a) <- xs ]
  26. in (errors, results)
  27. lookupMeta' :: String -> Meta -> T.Text
  28. lookupMeta' key meta = case lookupMeta key meta of
  29. Just value -> renderMeta value
  30. Nothing -> T.empty
  31. makeFullFilePath :: FilePath -> FilePath -> IO FilePath
  32. makeFullFilePath root path = makeAbsolute $ joinPath [root, path]
  33. renderMeta :: MetaValue -> T.Text
  34. renderMeta (MetaInlines meta) =
  35. case runPure $ writePlain (def Nothing) (Pandoc nullMeta [Plain meta]) of
  36. Left err -> T.empty
  37. Right text -> text
  38. getDate :: Pandoc -> T.Text
  39. getDate (Pandoc m _) = lookupMeta' "date" m
  40. sortByDate :: [(FilePath, Pandoc)] -> [(FilePath, Pandoc)]
  41. sortByDate = sortBy (flip (comparing $ getDate . snd))
  42. escapeHTML :: String -> String
  43. escapeHTML = concatMap f
  44. where
  45. f '>' = "&gt;"
  46. f '<' = "&lt;"
  47. f '&' = "&amp;"
  48. f '\"' = "&quot;"
  49. f '\'' = "&#39;"
  50. f x = [x]