Skip to content

Commit

Permalink
Hack Ginevra: Support <source> and <video>
Browse files Browse the repository at this point in the history
  • Loading branch information
nipafx committed Apr 13, 2024
1 parent 91f30d3 commit 5dbff88
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ public sealed interface HtmlElement extends KnownElement permits
OrderedList,
Paragraph,
Pre,
Source,
Span,
Strong,
UnorderedList {
UnorderedList,
Video {

Anchor a = new Anchor();
BlockQuote blockquote = new BlockQuote();
Expand All @@ -46,8 +48,10 @@ public sealed interface HtmlElement extends KnownElement permits
OrderedList ol = new OrderedList();
Paragraph p = new Paragraph();
Pre pre = new Pre();
Source source = new Source();
Span span = new Span();
Strong strong = new Strong();
UnorderedList ul = new UnorderedList();
Video video = new Video();

}
16 changes: 15 additions & 1 deletion ginevra/src/main/java/dev/nipafx/ginevra/html/Source.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
package dev.nipafx.ginevra.html;

record Source(String path) implements Src { }
public record Source(Src src, String type) implements HtmlElement {

public Source() {
this(Src.none(), null);
}

public Source src(Src src) {
return new Source(src, this.type);
}

public Source type(String type) {
return new Source(this.src, type);
}

}
6 changes: 4 additions & 2 deletions ginevra/src/main/java/dev/nipafx/ginevra/html/Src.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
public interface Src {

static Src none() {
return new Source(null);
return new Direct(null);
}

static Src direct(String path) {
return new Source(path);
return new Direct(path);
}

String path();

record Direct(String path) implements Src { }

}
127 changes: 127 additions & 0 deletions ginevra/src/main/java/dev/nipafx/ginevra/html/Video.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package dev.nipafx.ginevra.html;

import java.util.List;

public record Video(
Id id, Classes classes, Src src, Integer height, Integer width, Src poster, Preload preload,
Boolean autoplay, Boolean loop, Boolean muted, Boolean playinline,
Boolean controls, Boolean disablepictureinpicture, Boolean disableremoteplayback,
List<? extends Element> children) implements HtmlElement {

public Video() {
this(
Id.none(), Classes.none(), Src.none(), null, null, null, null,
null, null, null, null, null, null, null, List.of());
}

public Video id(Id id) {
return new Video(
id, classes, src, height, width, poster, preload,
autoplay, loop, muted, playinline, controls, disablepictureinpicture, disableremoteplayback, children);
}

public Video classes(Classes classes) {
return new Video(
id, classes, src, height, width, poster, preload,
autoplay, loop, muted, playinline, controls, disablepictureinpicture, disableremoteplayback, children);
}

public Video src(Src src) {
return new Video(
id, classes, src, height, width, poster, preload,
autoplay, loop, muted, playinline, controls, disablepictureinpicture, disableremoteplayback, children);
}

public Video height(Integer height) {
return new Video(
id, classes, src, height, width, poster, preload,
autoplay, loop, muted, playinline, controls, disablepictureinpicture, disableremoteplayback, children);
}

public Video width(Integer width) {
return new Video(
id, classes, src, height, width, poster, preload,
autoplay, loop, muted, playinline, controls, disablepictureinpicture, disableremoteplayback, children);
}

public Video poster(Src poster) {
return new Video(
id, classes, src, height, width, poster, preload,
autoplay, loop, muted, playinline, controls, disablepictureinpicture, disableremoteplayback, children);
}

public Video preload(Preload preload) {
return new Video(
id, classes, src, height, width, poster, preload,
autoplay, loop, muted, playinline, controls, disablepictureinpicture, disableremoteplayback, children);
}

public Video autoplay(Boolean autoplay) {
return new Video(
id, classes, src, height, width, poster, preload,
autoplay, loop, muted, playinline, controls, disablepictureinpicture, disableremoteplayback, children);
}

public Video loop(Boolean loop) {
return new Video(
id, classes, src, height, width, poster, preload,
autoplay, loop, muted, playinline, controls, disablepictureinpicture, disableremoteplayback, children);
}

public Video muted(Boolean muted) {
return new Video(
id, classes, src, height, width, poster, preload,
autoplay, loop, muted, playinline, controls, disablepictureinpicture, disableremoteplayback, children);
}

public Video playinline(Boolean playinline) {
return new Video(
id, classes, src, height, width, poster, preload,
autoplay, loop, muted, playinline, controls, disablepictureinpicture, disableremoteplayback, children);
}

public Video controls(Boolean controls) {
return new Video(
id, classes, src, height, width, poster, preload,
autoplay, loop, muted, playinline, controls, disablepictureinpicture, disableremoteplayback, children);
}

public Video disablepictureinpicture(Boolean disablepictureinpicture) {
return new Video(
id, classes, src, height, width, poster, preload,
autoplay, loop, muted, playinline, controls, disablepictureinpicture, disableremoteplayback, children);
}

public Video disableremoteplayback(Boolean disableremoteplayback) {
return new Video(
id, classes, src, height, width, poster, preload,
autoplay, loop, muted, playinline, controls, disablepictureinpicture, disableremoteplayback, children);
}

public Video children(List<? extends Element> children) {
return new Video(
id, classes, src, height, width, poster, preload,
autoplay, loop, muted, playinline, controls, disablepictureinpicture, disableremoteplayback, children);
}

public Video children(Element... children) {
return new Video(
id, classes, src, height, width, poster, preload,
autoplay, loop, muted, playinline, controls, disablepictureinpicture, disableremoteplayback, List.of(children));
}

public enum Preload {
NONE, METADATA, AUTO;

@Override
public String toString() {
return switch (this) {
case NONE -> "none";
case METADATA -> "metadata";
case AUTO -> "auto";
};
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import dev.nipafx.ginevra.html.OrderedList;
import dev.nipafx.ginevra.html.Paragraph;
import dev.nipafx.ginevra.html.Pre;
import dev.nipafx.ginevra.html.Source;
import dev.nipafx.ginevra.html.Span;
import dev.nipafx.ginevra.html.Strong;
import dev.nipafx.ginevra.html.Text;
import dev.nipafx.ginevra.html.UnorderedList;
import dev.nipafx.ginevra.html.Video;
import dev.nipafx.ginevra.outline.CustomQueryElement;
import dev.nipafx.ginevra.outline.Query.CollectionQuery;
import dev.nipafx.ginevra.outline.Query.RootQuery;
Expand Down Expand Up @@ -125,6 +127,7 @@ private List<KnownElement> resolve(Element element, ResourceGatherer resources)
.toList());
case Paragraph el -> el.children(resolveChildren(el.children(), resources));
case Pre el -> el.children(resolveChildren(el.children(), resources));
case Source el -> el.src(resources.includeResource(el.src()));
case Span el -> el.children(resolveChildren(el.children(), resources));
case Strong el -> el.children(resolveChildren(el.children(), resources));
case UnorderedList list -> list.children(list
Expand All @@ -134,6 +137,10 @@ private List<KnownElement> resolve(Element element, ResourceGatherer resources)
.flatMap(listItemChild -> resolve(listItemChild, resources).stream())
.toList()))
.toList());
case Video el -> el
.src(resources.includeResource(el.src()))
.poster(resources.includeResource(el.poster()))
.children(resolveChildren(el.children(), resources));
};
yield List.of(resolvedElement);
}
Expand Down
13 changes: 11 additions & 2 deletions ginevra/src/main/java/dev/nipafx/ginevra/render/HtmlRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.nipafx.ginevra.html.Classes;
import dev.nipafx.ginevra.html.Id;

import java.util.List;
import java.util.Map;

class HtmlRenderer {
Expand Down Expand Up @@ -32,17 +33,21 @@ public void open(String tag, Map<String, String> attributes) {
}

public void open(String tag, Id id, Classes classes, Map<String, String> attributes) {
open(tag, id, classes, attributes, List.of());
}

public void open(String tag, Id id, Classes classes, Map<String, String> attributes, List<String> properties) {
switch (state) {
case EMPTY -> builder.repeat("\t", indentation);
case OPENED, CLOSED -> builder.append("\n").repeat("\t", indentation);
case INLINE -> {
}
case INLINE -> { }
}

builder.append("<").append(tag);
attribute("id", id.asString());
attribute("class", classes.asCssString());
attributes.forEach(this::attribute);
properties.forEach(this::property);
builder.append(">");

indentation++;
Expand All @@ -56,6 +61,10 @@ private void attribute(String name, String value) {
builder.append(" ").append(name).append("=\"").append(value).append("\"");
}

private void property(String name) {
builder.append(" ").append(name);
}

public void close(String tag) {
indentation--;
switch (state) {
Expand Down
30 changes: 25 additions & 5 deletions ginevra/src/main/java/dev/nipafx/ginevra/render/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import dev.nipafx.ginevra.html.OrderedList;
import dev.nipafx.ginevra.html.Paragraph;
import dev.nipafx.ginevra.html.Pre;
import dev.nipafx.ginevra.html.Source;
import dev.nipafx.ginevra.html.Span;
import dev.nipafx.ginevra.html.Strong;
import dev.nipafx.ginevra.html.Text;
import dev.nipafx.ginevra.html.UnorderedList;
import dev.nipafx.ginevra.html.Video;
import dev.nipafx.ginevra.outline.Template;

import java.nio.file.Path;
Expand Down Expand Up @@ -140,8 +142,7 @@ case ListItem(var id, var classes, var text, var children) -> {
}
case Meta(var name, var content) -> html.selfClosed("meta", attributes("name", name, "content", content));
case OrderedList(var id, var classes, var start, var children) -> {
html.open("ol", id, classes,
attributes("start", start == null ? null : String.valueOf(start)));
html.open("ol", id, classes, attributes("start", start));
renderChildren(children, html);
html.close("ol");
}
Expand All @@ -155,6 +156,7 @@ case Pre(var id, var classes, var text, var children) -> {
renderChildren(text, children, html);
html.close("pre");
}
case Source(var src, var type) -> html.selfClosed("source", attributes("src", src.path(), "type", type));
case Span(var id, var classes, var text, var children) -> {
html.open("span", id, classes);
renderChildren(text, children, html);
Expand All @@ -170,6 +172,20 @@ case UnorderedList(var id, var classes, var children) -> {
renderChildren(children, html);
html.close("ul");
}
case Video(
var id, var classes, var src, var height, var width, var poster, var preload,
var autoplay, var loop, var muted, var playinline,
var controls, var disablepictureinpicture, var disableremoteplayback,
var children) -> {
var attributes = attributes(
"src", src.path(), "height", height, "width", width, "poster", poster.path(), "preload", preload,
"autoplay", autoplay, "loop", loop, "muted", muted, "playinline", playinline,
"disablepictureinpicture", disablepictureinpicture, "disableremoteplayback", disableremoteplayback);
var properties = controls ? List.of("controls") : List.<String> of();
html.open("video", id, classes, attributes, properties);
renderChildren(children, html);
html.close("video");
}
}
}
case JmlElement jmlElement -> {
Expand All @@ -196,13 +212,17 @@ private void renderChildren(List<? extends Element> children, HtmlRenderer rende
children.forEach(child -> writeToRenderer(child, renderer));
}

private static Map<String, String> attributes(String... namesAndValues) {
private static Map<String, String> attributes(Object... namesAndValues) {
if (namesAndValues.length % 2 != 0)
throw new IllegalArgumentException();

var attributes = new LinkedHashMap<String, String>();
for (int i = 0; i < namesAndValues.length; i += 2)
attributes.put(namesAndValues[i], namesAndValues[i + 1]);
for (int i = 0; i < namesAndValues.length; i += 2) {
if (!(namesAndValues[i] instanceof String name))
throw new IllegalArgumentException();
var value = namesAndValues[i + 1] == null ? null : namesAndValues[i + 1].toString();
attributes.put(name, value);
}
return attributes;
}

Expand Down

0 comments on commit 5dbff88

Please sign in to comment.