128 lines
3.8 KiB
Dart
128 lines
3.8 KiB
Dart
|
// Originally from linkify
|
||
|
// MIT License
|
||
|
//
|
||
|
// Copyright (c) 2019 Charles-William Crete
|
||
|
//
|
||
|
// 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.
|
||
|
|
||
|
import 'package:cwtch/third_party/linkify/linkify.dart';
|
||
|
|
||
|
final _urlRegex = RegExp(
|
||
|
r'^(.*?)((?:https?:\/\/|www\.)[^\s/$.?#].[^\s]*)',
|
||
|
caseSensitive: false,
|
||
|
dotAll: true,
|
||
|
);
|
||
|
|
||
|
final _looseUrlRegex = RegExp(
|
||
|
r'^(.*?)((https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*))',
|
||
|
caseSensitive: false,
|
||
|
dotAll: true,
|
||
|
);
|
||
|
|
||
|
final _protocolIdentifierRegex = RegExp(
|
||
|
r'^(https?:\/\/)',
|
||
|
caseSensitive: false,
|
||
|
);
|
||
|
|
||
|
class UrlLinkifier extends Linkifier {
|
||
|
const UrlLinkifier();
|
||
|
|
||
|
@override
|
||
|
List<LinkifyElement> parse(elements, options) {
|
||
|
final list = <LinkifyElement>[];
|
||
|
|
||
|
elements.forEach((element) {
|
||
|
if (element is TextElement) {
|
||
|
var match = options.looseUrl
|
||
|
? _looseUrlRegex.firstMatch(element.text)
|
||
|
: _urlRegex.firstMatch(element.text);
|
||
|
|
||
|
if (match == null) {
|
||
|
list.add(element);
|
||
|
} else {
|
||
|
final text = element.text.replaceFirst(match.group(0)!, '');
|
||
|
|
||
|
if (match.group(1)?.isNotEmpty == true) {
|
||
|
list.add(TextElement(match.group(1)!));
|
||
|
}
|
||
|
|
||
|
if (match.group(2)?.isNotEmpty == true) {
|
||
|
var originalUrl = match.group(2)!;
|
||
|
String? end;
|
||
|
|
||
|
if ((options.excludeLastPeriod) &&
|
||
|
originalUrl[originalUrl.length - 1] == ".") {
|
||
|
end = ".";
|
||
|
originalUrl = originalUrl.substring(0, originalUrl.length - 1);
|
||
|
}
|
||
|
|
||
|
var url = originalUrl;
|
||
|
|
||
|
// We do not, ever, change the original text of a message.
|
||
|
if (options.defaultToHttps) {
|
||
|
url = url.replaceFirst('http://', 'https://');
|
||
|
}
|
||
|
|
||
|
// These options are intended for the human-readable portion of
|
||
|
// the URI
|
||
|
if (options.humanize) {
|
||
|
originalUrl = originalUrl.replaceFirst(RegExp(r'https?://'), '');
|
||
|
}
|
||
|
|
||
|
if (options.removeWww) {
|
||
|
originalUrl = originalUrl.replaceFirst(RegExp(r'www\.'), '');
|
||
|
}
|
||
|
|
||
|
list.add(UrlElement(originalUrl, url));
|
||
|
|
||
|
|
||
|
if (end != null) {
|
||
|
list.add(TextElement(end));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (text.isNotEmpty) {
|
||
|
list.addAll(parse([TextElement(text)], options));
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
list.add(element);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
return list;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Represents an element containing a link
|
||
|
class UrlElement extends LinkableElement {
|
||
|
UrlElement(String url, [String? text]) : super(text, url);
|
||
|
|
||
|
@override
|
||
|
String toString() {
|
||
|
return "LinkElement: '$url' ($text)";
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
bool operator ==(other) => equals(other);
|
||
|
|
||
|
@override
|
||
|
bool equals(other) => other is UrlElement && super.equals(other);
|
||
|
}
|