generated from srid/ema-template
-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b38035c
commit 872dc82
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module Emanote.Model.Toc where | ||
|
||
import Data.Tree qualified as Tree | ||
import Relude | ||
import Text.Pandoc | ||
import Text.Pandoc.Shared (stringify) | ||
|
||
type Toc = Tree.Forest DocHeading | ||
|
||
data DocHeading = DocHeading | ||
{ headingId :: Text | ||
, headingName :: Text | ||
} | ||
deriving stock (Show, Eq) | ||
|
||
-- | Collect the heading and their level | ||
pandocToHeadings :: Pandoc -> [(Int, DocHeading)] | ||
pandocToHeadings (Pandoc _ blocks) = mapMaybe toHeading blocks | ||
where | ||
toHeading block = case block of | ||
Header hlvl (oid, _, _) inlines -> Just (hlvl, DocHeading oid (stringify inlines)) | ||
_ -> Nothing | ||
|
||
-- | Create the Toc | ||
newToc :: Pandoc -> Toc | ||
newToc = go [] 1 . pandocToHeadings | ||
where | ||
go acc lvl ((headingLvl, heading) : rest) | ||
| lvl == headingLvl = | ||
let | ||
-- collect following headings that are childs | ||
childs = go [] (lvl + 1) rest | ||
newAcc = Tree.Node heading childs : acc | ||
childCount = sum $ map length childs | ||
in | ||
go newAcc lvl (drop childCount rest) | ||
go acc _ _ = reverse acc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module Emanote.Model.TocSpec where | ||
|
||
import Control.Monad.Writer (runWriterT) | ||
import Data.Text qualified as Text | ||
import Data.Tree | ||
import Emanote.Model.Note (parseNoteOrg) | ||
import Emanote.Model.Toc | ||
import Relude | ||
import Test.Hspec | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "basic-toc" | ||
$ it "create toc tree" | ||
$ do | ||
((doc, _), []) <- runWriterT $ parseNoteOrg demo | ||
(fmap headingName <$> newToc doc) | ||
`shouldBe` [ Node | ||
{ rootLabel = "h1 1" | ||
, subForest = | ||
[ Node {rootLabel = "h2 1", subForest = []} | ||
, Node {rootLabel = "h2 2", subForest = []} | ||
] | ||
} | ||
, Node {rootLabel = "h1 2", subForest = []} | ||
] | ||
where | ||
demo = | ||
Text.unlines | ||
[ "* h1 1" | ||
, "** h2 1" | ||
, "** h2 2" | ||
, "* h1 2" | ||
, -- this is ignored because of missing h2 heading | ||
"*** h3 1" | ||
] |