Skip to content

Commit 52fda37

Browse files
committed
Implement bold/italic/strike-through support.
1 parent 40f1ec4 commit 52fda37

1 file changed

Lines changed: 43 additions & 11 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/services/youtube/YoutubeParsingHelper.java

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -940,18 +940,50 @@ public static String getTextFromObject(final JsonObject textObject, final boolea
940940
}
941941

942942
final StringBuilder textBuilder = new StringBuilder();
943-
for (final Object textPart : textObject.getArray("runs")) {
944-
final String text = ((JsonObject) textPart).getString("text");
945-
if (html && ((JsonObject) textPart).has("navigationEndpoint")) {
946-
final String url = getUrlFromNavigationEndpoint(((JsonObject) textPart)
947-
.getObject("navigationEndpoint"));
948-
if (!isNullOrEmpty(url)) {
949-
textBuilder.append("<a href=\"").append(url).append("\">").append(text)
950-
.append("</a>");
951-
continue;
943+
for (final Object o : textObject.getArray("runs")) {
944+
final JsonObject run = (JsonObject) o;
945+
String text = run.getString("text");
946+
947+
if (html) {
948+
if (run.has("navigationEndpoint")) {
949+
final String url = getUrlFromNavigationEndpoint(run
950+
.getObject("navigationEndpoint"));
951+
if (!isNullOrEmpty(url)) {
952+
text = "<a href=\"" + url + "\">" + text + "</a>";
953+
}
954+
}
955+
956+
final boolean bold = run.has("bold")
957+
&& run.getBoolean("bold");
958+
final boolean italic = run.has("italics")
959+
&& run.getBoolean("italics");
960+
final boolean strikethrough = run.has("strikethrough")
961+
&& run.getBoolean("strikethrough");
962+
963+
if (bold) {
964+
textBuilder.append("<b>");
965+
}
966+
if (italic) {
967+
textBuilder.append("<i>");
968+
}
969+
if (strikethrough) {
970+
textBuilder.append("<s>");
971+
}
972+
973+
textBuilder.append(text);
974+
975+
if (strikethrough) {
976+
textBuilder.append("</s>");
977+
}
978+
if (italic) {
979+
textBuilder.append("</i>");
980+
}
981+
if (bold) {
982+
textBuilder.append("</b>");
952983
}
984+
} else {
985+
textBuilder.append(text);
953986
}
954-
textBuilder.append(text);
955987
}
956988

957989
String text = textBuilder.toString();
@@ -991,7 +1023,7 @@ public static String getAttributedDescription(
9911023
final StringBuilder textBuilder = new StringBuilder();
9921024
int textStart = 0;
9931025

994-
for (final Object commandRun: commandRuns) {
1026+
for (final Object commandRun : commandRuns) {
9951027
if (!(commandRun instanceof JsonObject)) {
9961028
continue;
9971029
}

0 commit comments

Comments
 (0)