ソースを参照

[gopher] fix links parsing in text files

tags/0.8.6
Julien Blanchard 1年前
コミット
11097f940d
3個のファイルの変更47行の追加10行の削除
  1. 20
    0
      src/draw.rs
  2. 21
    2
      src/gopher/link.rs
  3. 6
    8
      src/gopher/parser.rs

+ 20
- 0
src/draw.rs ファイルの表示

@@ -310,6 +310,26 @@ pub fn gopher_link(gui: &Arc<Gui>, link_item: String) {
310 310
             let new_url = Gopher { source: url }.to_absolute_url().unwrap();
311 311
             insert_button(&gui, new_url, label);
312 312
         }
313
+        Ok(GopherLink::Ftp(url, label)) => {
314
+            let button_label = if label.is_empty() {
315
+                url.clone().to_string()
316
+            } else {
317
+                label
318
+            };
319
+            let ftp_label = format!("{} [FTP]", button_label);
320
+
321
+            insert_external_button(&gui, url, &ftp_label);
322
+        }
323
+        Ok(GopherLink::Finger(url, label)) => {
324
+            let button_label = if label.is_empty() {
325
+                url.clone().to_string()
326
+            } else {
327
+                label
328
+            };
329
+            let finger_label = format!("{} [Finger]", button_label);
330
+
331
+            insert_external_button(&gui, url, &finger_label);
332
+        }
313 333
         Ok(GopherLink::Unknown(_, _)) => (),
314 334
         Err(_) => (),
315 335
     }

+ 21
- 2
src/gopher/link.rs ファイルの表示

@@ -1,9 +1,14 @@
1
+extern crate regex;
2
+use regex::Regex;
3
+
1 4
 use std::str::FromStr;
2 5
 use url::Url;
3 6
 
4 7
 #[derive(Debug)]
5 8
 pub enum Link {
6 9
     File(Url, String),
10
+    Ftp(Url, String),
11
+    Finger(Url, String),
7 12
     Gemini(Url, String),
8 13
     Gopher(Url, String),
9 14
     Http(Url, String),
@@ -167,10 +172,10 @@ impl FromStr for Link {
167 172
                 Err(ParseError)
168 173
             }
169 174
         } else if line.contains("://") {
170
-            let url = String::from(line);
175
+            let url = extract_url(line);
171 176
             let label = String::from(line);
172 177
 
173
-            match make_link(url, label) {
178
+            match make_link(String::from(url), label) {
174 179
                 Some(link) => Ok(link),
175 180
                 None => Err(ParseError),
176 181
             }
@@ -183,6 +188,8 @@ impl FromStr for Link {
183 188
 pub fn make_link(url: String, label: String) -> Option<Link> {
184 189
     match Url::parse(&url) {
185 190
         Ok(url) => match url.scheme() {
191
+            "finger" => Some(Link::Finger(url, label)),
192
+            "ftp" => Some(Link::Ftp(url, label)),
186 193
             "gemini" => Some(Link::Gemini(url, label)),
187 194
             "gopher" => Some(Link::Gopher(url, label)),
188 195
             "http" => Some(Link::Http(url, label)),
@@ -193,3 +200,15 @@ pub fn make_link(url: String, label: String) -> Option<Link> {
193 200
         _ => None,
194 201
     }
195 202
 }
203
+
204
+const URL_REGEX: &str = r"((ftp|gopher|gemini|finger|http|https)://[a-zA-Z0-9-+&@#/%=~_|$?!:,.]*)";
205
+
206
+fn extract_url(line: &str) -> &str {
207
+    let url_regexp = Regex::new(URL_REGEX).unwrap();
208
+    if url_regexp.is_match(&line) {
209
+        let caps = url_regexp.captures(line).unwrap();
210
+        caps.get(1).map_or("", |m| m.as_str())
211
+    } else {
212
+        line
213
+    }
214
+}

+ 6
- 8
src/gopher/parser.rs ファイルの表示

@@ -35,14 +35,6 @@ impl FromStr for TextElement {
35 35
                 Ok(TextElement::Image(colors::cleanup(line)))
36 36
             } else if line.starts_with('9') {
37 37
                 Ok(TextElement::Binary(String::from(line)))
38
-            } else if line.contains("://") {
39
-                if line.contains("gopher://") {
40
-                    Ok(TextElement::LinkItem(String::from(line)))
41
-                } else if line.contains("http://") || line.contains("https://") {
42
-                    Ok(TextElement::ExternalLinkItem(String::from(line)))
43
-                } else {
44
-                    Ok(TextElement::Text(colors::colorize(line)))
45
-                }
46 38
             } else {
47 39
                 Ok(TextElement::Text(colors::colorize(line)))
48 40
             }
@@ -52,6 +44,12 @@ impl FromStr for TextElement {
52 44
                 Ok(TextElement::LinkItem(String::from(line)))
53 45
             } else if line.contains("http://") || line.contains("https://") {
54 46
                 Ok(TextElement::ExternalLinkItem(String::from(line)))
47
+            } else if line.contains("gemini://") {
48
+                Ok(TextElement::ExternalLinkItem(String::from(line)))
49
+            } else if line.contains("ftp://") {
50
+                Ok(TextElement::ExternalLinkItem(String::from(line)))
51
+            } else if line.contains("finger://") {
52
+                Ok(TextElement::ExternalLinkItem(String::from(line)))
55 53
             } else {
56 54
                 Ok(TextElement::Text(colors::colorize(line)))
57 55
             }

読み込み中…
キャンセル
保存