Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ways?] seem to work
  • Loading branch information
vorburger committed Aug 10, 2024
1 parent 9e78a1e commit 148278a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 21 deletions.
22 changes: 20 additions & 2 deletions java/dev/enola/common/io/metadata/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
* @param imageHTML HTML of an image for the IRI, e.g. <img...> with URL of a favicon or something
* like that; or an 😃 Emoji!
* <p>Always returns something (never null), but may be empty String.
* @param imageURL URL (not HTML) of an image for the IRI, but never the 😃 Emoji.
* <p>Always returns something (never null), but may be empty String.
* @param emoji An 😃 Emoji for the IRI, as String (not URL or HTML).
* <p>Always returns something (never null), but may be empty String.
* @param curie An IRI converted to a "CURIE" (e.g. rdfs:Class), if available.
* <p>Always returns something (never null), but may be empty String if no suitable CURIE could
* be determined.
Expand All @@ -42,17 +46,31 @@
@Immutable
// TODO Metadata implements Thing!
public record Metadata(
String iri, String imageHTML, String curie, String label, String descriptionHTML) {
String iri,
String imageHTML,
String imageURL,
String emoji,
String curie,
String label,
String descriptionHTML) {

public static final Comparator<Metadata> IRI_Comparator =
(o1, o2) ->
Comparator.nullsFirst(Comparator.<String>naturalOrder())
.compare(o1.iri, o2.iri);

public Metadata(
String iri, String imageHTML, String curie, String label, String descriptionHTML) {
String iri,
String imageHTML,
String imageURL,
String emoji,
String curie,
String label,
String descriptionHTML) {
this.iri = requireNonEmpty(iri, "iri", iri);
this.imageHTML = requireNonNull(imageHTML, () -> "imageHTML of " + iri).trim();
this.imageURL = requireNonNull(imageURL, () -> "imageURL of " + iri).trim();
this.emoji = requireNonNull(emoji, () -> "emoji of " + iri).trim();
this.curie = requireNonNull(curie, () -> "curie of " + iri);
this.label = requireNonEmpty(label, "label", iri);
this.descriptionHTML =
Expand Down
53 changes: 43 additions & 10 deletions java/dev/enola/thing/ThingMetadataProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,25 @@ public Metadata get(@Nullable Object object, String iri) {
}

private Metadata get(@Nullable Thing thing, String fallbackIRI) {
var imageURL = getImageURL_(thing);
if (imageURL == null) imageURL = "";

var emoji = getEmoji_(thing);
if (emoji == null) emoji = "";

var imageHTML = getImageHTML(thing);
var descriptionHTML = getDescriptionHTML(thing);
var curie = ns.toCURIE(fallbackIRI);
var label = getLabel(thing, curie, fallbackIRI);
var curieIfDifferentFromFallbackIRI = !curie.equals(fallbackIRI) ? curie : "";
return new Metadata(
fallbackIRI, imageHTML, curieIfDifferentFromFallbackIRI, label, descriptionHTML);
fallbackIRI,
imageHTML,
imageURL,
emoji,
curieIfDifferentFromFallbackIRI,
label,
descriptionHTML);
}

/**
Expand Down Expand Up @@ -149,21 +161,44 @@ private String getImageHTML(Thing thing) {
var thingImage = getImageHTML_(thing);
if (thingImage != null) return thingImage;

thingImage = getAlternative(thing, KIRI.RDFS.RANGE, thingX -> getImageHTML_(thingX));
if (thingImage != null) return thingImage;

thingImage = getAlternative(thing, KIRI.RDF.TYPE, thingX -> getImageHTML_(thingX));
if (thingImage != null) return thingImage;

return "";
}

private @Nullable String getImageHTML_(Thing thing) {
if (thing == null) return null;

var emoji = getEmoji_(thing);
if (emoji != null) return emoji;

var imageURL = getImageURL_(thing);
if (imageURL != null) return html(imageURL);

// TODO Also support (and test) https://schema.org/ImageObject
// for https://schema.org/thumbnail and https://schema.org/logo

return null;
}

private @Nullable String getEmoji_(Thing thing) {
var emoji = getString(thing, KIRI.E.EMOJI);
if (emoji != null) return emoji;

emoji = getAlternative(thing, KIRI.RDFS.RANGE, thingX -> getEmoji_(thingX));
if (emoji != null) return emoji;

emoji = getAlternative(thing, KIRI.RDF.TYPE, thingX -> getEmoji_(thingX));
return emoji;
}

private @Nullable String getImageURL_(Thing thing) {
var imageURL = getAlternative(thing, KIRI.RDFS.RANGE, thingX -> getImageURL__(thingX));
if (imageURL != null) return imageURL;

imageURL = getAlternative(thing, KIRI.RDF.TYPE, thingX -> getImageURL__(thingX));
return imageURL;
}

private @Nullable String getImageURL__(Thing thing) {
var imageURL = getString(thing, KIRI.SCHEMA.IMG);
if (imageURL != null) return html(imageURL);

Expand All @@ -173,8 +208,6 @@ private String getImageHTML(Thing thing) {
imageURL = getString(thing, KIRI.SCHEMA.THUMBNAIL_URL);
if (imageURL != null) return html(imageURL);

// TODO Also support (and test) https://schema.org/ImageObject
// for https://schema.org/thumbnail and https://schema.org/logo
return null;
}

Expand All @@ -186,7 +219,7 @@ private String html(String imageURL) {
private @Nullable String getAlternative(
Thing thing, String viaPropertyIRI, Function<Thing, String> alt) {
var alternativeThingIRI = getString(thing, viaPropertyIRI);
if (alternativeThingIRI != null) {
if (alternativeThingIRI != null && !alternativeThingIRI.equals(thing.iri())) {
var alternativeThing = tp.get(alternativeThingIRI);
if (alternativeThing != null) {
var alternativeSource = alt.apply(alternativeThing);
Expand Down
2 changes: 1 addition & 1 deletion java/dev/enola/thing/gen/graphviz/GraphvizGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public boolean convertInto(Iterable<Thing> from, Appendable out)
}

private String label(Metadata metadata) {
return metadata.imageHTML() + metadata.label();
return metadata.emoji() + metadata.label();
}

private void printThing(Thing thing, Appendable out) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public MarkdownIndexGenerator(

void generate(Writer writer, URI outputIRI, URI base, TemplateService ts) throws IOException {
var tree = new ImmutableTreeBuilder<ThingOrHeading>();
var rootMetadata = new ThingOrHeading(null, new Metadata("fake:/", "☸", "", "Things", ""));
var rootMetadata =
new ThingOrHeading(null, new Metadata("fake:/", "☸", "", "☸", "", "Things", ""));
tree.root(rootMetadata);

for (var metadata : metas) {
Expand Down
15 changes: 11 additions & 4 deletions java/dev/enola/thing/gen/markdown/MarkdownLinkWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public void writeMarkdownLink() throws IOException {
var sb = new StringBuilder();
new MarkdownLinkWriter(Templates.Format.Mustache)
.writeMarkdownLink(
new Metadata("https://example.org/greeting42", "", "", "greeting42", ""),
new Metadata(
"https://example.org/greeting42", "", "", "", "", "greeting42", ""),
sb,
URI.create("file:///out/greeting.md"),
URI.create("file:///out/"),
Expand All @@ -55,6 +56,8 @@ public void writeMarkdownLinkWithCurieAndSameLabel() throws IOException {
new Metadata(
"https://example.org/greeting42",
"",
"",
"",
"ex:greeting42",
"greeting42",
""),
Expand All @@ -74,6 +77,8 @@ public void writeMarkdownLinkWithCurieAndBetterLabel() throws IOException {
new Metadata(
"https://example.org/greeting42",
"",
"",
"",
"ex:greeting42",
"Da Greeting! ;)",
""),
Expand All @@ -91,7 +96,8 @@ public void unknownIRIs() throws IOException {
var sb = new StringBuilder();
new MarkdownLinkWriter(Templates.Format.Mustache)
.writeMarkdownLink(
new Metadata("https://unknown.org/whatever", "", "", "whatever", ""),
new Metadata(
"https://unknown.org/whatever", "", "", "", "", "whatever", ""),
sb,
URI.create("file:///out/greeting.md"),
URI.create("file:///out/"),
Expand All @@ -105,7 +111,7 @@ public void knownIRIs() throws IOException {
var sb = new StringBuilder();
new MarkdownLinkWriter(Templates.Format.Mustache)
.writeMarkdownLink(
new Metadata("https://enola.dev/emoji", "", "", "emoji", ""),
new Metadata("https://enola.dev/emoji", "", "", "", "", "emoji", ""),
sb,
URI.create("file:///out/greeting.md"),
URI.create("file:///out/"),
Expand All @@ -120,7 +126,8 @@ public void writeTemplateMarkdownLink() throws IOException {
var sb = new StringBuilder();
new MarkdownLinkWriter(Templates.Format.Mustache)
.writeMarkdownLink(
new Metadata("https://example.org/greeting42", "", "", "greeting42", ""),
new Metadata(
"https://example.org/greeting42", "", "", "", "", "greeting42", ""),
sb,
URI.create("file:///out/greeting.md"),
URI.create("file:///out/"),
Expand Down
4 changes: 2 additions & 2 deletions test/graphviz.expected.gv
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ digraph {
"https://example.org/greeting3" -> "classpath:/graphviz.ttl" [label="enola:origin"]

"https://example.org/world" [shape=plain label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
<TR><TD COLSPAN="2"><img src="https://en.wikipedia.org/wiki/Earth#/media/File:The_Blue_Marble_(remastered).jpg" style="max-height: 1em;">ex:world</TD></TR>
<TR><TD COLSPAN="2">ex:world</TD></TR>
<TR><TD ALIGN="left">enola:wikipedia</TD><TD>https://en.wikipedia.org/wiki/Earth</TD></TR>
<TR><TD ALIGN="left">schema:image</TD><TD>https://en.wikipedia.org/wiki/Earth#/media/File:The_Blue_Marble_(remastered).jpg</TD></TR>
<TR><TD ALIGN="left">schema:image</TD><TD>https://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/The_Blue_Marble_%28remastered%29.jpg/480px-The_Blue_Marble_%28remastered%29.jpg</TD></TR>
</TABLE>>]
"https://example.org/world" -> "classpath:/graphviz.ttl" [label="enola:origin"]

Expand Down
2 changes: 1 addition & 1 deletion test/graphviz.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@

:world
enola:wikipedia "https://en.wikipedia.org/wiki/Earth";
schema:image "https://en.wikipedia.org/wiki/Earth#/media/File:The_Blue_Marble_(remastered).jpg".
schema:image "https://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/The_Blue_Marble_%28remastered%29.jpg/480px-The_Blue_Marble_%28remastered%29.jpg".

0 comments on commit 148278a

Please sign in to comment.