Browse Source

Extract gopher links with linkify

tags/0.8.15
Julien Blanchard 10 months ago
parent
commit
a049ed6f26
3 changed files with 25 additions and 19 deletions
  1. 10
    0
      Cargo.lock
  2. 1
    0
      Cargo.toml
  3. 14
    19
      src/gopher/link.rs

+ 10
- 0
Cargo.lock View File

@@ -123,6 +123,7 @@ dependencies = [
123 123
  "glib-sys",
124 124
  "gtk",
125 125
  "lazy_static",
126
+ "linkify",
126 127
  "native-tls",
127 128
  "open",
128 129
  "openssl",
@@ -502,6 +503,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
502 503
 checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49"
503 504
 
504 505
 [[package]]
506
+name = "linkify"
507
+version = "0.4.0"
508
+source = "registry+https://github.com/rust-lang/crates.io-index"
509
+checksum = "03951527dd24d2c59f407502e7d88e0948ef06fac23335b556a4c2bc03c22096"
510
+dependencies = [
511
+ "memchr",
512
+]
513
+
514
+[[package]]
505 515
 name = "log"
506 516
 version = "0.4.8"
507 517
 source = "registry+https://github.com/rust-lang/crates.io-index"

+ 1
- 0
Cargo.toml View File

@@ -16,6 +16,7 @@ pango = "*"
16 16
 pango-sys = "*"
17 17
 open = "*"
18 18
 regex = "*"
19
+linkify = "*"
19 20
 native-tls = "*"
20 21
 openssl = "*"
21 22
 url = "*"

+ 14
- 19
src/gopher/link.rs View File

@@ -1,5 +1,5 @@
1
-extern crate regex;
2
-use regex::Regex;
1
+extern crate linkify;
2
+use linkify::LinkFinder;
3 3
 
4 4
 use std::str::FromStr;
5 5
 use url::Url;
@@ -170,14 +170,14 @@ impl FromStr for Link {
170 170
             } else {
171 171
                 Err(ParseError)
172 172
             }
173
-        // } else if line.contains("://") {
174
-        //     let url = extract_url(line);
175
-        //     let label = String::from(line);
176
-
177
-        //     match make_link(String::from(url), label) {
178
-        //         Some(link) => Ok(link),
179
-        //         None => Err(ParseError),
180
-        //     }
173
+        } else if line.contains("://") {
174
+            let url = extract_url(line);
175
+            let label = String::from(line);
176
+
177
+            match make_link(String::from(url), label) {
178
+                Some(link) => Ok(link),
179
+                None => Err(ParseError),
180
+            }
181 181
         } else {
182 182
             Err(ParseError)
183 183
         }
@@ -200,14 +200,9 @@ pub fn make_link(url: String, label: String) -> Option<Link> {
200 200
     }
201 201
 }
202 202
 
203
-const URL_REGEX: &str = r"((ftp|gopher|gemini|finger|http|https)://[a-zA-Z0-9-+&@#/%=~_|$?!:,.]*)";
204
-
205 203
 fn extract_url(line: &str) -> &str {
206
-    let url_regexp = Regex::new(URL_REGEX).unwrap();
207
-    if url_regexp.is_match(&line) {
208
-        let caps = url_regexp.captures(line).unwrap();
209
-        caps.get(1).map_or("", |m| m.as_str())
210
-    } else {
211
-        line
212
-    }
204
+    let finder = LinkFinder::new();
205
+    let links: Vec<_> = finder.links(line).collect();
206
+    let link = &links[0];
207
+    link.as_str()
213 208
 }

Loading…
Cancel
Save