Fix issue where markdown with multiple characters was being cut off sometimes

This commit is contained in:
Grahame Grieve 2023-11-13 07:38:59 +11:00
parent 4c6a318749
commit 90b0e0d3d6
2 changed files with 35 additions and 17 deletions

View File

@ -91,6 +91,7 @@ import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.TableGenerationMo
import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.TableModel; import org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.TableModel;
import org.hl7.fhir.utilities.xhtml.NodeType; import org.hl7.fhir.utilities.xhtml.NodeType;
import org.hl7.fhir.utilities.xhtml.XhtmlNode; import org.hl7.fhir.utilities.xhtml.XhtmlNode;
import org.hl7.fhir.utilities.xhtml.XhtmlNodeList;
import org.hl7.fhir.utilities.xhtml.XhtmlParser; import org.hl7.fhir.utilities.xhtml.XhtmlParser;
public class StructureDefinitionRenderer extends ResourceRenderer { public class StructureDefinitionRenderer extends ResourceRenderer {
@ -3381,57 +3382,70 @@ public class StructureDefinitionRenderer extends ResourceRenderer {
} }
public XhtmlNode compareMarkdown(String location, PrimitiveType md, PrimitiveType compare, int mode) throws FHIRException, IOException { public XhtmlNode compareMarkdown(String location, PrimitiveType md, PrimitiveType compare, int mode) throws FHIRException, IOException {
XhtmlNode ndiv = new XhtmlNode(NodeType.Element, "div");
if (compare == null || mode == GEN_MODE_DIFF) { if (compare == null || mode == GEN_MODE_DIFF) {
if (md.hasValue()) { if (md.hasValue()) {
String xhtml = hostMd.processMarkdown(location, md); String xhtml = hostMd.processMarkdown(location, md);
if (Utilities.noString(xhtml)) { if (Utilities.noString(xhtml)) {
return null; return null;
} }
XhtmlNode x = new XhtmlNode(NodeType.Element, "div");
try { try {
renderStatusDiv(md, x).add(new XhtmlParser().parseFragment(xhtml)); renderStatusDiv(md, ndiv).addChildren(fixFontSizes(new XhtmlParser().parseMDFragment(xhtml), 11));
} catch (Exception e) { } catch (Exception e) {
x.span("color: maroon").tx(e.getLocalizedMessage()); ndiv.span("color: maroon").tx(e.getLocalizedMessage());
e.printStackTrace();
} }
return x; return ndiv;
} else { } else {
return null; return null;
} }
} else if (areEqual(compare, md)) { } else if (areEqual(compare, md)) {
if (md.hasValue()) { if (md.hasValue()) {
String xhtml = "<div>"+hostMd.processMarkdown(location, md)+"</div>"; String xhtml = hostMd.processMarkdown(location, md);
XhtmlNode div = new XhtmlParser().parseFragment(xhtml); List<XhtmlNode> nodes = new XhtmlParser().parseMDFragment(xhtml);
for (XhtmlNode n : div.getChildNodes()) { for (XhtmlNode n : nodes) {
if (n.getNodeType() == NodeType.Element) { if (n.getNodeType() == NodeType.Element) {
n.style(unchangedStyle()); n.style(unchangedStyle());
} }
} }
return div; ndiv.addChildren(nodes);
return ndiv;
} else { } else {
return null; return null;
} }
} else { } else {
XhtmlNode ndiv = new XhtmlNode(NodeType.Element, "div");
if (md.hasValue()) { if (md.hasValue()) {
String xhtml = "<div>"+hostMd.processMarkdown(location, md)+"</div>"; String xhtml = hostMd.processMarkdown(location, md);
XhtmlNode div = new XhtmlParser().parseFragment(xhtml); List<XhtmlNode> div = new XhtmlParser().parseMDFragment(xhtml);
ndiv.copyAllContent(div); ndiv.addChildren(div);
} }
if (compare.hasValue()) { if (compare.hasValue()) {
String xhtml = "<div>"+hostMd.processMarkdown(location, compare)+"</div>"; String xhtml = "<div>"+hostMd.processMarkdown(location, compare)+"</div>";
XhtmlNode div = new XhtmlParser().parseFragment(xhtml); List<XhtmlNode> div = new XhtmlParser().parseMDFragment(xhtml);
for (XhtmlNode n : div.getChildNodes()) { for (XhtmlNode n : div) {
if (n.getNodeType() == NodeType.Element) { if (n.getNodeType() == NodeType.Element) {
n.style(removedStyle()); n.style(removedStyle());
} }
} }
ndiv.br(); ndiv.br();
ndiv.copyAllContent(div); ndiv.addChildren(div);
} }
return ndiv; return ndiv;
} }
} }
private List<XhtmlNode> fixFontSizes(List<XhtmlNode> nodes, int size) {
for (XhtmlNode x : nodes) {
if (Utilities.existsInList(x.getName(), "p", "li") && !x.hasAttribute("style")) {
x.style("font-size: "+size+"px");
}
if (x.hasChildren()) {
fixFontSizes(x.getChildNodes(), size);
}
}
return nodes;
}
private boolean areEqual(PrimitiveType compare, PrimitiveType md) { private boolean areEqual(PrimitiveType compare, PrimitiveType md) {
if (compare == null && md == null) { if (compare == null && md == null) {
return true; return true;
@ -3934,7 +3948,6 @@ public class StructureDefinitionRenderer extends ResourceRenderer {
list.merge(new DiscriminatorWithStatus(d)); list.merge(new DiscriminatorWithStatus(d));
} }
} }
x.tx(", and can be differentiated using the following discriminators: ");
var ul = x.ul(); var ul = x.ul();
for (DiscriminatorWithStatus rc : list) { for (DiscriminatorWithStatus rc : list) {
rc.render(x.li()); rc.render(x.li());
@ -4316,7 +4329,7 @@ public class StructureDefinitionRenderer extends ResourceRenderer {
MarkdownType newBinding = PublicationHacker.fixBindingDescriptions(context.getContext(), binding.getDescriptionElement()); MarkdownType newBinding = PublicationHacker.fixBindingDescriptions(context.getContext(), binding.getDescriptionElement());
if (mode == GEN_MODE_SNAP || mode == GEN_MODE_MS) { if (mode == GEN_MODE_SNAP || mode == GEN_MODE_MS) {
bindingDesc = new XhtmlNode(NodeType.Element, "div"); bindingDesc = new XhtmlNode(NodeType.Element, "div");
bindingDesc.add(new XhtmlParser().parseFragment(hostMd.processMarkdown("Binding.description", newBinding))); bindingDesc.addChildren(new XhtmlParser().parseMDFragment(hostMd.processMarkdown("Binding.description", newBinding)));
} else { } else {
StringType oldBinding = compBinding != null && compBinding.hasDescription() ? PublicationHacker.fixBindingDescriptions(context.getContext(), compBinding.getDescriptionElement()) : null; StringType oldBinding = compBinding != null && compBinding.hasDescription() ? PublicationHacker.fixBindingDescriptions(context.getContext(), compBinding.getDescriptionElement()) : null;

View File

@ -1285,6 +1285,11 @@ public class XhtmlParser {
} }
} }
public List<XhtmlNode> parseMDFragment(String source) throws IOException, FHIRException {
XhtmlNode div = parseFragment( "<div>"+source+"</div>");
return div.getChildNodes();
}
public XhtmlNode parseFragment(String source) throws IOException, FHIRException { public XhtmlNode parseFragment(String source) throws IOException, FHIRException {
rdr = new StringReader(source); rdr = new StringReader(source);
try { try {