From fd714e93ff0f4f8a43f15713cca566d208d54285 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Fri, 10 Jan 2020 18:58:42 +0100 Subject: [PATCH] Require SJW, generate blog config as a standalone file because it's always nice to have (if you want to expose its content as a read-only API) and handle hablo.js compilation with SJW --- hablo.cabal | 1 + src/JS.hs | 47 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/hablo.cabal b/hablo.cabal index b426c5c..5fa20fd 100644 --- a/hablo.cabal +++ b/hablo.cabal @@ -59,6 +59,7 @@ executable hablo , template >= 0.2.0 && < 0.3 , text >= 1.2.3 && < 1.3 , time >= 1.8.0 && < 1.9 + , SJW >= 0.1.2 && < 0.2 , unix >= 2.7.2 && < 2.8 ghc-options: -Wall -dynamic hs-source-dirs: src diff --git a/src/JS.hs b/src/JS.hs index 22f6372..e5148e1 100644 --- a/src/JS.hs +++ b/src/JS.hs @@ -7,34 +7,51 @@ import Blog (Blog(..), Path(..)) import qualified Blog (get) import Control.Monad.IO.Class (MonadIO(..)) import Control.Monad.Reader (ReaderT) -import Data.ByteString.Lazy (ByteString, concat, readFile, writeFile) +import Data.ByteString.Lazy ( + ByteString, concat, intercalate, fromStrict, readFile, writeFile + ) import Data.ByteString.Lazy.Char8 (pack) -import qualified Files (find) +import Data.Text.Encoding (encodeUtf8) import JSON (exportBlog) import Paths_hablo (getDataDir) import Pretty ((.$)) +import SJW (compile, source, sourceCode) import System.Directory (createDirectoryIfMissing) +import System.Exit (die) import System.FilePath (()) import Prelude hiding (concat, readFile, writeFile) -compile :: [ByteString] -> ByteString -compile sources = concat (header:sources ++ [footer]) +object :: [ByteString] -> ByteString +object sources = concat [header, intercalate ",\n" sources, footer] where - header = "(function() {\n" - footer = "})();" + header = "return {\n" + footer = "\n};" var :: (String, ByteString) -> ByteString -var (varName, content) = concat ["var ", pack varName, " = ", content, ";\n"] +var (varName, content) = concat ["\t", pack varName, " : ", content] -generate :: ReaderT Blog IO () -generate = do - destinationDir <- ( "js") <$> (Blog.get $path.$root) +generateConfig :: FilePath -> ReaderT Blog IO () +generateConfig destinationDir = do blogJSON <- exportBlog remarkablePath <- Blog.get $path.$remarkableConfig liftIO $ do remarkableJSON <- maybe (return "{html: true}") readFile remarkablePath - let jsVars = var <$> [("blog", blogJSON), ("remarkableConfig", remarkableJSON)] - jsFiles <- ( "js") <$> getDataDir >>= Files.find - jsCode <- mapM readFile jsFiles - createDirectoryIfMissing False destinationDir - writeFile (destinationDir "hablo.js") $ compile (jsVars ++ jsCode ) + let jsVars = [("blog", blogJSON), ("remarkableConfig", remarkableJSON)] + writeFile configModule . object $ var <$> jsVars + where + configModule = destinationDir "Hablo" "Config.js" + +generateMain :: FilePath -> IO () +generateMain destinationDir = do + habloSources <- ( "js") <$> getDataDir + result <- compile $ source [destinationDir, "unitJS", habloSources] + maybe (die "JS compilation failed\n") output =<< sourceCode result + where + output = writeFile (destinationDir "hablo.js") . fromStrict . encodeUtf8 + +generate :: ReaderT Blog IO () +generate = do + destinationDir <- ( "js") <$> (Blog.get $path.$root) + liftIO . createDirectoryIfMissing True $ destinationDir "Hablo" + generateConfig destinationDir + liftIO $ generateMain destinationDir