module Web.Site.Compilers
(
haskellCompiler,
mathReaderWith,
mathWriterWith,
getTocOptionsWith,
siteContext,
cleanupIndexUrl,
)
where
import Data.ByteString.Lazy (ByteString)
import Hakyll
import Text.Pandoc (compileTemplate, runPure, runWithDefaultPartials)
import Text.Pandoc.Options
haskellCompiler ::
[String] ->
Compiler (Item ByteString)
haskellCompiler :: [String] -> Compiler (Item ByteString)
haskellCompiler [String]
args = do
file <- Compiler String
getResourceFilePath
emptyItem >>= withItemBody (run file)
where
run :: String -> ByteString -> Compiler ByteString
run String
f = String -> [String] -> ByteString -> Compiler ByteString
unixFilterLBS String
"runhaskell" ([String] -> ByteString -> Compiler ByteString)
-> [String] -> ByteString -> Compiler ByteString
forall a b. (a -> b) -> a -> b
$ [[String]] -> [String]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[String]
defaultArgs, [String]
args, [String
f]]
defaultArgs :: [String]
defaultArgs = [String
"-XGHC2021", String
"-XOverloadedStrings"]
emptyItem :: Compiler (Item ByteString)
emptyItem = ByteString -> Compiler (Item ByteString)
forall a. a -> Compiler (Item a)
makeItem ByteString
""
mathReaderWith :: ReaderOptions -> ReaderOptions
mathReaderWith :: ReaderOptions -> ReaderOptions
mathReaderWith ReaderOptions
options =
ReaderOptions
options
{ readerExtensions =
readerExtensions options
<> extensionsFromList
[ Ext_tex_math_single_backslash,
Ext_tex_math_double_backslash,
Ext_tex_math_dollars,
Ext_latex_macros
]
}
mathWriterWith :: WriterOptions -> WriterOptions
mathWriterWith :: WriterOptions -> WriterOptions
mathWriterWith WriterOptions
options =
WriterOptions
options
{
writerHTMLMathMethod = MathJax ""
}
getTocOptionsWith :: WriterOptions -> Compiler WriterOptions
getTocOptionsWith :: WriterOptions -> Compiler WriterOptions
getTocOptionsWith WriterOptions
options = do
identifier <- Compiler Identifier
getUnderlying
tocField <- getMetadataField identifier "toc"
return $ getOptions tocField
where
getOptions :: Maybe String -> WriterOptions
getOptions Maybe String
Nothing = WriterOptions
options
getOptions (Just String
_) =
WriterOptions
options
{ writerTableOfContents = True,
writerTOCDepth = 3,
writerTemplate = tocTemplate
}
tocTemplate :: Maybe (Template Text)
tocTemplate
| Right (Right Template Text
t) <- Text -> Either PandocError (Either String (Template Text))
build Text
templateSource = Template Text -> Maybe (Template Text)
forall a. a -> Maybe a
Just Template Text
t
| Bool
otherwise = Maybe (Template Text)
forall a. Maybe a
Nothing
build :: Text -> Either PandocError (Either String (Template Text))
build = PandocPure (Either String (Template Text))
-> Either PandocError (Either String (Template Text))
forall a. PandocPure a -> Either PandocError a
runPure (PandocPure (Either String (Template Text))
-> Either PandocError (Either String (Template Text)))
-> (Text -> PandocPure (Either String (Template Text)))
-> Text
-> Either PandocError (Either String (Template Text))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WithDefaultPartials PandocPure (Either String (Template Text))
-> PandocPure (Either String (Template Text))
forall (m :: * -> *) a. WithDefaultPartials m a -> m a
runWithDefaultPartials (WithDefaultPartials PandocPure (Either String (Template Text))
-> PandocPure (Either String (Template Text)))
-> (Text
-> WithDefaultPartials PandocPure (Either String (Template Text)))
-> Text
-> PandocPure (Either String (Template Text))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String
-> Text
-> WithDefaultPartials PandocPure (Either String (Template Text))
forall (m :: * -> *) a.
(TemplateMonad m, TemplateTarget a) =>
String -> Text -> m (Either String (Template a))
compileTemplate String
""
templateSource :: Text
templateSource = Text
"<nav class='toc'><h2>Contents</h2>\n$toc$\n</nav>\n$body$"
siteContext :: Context String
siteContext :: Context String
siteContext = String -> (Item String -> Compiler String) -> Context String
forall a. String -> (Item a -> Compiler String) -> Context a
field String
"url" Item String -> Compiler String
forall {a}. Item a -> Compiler String
clean Context String -> Context String -> Context String
forall a. Semigroup a => a -> a -> a
<> Context String
defaultContext
where
clean :: Item a -> Compiler String
clean Item a
item = do
path <- Identifier -> Compiler (Maybe String)
getRoute (Item a -> Identifier
forall a. Item a -> Identifier
itemIdentifier Item a
item)
case path of
Maybe String
Nothing -> String -> Compiler String
forall a. String -> Compiler a
noResult String
"no route for identifier"
Just String
s -> String -> Compiler String
forall a. a -> Compiler a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> Compiler String)
-> (String -> String) -> String -> Compiler String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
cleanupIndexUrl (String -> String) -> (String -> String) -> String -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> String
toUrl (String -> Compiler String) -> String -> Compiler String
forall a b. (a -> b) -> a -> b
$ String
s
cleanupIndexUrl :: String -> String
cleanupIndexUrl :: String -> String
cleanupIndexUrl url :: String
url@(Char
'/' : String
_)
| Maybe String
Nothing <- Maybe String
prefix = String
url
| Just String
s <- Maybe String
prefix = String
s
where
prefix :: Maybe String
prefix = String -> String -> Maybe String
needlePrefix String
"index.html" String
url
cleanupIndexUrl String
url = String
url