From 7e34c2d78f967c7164560bc2f6a84cec72d934b6 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Fri, 12 Jan 2024 21:13:25 +0530 Subject: [PATCH] lua: Look up in all layers --- emanote/emanote.cabal | 2 +- emanote/src/Emanote/Model/Note.hs | 19 ++++++++++++++++--- emanote/src/Emanote/Source/Loc.hs | 14 ++++++++++++++ emanote/src/Emanote/Source/Patch.hs | 8 ++------ 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/emanote/emanote.cabal b/emanote/emanote.cabal index e08f2c906..2c6f8180b 100644 --- a/emanote/emanote.cabal +++ b/emanote/emanote.cabal @@ -1,6 +1,6 @@ cabal-version: 2.4 name: emanote -version: 1.3.5.0 +version: 1.3.5.1 license: AGPL-3.0-only copyright: 2022 Sridhar Ratnakumar maintainer: srid@srid.ca diff --git a/emanote/src/Emanote/Model/Note.hs b/emanote/src/Emanote/Model/Note.hs index 316e61216..371bb4968 100644 --- a/emanote/src/Emanote/Model/Note.hs +++ b/emanote/src/Emanote/Model/Note.hs @@ -36,6 +36,7 @@ import Text.Pandoc.Definition (Pandoc (..)) import Text.Pandoc.Readers.Org (readOrg) import Text.Pandoc.Scripting (ScriptingEngine) import Text.Pandoc.Walk qualified as W +import UnliftIO.Directory (doesPathExist) data Feed = Feed { _feedEnable :: Bool @@ -300,7 +301,7 @@ parseNote :: forall m. (MonadIO m, MonadLogger m) => ScriptingEngine -> - FilePath -> + [FilePath] -> R.LMLRoute -> FilePath -> Text -> @@ -324,7 +325,7 @@ parseNoteOrg s = -- TODO: Merge Pandoc's Meta in here? pure (preparePandoc doc, defaultFrontMatter) -parseNoteMarkdown :: (MonadIO m, MonadLogger m) => ScriptingEngine -> FilePath -> FilePath -> Text -> WriterT [Text] m (Pandoc, Aeson.Value) +parseNoteMarkdown :: (MonadIO m, MonadLogger m) => ScriptingEngine -> [FilePath] -> FilePath -> Text -> WriterT [Text] m (Pandoc, Aeson.Value) parseNoteMarkdown scriptingEngine pluginBaseDir fp md = do case Markdown.parseMarkdown fp md of Left err -> do @@ -335,7 +336,19 @@ parseNoteMarkdown scriptingEngine pluginBaseDir fp md = do -- -- Some are user-defined; some builtin. They operate on Pandoc, or the -- frontmatter meta. - let filterPaths = (pluginBaseDir ) <$> SData.lookupAeson @[FilePath] mempty ("pandoc" :| ["filters"]) frontmatter + filterPaths <- fmap catMaybes $ forM (SData.lookupAeson @[FilePath] mempty ("pandoc" :| ["filters"]) frontmatter) $ \p -> do + res :: [FilePath] <- flip mapMaybeM pluginBaseDir $ \baseDir -> do + doesPathExist (baseDir p) >>= \case + False -> do + pure Nothing + True -> + pure $ Just $ baseDir p + case res of + [] -> do + tell [toText $ "Pandoc filter " <> p <> " not found in any of: " <> show pluginBaseDir] + pure Nothing + (x : _) -> pure $ Just x + doc <- applyPandocFilters scriptingEngine filterPaths $ preparePandoc doc' let meta = applyNoteMetaFilters doc frontmatter pure (doc, meta) diff --git a/emanote/src/Emanote/Source/Loc.hs b/emanote/src/Emanote/Source/Loc.hs index 4849f43d4..8ce6700aa 100644 --- a/emanote/src/Emanote/Source/Loc.hs +++ b/emanote/src/Emanote/Source/Loc.hs @@ -14,6 +14,7 @@ module Emanote.Source.Loc ( -- * Dealing with layers of locs LocLayers, primaryLayer, + userLayersToSearch, ) where import Data.Set qualified as Set @@ -46,6 +47,19 @@ primaryLayer = LocUser _ _ -> True _ -> False +{- | List of user layers, highest precedent being at first. + +This is useful to delay searching for content in layers. +-} +userLayersToSearch :: LocLayers -> [FilePath] +userLayersToSearch = + mapMaybe + ( \case + LocUser _ fp -> Just fp + LocDefault _ -> Nothing + ) + . Set.toAscList + defaultLayer :: FilePath -> Loc defaultLayer = LocDefault diff --git a/emanote/src/Emanote/Source/Patch.hs b/emanote/src/Emanote/Source/Patch.hs index 7d5d84986..cd7a1479b 100644 --- a/emanote/src/Emanote/Source/Patch.hs +++ b/emanote/src/Emanote/Source/Patch.hs @@ -22,7 +22,7 @@ import Emanote.Prelude ( logD, ) import Emanote.Route qualified as R -import Emanote.Source.Loc (Loc, LocLayers, locPath, locResolve, primaryLayer) +import Emanote.Source.Loc (Loc, LocLayers, locResolve, userLayersToSearch) import Emanote.Source.Pattern (filePatterns, ignorePatterns) import Heist.Extra.TemplateState qualified as T import Optics.Operators ((%~)) @@ -91,12 +91,8 @@ patchModel' layers noteF storkIndexTVar scriptingEngine fpType fp action = do case action of UM.Refresh refreshAction overlays -> do let fpAbs = locResolve $ head overlays - -- TODO: This should automatically be computed, instead of being passed. - -- We need access to the model though! With dependency management to boot. - -- Until this, `layers` is threaded through as a hack. - currentLayerPath = locPath $ primaryLayer layers s <- readRefreshedFile refreshAction fpAbs - note <- N.parseNote scriptingEngine currentLayerPath r fpAbs (decodeUtf8 s) + note <- N.parseNote scriptingEngine (userLayersToSearch layers) r fpAbs (decodeUtf8 s) pure $ M.modelInsertNote $ noteF note UM.Delete -> do log $ "Removing note: " <> toText fp