Skip to content

Commit

Permalink
Added Tag#toAnsi()
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Sep 18, 2023
1 parent 3f3450e commit 3c14883
Show file tree
Hide file tree
Showing 7 changed files with 385 additions and 309 deletions.
24 changes: 24 additions & 0 deletions src/main/java/dev/latvian/apps/webutils/html/PairedTag.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dev.latvian.apps.webutils.html;

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;
Expand Down Expand Up @@ -136,4 +139,25 @@ public void write(Writer writer) throws Throwable {
writer.write('>');
}
}

@Override
public void ansi(AnsiComponent component, int depth) {
int col = TagUtils.ANSI_COLORS[depth % TagUtils.ANSI_COLORS.length];

if (!this.name.isEmpty()) {
component.append(Ansi.of("<" + this.name).color(col));
TagUtils.ansiAttributes(component, this.attributes, depth);
component.append(Ansi.of(">").color(col));
}

if (this.content != null && !this.content.isEmpty()) {
for (var tag : this.content) {
tag.ansi(component, depth + 1);
}
}

if (!this.name.isEmpty()) {
component.append(Ansi.of("</" + this.name + ">").color(col));
}
}
}
7 changes: 0 additions & 7 deletions src/main/java/dev/latvian/apps/webutils/html/RawTag.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dev.latvian.apps.webutils.html;

import java.io.Writer;

public class RawTag extends StringTag {
public RawTag(String string) {
super(string);
Expand All @@ -16,9 +14,4 @@ public String getRawContent() {
public StringTag child(String string) {
return new RawTag(string);
}

@Override
public void write(Writer writer) throws Throwable {
writer.write(string);
}
}
9 changes: 8 additions & 1 deletion src/main/java/dev/latvian/apps/webutils/html/StringTag.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.latvian.apps.webutils.html;

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

import java.io.Writer;

public class StringTag extends Tag {
Expand Down Expand Up @@ -30,6 +32,11 @@ public StringTag child(String string) {

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

@Override
public void ansi(AnsiComponent component, int depth) {
component.append(getRawContent());
}
}
41 changes: 36 additions & 5 deletions src/main/java/dev/latvian/apps/webutils/html/Tag.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package dev.latvian.apps.webutils.html;

import dev.latvian.apps.webutils.ansi.Ansi;
import dev.latvian.apps.webutils.ansi.AnsiComponent;
import dev.latvian.apps.webutils.net.Response;
import io.javalin.http.Context;
import io.javalin.http.HttpStatus;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.io.StringWriter;
Expand Down Expand Up @@ -54,12 +54,10 @@ public final Tag add(TagConvertible tag) {
return this;
}

@ApiStatus.Internal
public Tag attr(String key, Object value) {
throw new IllegalStateException("This tag type does not support attributes");
}

@ApiStatus.Internal
public Tag attr(String key) {
return attr(key, "<NO_VALUE>");
}
Expand Down Expand Up @@ -98,6 +96,8 @@ public void replace(Pattern pattern, BiConsumer<Tag, Matcher> replace) {

public abstract void write(Writer writer) throws Throwable;

public abstract void ansi(AnsiComponent component, int depth);

@Override
public String toString() {
var writer = new StringWriter();
Expand All @@ -114,6 +114,12 @@ public String toString() {
return writer.toString();
}

public AnsiComponent toAnsi() {
var component = Ansi.of();
ansi(component, 0);
return component;
}

public void result(Context ctx) {
result(ctx, HttpStatus.OK);
}
Expand Down Expand Up @@ -158,14 +164,12 @@ public Tag raw(Object string) {
return add(new RawTag(String.valueOf(string)));
}

@ApiStatus.Internal
public UnpairedTag unpaired(String name) {
var tag = new UnpairedTag(name);
add(tag);
return tag;
}

@ApiStatus.Internal
public PairedTag paired(String name) {
var tag = new PairedTag(name);
add(tag);
Expand Down Expand Up @@ -343,6 +347,29 @@ public Tag h3() {
return paired("h3");
}

public Tag h4() {
return paired("h4");
}

public Tag h5() {
return paired("h5");
}

public Tag h6() {
return paired("h6");
}

public Tag heading(int heading) {
return switch (heading) {
case 1 -> h1();
case 2 -> h2();
case 3 -> h3();
case 4 -> h4();
case 5 -> h5();
default -> heading <= 0 ? this : h6();
};
}

public Tag ol() {
return paired("ol");
}
Expand Down Expand Up @@ -434,6 +461,10 @@ public Tag iframe(String name) {
return paired("iframe").attr("name", name);
}

public Tag pre() {
return paired("pre");
}

// Form

public String getPrefix() {
Expand Down
Loading

0 comments on commit 3c14883

Please sign in to comment.