Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include YAML frontmatter in the Stork index #398

Merged
merged 18 commits into from
Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions default/index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,5 @@ js:
emanote:
# Whether to automatically treat folder notes as a folgezettel parent of its contents
folder-folgezettel: true
stork:
frontmatter-handling: omit
10 changes: 8 additions & 2 deletions src/Emanote/Model/Stork.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ where

import Control.Monad.Logger (MonadLoggerIO)
import Data.IxSet.Typed qualified as Ix
import Emanote.Model.Meta (lookupRouteMeta)
import Emanote.Model.Note qualified as N
import Emanote.Model.Stork.Index (File (File), Input (Input), readOrBuildStorkIndex)
import Emanote.Model.Stork.Index (File (File), Handling (Handling_Omit), Input (Input), readOrBuildStorkIndex)
import Emanote.Model.Title qualified as Tit
import Emanote.Model.Type (Model)
import Emanote.Model.Type qualified as M
Expand All @@ -19,7 +20,7 @@ import System.FilePath ((</>))

renderStorkIndex :: (MonadIO m, MonadLoggerIO m) => Model -> m LByteString
renderStorkIndex model = do
readOrBuildStorkIndex (model ^. M.modelStorkIndex) (Input $ storkFiles model)
readOrBuildStorkIndex (model ^. M.modelStorkIndex) (Input (storkFiles model) (frontmatterHandling model))

storkFiles :: Model -> [File]
storkFiles model =
Expand All @@ -29,3 +30,8 @@ storkFiles model =
((baseDir </>) $ R.withLmlRoute R.encodeRoute $ note ^. N.noteRoute)
(SR.siteRouteUrl model $ SR.lmlSiteRoute $ note ^. N.noteRoute)
(Tit.toPlain $ note ^. N.noteTitle)

frontmatterHandling :: Model -> Handling
frontmatterHandling model =
let indexRoute = M.modelIndexRoute model
in lookupRouteMeta Handling_Omit ("emanote" :| ["stork", "frontmatter-handling"]) indexRoute model
54 changes: 49 additions & 5 deletions src/Emanote/Model/Stork/Index.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ module Emanote.Model.Stork.Index
readOrBuildStorkIndex,
File (File),
Input (Input),
Handling (Handling_Ignore, Handling_Omit, Handling_Parse),
)
where

import Control.Monad.Logger (MonadLoggerIO)
import Data.Aeson (FromJSON (parseJSON))
import Data.Aeson qualified as Aeson
import Data.Text qualified as T
import Data.Time (NominalDiffTime, diffUTCTime, getCurrentTime)
import Emanote.Prelude (log, logD, logW)
import Numeric (showGFloat)
import Relude
import System.Process.ByteString (readProcessWithExitCode)
import System.Which (staticWhich)
import Toml (TomlCodec, encode, list, string, text, (.=))
import Toml (Key, TomlCodec, diwrap, encode, list, string, table, text, textBy, (.=))

-- | In-memory Stork index tracked in a @TVar@
newtype IndexVar = IndexVar (TVar (Maybe LByteString))
Expand Down Expand Up @@ -59,7 +62,7 @@ storkBin = $(staticWhich "stork")

runStork :: MonadIO m => Input -> m LByteString
runStork input = do
let storkToml = handleTomlandBug $ Toml.encode inputCodec input
let storkToml = handleTomlandBug $ Toml.encode storkInputCodec $ StorkInput input
(_, !index, _) <-
liftIO $
readProcessWithExitCode
Expand All @@ -79,8 +82,20 @@ runStork input = do
-- title (but why would they?)
T.replace "\\\\U" "\\U"

newtype Input = Input
{ inputFiles :: [File]
data Input = Input
{ inputFiles :: [File],
inputFrontmatterHandling :: Handling
}
deriving stock (Eq, Show)

data Handling
= Handling_Ignore
| Handling_Omit
| Handling_Parse
deriving stock (Eq, Show)

newtype StorkInput = StorkInput
{ globalInput :: Input
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, newtype Config and configInput (to keep with the field naming convention).

}
deriving stock (Eq, Show)

Expand All @@ -98,7 +113,36 @@ fileCodec =
<*> Toml.text "url" .= fileUrl
<*> Toml.text "title" .= fileTitle

showHandling :: Handling -> Text
showHandling handling = case handling of
Handling_Ignore -> "Ignore"
Handling_Omit -> "Omit"
Handling_Parse -> "Parse"

parseHandling :: Text -> Either Text Handling
parseHandling handling = case handling of
"Ignore" -> Right Handling_Ignore
"Omit" -> Right Handling_Omit
"Parse" -> Right Handling_Parse
other -> Left $ "Unsupported value for frontmatter handling: " <> other

handlingCodec :: Toml.Key -> TomlCodec Handling
handlingCodec = textBy showHandling parseHandling

inputCodec :: TomlCodec Input
inputCodec =
Input
<$> Toml.list fileCodec "input.files" .= inputFiles
<$> Toml.list fileCodec "files" .= inputFiles
<*> Toml.diwrap (handlingCodec "frontmatter_handling") .= inputFrontmatterHandling

storkInputCodec :: TomlCodec StorkInput
storkInputCodec =
StorkInput
<$> Toml.table inputCodec "input" .= globalInput

instance FromJSON Handling where
parseJSON = Aeson.withText "FrontmatterHandling" $ \case
"ignore" -> pure Handling_Ignore
"omit" -> pure Handling_Omit
"parse" -> pure Handling_Parse
_ -> fail "Unsupported value for frontmatter-handling"