Skip to content

Commit

Permalink
Changed Tag to interface, removed parent tag, .end()
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Oct 1, 2023
1 parent 5f156fe commit 7f4d6eb
Show file tree
Hide file tree
Showing 13 changed files with 276 additions and 310 deletions.
2 changes: 1 addition & 1 deletion src/main/java/dev/latvian/apps/webutils/ansi/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void fill(Tag cellTag) {
}

public String unformattedValue() {
return tag == null ? value.toUnformattedString() : tag.getRawContent();
return tag == null ? value.toUnformattedString() : tag.toRawString();
}

public int unformattedLength() {
Expand Down
28 changes: 0 additions & 28 deletions src/main/java/dev/latvian/apps/webutils/html/LazyRawTag.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package dev.latvian.apps.webutils.html;

import dev.latvian.apps.webutils.data.Lazy;

public record LazyTagConvertible(Lazy<? extends TagConvertible> lazy) implements TagConvertible {
@Override
public void appendHTMLTag(Tag parent) {
var t = lazy.get();

if (t != null) {
t.appendHTMLTag(parent);
}
}
}
44 changes: 18 additions & 26 deletions src/main/java/dev/latvian/apps/webutils/html/PairedTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import dev.latvian.apps.webutils.ansi.Ansi;
import dev.latvian.apps.webutils.ansi.AnsiComponent;

import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -40,7 +39,6 @@ public Tag addAnd(Tag content) {
this.content = new ArrayList<>();
}

content.parent = this;
this.content.add(content);
return content;
}
Expand All @@ -54,21 +52,6 @@ public Tag getChild(int index) {
return content.get(index);
}

@Override
public String getRawContent() {
if (content != null && !content.isEmpty()) {
var builder = new StringBuilder();

for (var tag : content) {
builder.append(tag.getRawContent());
}

return builder.toString();
}

return "";
}

@Override
public boolean isEmpty() {
return content == null || content.isEmpty();
Expand Down Expand Up @@ -119,24 +102,33 @@ public void replace(Pattern pattern, BiConsumer<Tag, Matcher> replace) {
}

@Override
public void write(Writer writer) throws Throwable {
public void append(StringBuilder builder, boolean header) {
if (!this.name.isEmpty()) {
writer.write('<');
writer.write(this.name);
TagUtils.writeAttributes(writer, this.attributes);
writer.write('>');
builder.append('<');
builder.append(this.name);
TagUtils.writeAttributes(builder, this.attributes);
builder.append('>');
}

if (this.content != null && !this.content.isEmpty()) {
for (var tag : this.content) {
tag.write(writer);
tag.append(builder, header);
}
}

if (!this.name.isEmpty()) {
writer.write("</");
writer.write(this.name);
writer.write('>');
builder.append("</");
builder.append(this.name);
builder.append('>');
}
}

@Override
public void appendRaw(StringBuilder builder) {
if (content != null && !content.isEmpty()) {
for (var tag : content) {
tag.appendRaw(builder);
}
}
}

Expand Down
17 changes: 16 additions & 1 deletion src/main/java/dev/latvian/apps/webutils/html/RawTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@ public RawTag(String string) {
}

@Override
public String getRawContent() {
public String toRawString() {
return string;
}

@Override
public String toTagString(boolean header) {
return string;
}

@Override
public void append(StringBuilder builder, boolean header) {
builder.append(string);
}

@Override
public void appendRaw(StringBuilder builder) {
builder.append(string);
}

@Override
public StringTag child(String string) {
return new RawTag(string);
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/dev/latvian/apps/webutils/html/ResponseTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.latvian.apps.webutils.html;

import dev.latvian.apps.webutils.net.FileResponse;
import dev.latvian.apps.webutils.net.Response;
import io.javalin.http.HttpStatus;

import java.nio.charset.StandardCharsets;

public interface ResponseTag {
String getMimeType();

default Response asResponse() {
return asResponse(HttpStatus.OK, true);
}

default Response asResponse(HttpStatus status, boolean header) {
return FileResponse.of(status, getMimeType(), ((Tag) this).toTagString(header).getBytes(StandardCharsets.UTF_8));
}
}
24 changes: 9 additions & 15 deletions src/main/java/dev/latvian/apps/webutils/html/RootTag.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package dev.latvian.apps.webutils.html;

import dev.latvian.apps.webutils.net.FileResponse;
import dev.latvian.apps.webutils.net.Response;
import io.javalin.http.HttpStatus;
import dev.latvian.apps.webutils.net.MimeType;

import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class RootTag extends PairedTag {
public class RootTag extends PairedTag implements ResponseTag {
public static String SITE_NAME = "";
public static String ROOT_URL = "";
public static String ICON_PATH = "";
Expand Down Expand Up @@ -79,18 +75,16 @@ public void refresh(String url) {
}

@Override
public Tag getRoot() {
return this;
}
public void append(StringBuilder builder, boolean header) {
if (header) {
builder.append("<!DOCTYPE html>");
}

@Override
public void write(Writer writer) throws Throwable {
writer.write("<!DOCTYPE html>");
super.write(writer);
super.append(builder, header);
}

@Override
public Response asResponse(HttpStatus status) {
return FileResponse.of(status, "text/html; charset=utf-8", toString().getBytes(StandardCharsets.UTF_8));
public String getMimeType() {
return MimeType.HTML;
}
}
23 changes: 7 additions & 16 deletions src/main/java/dev/latvian/apps/webutils/html/StringTag.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
package dev.latvian.apps.webutils.html;

import dev.latvian.apps.webutils.ansi.AnsiComponent;

import java.io.Writer;

public class StringTag extends Tag {
public class StringTag implements Tag {
public final String string;

public StringTag(String string) {
this.string = string;
}

@Override
public String getRawContent() {
return TagUtils.encode(string);
public String toString() {
return string;
}

@Override
public boolean isEmpty() {
return string.isEmpty();
}

@Override
public boolean isEmptyRecursively() {
return string.isEmpty();
}

public StringTag child(String string) {
return new StringTag(string);
}

@Override
public void write(Writer writer) throws Throwable {
writer.write(getRawContent());
public void append(StringBuilder builder, boolean header) {
TagUtils.encode(builder, string);
}

@Override
public void ansi(AnsiComponent component, int depth, int indent) {
component.append(getRawContent());
public void appendRaw(StringBuilder builder) {
TagUtils.encode(builder, string);
}
}
Loading

0 comments on commit 7f4d6eb

Please sign in to comment.