diff options
author | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2024-03-10 00:58:26 +0000 |
---|---|---|
committer | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2024-03-10 00:58:26 +0000 |
commit | 15f1845aeecf922ce058533d670c716ed4b6b9e0 (patch) | |
tree | 5020d565923388a5ff05235bdf102493033ac51c /app/src/main | |
parent | ec380b2c079cc9df76284b4a5a876fa2b010b961 (diff) | |
download | infinity-for-reddit-15f1845aeecf922ce058533d670c716ed4b6b9e0.tar infinity-for-reddit-15f1845aeecf922ce058533d670c716ed4b6b9e0.tar.gz infinity-for-reddit-15f1845aeecf922ce058533d670c716ed4b6b9e0.tar.bz2 infinity-for-reddit-15f1845aeecf922ce058533d670c716ed4b6b9e0.tar.lz infinity-for-reddit-15f1845aeecf922ce058533d670c716ed4b6b9e0.tar.xz infinity-for-reddit-15f1845aeecf922ce058533d670c716ed4b6b9e0.tar.zst infinity-for-reddit-15f1845aeecf922ce058533d670c716ed4b6b9e0.zip |
Spoiler richtext_json.
Diffstat (limited to 'app/src/main')
-rw-r--r-- | app/src/main/java/ml/docilealligator/infinityforreddit/markdown/RichTextJSONConverter.java | 64 |
1 files changed, 54 insertions, 10 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/RichTextJSONConverter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/RichTextJSONConverter.java index 71e9f416..8d05bc49 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/RichTextJSONConverter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/markdown/RichTextJSONConverter.java @@ -108,8 +108,8 @@ public class RichTextJSONConverter implements Visitor { String className = node.getClass().getName(); if (formatMap.containsKey(className)) { formatNum += formatMap.get(className); - node = node.getFirstChild(); } + node = node.getFirstChild(); } if (node instanceof Text) { int start = textSB.length(); @@ -126,6 +126,28 @@ public class RichTextJSONConverter implements Visitor { return null; } + private String getAllText(Node node) { + node = node.getFirstChild(); + while (node != null) { + Node next = node; + while (next != null && next.getFirstChild() != null) { + next = next.getFirstChild(); + } + + if (next instanceof Text) { + textSB.append(((Text) next).getLiteral()); + } else if (next instanceof Code) { + textSB.append(((Code) next).getLiteral()); + } + + node = node.getNext(); + } + + String text = textSB.toString(); + textSB.delete(0, text.length()); + return text; + } + private void convertToRawTextJSONObject(JSONArray contentArray) throws JSONException { for (int i = 0; i < contentArray.length(); i++) { JSONObject content = contentArray.getJSONObject(i); @@ -475,7 +497,7 @@ public class RichTextJSONConverter implements Visitor { @Override public void visit(LinkReferenceDefinition linkReferenceDefinition) { - + //Not supported by Reddit } @Override @@ -489,16 +511,38 @@ public class RichTextJSONConverter implements Visitor { Superscript can still has inline spans, thus checking children's next node until the end. Superscript must use ^(), not ^ right now. */ - Node child = customNode.getFirstChild(); - while (child != null) { - JSONArray format = getFormatArray(customNode); - if (format != null) { - formats.add(format); + if (customNode instanceof Superscript) { + Node child = customNode.getFirstChild(); + while (child != null) { + JSONArray format = getFormatArray(customNode); + if (format != null) { + formats.add(format); + } + + Node next = child.getNext(); + child.unlink(); + child = next; } + } else if (customNode instanceof SpoilerNode) { + //Spoiler cannot have styles + try { + JSONObject nodeJSON = new JSONObject(); + nodeJSON.put(TYPE, SPOILER_E); + + contentArrayStack.push(new JSONArray()); + + JSONArray cArray = contentArrayStack.pop(); - Node next = child.getNext(); - child.unlink(); - child = next; + JSONObject contentJSONObject = new JSONObject(); + contentJSONObject.put(TYPE, TEXT_E); + contentJSONObject.put(TEXT, getAllText(customNode)); + + cArray.put(contentJSONObject); + nodeJSON.put(CONTENT, cArray); + contentArrayStack.peek().put(nodeJSON); + } catch (JSONException e) { + throw new RuntimeException(e); + } } } } |