<commit_before>{-# LANGUAGE OverloadedStrings #-}

module Data.Mole.Builder.JavaScript where


import qualified Data.Set as S
import qualified Data.Map as M
import           Data.Map (Map)

import           Data.Maybe
import           Data.Monoid

import qualified Data.Text          as T
import           Data.ByteString.Char8 (pack)

import           Data.Mole.Types
import           Data.Mole.Builder.Internal.Fingerprint
import           Data.Mole.Builder.Internal.Template



javascriptBuilder :: String -> String -> Handle -> AssetId -> IO Builder
javascriptBuilder pubId src _ _ = do
    body <- readFile src

    let t@(Template fragments) = template [("__assetUrl(\"", "\")")] body
    let deps = catMaybes $ (flip map) fragments $ \f -> case f of
                    (Lit _) -> Nothing
                    (Var _ x) -> Just $ AssetId (T.unpack x)

    return $ Builder
        { assetSources      = S.singleton src
        , assetDependencies = S.fromList deps
        , packageAsset      = r t
        , sourceFingerprint = pack body
        }

  where
    r :: Template -> Map AssetId String -> Either Error Result
    r t m = do
        body <- render t $ \(a,b) x -> do
              case M.lookup (AssetId (T.unpack x)) m of
                   Nothing -> Left (UndeclaredDependency (AssetId (T.unpack x)))
                   Just v -> Right $ a <> T.pack v <> b

        return $ Result (fingerprint (pack body) pubId) $ Just (pack body, "text/javascript")<commit_msg>Use Text instaed of BS8 to encode JavaScript contents<commit_after>{-# LANGUAGE OverloadedStrings #-}

module Data.Mole.Builder.JavaScript where


import qualified Data.Set as S
import qualified Data.Map as M
import           Data.Map (Map)

import           Data.Maybe
import           Data.Monoid

import qualified Data.Text          as T
import qualified Data.Text.Encoding as T

import           Data.Mole.Types
import           Data.Mole.Builder.Internal.Fingerprint
import           Data.Mole.Builder.Internal.Template



javascriptBuilder :: String -> String -> Handle -> AssetId -> IO Builder
javascriptBuilder pubId src _ _ = do
    body <- readFile src

    let t@(Template fragments) = template [("__assetUrl(\"", "\")")] body
    let deps = catMaybes $ (flip map) fragments $ \f -> case f of
                    (Lit _) -> Nothing
                    (Var _ x) -> Just $ AssetId (T.unpack x)

    return $ Builder
        { assetSources      = S.singleton src
        , assetDependencies = S.fromList deps
        , packageAsset      = r t
        , sourceFingerprint = T.encodeUtf8 $ T.pack body
        }

  where
    r :: Template -> Map AssetId String -> Either Error Result
    r t m = do
        body <- render t $ \(a,b) x -> do
              case M.lookup (AssetId (T.unpack x)) m of
                   Nothing -> Left (UndeclaredDependency (AssetId (T.unpack x)))
                   Just v -> Right $ a <> T.pack v <> b

        let body' = T.encodeUtf8 $ T.pack $ body
        return $ Result (fingerprint body' pubId) $ Just (body', "text/javascript")
{'fraction_non_alphanumeric': 0.09977101733725875, 'fraction_numerical': 0.0013084723585214263, 'mean_word_length': 1.9545893719806764, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 31, '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': '6942049', 'n_tokens_mistral': 895, 'n_tokens_neox': 846, 'n_words': 289}