-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#133] Add initial math support to GFM
- Loading branch information
1 parent
31f49df
commit 9253d03
Showing
9 changed files
with
167 additions
and
46 deletions.
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
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
93 changes: 50 additions & 43 deletions
93
src/commonMain/kotlin/org/intellij/markdown/flavours/gfm/lexer/_GFMLexer.kt
Large diffs are not rendered by default.
Oops, something went wrong.
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
50 changes: 50 additions & 0 deletions
50
src/commonMain/kotlin/org/intellij/markdown/parser/sequentialparsers/impl/MathParser.kt
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 org.intellij.markdown.parser.sequentialparsers.impl | ||
|
||
import org.intellij.markdown.flavours.gfm.GFMElementTypes | ||
import org.intellij.markdown.flavours.gfm.GFMTokenTypes | ||
import org.intellij.markdown.parser.sequentialparsers.RangesListBuilder | ||
import org.intellij.markdown.parser.sequentialparsers.SequentialParser | ||
import org.intellij.markdown.parser.sequentialparsers.TokensCache | ||
|
||
class MathParser : SequentialParser { | ||
override fun parse(tokens: TokensCache, rangesToGlue: List<IntRange>): SequentialParser.ParsingResult { | ||
val result = SequentialParser.ParsingResultBuilder() | ||
val delegateIndices = RangesListBuilder() | ||
var iterator: TokensCache.Iterator = tokens.RangesListIterator(rangesToGlue) | ||
|
||
while (iterator.type != null) { | ||
if (iterator.type == GFMTokenTypes.DOLLAR) { | ||
|
||
val endIterator = findOfSize(iterator.advance(), iterator.length) | ||
|
||
if (endIterator != null) { | ||
if (iterator.length == 1) { | ||
result.withNode(SequentialParser.Node(iterator.index..endIterator.index + 1, GFMElementTypes.INLINE_MATH)) | ||
} else { | ||
result.withNode(SequentialParser.Node(iterator.index..endIterator.index + 1, GFMElementTypes.BLOCK_MATH)) | ||
} | ||
iterator = endIterator.advance() | ||
continue | ||
} | ||
} | ||
delegateIndices.put(iterator.index) | ||
iterator = iterator.advance() | ||
} | ||
|
||
return result.withFurtherProcessing(delegateIndices.get()) | ||
} | ||
|
||
private fun findOfSize(it: TokensCache.Iterator, length: Int): TokensCache.Iterator? { | ||
var iterator = it | ||
while (iterator.type != null) { | ||
if (iterator.type == GFMTokenTypes.DOLLAR) { | ||
if (iterator.length == length) { | ||
return iterator | ||
} | ||
} | ||
|
||
iterator = iterator.advance() | ||
} | ||
return null | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This expression uses `\$` to display a dollar sign: $\sqrt{\$4}$ | ||
|
||
$$\sum_{k=1}^n a_k^2$$ |
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,26 @@ | ||
Markdown:MARKDOWN_FILE | ||
Markdown:PARAGRAPH | ||
Markdown:TEXT('This expression uses') | ||
WHITE_SPACE(' ') | ||
Markdown:CODE_SPAN | ||
Markdown:BACKTICK('`') | ||
Markdown:TEXT('\$') | ||
Markdown:BACKTICK('`') | ||
WHITE_SPACE(' ') | ||
Markdown:TEXT('to display a dollar sign') | ||
Markdown::(':') | ||
WHITE_SPACE(' ') | ||
Markdown:INLINE_MATH | ||
Markdown:DOLLAR('$') | ||
Markdown:TEXT('\sqrt{\$4}') | ||
Markdown:DOLLAR('$') | ||
Markdown:EOL('\n') | ||
Markdown:EOL('\n') | ||
Markdown:PARAGRAPH | ||
Markdown:BLOCK_MATH | ||
Markdown:DOLLAR('$$') | ||
Markdown:TEXT('\sum') | ||
Markdown:EMPH('_') | ||
Markdown:TEXT('{k=1}^n a_k^2') | ||
Markdown:DOLLAR('$$') | ||
Markdown:EOL('\n') |