|
| 1 | +#!/usr/bin/env cabal |
| 2 | +{- cabal: |
| 3 | +build-depends: |
| 4 | + , base >=4.16 |
| 5 | + , bytestring >=0.11 |
| 6 | + , Cabal-syntax ^>=3.10 || ^>=3.12 |
| 7 | + , pandoc ^>=3.6.4 |
| 8 | + , text >=2.1 |
| 9 | +-} |
| 10 | +{-# LANGUAGE LambdaCase #-} |
| 11 | + |
| 12 | +module Main (main) where |
| 13 | + |
| 14 | +import qualified Data.ByteString as BS |
| 15 | +import Data.Text (Text) |
| 16 | +import qualified Data.Text as T |
| 17 | +import qualified Data.Text.IO as TIO |
| 18 | +import Distribution.PackageDescription.Parsec |
| 19 | + (parseGenericPackageDescriptionMaybe) |
| 20 | +import qualified Distribution.Types.GenericPackageDescription as GenericPackageDescription |
| 21 | +import qualified Distribution.Types.PackageDescription as PackageDescription |
| 22 | +import Distribution.Utils.ShortText (fromShortText) |
| 23 | +import System.IO (hPutStrLn, stderr) |
| 24 | +import Text.Pandoc (runIOorExplode) |
| 25 | +import Text.Pandoc.Extensions (githubMarkdownExtensions) |
| 26 | +import Text.Pandoc.Options (ReaderOptions (..), WriterOptions (..), |
| 27 | + def) |
| 28 | +import Text.Pandoc.Readers (readHaddock, readMarkdown) |
| 29 | +import Text.Pandoc.Transforms (headerShift) |
| 30 | +import Text.Pandoc.Writers (writeHaddock) |
| 31 | + |
| 32 | +main :: IO () |
| 33 | +main = do |
| 34 | + putStrLn "Generating prologue.haddock from package description..." |
| 35 | + let readmeHeaderFile = "scripts/generate-readme-header.md" |
| 36 | + readmeHeaderContent <- TIO.readFile readmeHeaderFile |
| 37 | + let lsmTreeCabalFile = "lsm-tree.cabal" |
| 38 | + lsmTreeCabalContent <- BS.readFile lsmTreeCabalFile |
| 39 | + case parseGenericPackageDescriptionMaybe lsmTreeCabalContent of |
| 40 | + Nothing -> hPutStrLn stderr $ "error: Could not parse '" <> lsmTreeCabalFile <> "'" |
| 41 | + Just genericPackageDescription -> do |
| 42 | + let packageDescription = GenericPackageDescription.packageDescription genericPackageDescription |
| 43 | + let description = T.pack . fromShortText $ PackageDescription.description packageDescription |
| 44 | + header <- |
| 45 | + runIOorExplode $ do |
| 46 | + doc <- readMarkdown def{readerExtensions = githubMarkdownExtensions} readmeHeaderContent |
| 47 | + writeHaddock def doc |
| 48 | + let prologue = T.unlines [header, description] |
| 49 | + TIO.writeFile "prologue.haddock" prologue |
0 commit comments