diff --git a/java/dev/enola/common/io/metadata/Metadata.java b/java/dev/enola/common/io/metadata/Metadata.java index bd47e7332..4e0d5aefd 100644 --- a/java/dev/enola/common/io/metadata/Metadata.java +++ b/java/dev/enola/common/io/metadata/Metadata.java @@ -30,6 +30,10 @@ * @param imageHTML HTML of an image for the IRI, e.g. with URL of a favicon or something * like that; or an 😃 Emoji! *

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. + *

Always returns something (never null), but may be empty String. + * @param emoji An 😃 Emoji for the IRI, as String (not URL or HTML). + *

Always returns something (never null), but may be empty String. * @param curie An IRI converted to a "CURIE" (e.g. rdfs:Class), if available. *

Always returns something (never null), but may be empty String if no suitable CURIE could * be determined. @@ -42,7 +46,13 @@ @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 IRI_Comparator = (o1, o2) -> @@ -50,9 +60,17 @@ public record Metadata( .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 = diff --git a/java/dev/enola/thing/ThingMetadataProvider.java b/java/dev/enola/thing/ThingMetadataProvider.java index ee819830c..9890a53bf 100644 --- a/java/dev/enola/thing/ThingMetadataProvider.java +++ b/java/dev/enola/thing/ThingMetadataProvider.java @@ -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); } /** @@ -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); @@ -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; } @@ -186,7 +219,7 @@ private String html(String imageURL) { private @Nullable String getAlternative( Thing thing, String viaPropertyIRI, Function 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); diff --git a/java/dev/enola/thing/gen/graphviz/GraphvizGenerator.java b/java/dev/enola/thing/gen/graphviz/GraphvizGenerator.java index 88d9e2ad2..fee1f8785 100644 --- a/java/dev/enola/thing/gen/graphviz/GraphvizGenerator.java +++ b/java/dev/enola/thing/gen/graphviz/GraphvizGenerator.java @@ -72,7 +72,7 @@ public boolean convertInto(Iterable 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 { diff --git a/java/dev/enola/thing/gen/markdown/MarkdownIndexGenerator.java b/java/dev/enola/thing/gen/markdown/MarkdownIndexGenerator.java index e7f0eb1cc..766510bf1 100644 --- a/java/dev/enola/thing/gen/markdown/MarkdownIndexGenerator.java +++ b/java/dev/enola/thing/gen/markdown/MarkdownIndexGenerator.java @@ -67,7 +67,8 @@ public MarkdownIndexGenerator( void generate(Writer writer, URI outputIRI, URI base, TemplateService ts) throws IOException { var tree = new ImmutableTreeBuilder(); - 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) { diff --git a/java/dev/enola/thing/gen/markdown/MarkdownLinkWriterTest.java b/java/dev/enola/thing/gen/markdown/MarkdownLinkWriterTest.java index b5dce3c51..4404f078d 100644 --- a/java/dev/enola/thing/gen/markdown/MarkdownLinkWriterTest.java +++ b/java/dev/enola/thing/gen/markdown/MarkdownLinkWriterTest.java @@ -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/"), @@ -55,6 +56,8 @@ public void writeMarkdownLinkWithCurieAndSameLabel() throws IOException { new Metadata( "https://example.org/greeting42", "", + "", + "", "ex:greeting42", "greeting42", ""), @@ -74,6 +77,8 @@ public void writeMarkdownLinkWithCurieAndBetterLabel() throws IOException { new Metadata( "https://example.org/greeting42", "", + "", + "", "ex:greeting42", "Da Greeting! ;)", ""), @@ -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/"), @@ -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/"), @@ -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/"), diff --git a/test/graphviz.expected.gv b/test/graphviz.expected.gv index d18637a5b..f998a7cca 100644 --- a/test/graphviz.expected.gv +++ b/test/graphviz.expected.gv @@ -15,9 +15,9 @@ digraph { "https://example.org/greeting3" -> "classpath:/graphviz.ttl" [label="enola:origin"] "https://example.org/world" [shape=plain label=< - + - +
ex:world
ex:world
enola:wikipediahttps://en.wikipedia.org/wiki/Earth
schema:imagehttps://en.wikipedia.org/wiki/Earth#/media/File:The_Blue_Marble_(remastered).jpg
schema:imagehttps://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/The_Blue_Marble_%28remastered%29.jpg/480px-The_Blue_Marble_%28remastered%29.jpg
>] "https://example.org/world" -> "classpath:/graphviz.ttl" [label="enola:origin"] diff --git a/test/graphviz.ttl b/test/graphviz.ttl index 5b4210d6e..7d11ced63 100644 --- a/test/graphviz.ttl +++ b/test/graphviz.ttl @@ -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".