Skip to content

Commit

Permalink
Add support for formatting attributes in layout element
Browse files Browse the repository at this point in the history
Fixes #99
  • Loading branch information
michel-kraemer committed Mar 11, 2021
1 parent fbf8e3d commit 383b6a5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[submodule "citeproc-java/src/test/resources/test-suite"]
path = citeproc-java/src/test/resources/test-suite
url = https://github.com/citation-style-language/test-suite.git
shallow = true
[submodule "citeproc-java/src/test/resources/citeproc-js"]
path = citeproc-java/src/test/resources/citeproc-js
url = https://github.com/Juris-M/citeproc-js.git
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.undercouch.citeproc.csl.CSLCitationItem;
import de.undercouch.citeproc.csl.internal.behavior.Affixes;
import de.undercouch.citeproc.csl.internal.behavior.FormattingAttributes;
import de.undercouch.citeproc.helper.NodeHelper;
import org.apache.commons.lang3.CharSet;
import org.w3c.dom.Node;
Expand All @@ -18,7 +19,8 @@ public class SCitationLayout extends SRenderingElementContainer {
private static final Pattern TRIM_END = Pattern.compile("\\p{Space}+$");
private static final Pattern IGNORE_END = Pattern.compile("[\\p{Space}\\p{Pf}\"']+$");

protected final Affixes affixes;
private final Affixes affixes;
private final int formattingAttributes;
private final String delimiter;

/**
Expand All @@ -28,6 +30,7 @@ public class SCitationLayout extends SRenderingElementContainer {
public SCitationLayout(Node node) {
super(node);
affixes = new Affixes(node);
formattingAttributes = FormattingAttributes.of(node);
delimiter = NodeHelper.getAttrValue(node, "delimiter");
}

Expand Down Expand Up @@ -65,7 +68,7 @@ private void renderInternal(RenderContext ctx) {
tmp.emit(suffix, Token.Type.SUFFIX);
}
}
ctx.emit(tmp.getResult());
ctx.emit(tmp.getResult(), formattingAttributes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.undercouch.citeproc.csl.internal;

import de.undercouch.citeproc.csl.internal.behavior.Affixes;
import de.undercouch.citeproc.csl.internal.behavior.FormattingAttributes;
import de.undercouch.citeproc.csl.internal.rendering.SRenderingElement;
import org.w3c.dom.Node;

Expand All @@ -10,6 +11,7 @@
*/
public class SLayout extends SRenderingElementContainer {
private final Affixes affixes;
private final int formattingAttributes;

/**
* Construct the layout element from an XML node
Expand All @@ -18,6 +20,7 @@ public class SLayout extends SRenderingElementContainer {
public SLayout(Node node) {
super(node);
affixes = new Affixes(node);
formattingAttributes = FormattingAttributes.of(node);
}

@Override
Expand All @@ -26,22 +29,24 @@ public void render(RenderContext ctx) {
}

private void renderInternal(RenderContext ctx) {
RenderContext tmp = new RenderContext(ctx);
for (int i = 0; i < elements.size(); i++) {
SRenderingElement e = elements.get(i);
if (i == 0) {
// render first field
RenderContext tmp = new RenderContext(ctx);
e.render(tmp);
for (Token t : tmp.getResult().getTokens()) {
RenderContext innerTmp = new RenderContext(tmp);
e.render(innerTmp);
for (Token t : innerTmp.getResult().getTokens()) {
// set flag in token
Token nt = new Token.Builder(t)
.firstField(true)
.build();
ctx.emit(nt);
tmp.emit(nt);
}
} else {
e.render(ctx);
e.render(tmp);
}
}
ctx.emit(tmp.getResult(), formattingAttributes);
}
}
1 change: 1 addition & 0 deletions citeproc-java/src/test/resources/citeproc-js
Submodule citeproc-js added at 27c95b
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
mode: bibliography

style:
<style xmlns="http://purl.org/net/xbiblio/csl" version="1.0">
<bibliography>
<layout vertical-align="sup">
<names variable="author" suffix=". ">
<name initialize-with=". " />
</names>
<text variable="title"/>
</layout>
</bibliography>
</style>

items:
- id: item1
author:
- given: Given
family: Name
title: My title
- id: item2
author:
- given: Another
family: Name

result:
html: |-
<div class="csl-bib-body">
<div class="csl-entry"><sup>G. Name. My title</sup></div>
<div class="csl-entry"><sup>A. Name. </sup></div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
mode: citation

style:
<style xmlns="http://purl.org/net/xbiblio/csl" version="1.0">
<citation>
<layout delimiter="," vertical-align="sup">
<text variable="citation-number"/>
</layout>
</citation>
</style>

items:
- id: item1
author:
- given: Given
family: Name
title: My title
- id: item2
author:
- given: Another
family: Name

result:
html:
<sup>1,2</sup>

0 comments on commit 383b6a5

Please sign in to comment.