Skip to content

Commit

Permalink
storkFiles: use baseDir of appropriate layer (#495)
Browse files Browse the repository at this point in the history
* storkFiles: use baseDir of appropriate layer

* changelog

* Rm unused function
  • Loading branch information
srid authored Jan 12, 2024
1 parent 7e34c2d commit 65d1b55
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 37 deletions.
5 changes: 4 additions & 1 deletion emanote/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
- Allow specifying custom page title in sidebar ([\#488](https://github.com/srid/emanote/pull/488))
- Allow specifying `lang` attribute for HTML page in YAML config ([\#485](https://github.com/srid/emanote/pull/485))
- KaTeX support ([\#489](https://github.com/srid/emanote/pull/489))
- Lua filters: filter paths will now be looked up in all layers now.
- Bug fixes:
- Emanote no longer crashes when run on an empty directory ([\#487](https://github.com/srid/emanote/issues/487))
- Fix empty stork index generation when using more than 1 layer ([\#493](https://github.com/srid/emanote/issues/493))
- Stork search fixes
- Fix empty stork index generation when using more than 1 layer ([\#493](https://github.com/srid/emanote/issues/493))
- Stork search index is now uses note path from their associated layer ([\#495](https://github.com/srid/emanote/pull/495))

## 1.2.0.0 (2023-08-24)

Expand Down
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.1
version: 1.3.6.0
license: AGPL-3.0-only
copyright: 2022 Sridhar Ratnakumar
maintainer: [email protected]
Expand Down
17 changes: 10 additions & 7 deletions emanote/src/Emanote/Model/Note.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import Emanote.Pandoc.Markdown.Syntax.HashTag qualified as HT
import Emanote.Route qualified as R
import Emanote.Route.Ext (FileType (Folder))
import Emanote.Route.R (R)
import Emanote.Source.Loc (Loc)
import Network.URI.Slug (Slug)
import Optics.Core ((%), (.~))
import Optics.TH (makeLenses)
Expand All @@ -48,6 +49,8 @@ data Feed = Feed

data Note = Note
{ _noteRoute :: R.LMLRoute
, _noteLayerLoc :: Maybe Loc
-- ^ The layer from which this note came. Nothing if the note was auto-generated.
, _noteDoc :: Pandoc
, _noteMeta :: Aeson.Value
, _noteTitle :: Tit.Title
Expand Down Expand Up @@ -274,16 +277,16 @@ ambiguousNoteURL urlPath rs =

mkEmptyNoteWith :: R.LMLRoute -> [B.Block] -> Note
mkEmptyNoteWith someR (Pandoc mempty -> doc) =
mkNoteWith someR doc meta mempty
mkNoteWith someR Nothing doc meta mempty
where
meta = Aeson.Null

mkNoteWith :: R.LMLRoute -> Pandoc -> Aeson.Value -> [Text] -> Note
mkNoteWith r doc' meta errs =
mkNoteWith :: R.LMLRoute -> Maybe Loc -> Pandoc -> Aeson.Value -> [Text] -> Note
mkNoteWith r layerLoc doc' meta errs =
let (doc'', tit) = queryNoteTitle r doc' meta
feed = queryNoteFeed meta
doc = if null errs then doc'' else pandocPrepend (errorDiv errs) doc''
in Note r doc meta tit errs feed
in Note r layerLoc doc meta tit errs feed
where
-- Prepend to block to the beginning of a Pandoc document (never before H1)
pandocPrepend :: B.Block -> Pandoc -> Pandoc
Expand All @@ -303,17 +306,17 @@ parseNote ::
ScriptingEngine ->
[FilePath] ->
R.LMLRoute ->
FilePath ->
(Loc, FilePath) ->
Text ->
m Note
parseNote scriptingEngine pluginBaseDir r fp s = do
parseNote scriptingEngine pluginBaseDir r (layerLoc, fp) s = do
((doc, meta), errs) <- runWriterT $ do
case r of
R.LMLRoute_Md _ ->
parseNoteMarkdown scriptingEngine pluginBaseDir fp s
R.LMLRoute_Org _ -> do
parseNoteOrg s
pure $ mkNoteWith r doc meta errs
pure $ mkNoteWith r (Just layerLoc) doc meta errs

parseNoteOrg :: (MonadWriter [Text] m) => Text -> m (Pandoc, Aeson.Value)
parseNoteOrg s =
Expand Down
23 changes: 12 additions & 11 deletions emanote/src/Emanote/Model/Stork.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ renderStorkIndex model = do

storkFiles :: Model -> [File]
storkFiles model =
let baseDir = Loc.locPath . Loc.primaryLayer $ model ^. M.modelLayers
in Ix.toList (model ^. M.modelNotes) <&> \note ->
let fp = ((baseDir </>) $ R.withLmlRoute R.encodeRoute $ note ^. N.noteRoute)
ft = case note ^. N.noteRoute of
R.LMLRoute_Md _ -> FileType_Markdown
R.LMLRoute_Org _ -> FileType_PlainText
in File
fp
(SR.siteRouteUrl model $ SR.lmlSiteRoute (R.LMLView_Html, note ^. N.noteRoute))
(Tit.toPlain $ note ^. N.noteTitle)
ft
flip mapMaybe (Ix.toList (model ^. M.modelNotes)) $ \note -> do
baseDir <- Loc.locPath <$> note ^. N.noteLayerLoc
let fp = ((baseDir </>) $ R.withLmlRoute R.encodeRoute $ note ^. N.noteRoute)
ft = case note ^. N.noteRoute of
R.LMLRoute_Md _ -> FileType_Markdown
R.LMLRoute_Org _ -> FileType_PlainText
pure
$ File
fp
(SR.siteRouteUrl model $ SR.lmlSiteRoute (R.LMLView_Html, note ^. N.noteRoute))
(Tit.toPlain $ note ^. N.noteTitle)
ft

frontmatterHandling :: Model -> Handling
frontmatterHandling model =
Expand Down
20 changes: 5 additions & 15 deletions emanote/src/Emanote/Source/Loc.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE DeriveAnyClass #-}

-- | Notebook location
module Emanote.Source.Loc (
-- * Type
Expand All @@ -13,11 +15,11 @@ module Emanote.Source.Loc (

-- * Dealing with layers of locs
LocLayers,
primaryLayer,
userLayersToSearch,
) where

import Data.Set qualified as Set
import Deriving.Aeson qualified as Aeson
import Relude
import System.FilePath ((</>))

Expand All @@ -30,23 +32,11 @@ data Loc
LocUser Int FilePath
| -- | The default location (ie., emanote default layer)
LocDefault FilePath
deriving stock (Eq, Ord, Show)
deriving stock (Eq, Ord, Show, Generic)
deriving anyclass (Aeson.ToJSON)

type LocLayers = Set Loc

{- | Return the "primary" `LocUser` layer
The primary layer takes the highest precedence, hence is specified in the
leftmost position, i.e, `-L primary/layer;foo`.
-}
primaryLayer :: (HasCallStack) => LocLayers -> Loc
primaryLayer =
Set.findMin . Set.filter isUserLayer
where
isUserLayer = \case
LocUser _ _ -> True
_ -> False

{- | List of user layers, highest precedent being at first.
This is useful to delay searching for content in layers.
Expand Down
4 changes: 2 additions & 2 deletions emanote/src/Emanote/Source/Patch.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ patchModel' layers noteF storkIndexTVar scriptingEngine fpType fp action = do

case action of
UM.Refresh refreshAction overlays -> do
let fpAbs = locResolve $ head overlays
s <- readRefreshedFile refreshAction fpAbs
let fpAbs = head overlays
s <- readRefreshedFile refreshAction $ locResolve fpAbs
note <- N.parseNote scriptingEngine (userLayersToSearch layers) r fpAbs (decodeUtf8 s)
pure $ M.modelInsertNote $ noteF note
UM.Delete -> do
Expand Down

0 comments on commit 65d1b55

Please sign in to comment.