From 1e65dde0e8b5a42890c382574c997fa9afeae9a8 Mon Sep 17 00:00:00 2001 From: Hancong Zhang Date: Thu, 13 Aug 2026 19:15:39 +0200 Subject: [PATCH] add formatting attribute --- .../csl/internal/rendering/SDate.java | 16 +++++++- .../csl/internal/rendering/SDatePart.java | 5 ++- .../java/de/undercouch/citeproc/CSLTest.java | 38 +++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/citeproc-java/src/main/java/de/undercouch/citeproc/csl/internal/rendering/SDate.java b/citeproc-java/src/main/java/de/undercouch/citeproc/csl/internal/rendering/SDate.java index 0d053d21..a1528ada 100644 --- a/citeproc-java/src/main/java/de/undercouch/citeproc/csl/internal/rendering/SDate.java +++ b/citeproc-java/src/main/java/de/undercouch/citeproc/csl/internal/rendering/SDate.java @@ -3,6 +3,7 @@ import de.undercouch.citeproc.csl.CSLDate; import de.undercouch.citeproc.csl.internal.RenderContext; import de.undercouch.citeproc.csl.internal.behavior.Affixes; +import de.undercouch.citeproc.csl.internal.behavior.FormattingAttributes; import de.undercouch.citeproc.csl.internal.locale.LDate; import de.undercouch.citeproc.csl.internal.token.TextToken; import de.undercouch.citeproc.csl.internal.token.Token; @@ -32,6 +33,7 @@ public class SDate implements SRenderingElement { private final String datePartsAttr; private final List dateParts = new ArrayList<>(); private final Affixes affixes; + private final int formattingAttributes; /** * Creates the date element from an XML node @@ -72,11 +74,23 @@ public SDate(Node node) { } affixes = new Affixes(node); + formattingAttributes = FormattingAttributes.of(node); } @Override public void render(RenderContext ctx) { - affixes.wrap(this::renderInternal).accept(ctx); + affixes.wrap(this::renderWithFormatting).accept(ctx); + } + + private void renderWithFormatting(RenderContext ctx) { + if (formattingAttributes == 0) { + renderInternal(ctx); + return; + } + + RenderContext tmp = new RenderContext(ctx); + renderInternal(tmp); + ctx.emit(tmp.getResult(), formattingAttributes); } private void renderInternal(RenderContext ctx) { diff --git a/citeproc-java/src/main/java/de/undercouch/citeproc/csl/internal/rendering/SDatePart.java b/citeproc-java/src/main/java/de/undercouch/citeproc/csl/internal/rendering/SDatePart.java index 5b1687b8..b9e7469d 100644 --- a/citeproc-java/src/main/java/de/undercouch/citeproc/csl/internal/rendering/SDatePart.java +++ b/citeproc-java/src/main/java/de/undercouch/citeproc/csl/internal/rendering/SDatePart.java @@ -3,6 +3,7 @@ import de.undercouch.citeproc.csl.internal.RenderContext; import de.undercouch.citeproc.csl.internal.SElement; import de.undercouch.citeproc.csl.internal.behavior.Affixes; +import de.undercouch.citeproc.csl.internal.behavior.FormattingAttributes; import de.undercouch.citeproc.csl.internal.behavior.StripPeriods; import de.undercouch.citeproc.csl.internal.locale.LTerm; import de.undercouch.citeproc.helper.NodeHelper; @@ -23,6 +24,7 @@ public class SDatePart implements SElement { private final Affixes affixes; private final StripPeriods stripPeriods; private final String rangeDelimiter; + private final int formattingAttributes; /** * Creates the date-part element from an XML node @@ -40,6 +42,7 @@ public SDatePart(Node node) { form = NodeHelper.getAttrValue(node, "form"); affixes = new Affixes(node); stripPeriods = new StripPeriods(node); + formattingAttributes = FormattingAttributes.of(node); String rd = NodeHelper.getAttrValue(node, "range-delimiter"); rangeDelimiter = Objects.requireNonNullElse(rd, "–"); @@ -73,7 +76,7 @@ private void renderInternal(RenderContext ctx) { } if (value != null) { - ctx.emit(value); + ctx.emit(value, formattingAttributes); } } diff --git a/citeproc-java/src/test/java/de/undercouch/citeproc/CSLTest.java b/citeproc-java/src/test/java/de/undercouch/citeproc/CSLTest.java index f3fac18e..2728b71e 100644 --- a/citeproc-java/src/test/java/de/undercouch/citeproc/CSLTest.java +++ b/citeproc-java/src/test/java/de/undercouch/citeproc/CSLTest.java @@ -703,6 +703,44 @@ public void emptyAsciiDoc() throws Exception { ); } + /** + * Make sure formatting attributes on date and date-part elements are + * preserved in rendered output. + * @throws Exception if something goes wrong + */ + @Test + public void dateFormattingAttributes() throws Exception { + CSLItemData item = new CSLItemDataBuilder() + .id("DATE-FORMATTING") + .type(CSLType.ARTICLE_JOURNAL) + .issued(2007) + .build(); + + String dateStyle = ""; + + String datePartStyle = ""; + + String dateBibliography = CSL.makeAdhocBibliography(dateStyle, "html", item).makeString(); + assertTrue(dateBibliography.contains("2007")); + + String datePartBibliography = CSL.makeAdhocBibliography(datePartStyle, "html", item).makeString(); + assertTrue(datePartBibliography.contains("2007")); + } + /** * Test if parsing a style with multiple if nodes within a choose node * throws an exception