diff --git a/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/DisplayMaths.java b/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/DisplayMaths.kt similarity index 63% rename from matrix-sdk-android/src/main/java/org/commonmark/ext/maths/DisplayMaths.java rename to matrix-sdk-android/src/main/java/org/commonmark/ext/maths/DisplayMaths.kt index 3cf574b824..bf6a89a26a 100644 --- a/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/DisplayMaths.java +++ b/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/DisplayMaths.kt @@ -13,20 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package org.commonmark.ext.maths -package org.commonmark.ext.maths; +import org.commonmark.node.CustomBlock -import org.commonmark.node.CustomBlock; - -public class DisplayMaths extends CustomBlock { - public enum DisplayDelimiter { - DOUBLE_DOLLAR, - SQUARE_BRACKET_ESCAPED - }; - - private DisplayDelimiter delimiter; - - public DisplayMaths(DisplayDelimiter delimiter) { - this.delimiter = delimiter; +class DisplayMaths(private val delimiter: DisplayDelimiter) : CustomBlock() { + enum class DisplayDelimiter { + DOUBLE_DOLLAR, SQUARE_BRACKET_ESCAPED } -} +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/InlineMaths.java b/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/InlineMaths.java deleted file mode 100644 index 982570a58d..0000000000 --- a/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/InlineMaths.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2020 New Vector Ltd - * - * 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; - -public class InlineMaths extends CustomNode implements Delimited { - public enum InlineDelimiter { - SINGLE_DOLLAR, - ROUND_BRACKET_ESCAPED - }; - - private InlineDelimiter delimiter; - - public InlineMaths(InlineDelimiter delimiter) { - this.delimiter = delimiter; - } - - @Override - public String getOpeningDelimiter() { - switch (delimiter) { - case SINGLE_DOLLAR: - return "$"; - case ROUND_BRACKET_ESCAPED: - return "\\("; - } - return null; - } - - @Override - public String getClosingDelimiter() { - switch (delimiter) { - case SINGLE_DOLLAR: - return "$"; - case ROUND_BRACKET_ESCAPED: - return "\\)"; - } - return null; - } -} diff --git a/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/InlineMaths.kt b/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/InlineMaths.kt new file mode 100644 index 0000000000..cb24949317 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/InlineMaths.kt @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 New Vector Ltd + * + * 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 -> "\\)" + } + } +} diff --git a/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/MathsExtension.java b/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/MathsExtension.java deleted file mode 100644 index 31706b3f2d..0000000000 --- a/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/MathsExtension.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2020 New Vector Ltd - * - * 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.NodeRenderer; -import org.commonmark.renderer.html.HtmlNodeRendererContext; -import org.commonmark.renderer.html.HtmlNodeRendererFactory; -import org.commonmark.renderer.html.HtmlRenderer; - -public class MathsExtension implements Parser.ParserExtension, HtmlRenderer.HtmlRendererExtension { - - private MathsExtension() { - } - - public static Extension create() { - return new MathsExtension(); - } - - @Override - public void extend(Parser.Builder parserBuilder) { - parserBuilder.customDelimiterProcessor(new DollarMathsDelimiterProcessor()); - } - - @Override - public void extend(HtmlRenderer.Builder rendererBuilder) { - rendererBuilder.nodeRendererFactory(new HtmlNodeRendererFactory() { - @Override - public NodeRenderer create(HtmlNodeRendererContext context) { - return new MathsHtmlNodeRenderer(context); - } - }); - } -} diff --git a/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/MathsExtension.kt b/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/MathsExtension.kt new file mode 100644 index 0000000000..4a5d4e440e --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/MathsExtension.kt @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2020 New Vector Ltd + * + * 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() + } + } +} diff --git a/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/DollarMathsDelimiterProcessor.java b/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/DollarMathsDelimiterProcessor.java deleted file mode 100644 index 809c070f29..0000000000 --- a/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/DollarMathsDelimiterProcessor.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2020 New Vector Ltd - * - * 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.Node; -import org.commonmark.node.Text; -import org.commonmark.parser.delimiter.DelimiterProcessor; -import org.commonmark.parser.delimiter.DelimiterRun; - -public class DollarMathsDelimiterProcessor implements DelimiterProcessor { - @Override - public char getOpeningCharacter() { - return '$'; - } - - @Override - public char getClosingCharacter() { - return '$'; - } - - @Override - public int getMinLength() { - return 1; - } - - @Override - public int getDelimiterUse(DelimiterRun opener, DelimiterRun closer) { - if (opener.length() == 1 && closer.length() == 1) - return 1; // inline - else if (opener.length() == 2 && closer.length() == 2) - return 2; // display - else - return 0; - } - - @Override - public void process(Text opener, Text closer, int delimiterUse) { - Node maths = delimiterUse == 1 ? new InlineMaths(InlineMaths.InlineDelimiter.SINGLE_DOLLAR) : - new DisplayMaths(DisplayMaths.DisplayDelimiter.DOUBLE_DOLLAR); - - Node tmp = opener.getNext(); - while (tmp != null && tmp != closer) { - Node next = tmp.getNext(); - maths.appendChild(tmp); - tmp = next; - } - - opener.insertAfter(maths); - } -} diff --git a/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/DollarMathsDelimiterProcessor.kt b/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/DollarMathsDelimiterProcessor.kt new file mode 100644 index 0000000000..a3daa26361 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/DollarMathsDelimiterProcessor.kt @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2020 New Vector Ltd + * + * 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) + } +} diff --git a/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/MathsHtmlNodeRenderer.java b/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/MathsHtmlNodeRenderer.java deleted file mode 100644 index a84129cd28..0000000000 --- a/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/MathsHtmlNodeRenderer.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (c) 2020 New Vector Ltd - * - * 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; -import java.util.Map; - -public class MathsHtmlNodeRenderer extends MathsNodeRenderer { - private final HtmlNodeRendererContext context; - private final HtmlWriter html; - - public MathsHtmlNodeRenderer(HtmlNodeRendererContext context) { - this.context = context; - this.html = context.getWriter(); - } - - @Override - public void render(Node node) { - boolean display = node.getClass() == DisplayMaths.class; - Node contents = node.getFirstChild(); // should be the only child - String latex = ((Text) contents).getLiteral(); - Map attributes = context.extendAttributes(node, display ? "div" : "span", Collections.singletonMap("data-mx-maths", - latex)); - html.tag(display ? "div" : "span", attributes); - html.tag("code"); - context.render(contents); - html.tag("/code"); - html.tag(display ? "/div" : "/span"); - } -} diff --git a/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/MathsHtmlNodeRenderer.kt b/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/MathsHtmlNodeRenderer.kt new file mode 100644 index 0000000000..f46a21afd8 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/MathsHtmlNodeRenderer.kt @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 New Vector Ltd + * + * 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") + } +} diff --git a/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/MathsNodeRenderer.java b/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/MathsNodeRenderer.kt similarity index 51% rename from matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/MathsNodeRenderer.java rename to matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/MathsNodeRenderer.kt index 4d2a35f33d..f6edd5385a 100644 --- a/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/MathsNodeRenderer.java +++ b/matrix-sdk-android/src/main/java/org/commonmark/ext/maths/internal/MathsNodeRenderer.kt @@ -13,23 +13,20 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package org.commonmark.ext.maths.internal -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 -import org.commonmark.ext.maths.DisplayMaths; -import org.commonmark.ext.maths.InlineMaths; -import org.commonmark.node.Node; -import org.commonmark.renderer.NodeRenderer; - -import java.util.HashSet; -import java.util.Set; - -abstract class MathsNodeRenderer implements NodeRenderer { - @Override - public Set> getNodeTypes() { - final Set> types = new HashSet>(); - types.add(InlineMaths.class); - types.add(DisplayMaths.class); - return types; +abstract class MathsNodeRenderer : NodeRenderer { + override fun getNodeTypes(): Set> { + val types: MutableSet> = HashSet() + types.add(InlineMaths::class.java) + types.add(DisplayMaths::class.java) + return types } -} +} \ No newline at end of file