Skip to content

Commit

Permalink
lua: Look up in all layers
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Jan 12, 2024
1 parent 6b7189f commit 7e34c2d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion emanote/emanote.cabal
Original file line number Diff line number Diff line change
@@ -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: [email protected]
Expand Down
19 changes: 16 additions & 3 deletions emanote/src/Emanote/Model/Note.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -300,7 +301,7 @@ parseNote ::
forall m.
(MonadIO m, MonadLogger m) =>
ScriptingEngine ->
FilePath ->
[FilePath] ->
R.LMLRoute ->
FilePath ->
Text ->
Expand All @@ -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
Expand All @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions emanote/src/Emanote/Source/Loc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Emanote.Source.Loc (
-- * Dealing with layers of locs
LocLayers,
primaryLayer,
userLayersToSearch,
) where

import Data.Set qualified as Set
Expand Down Expand Up @@ -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

Expand Down
8 changes: 2 additions & 6 deletions emanote/src/Emanote/Source/Patch.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ((%~))
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7e34c2d

Please sign in to comment.