More flexible capability to control showing reference (#646)

This commit is contained in:
Chen 2023-04-13 16:41:42 +08:00 committed by GitHub
parent 8300f75f22
commit a066a3d283
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 152 additions and 0 deletions

View File

@ -102,6 +102,7 @@ impl Synthesize for HeadingElem {
self.push_numbering(self.numbering(styles));
self.push_supplement(self.supplement(styles));
self.push_outlined(self.outlined(styles));
self.push_supplement(self.supplement(styles));
Ok(())
}
}

View File

@ -85,12 +85,62 @@ pub struct RefElem {
/// A synthesized citation.
#[synthesized]
pub citation: Option<CiteElem>,
/// Content of the element, it should be referable.
///
/// ```example
/// #set heading(numbering: (..nums) => {
/// nums.pos().map(str).join(".")
/// }, supplement: [Chapt])
///
/// #show ref: it => {
/// if it.has("element") and it.element.func() == heading {
/// let element = it.element
/// "["
/// element.supplement
/// "-"
/// numbering(element.numbering, ..counter(heading).at(element.location()))
/// "]"
/// } else {
/// it
/// }
/// }
///
/// = Introduction <intro>
/// = Summary <sum>
/// == Subsection <sub>
/// @intro
///
/// @sum
///
/// @sub
/// ```
#[synthesized]
pub element: Option<Content>,
}
impl Synthesize for RefElem {
fn synthesize(&mut self, vt: &mut Vt, styles: StyleChain) -> SourceResult<()> {
let citation = self.to_citation(vt, styles)?;
self.push_citation(Some(citation));
if !vt.introspector.init() {
self.push_element(None);
return Ok(());
}
// find the element content
let target = self.target();
let elem = vt.introspector.query_label(&self.target());
// not in bibliography, but in document, then push the element
if let (false, Ok(elem)) =
(BibliographyElem::has(vt, &target.0), elem.at(self.span()))
{
self.push_element(Some(elem));
} else {
self.push_element(None);
}
Ok(())
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 175 KiB

View File

@ -19,3 +19,104 @@ As seen in @intro, we proceed.
// Error: 1-5 label occurs multiple times in the document
@foo
---
#show ref: it => {
if it.element != none and it.element.func() == figure {
let element = it.element
"["
element.supplement
"-"
str(element.counter.at(element.location()).at(0))
"]"
// it
} else {
it
}
}
#figure(
image("/cylinder.svg", height: 3cm),
caption: [A sylinder.],
supplement: "Fig",
) <fig1>
#figure(
image("/tiger.jpg", height: 3cm),
caption: [A tiger.],
supplement: "Figg",
) <fig2>
#figure(
$ A = 1 $,
kind: "equation",
supplement: "Equa",
) <eq1>
@fig1
@fig2
@eq1
---
#set heading(numbering: (..nums) => {
nums.pos().map(str).join(".")
}, supplement: [Chapt])
#show ref: it => {
if it.element != none and it.element.func() == heading {
let element = it.element
"["
emph(element.supplement)
"-"
numbering(element.numbering, ..counter(heading).at(element.location()))
"]"
} else {
it
}
}
= Introduction <intro>
= Summary <sum>
== Subsection <sub>
@intro
@sum
@sub
---
#show ref: it => {
if it.element != none {
if it.element.func() == text {
let element = it.element
"["
element
"]"
} else if it.element.func() == underline {
let element = it.element
"{"
element
"}"
} else {
it
}
} else {
it
}
}
@txt
Ref something unreferable <txt>
@under
#underline[
Some underline text.
] <under>