-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
550 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/jerboa/util/markwon/script/Superscript.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.jerboa.util.markwon.script; | ||
|
||
import org.commonmark.node.CustomNode; | ||
import org.commonmark.node.Visitor; | ||
|
||
// Source : https://codeberg.org/Bazsalanszky/Eternity/src/commit/3c871e26781d26df7b7f92b8633580b6087a8223/app/src/main/java/eu/toldi/infinityforlemmy/markdown/Superscript.java | ||
public class Superscript extends CustomNode { | ||
private boolean isBracketed; | ||
|
||
@Override | ||
public void accept(Visitor visitor) { | ||
visitor.visit(this); | ||
} | ||
|
||
public boolean isBracketed() { | ||
return isBracketed; | ||
} | ||
|
||
public void setBracketed(boolean bracketed) { | ||
isBracketed = bracketed; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
app/src/main/java/com/jerboa/util/markwon/script/SuperscriptClosingInlineProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.jerboa.util.markwon.script; | ||
|
||
import static io.noties.markwon.inlineparser.InlineParserUtils.mergeChildTextNodes; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import org.commonmark.node.Node; | ||
|
||
import io.noties.markwon.inlineparser.InlineProcessor; | ||
|
||
public class SuperscriptClosingInlineProcessor extends InlineProcessor { | ||
@NonNull | ||
private final SuperscriptOpeningStorage superscriptOpeningStorage; | ||
|
||
public SuperscriptClosingInlineProcessor(@NonNull SuperscriptOpeningStorage superscriptOpeningStorage) { | ||
this.superscriptOpeningStorage = superscriptOpeningStorage; | ||
} | ||
|
||
@Override | ||
public char specialCharacter() { | ||
return ')'; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
protected Node parse() { | ||
SuperscriptOpeningBracket superscriptOpening = superscriptOpeningStorage.pop(block); | ||
if (superscriptOpening == null) { | ||
return null; | ||
} | ||
index++; | ||
|
||
Superscript superscript = new Superscript(); | ||
superscript.setBracketed(true); | ||
Node node = superscriptOpening.node.getNext(); | ||
while (node != null) { | ||
Node next = node.getNext(); | ||
superscript.appendChild(node); | ||
node = next; | ||
} | ||
|
||
// Process delimiters such as emphasis inside spoiler | ||
processDelimiters(superscriptOpening.previousDelimiter); | ||
mergeChildTextNodes(superscript); | ||
// We don't need the corresponding text node anymore, we turned it into a spoiler node | ||
superscriptOpening.node.unlink(); | ||
|
||
return superscript; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/jerboa/util/markwon/script/SuperscriptOpening.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.jerboa.util.markwon.script; | ||
|
||
import org.commonmark.node.Node; | ||
|
||
public class SuperscriptOpening { | ||
/** | ||
* Node that contains non-bracketed superscript opening markdown ({@code ^}). | ||
*/ | ||
public final Node node; | ||
|
||
public final Integer start; | ||
|
||
public SuperscriptOpening(Node node, int start) { | ||
this.node = node; | ||
this.start = start; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
app/src/main/java/com/jerboa/util/markwon/script/SuperscriptOpeningBracket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.jerboa.util.markwon.script; | ||
|
||
import org.commonmark.internal.Delimiter; | ||
import org.commonmark.node.Node; | ||
|
||
public class SuperscriptOpeningBracket { | ||
/** | ||
* Node that contains superscript opening bracket markdown ({@code ^(}). | ||
*/ | ||
public final Node node; | ||
|
||
/** | ||
* Previous superscript opening bracket. | ||
*/ | ||
public final SuperscriptOpeningBracket previous; | ||
|
||
/** | ||
* Previous delimiter (emphasis, etc) before this bracket. | ||
*/ | ||
public final Delimiter previousDelimiter; | ||
|
||
public final Integer start; | ||
|
||
public SuperscriptOpeningBracket(Node node, SuperscriptOpeningBracket previous, Delimiter previousDelimiter) { | ||
this.node = node; | ||
this.previous = previous; | ||
this.previousDelimiter = previousDelimiter; | ||
this.start = null; | ||
} | ||
} | ||
|
50 changes: 50 additions & 0 deletions
50
app/src/main/java/com/jerboa/util/markwon/script/SuperscriptOpeningInlineProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.jerboa.util.markwon.script; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import org.commonmark.node.Node; | ||
import org.commonmark.node.Text; | ||
|
||
import io.noties.markwon.inlineparser.InlineProcessor; | ||
|
||
public class SuperscriptOpeningInlineProcessor extends InlineProcessor { | ||
@NonNull | ||
private final SuperscriptOpeningStorage superscriptOpeningStorage; | ||
|
||
public SuperscriptOpeningInlineProcessor(@NonNull SuperscriptOpeningStorage superscriptOpeningStorage) { | ||
this.superscriptOpeningStorage = superscriptOpeningStorage; | ||
} | ||
|
||
@Override | ||
public char specialCharacter() { | ||
return '^'; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
protected Node parse() { | ||
index++; | ||
char c = peek(); | ||
if (c != '\0' && !Character.isWhitespace(c)) { | ||
if (c == '(') { | ||
index++; | ||
Text node = text("^("); | ||
superscriptOpeningStorage.add(block, node, lastDelimiter()); | ||
return node; | ||
} | ||
|
||
if (lastDelimiter() != null && lastDelimiter().canOpen && block.getLastChild() != null) { | ||
if (lastDelimiter().node == this.block.getLastChild()) { | ||
if (lastDelimiter().delimiterChar == peek()) { | ||
index--; | ||
return null; | ||
} | ||
} | ||
} | ||
|
||
return new Superscript(); | ||
} | ||
return null; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
app/src/main/java/com/jerboa/util/markwon/script/SuperscriptOpeningStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.jerboa.util.markwon.script; | ||
|
||
import androidx.annotation.Nullable; | ||
|
||
import org.commonmark.internal.Delimiter; | ||
import org.commonmark.node.Node; | ||
|
||
public class SuperscriptOpeningStorage { | ||
@Nullable | ||
private SuperscriptOpeningBracket lastBracket; | ||
private Node currentBlock; | ||
|
||
public void clear() { | ||
lastBracket = null; | ||
} | ||
|
||
public void add(Node block, Node node, Delimiter lastDelimiter) { | ||
updateBlock(block); | ||
lastBracket = new SuperscriptOpeningBracket(node, lastBracket, lastDelimiter); | ||
} | ||
|
||
@Nullable | ||
public SuperscriptOpeningBracket pop(Node block) { | ||
updateBlock(block); | ||
SuperscriptOpeningBracket opening = lastBracket; | ||
if (opening != null) { | ||
lastBracket = opening.previous; | ||
} | ||
return opening; | ||
} | ||
|
||
private void updateBlock(Node block) { | ||
if (block != currentBlock) { | ||
clear(); | ||
} | ||
currentBlock = block; | ||
} | ||
} |
Oops, something went wrong.