Line data Source code
1 : // Originally from linkify https://github.com/Cretezy/linkify/blob/ba536fa85e7e3a16e580f153616f399458986183/lib/linkify.dart 2 : // Removed options `removeWWW` and `humanize` 3 : // 4 : // MIT License 5 : // 6 : // Copyright (c) 2019 Charles-William Crete 7 : // 8 : // Permission is hereby granted, free of charge, to any person obtaining a copy 9 : // of this software and associated documentation files (the "Software"), to deal 10 : // in the Software without restriction, including without limitation the rights 11 : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 : // copies of the Software, and to permit persons to whom the Software is 13 : // furnished to do so, subject to the following conditions: 14 : // 15 : // The above copyright notice and this permission notice shall be included in all 16 : // copies or substantial portions of the Software. 17 : // 18 : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 24 : // SOFTWARE. 25 : 26 : import 'uri.dart'; 27 : export 'uri.dart' show UrlLinkifier, UrlElement; 28 : 29 : abstract class LinkifyElement { 30 : final String text; 31 : 32 0 : LinkifyElement(this.text); 33 : 34 0 : @override 35 0 : bool operator ==(other) => equals(other); 36 : 37 0 : bool equals(other) => other is LinkifyElement && other.text == text; 38 : } 39 : 40 : class BoldElement extends LinkifyElement { 41 0 : BoldElement(String text) : super(text); 42 : } 43 : 44 : class ItalicElement extends LinkifyElement { 45 0 : ItalicElement(String text) : super(text); 46 : } 47 : 48 : class SuperElement extends LinkifyElement { 49 0 : SuperElement(String text) : super(text); 50 : } 51 : 52 : class SubElement extends LinkifyElement { 53 0 : SubElement(String text) : super(text); 54 : } 55 : 56 : class StrikeElement extends LinkifyElement { 57 0 : StrikeElement(String text) : super(text); 58 : } 59 : 60 : class CodeElement extends LinkifyElement { 61 0 : CodeElement(String text) : super(text); 62 : } 63 : 64 : class LinkableElement extends LinkifyElement { 65 : final String url; 66 : 67 0 : LinkableElement(String? text, this.url) : super(text ?? url); 68 : 69 0 : @override 70 0 : bool operator ==(other) => equals(other); 71 : 72 0 : @override 73 0 : bool equals(other) => other is LinkableElement && super.equals(other) && other.url == url; 74 : } 75 : 76 : /// Represents an element containing text 77 : class TextElement extends LinkifyElement { 78 0 : TextElement(String text) : super(text); 79 : 80 0 : @override 81 : String toString() { 82 0 : return "TextElement: '$text'"; 83 : } 84 : 85 0 : @override 86 0 : bool operator ==(other) => equals(other); 87 : 88 0 : @override 89 0 : bool equals(other) => other is TextElement && super.equals(other); 90 : } 91 : 92 : abstract class Linkifier { 93 4 : const Linkifier(); 94 : 95 : List<LinkifyElement> parse(List<LinkifyElement> elements, LinkifyOptions options); 96 : } 97 : 98 : class LinkifyOptions { 99 : /// Enables loose URL parsing (any string with "." is a URL). 100 : final bool looseUrl; 101 : 102 : /// When used with [looseUrl], default to `https` instead of `http`. 103 : final bool defaultToHttps; 104 : 105 : /// Excludes `.` at end of URLs. 106 : final bool excludeLastPeriod; 107 : 108 : final bool messageFormatting; 109 : final bool parseLinks; 110 : 111 8 : const LinkifyOptions({this.looseUrl = false, this.defaultToHttps = false, this.excludeLastPeriod = true, this.messageFormatting = false, this.parseLinks = false}); 112 : } 113 : 114 : const _urlLinkifier = UrlLinkifier(); 115 : const defaultLinkifiers = [_urlLinkifier]; 116 : 117 : /// Turns [text] into a list of [LinkifyElement] 118 : /// 119 : /// Use [humanize] to remove http/https from the start of the URL shown. 120 : /// Will default to `false` (if `null`) 121 : /// 122 : /// Uses [linkTypes] to enable some types of links (URL, email). 123 : /// Will default to all (if `null`). 124 0 : List<LinkifyElement> linkify( 125 : String text, { 126 : LinkifyOptions options = const LinkifyOptions(), 127 : List<Linkifier> linkifiers = defaultLinkifiers, 128 : }) { 129 0 : var list = <LinkifyElement>[TextElement(text)]; 130 : 131 0 : if (text.isEmpty) { 132 0 : return []; 133 : } 134 : 135 0 : if (linkifiers.isEmpty) { 136 : return list; 137 : } 138 : 139 0 : linkifiers.forEach((linkifier) { 140 0 : list = linkifier.parse(list, options); 141 : }); 142 : 143 : return list; 144 : }