Skip to content

Commit

Permalink
Add Text.Spintax.RandomPhrase module
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelBoucey committed Nov 21, 2024
1 parent 157fcb8 commit 31a2237
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
33 changes: 19 additions & 14 deletions .github/workflows/haskell-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
#
# For more information, see https://github.com/haskell-CI/haskell-ci
#
# version: 0.19.20240708
# version: 0.19.20241121
#
# REGENDATA ("0.19.20240708",["github","Spintax.cabal"])
# REGENDATA ("0.19.20241121",["github","Spintax.cabal"])
#
name: Haskell-CI
on:
Expand Down Expand Up @@ -70,15 +70,29 @@ jobs:
allow-failure: false
fail-fast: false
steps:
- name: apt
- name: apt-get install
run: |
apt-get update
apt-get install -y --no-install-recommends gnupg ca-certificates dirmngr curl git software-properties-common libtinfo5
- name: Install GHCup
run: |
mkdir -p "$HOME/.ghcup/bin"
curl -sL https://downloads.haskell.org/ghcup/0.1.30.0/x86_64-linux-ghcup-0.1.30.0 > "$HOME/.ghcup/bin/ghcup"
chmod a+x "$HOME/.ghcup/bin/ghcup"
"$HOME/.ghcup/bin/ghcup" install ghc "$HCVER" || (cat "$HOME"/.ghcup/logs/*.* && false)
- name: Install cabal-install
run: |
"$HOME/.ghcup/bin/ghcup" install cabal 3.12.1.0 || (cat "$HOME"/.ghcup/logs/*.* && false)
echo "CABAL=$HOME/.ghcup/bin/cabal-3.12.1.0 -vnormal+nowrap" >> "$GITHUB_ENV"
- name: Install GHC (GHCup)
if: matrix.setup-method == 'ghcup'
run: |
"$HOME/.ghcup/bin/ghcup" install ghc "$HCVER" || (cat "$HOME"/.ghcup/logs/*.* && false)
HC=$("$HOME/.ghcup/bin/ghcup" whereis ghc "$HCVER")
HCPKG=$(echo "$HC" | sed 's#ghc$#ghc-pkg#')
HADDOCK=$(echo "$HC" | sed 's#ghc$#haddock#')
echo "HC=$HC" >> "$GITHUB_ENV"
echo "HCPKG=$HCPKG" >> "$GITHUB_ENV"
echo "HADDOCK=$HADDOCK" >> "$GITHUB_ENV"
env:
HCKIND: ${{ matrix.compilerKind }}
HCNAME: ${{ matrix.compiler }}
Expand All @@ -89,21 +103,12 @@ jobs:
echo "LANG=C.UTF-8" >> "$GITHUB_ENV"
echo "CABAL_DIR=$HOME/.cabal" >> "$GITHUB_ENV"
echo "CABAL_CONFIG=$HOME/.cabal/config" >> "$GITHUB_ENV"
HCDIR=/opt/$HCKIND/$HCVER
HC=$("$HOME/.ghcup/bin/ghcup" whereis ghc "$HCVER")
HCPKG=$(echo "$HC" | sed 's#ghc$#ghc-pkg#')
HADDOCK=$(echo "$HC" | sed 's#ghc$#haddock#')
echo "HC=$HC" >> "$GITHUB_ENV"
echo "HCPKG=$HCPKG" >> "$GITHUB_ENV"
echo "HADDOCK=$HADDOCK" >> "$GITHUB_ENV"
echo "CABAL=$HOME/.ghcup/bin/cabal-3.12.1.0 -vnormal+nowrap" >> "$GITHUB_ENV"
HCNUMVER=$(${HC} --numeric-version|perl -ne '/^(\d+)\.(\d+)\.(\d+)(\.(\d+))?$/; print(10000 * $1 + 100 * $2 + ($3 == 0 ? $5 != 1 : $3))')
echo "HCNUMVER=$HCNUMVER" >> "$GITHUB_ENV"
echo "ARG_TESTS=--enable-tests" >> "$GITHUB_ENV"
echo "ARG_BENCH=--enable-benchmarks" >> "$GITHUB_ENV"
echo "HEADHACKAGE=false" >> "$GITHUB_ENV"
echo "ARG_COMPILER=--$HCKIND --with-compiler=$HC" >> "$GITHUB_ENV"
echo "GHCJSARITH=0" >> "$GITHUB_ENV"
env:
HCKIND: ${{ matrix.compilerKind }}
HCNAME: ${{ matrix.compiler }}
Expand Down Expand Up @@ -216,8 +221,8 @@ jobs:
rm -f cabal.project.local
$CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks all
- name: save cache
uses: actions/cache/save@v4
if: always()
uses: actions/cache/save@v4
with:
key: ${{ runner.os }}-${{ matrix.compiler }}-${{ github.sha }}
path: ~/.cabal/store
5 changes: 3 additions & 2 deletions Spintax.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Spintax
version: 0.3.6.1
version: 0.3.7.0
synopsis: Random text generation based on spintax
description: Random text generation based on spintax with nested alternatives and empty options.
homepage: https://github.com/MichelBoucey/spintax
Expand All @@ -15,8 +15,9 @@ cabal-version: >=1.10
Tested-With: GHC ==8.8.4 || ==8.10.7 || ==9.0.2 || ==9.2.8 || ==9.4.8 || ==9.6.3 || ==9.8.2 || ==9.10.1

library
hs-source-dirs: src
hs-source-dirs: src/
exposed-modules: Text.Spintax
, Text.Spintax.RandomPhrase
build-depends: attoparsec >= 0.12.1.6 && < 0.15
, base >= 4.7 && < 5
, extra >= 1.4.3 && < 1.9
Expand Down
15 changes: 14 additions & 1 deletion src/Text/Spintax.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}

module Text.Spintax (spintax) where
module Text.Spintax where

import Control.Applicative ((<|>))
import Control.Monad.Reader (ask, runReaderT)
import Data.Attoparsec.Text
import Data.List as L (intersperse)
import qualified Data.List.Extra as E
import qualified Data.Text as T
import System.Random.MWC
Expand Down Expand Up @@ -84,3 +85,15 @@ spintax template =
ctt '|' = False
ctt _ = True

-- * Utils

-- | Write a spintax alternative
--
-- >λ> writeSpintaxAlternative ["apple","apricot","banana","coconut"]
-- "{apple|apricot|banana|coconut}"
writeSpintaxAlternative :: [T.Text] -> T.Text
writeSpintaxAlternative = writeSpintaxExpression "|"

writeSpintaxExpression :: T.Text -> [T.Text] -> T.Text
writeSpintaxExpression s l = "{" <> T.concat (L.intersperse s l) <> "}"

15 changes: 15 additions & 0 deletions src/Text/Spintax/RandomPhrase.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Text.Spintax.RandomPhrase where

import qualified Data.Text as T
import Text.Spintax

type RandomPhrase = T.Text

-- | Generate random passphrase or unique id
--
-- >λ> randomPhrase "-" [["blacky","monk","gillespie","coltrane"],["apple","apricot","banana","coconut"],["kant","hegel","husserl","habermas"]]
-- > Right "coltrane-coconut-kant"
--
randomPhrase :: T.Text -> [[T.Text]] -> IO (Either String RandomPhrase)
randomPhrase s ls = spintax $ writeSpintaxExpression s $ writeSpintaxAlternative <$> ls

0 comments on commit 31a2237

Please sign in to comment.