-
Notifications
You must be signed in to change notification settings - Fork 731
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2133 from NickHu/maths
Maths support (MSC2191)
- Loading branch information
Showing
17 changed files
with
291 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add labs support for rendering LaTeX maths (MSC2191) |
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
24 changes: 24 additions & 0 deletions
24
matrix-sdk-android/src/main/java/org/commonmark/ext/maths/DisplayMaths.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,24 @@ | ||
/* | ||
* Copyright (c) 2021 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.commonmark.ext.maths | ||
|
||
import org.commonmark.node.CustomBlock | ||
|
||
class DisplayMaths(private val delimiter: DisplayDelimiter) : CustomBlock() { | ||
enum class DisplayDelimiter { | ||
DOUBLE_DOLLAR, SQUARE_BRACKET_ESCAPED | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
matrix-sdk-android/src/main/java/org/commonmark/ext/maths/InlineMaths.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,39 @@ | ||
/* | ||
* Copyright (c) 2021 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.commonmark.ext.maths | ||
|
||
import org.commonmark.node.CustomNode | ||
import org.commonmark.node.Delimited | ||
|
||
class InlineMaths(private val delimiter: InlineDelimiter) : CustomNode(), Delimited { | ||
enum class InlineDelimiter { | ||
SINGLE_DOLLAR, ROUND_BRACKET_ESCAPED | ||
} | ||
|
||
override fun getOpeningDelimiter(): String { | ||
return when (delimiter) { | ||
InlineDelimiter.SINGLE_DOLLAR -> "$" | ||
InlineDelimiter.ROUND_BRACKET_ESCAPED -> "\\(" | ||
} | ||
} | ||
|
||
override fun getClosingDelimiter(): String { | ||
return when (delimiter) { | ||
InlineDelimiter.SINGLE_DOLLAR -> "$" | ||
InlineDelimiter.ROUND_BRACKET_ESCAPED -> "\\)" | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
matrix-sdk-android/src/main/java/org/commonmark/ext/maths/MathsExtension.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,38 @@ | ||
/* | ||
* Copyright (c) 2021 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.commonmark.ext.maths | ||
|
||
import org.commonmark.Extension | ||
import org.commonmark.ext.maths.internal.DollarMathsDelimiterProcessor | ||
import org.commonmark.ext.maths.internal.MathsHtmlNodeRenderer | ||
import org.commonmark.parser.Parser | ||
import org.commonmark.renderer.html.HtmlRenderer | ||
|
||
class MathsExtension private constructor() : Parser.ParserExtension, HtmlRenderer.HtmlRendererExtension { | ||
override fun extend(parserBuilder: Parser.Builder) { | ||
parserBuilder.customDelimiterProcessor(DollarMathsDelimiterProcessor()) | ||
} | ||
|
||
override fun extend(rendererBuilder: HtmlRenderer.Builder) { | ||
rendererBuilder.nodeRendererFactory { context -> MathsHtmlNodeRenderer(context) } | ||
} | ||
|
||
companion object { | ||
fun create(): Extension { | ||
return MathsExtension() | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...-android/src/main/java/org/commonmark/ext/maths/internal/DollarMathsDelimiterProcessor.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,53 @@ | ||
/* | ||
* Copyright (c) 2021 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.commonmark.ext.maths.internal | ||
|
||
import org.commonmark.ext.maths.DisplayMaths | ||
import org.commonmark.ext.maths.InlineMaths | ||
import org.commonmark.node.Text | ||
import org.commonmark.parser.delimiter.DelimiterProcessor | ||
import org.commonmark.parser.delimiter.DelimiterRun | ||
|
||
class DollarMathsDelimiterProcessor : DelimiterProcessor { | ||
override fun getOpeningCharacter(): Char { | ||
return '$' | ||
} | ||
|
||
override fun getClosingCharacter(): Char { | ||
return '$' | ||
} | ||
|
||
override fun getMinLength(): Int { | ||
return 1 | ||
} | ||
|
||
override fun getDelimiterUse(opener: DelimiterRun, closer: DelimiterRun): Int { | ||
return if (opener.length() == 1 && closer.length() == 1) 1 // inline | ||
else if (opener.length() == 2 && closer.length() == 2) 2 // display | ||
else 0 | ||
} | ||
|
||
override fun process(opener: Text, closer: Text, delimiterUse: Int) { | ||
val maths = if (delimiterUse == 1) InlineMaths(InlineMaths.InlineDelimiter.SINGLE_DOLLAR) else DisplayMaths(DisplayMaths.DisplayDelimiter.DOUBLE_DOLLAR) | ||
var tmp = opener.next | ||
while (tmp != null && tmp !== closer) { | ||
val next = tmp.next | ||
maths.appendChild(tmp) | ||
tmp = next | ||
} | ||
opener.insertAfter(maths) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/MathsHtmlNodeRenderer.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,39 @@ | ||
/* | ||
* Copyright (c) 2021 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.commonmark.ext.maths.internal | ||
|
||
import org.commonmark.ext.maths.DisplayMaths | ||
import org.commonmark.node.Node | ||
import org.commonmark.node.Text | ||
import org.commonmark.renderer.html.HtmlNodeRendererContext | ||
import org.commonmark.renderer.html.HtmlWriter | ||
import java.util.Collections | ||
|
||
class MathsHtmlNodeRenderer(private val context: HtmlNodeRendererContext) : MathsNodeRenderer() { | ||
private val html: HtmlWriter = context.writer | ||
override fun render(node: Node) { | ||
val display = node.javaClass == DisplayMaths::class.java | ||
val contents = node.firstChild // should be the only child | ||
val latex = (contents as Text).literal | ||
val attributes = context.extendAttributes(node, if (display) "div" else "span", Collections.singletonMap("data-mx-maths", | ||
latex)) | ||
html.tag(if (display) "div" else "span", attributes) | ||
html.tag("code") | ||
context.render(contents) | ||
html.tag("/code") | ||
html.tag(if (display) "/div" else "/span") | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/MathsNodeRenderer.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,32 @@ | ||
/* | ||
* Copyright (c) 2021 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.commonmark.ext.maths.internal | ||
|
||
import org.commonmark.ext.maths.InlineMaths | ||
import org.commonmark.ext.maths.DisplayMaths | ||
import org.commonmark.ext.maths.internal.MathsNodeRenderer | ||
import org.commonmark.node.Node | ||
import org.commonmark.renderer.NodeRenderer | ||
import java.util.HashSet | ||
|
||
abstract class MathsNodeRenderer : NodeRenderer { | ||
override fun getNodeTypes(): Set<Class<out Node>> { | ||
val types: MutableSet<Class<out Node>> = HashSet() | ||
types.add(InlineMaths::class.java) | ||
types.add(DisplayMaths::class.java) | ||
return types | ||
} | ||
} |
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
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
Oops, something went wrong.