Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support TeX content in message list #1335

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/Katex/KaTeX_AMS-Regular.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_Caligraphic-Bold.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_Caligraphic-Regular.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_Fraktur-Bold.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_Fraktur-Regular.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_Main-Bold.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_Main-BoldItalic.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_Main-Italic.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_Main-Regular.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_Math-BoldItalic.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_Math-Italic.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_SansSerif-Bold.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_SansSerif-Italic.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_SansSerif-Regular.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_Script-Regular.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_Size1-Regular.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_Size2-Regular.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_Size3-Regular.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_Size4-Regular.ttf
Binary file not shown.
Binary file added assets/Katex/KaTeX_Typewriter-Regular.ttf
Binary file not shown.
21 changes: 21 additions & 0 deletions assets/Katex/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2013-2020 Khan Academy and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions lib/licenses.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import 'package:flutter/services.dart';
Stream<LicenseEntry> additionalLicenses() async* {
// Alphabetic by path.

yield LicenseEntryWithLineBreaks(
['KaTeX'],
await rootBundle.loadString('assets/KaTeX/LICENSE'));
yield LicenseEntryWithLineBreaks(
['Noto Color Emoji'],
await rootBundle.loadString('assets/Noto_Color_Emoji/LICENSE'));
Expand Down
99 changes: 75 additions & 24 deletions lib/model/content.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:html/dom.dart' as dom;
import 'package:html/parser.dart';

import '../api/model/model.dart';
import '../api/model/submessage.dart';
import 'code_block.dart';
import 'katex.dart';

/// A node in a parse tree for Zulip message-style content.
///
Expand Down Expand Up @@ -334,23 +336,46 @@ class CodeBlockSpanNode extends ContentNode {
}
}

class MathBlockNode extends BlockContentNode {
const MathBlockNode({super.debugHtmlNode, required this.texSource});
class KatexSpan extends ContentNode {
const KatexSpan({
required this.spanClasses,
required this.spanStyle,
required this.text,
this.spans = const [],
});

final String texSource;
final List<String> spanClasses;
final KatexSpanStyle? spanStyle;
final String? text;
final List<KatexSpan> spans;

@override
bool operator ==(Object other) {
return other is MathBlockNode && other.texSource == texSource;
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(StringProperty('spanClass', spanClasses.join(', ')));
properties.add(KatexSpanStyleProperty('spanStyle', spanStyle));
properties.add(StringProperty('text', text));
}

@override
int get hashCode => Object.hash('MathBlockNode', texSource);
List<DiagnosticsNode> debugDescribeChildren() {
return spans.map((node) => node.toDiagnosticsNode()).toList();
}
}

class MathBlockNode extends BlockContentNode {
const MathBlockNode({
super.debugHtmlNode,
required this.texSource,
required this.spans,
});

final String texSource;
final List<KatexSpan> spans;

@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(StringProperty('texSource', texSource));
List<DiagnosticsNode> debugDescribeChildren() {
return spans.map((node) => node.toDiagnosticsNode()).toList();
}
}

Expand Down Expand Up @@ -1055,12 +1080,12 @@ class _ZulipContentParser {
return inlineParser.parseBlockInline(nodes);
}

BlockContentNode parseMathBlock(dom.Element element) {
final debugHtmlNode = kDebugMode ? element : null;
final texSource = _parseMath(element, block: true);
if (texSource == null) return UnimplementedBlockContentNode(htmlNode: element);
return MathBlockNode(texSource: texSource, debugHtmlNode: debugHtmlNode);
}
// BlockContentNode parseMathBlock(dom.Element element) {
// final debugHtmlNode = kDebugMode ? element : null;
// final texSource = _parseMath(element, block: true);
// if (texSource == null) return UnimplementedBlockContentNode(htmlNode: element);
// return MathBlockNode(texSource: texSource, debugHtmlNode: debugHtmlNode);
// }

BlockContentNode parseListNode(dom.Element element) {
ListStyle? listStyle;
Expand Down Expand Up @@ -1453,6 +1478,29 @@ class _ZulipContentParser {
return tableNode ?? UnimplementedBlockContentNode(htmlNode: tableElement);
}

BlockContentNode? parseKatexBlock(dom.Element element) {
assert(element.localName == 'span' && element.className == 'katex-display');
if (element.nodes.length != 1) return null;
final child = element.nodes.single;
if (child is! dom.Element) return null;
if (child.localName != 'span') return null;
if (child.className != 'katex') return null;

if (child.nodes.length != 2) return null;
final grandchild = child.nodes.last;
if (grandchild is! dom.Element) return null;
if (grandchild.localName != 'span') return null;
if (grandchild.className != 'katex-html') return null;

try {
final spans = parseKatexSpans(grandchild);
return MathBlockNode(texSource: '', spans: spans);
} on KatexHtmlParseError catch (e, st) {
print('$e\n$st');
return null;
}
}

BlockContentNode parseBlockContent(dom.Node node) {
final debugHtmlNode = kDebugMode ? node : null;
if (node is! dom.Element) {
Expand All @@ -1474,15 +1522,18 @@ class _ZulipContentParser {
// Oddly, the way a math block gets encoded in Zulip HTML is inside a <p>.
if (element.nodes case [dom.Element(localName: 'span') && var child, ...]) {
if (child.className == 'katex-display') {
if (element.nodes case [_]
|| [_, dom.Element(localName: 'br'),
dom.Text(text: "\n")]) {
// This might be too specific; we'll find out when we do #190.
// The case with the `<br>\n` can happen when at the end of a quote;
// it seems like a glitch in the server's Markdown processing,
// so hopefully there just aren't any further such glitches.
return parseMathBlock(child);
}
// if (element.nodes case [_]
// || [_, dom.Element(localName: 'br'),
// dom.Text(text: "\n")]) {
// // This might be too specific; we'll find out when we do #190.
// // The case with the `<br>\n` can happen when at the end of a quote;
// // it seems like a glitch in the server's Markdown processing,
// // so hopefully there just aren't any further such glitches.
// return parseMathBlock(child);
// }
final block = parseKatexBlock(child);
if (block == null) return UnimplementedBlockContentNode(htmlNode: child);
return block;
}
}

Expand Down
Loading
Loading