use of com.vladsch.flexmark.util.html.Attributes in project flexmark-java by vsch.
the class FlexmarkHtmlParser method transferToParentOnly.
private void transferToParentOnly(String... includes) {
if (myStateStack.isEmpty())
throw new IllegalStateException("transferIdToParent with an empty stack");
final Attributes attributes = new Attributes();
for (String include : includes) {
Attribute attribute = myState.myAttributes.get(include);
if (attribute != null) {
myState.myAttributes.remove(include);
attributes.addValue(attribute);
}
}
if (!attributes.isEmpty()) {
final State parentState = myStateStack.peek();
for (String attrName : attributes.keySet()) {
parentState.myAttributes.addValue(attributes.get(attrName));
}
}
}
use of com.vladsch.flexmark.util.html.Attributes in project flexmark-java by vsch.
the class TablesTest method attributeProviderIsApplied.
@Test
public void attributeProviderIsApplied() {
AttributeProviderFactory factory = new IndependentAttributeProviderFactory() {
@Override
public AttributeProvider create(LinkResolverContext context) {
return new AttributeProvider() {
@Override
public void setAttributes(Node node, AttributablePart part, Attributes attributes) {
if (node instanceof TableBlock) {
attributes.replaceValue("test", "block");
} else if (node instanceof TableHead) {
attributes.replaceValue("test", "head");
} else if (node instanceof TableBody) {
attributes.replaceValue("test", "body");
} else if (node instanceof TableRow) {
attributes.replaceValue("test", "row");
} else if (node instanceof TableCell) {
attributes.replaceValue("test", "cell");
}
}
};
}
};
HtmlRenderer renderer = HtmlRenderer.builder().attributeProviderFactory(factory).extensions(EXTENSIONS).build();
String rendered = renderer.render(PARSER.parse("Abc|Def\n---|---\n1|2"));
assertThat(rendered, is("<table test=\"block\">\n" + "<thead test=\"head\">\n" + "<tr test=\"row\"><th test=\"cell\">Abc</th><th test=\"cell\">Def</th></tr>\n" + "</thead>\n" + "<tbody test=\"body\">\n" + "<tr test=\"row\"><td test=\"cell\">1</td><td test=\"cell\">2</td></tr>\n" + "</tbody>\n" + "</table>\n"));
}
use of com.vladsch.flexmark.util.html.Attributes in project flexmark-java by vsch.
the class FlexmarkHtmlParser method processAttributes.
void processAttributes(Node node) {
Attributes attributes = myState.myAttributes;
if (myOptions.outputAttributesIdAttr || !myOptions.outputAttributesNamesRegex.isEmpty()) {
final org.jsoup.nodes.Attributes nodeAttributes = node.attributes();
boolean idDone = false;
if (myOptions.outputAttributesIdAttr) {
String id = nodeAttributes.get("id");
if (id == null || id.isEmpty()) {
id = nodeAttributes.get("name");
}
if (id != null && !id.isEmpty()) {
attributes.replaceValue("id", id);
idDone = true;
}
}
if (!myOptions.outputAttributesNamesRegex.isEmpty()) {
for (org.jsoup.nodes.Attribute attribute : nodeAttributes) {
if (idDone && (attribute.getKey().equals("id") || attribute.getKey().equals("name"))) {
continue;
}
if (attribute.getKey().matches(myOptions.outputAttributesNamesRegex)) {
attributes.replaceValue(attribute.getKey(), attribute.getValue());
}
}
}
}
}
use of com.vladsch.flexmark.util.html.Attributes in project flexmark-java by vsch.
the class FlexmarkHtmlParser method outputAttributes.
void outputAttributes(FormattingAppendable out) {
Attributes attributes = myState.myAttributes;
if (!attributes.isEmpty()) {
// have some
String sep = "";
out.append("{");
for (String attrName : attributes.keySet()) {
String value = attributes.getValue(attrName);
out.append(sep);
if (attrName.equals("id") || attrName.equals("name")) {
out.append("#").append(value);
} else if (attrName.equals("class")) {
out.append(".").append(value);
} else {
out.append(attrName).append("=");
if (!value.contains("\"")) {
out.append('"').append(value).append('"');
} else if (!value.contains("\'")) {
out.append('\'').append(value).append('\'');
} else {
out.append('"').append(value.replace("\"", "\\\"")).append('"');
}
}
sep = " ";
}
out.append("}");
}
}
use of com.vladsch.flexmark.util.html.Attributes in project flexmark-java by vsch.
the class FlexmarkHtmlParser method transferToParentExcept.
private void transferToParentExcept(String... excludes) {
if (myStateStack.isEmpty())
throw new IllegalStateException("transferIdToParent with an empty stack");
final Attributes attributes = new Attributes(myState.myAttributes);
myState.myAttributes.clear();
for (String exclude : excludes) {
myState.myAttributes.addValue(attributes.get(exclude));
attributes.remove(exclude);
}
if (!attributes.isEmpty()) {
final State parentState = myStateStack.peek();
for (String attrName : attributes.keySet()) {
parentState.myAttributes.addValue(attributes.get(attrName));
}
}
}
Aggregations