Skip to content

Commit

Permalink
Add use-last parameter
Browse files Browse the repository at this point in the history
Closes #13.
This allows hydra to alternatively display the last instead of the first
candidate on a page, mimicking the behavior of LaTeX.
  • Loading branch information
freundTech committed Jun 29, 2024
1 parent d6e869f commit 41d4666
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 20 deletions.
53 changes: 33 additions & 20 deletions src/core.typ
Original file line number Diff line number Diff line change
Expand Up @@ -85,38 +85,42 @@
let next-ancestor = none

if ctx.ancestors != none {
let prev = query(selector(ctx.ancestors.target).before(ctx.anchor-loc))
let next = query(selector(ctx.ancestors.target).after(ctx.anchor-loc))
let prev-ancestors = query(selector(ctx.ancestors.target).before(ctx.anchor-loc))
let next-ancestors = query(selector(ctx.ancestors.target).after(ctx.anchor-loc))

if ctx.ancestors.filter != none {
prev = prev.filter(x => (ctx.ancestors.filter)(ctx, x))
next = next.filter(x => (ctx.ancestors.filter)(ctx, x))
prev-ancestors = prev-ancestors.filter(x => (ctx.ancestors.filter)(ctx, x))
next-ancestors = next-ancestors.filter(x => (ctx.ancestors.filter)(ctx, x))
}

if prev != () {
prev-ancestor = prev.last()
if prev-ancestors != () {
prev-ancestor = prev-ancestors.last()
look-behind = look-behind.after(prev-ancestor.location())
}

if next != () {
next-ancestor = next.first()
look-ahead = look-ahead.before(next-ancestor.location())
if next-ancestors != () {
next-ancestor = next-ancestors.first()
if not ctx.use-last {
look-ahead = look-ahead.before(next-ancestor.location())
}
}
}

let prev = query(look-behind)
let next = query(look-ahead)
let prev-targets = query(look-behind)
let next-targets = query(look-ahead)

if ctx.primary.filter != none {
prev = prev.filter(x => (ctx.primary.filter)(ctx, x))
next = next.filter(x => (ctx.primary.filter)(ctx, x))
prev-targets = prev-targets.filter(x => (ctx.primary.filter)(ctx, x))
next-targets = next-targets.filter(x => (ctx.primary.filter)(ctx, x))
}
next-targets = next-targets.filter(x => x.location().page() == ctx.anchor-loc.page())

let prev = if prev != () { prev.last() }
let next = if next != () { next.first() }
let prev = if prev-targets != () { prev-targets.last() }
let next = if next-targets != () { next-targets.first() }
let last = if next-targets != () { next-targets.last() }

(
primary: (prev: prev, next: next),
primary: (prev: prev, next: next, last: last),
ancestor: (prev: prev-ancestor, next: next-ancestor),
)
}
Expand Down Expand Up @@ -227,11 +231,20 @@
let candidates = get-candidates(ctx)
let prev-eligible = candidates.primary.prev != none and (ctx.prev-filter)(ctx, candidates)
let next-eligible = candidates.primary.next != none and (ctx.next-filter)(ctx, candidates)
let last-eligible = candidates.primary.last != none and (ctx.next-filter)(ctx, candidates)
let active-redundant = is-active-redundant(ctx, candidates)

if prev-eligible and not active-redundant {
(ctx.display)(ctx, candidates.primary.prev)
} else if next-eligible and not ctx.skip-starting {
(ctx.display)(ctx, candidates.primary.next)
if ctx.use-last and not active-redundant{
if last-eligible {
(ctx.display)(ctx, candidates.primary.last)
} else if prev-eligible {
(ctx.display)(ctx, candidates.primary.prev)
}
} else if not ctx.use-last {
if prev-eligible and not active-redundant {
(ctx.display)(ctx, candidates.primary.prev)
} else if next-eligible and not ctx.skip-starting {
(ctx.display)(ctx, candidates.primary.next)
}
}
}
5 changes: 5 additions & 0 deletions src/lib.typ
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
/// display. If this is `auto`, the default implementaion will be used.
/// - skip-starting (bool): Whether `hydra` should show the current candidate even if it's on top of
/// the current page.
/// - use-last (bool): If hydra should show the name of the first of last candidate on the page.
// Defaults to false.
/// - dir (direction, auto): The reading direction of the document. If this is `auto`, the text
/// direction is used. Be cautious about leaving this option on `auto` if you switch text
/// direction mid-page and use hydra outside of footers or headers.
Expand All @@ -47,6 +49,7 @@
next-filter: auto,
display: auto,
skip-starting: true,
use-last: false,
dir: auto,
binding: auto,
book: false,
Expand All @@ -57,6 +60,7 @@
util.assert.types("next-filter", next-filter, function, auto)
util.assert.types("display", display, function, auto)
util.assert.types("skip-starting", skip-starting, bool)
util.assert.types("use-last", use-last, bool)
util.assert.enum("dir", dir, ltr, rtl, auto)
util.assert.enum("binding", binding, left, right, auto)
util.assert.types("book", book, bool)
Expand All @@ -78,6 +82,7 @@
next-filter: util.auto-or(next-filter, () => default-filter),
display: util.auto-or(display, () => core.display),
skip-starting: skip-starting,
use-last: use-last,
dir: dir,
binding: binding,
book: book,
Expand Down
1 change: 1 addition & 0 deletions tests/features/use-last/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# added by typst-test
Binary file added tests/features/use-last/basic/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/features/use-last/basic/ref/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/features/use-last/basic/ref/3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/features/use-last/basic/ref/4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/features/use-last/basic/ref/5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/features/use-last/basic/ref/6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/features/use-last/basic/ref/7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions tests/features/use-last/basic/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Synopsis:
// - <explanation>

#import "/src/lib.typ": hydra

#set page(
paper: "a7",
header: context hydra(use-last: true),
)
#set heading(numbering: "1.1")
#show heading.where(level: 1): it => pagebreak(weak: true) + it
#set par(justify: true)

= Introduction
#lorem(200)

= Content
== First Section
#lorem(50)
== Second Section
#lorem(100)
== Third section
#lorem(100)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions tests/features/use-last/multiple-ancestors/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Synopsis:
// - When there are multiple ancestors on one page hydra should still show the last heading

#import "/src/lib.typ": hydra

#set page(
paper: "a7",
header: context hydra(2, use-last: true),
)
#set heading(numbering: "1.1")
#set par(justify: true)


= Introduction
== First Section
#lorem(50)
== Second Section
#lorem(100)
== Third section
#lorem(50)

= Other
#lorem(10)
== test
#lorem(10)
= More
#lorem(5)
== more tests
#lorem(10)

0 comments on commit 41d4666

Please sign in to comment.