[YouTube] Cleanup description helper

Remove unneeded isClose field, and make constants private
This commit is contained in:
Stypox 2024-04-08 09:47:15 +02:00
parent b80c3f5d51
commit a90237816a
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
2 changed files with 21 additions and 24 deletions

View File

@ -24,13 +24,13 @@ public final class YoutubeDescriptionHelper {
private YoutubeDescriptionHelper() { private YoutubeDescriptionHelper() {
} }
public static final String LINK_CLOSE = "</a>"; private static final String LINK_CLOSE = "</a>";
public static final String STRIKETHROUGH_OPEN = "<s>"; private static final String STRIKETHROUGH_OPEN = "<s>";
public static final String STRIKETHROUGH_CLOSE = "</s>"; private static final String STRIKETHROUGH_CLOSE = "</s>";
public static final String BOLD_OPEN = "<b>"; private static final String BOLD_OPEN = "<b>";
public static final String BOLD_CLOSE = "</b>"; private static final String BOLD_CLOSE = "</b>";
public static final String ITALIC_OPEN = "<i>"; private static final String ITALIC_OPEN = "<i>";
public static final String ITALIC_CLOSE = "</i>"; private static final String ITALIC_CLOSE = "</i>";
// special link chips (e.g. for YT videos, YT channels or social media accounts): // special link chips (e.g. for YT videos, YT channels or social media accounts):
// (u00a0) u00a0 u00a0 [/] u00a0 <link content> u00a0 u00a0 // (u00a0) u00a0 u00a0 [/] u00a0 <link content> u00a0 u00a0
@ -44,30 +44,26 @@ public final class YoutubeDescriptionHelper {
@Nonnull final String open; @Nonnull final String open;
@Nonnull final String close; @Nonnull final String close;
final int pos; final int pos;
final boolean isClose;
@Nullable final Function<String, String> transformContent; @Nullable final Function<String, String> transformContent;
int openPosInOutput = -1; int openPosInOutput = -1;
Run( Run(
@Nonnull final String open, @Nonnull final String open,
@Nonnull final String close, @Nonnull final String close,
final int pos, final int pos
final boolean isClose
) { ) {
this(open, close, pos, isClose, null); this(open, close, pos, null);
} }
Run( Run(
@Nonnull final String open, @Nonnull final String open,
@Nonnull final String close, @Nonnull final String close,
final int pos, final int pos,
final boolean isClose,
@Nullable final Function<String, String> transformContent @Nullable final Function<String, String> transformContent
) { ) {
this.open = open; this.open = open;
this.close = close; this.close = close;
this.pos = pos; this.pos = pos;
this.isClose = isClose;
this.transformContent = transformContent; this.transformContent = transformContent;
} }
@ -87,6 +83,7 @@ public final class YoutubeDescriptionHelper {
* @param attributedDescription the JSON object of the attributed description * @param attributedDescription the JSON object of the attributed description
* @return the parsed description, in HTML format, as a string * @return the parsed description, in HTML format, as a string
*/ */
@Nullable
public static String attributedDescriptionToHtml( public static String attributedDescriptionToHtml(
@Nullable final JsonObject attributedDescription @Nullable final JsonObject attributedDescription
) { ) {
@ -243,10 +240,8 @@ public final class YoutubeDescriptionHelper {
final String open = "<a href=\"" + Entities.escape(url) + "\">"; final String open = "<a href=\"" + Entities.escape(url) + "\">";
final Function<String, String> transformContent = getTransformContentFun(run); final Function<String, String> transformContent = getTransformContentFun(run);
openers.add(new Run(open, LINK_CLOSE, startIndex, false, openers.add(new Run(open, LINK_CLOSE, startIndex, transformContent));
transformContent)); closers.add(new Run(open, LINK_CLOSE, startIndex + length, transformContent));
closers.add(new Run(open, LINK_CLOSE, startIndex + length, true,
transformContent));
}); });
} }
@ -297,19 +292,19 @@ public final class YoutubeDescriptionHelper {
final int end = start + length; final int end = start + length;
if (run.has("strikethrough")) { if (run.has("strikethrough")) {
openers.add(new Run(STRIKETHROUGH_OPEN, STRIKETHROUGH_CLOSE, start, false)); openers.add(new Run(STRIKETHROUGH_OPEN, STRIKETHROUGH_CLOSE, start));
closers.add(new Run(STRIKETHROUGH_OPEN, STRIKETHROUGH_CLOSE, end, true)); closers.add(new Run(STRIKETHROUGH_OPEN, STRIKETHROUGH_CLOSE, end));
} }
if (run.getBoolean("italic", false)) { if (run.getBoolean("italic", false)) {
openers.add(new Run(ITALIC_OPEN, ITALIC_CLOSE, start, false)); openers.add(new Run(ITALIC_OPEN, ITALIC_CLOSE, start));
closers.add(new Run(ITALIC_OPEN, ITALIC_CLOSE, end, true)); closers.add(new Run(ITALIC_OPEN, ITALIC_CLOSE, end));
} }
if (run.has("weightLabel") if (run.has("weightLabel")
&& !"FONT_WEIGHT_NORMAL".equals(run.getString("weightLabel"))) { && !"FONT_WEIGHT_NORMAL".equals(run.getString("weightLabel"))) {
openers.add(new Run(BOLD_OPEN, BOLD_CLOSE, start, false)); openers.add(new Run(BOLD_OPEN, BOLD_CLOSE, start));
closers.add(new Run(BOLD_OPEN, BOLD_CLOSE, end, true)); closers.add(new Run(BOLD_OPEN, BOLD_CLOSE, end));
} }
}); });
} }

View File

@ -3,6 +3,8 @@ package org.schabi.newpipe.extractor.stream;
import java.io.Serializable; import java.io.Serializable;
import java.util.Objects; import java.util.Objects;
import javax.annotation.Nullable;
public class Description implements Serializable { public class Description implements Serializable {
public static final int HTML = 1; public static final int HTML = 1;
@ -13,7 +15,7 @@ public class Description implements Serializable {
private final String content; private final String content;
private final int type; private final int type;
public Description(final String content, final int type) { public Description(@Nullable final String content, final int type) {
this.type = type; this.type = type;
if (content == null) { if (content == null) {
this.content = ""; this.content = "";