<commit_before>{-# LANGUAGE OverloadedStrings, TypeOperators, KindSignatures, GADTs, MultiParamTypeClasses, ScopedTypeVariables, StandaloneDeriving #-}

module Pages.Utils where

import           Data.String
import qualified Data.Text as Text
import           Data.Text (Text)
import           Data.Text.Lazy.Encoding
import qualified Data.Text.Lazy as LT
import qualified Data.Text.Lazy.IO as LTIO
import           Data.Text.Template

import           Network.CGI

import           Paths_thermopolis

class Monad f => ContentReader f where 
 readFileC :: FilePath -> f LT.Text

instance ContentReader IO where
 readFileC fileName = do
--         print fileName
        dir <- getDataDir
        LTIO.readFile $ dir ++ "/include/" ++ fileName

newtype Page = Page LT.Text

instance IsString Page where
  fromString = Page . fromString

-- This uses a call-by-value semantics for the Pages, aka the 
-- string interpretation is done before injecting into a page.
readPage :: ContentReader f => FilePath -> [(Text,Page)] -> f Page
readPage filePath env = do
        f <- readFileC filePath
        Page <$> substituteA (LT.toStrict f) context
  where 
        context nm = case lookup nm env of
            Just (Page f) -> return $ LT.toStrict $ f
            Nothing       -> fail $ "readPage " ++ show filePath ++ ", can not find var " ++ show nm

outputPage :: MonadCGI m => Page -> m CGIResult
outputPage (Page v) = outputFPS $ encodeUtf8 $ v

<commit_msg>Use config always.<commit_after>{-# LANGUAGE OverloadedStrings, TypeOperators, KindSignatures, GADTs, MultiParamTypeClasses, ScopedTypeVariables, StandaloneDeriving #-}

module Pages.Utils where

import           Config

import           Data.String
import qualified Data.Text as Text
import           Data.Text (Text)
import           Data.Text.Lazy.Encoding
import qualified Data.Text.Lazy as LT
import qualified Data.Text.Lazy.IO as LTIO
import           Data.Text.Template

import           Network.CGI

import           Paths_thermopolis

class Monad f => ContentReader f where 
 readFileC :: FilePath -> f LT.Text

instance ContentReader IO where
 readFileC fileName = do
--         print fileName
        dir <- getDataDir
        LTIO.readFile $ dir ++ "/include/" ++ fileName

newtype Page = Page LT.Text

instance IsString Page where
  fromString = Page . fromString

-- This uses a call-by-value semantics for the Pages, aka the 
-- string interpretation is done before injecting into a page.
readPage :: ContentReader f => FilePath -> [(Text,Page)] -> f Page
readPage filePath env = do
        f <- readFileC filePath
        Page <$> substituteA (LT.toStrict f) context
  where 
        context nm = case lookup nm (env  ++ baseEnv) of
            Just (Page f) -> return $ LT.toStrict $ f
            Nothing       -> fail $ "readPage " ++ show filePath ++ ", can not find var " ++ show nm
        baseEnv = [("webRoot",fromString (webRoot config))]

outputPage :: MonadCGI m => Page -> m CGIResult
outputPage (Page v) = outputFPS $ encodeUtf8 $ v


{'fraction_non_alphanumeric': 0.08504301786896096, 'fraction_numerical': 0.0006618133686300463, 'mean_word_length': 2.70920245398773, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2085195', 'n_tokens_mistral': 888, 'n_tokens_neox': 848, 'n_words': 311}