Browse Source

Simpler parsing

tags/0.8.15
Julien Blanchard 10 months ago
parent
commit
4a5b575001
2 changed files with 25 additions and 45 deletions
  1. 19
    38
      src/gemini/parser.rs
  2. 6
    7
      src/gopher/parser.rs

+ 19
- 38
src/gemini/parser.rs View File

@@ -1,8 +1,3 @@
1
-extern crate regex;
2
-use regex::Regex;
3
-
4
-use crate::colors::*;
5
-
6 1
 use std::str::FromStr;
7 2
 
8 3
 #[derive(Debug)]
@@ -20,47 +15,33 @@ pub enum TextElement {
20 15
 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
21 16
 pub struct ParseError;
22 17
 
23
-const H1_REGEX: &str = r"^#\s+(.*)$";
24
-const H2_REGEX: &str = r"^##\s+(.*)$";
25
-const H3_REGEX: &str = r"^###\s+(.*)$";
26
-const LIST_ITEM_REGEX: &str = r"^\*\s+([^*]*)$";
27
-const LINK_ITEM_REGEX: &str = r"^=>\s*(\S*)\s*(.*)?$";
28
-
29 18
 impl FromStr for TextElement {
30 19
     type Err = ParseError;
31 20
 
32 21
     // Parses a &str into an instance of 'TextElement'
33 22
     fn from_str(line: &str) -> Result<TextElement, ParseError> {
34
-        let h1_regexp = Regex::new(H1_REGEX).unwrap();
35
-        let h2_regexp = Regex::new(H2_REGEX).unwrap();
36
-        let h3_regexp = Regex::new(H3_REGEX).unwrap();
37
-        let list_item_regexp = Regex::new(LIST_ITEM_REGEX).unwrap();
38
-        let link_item_regexp = Regex::new(LINK_ITEM_REGEX).unwrap();
39
-
40
-        if h1_regexp.is_match(&line) {
41
-            let caps = h1_regexp.captures(&line).unwrap();
42
-            let header = caps.get(1).map_or("", |m| m.as_str());
43
-            Ok(TextElement::H1(String::from(header)))
44
-        } else if h2_regexp.is_match(&line) {
45
-            let caps = h2_regexp.captures(&line).unwrap();
46
-            let header = caps.get(1).map_or("", |m| m.as_str());
47
-            Ok(TextElement::H2(String::from(header)))
48
-        } else if h3_regexp.is_match(&line) {
49
-            let caps = h3_regexp.captures(&line).unwrap();
50
-            let header = caps.get(1).map_or("", |m| m.as_str());
51
-            Ok(TextElement::H3(String::from(header)))
52
-        } else if list_item_regexp.is_match(&line) {
53
-            let caps = list_item_regexp.captures(&line).unwrap();
54
-            let header = caps.get(1).map_or("", |m| m.as_str());
55
-            Ok(TextElement::ListItem(String::from(header)))
56
-        } else if link_item_regexp.is_match(&line) {
23
+        if line.starts_with("###") {
24
+            let text = line.split_at(3).1.trim();
25
+            Ok(TextElement::H3(String::from(text)))
26
+        } else if line.starts_with("##") {
27
+            let text = line.split_at(2).1.trim();
28
+            Ok(TextElement::H2(String::from(text)))
29
+        } else if line.starts_with("#") {
30
+            let text = line.split_at(1).1.trim();
31
+            Ok(TextElement::H1(String::from(text)))
32
+        } else if line.starts_with("*") {
33
+            let text = line.split_at(1).1.trim();
34
+            Ok(TextElement::ListItem(String::from(text)))
35
+        } else if line.starts_with("=>") {
57 36
             Ok(TextElement::LinkItem(String::from(line)))
58 37
         } else if line.starts_with("```") {
59
-            Ok(TextElement::MonoText(String::from(line)))
60
-        } else if line.starts_with("> ") {
61
-            Ok(TextElement::Quote(String::from(line)))
38
+            let text = line.split_at(3).1.trim();
39
+            Ok(TextElement::MonoText(String::from(text)))
40
+        } else if line.starts_with(">") {
41
+            let text = line.split_at(1).1.trim();
42
+            Ok(TextElement::Quote(String::from(text)))
62 43
         } else {
63
-            Ok(TextElement::Text(colors::colorize(line)))
44
+            Ok(TextElement::Text(String::from(line)))
64 45
         }
65 46
     }
66 47
 }

+ 6
- 7
src/gopher/parser.rs View File

@@ -1,4 +1,3 @@
1
-use crate::colors::*;
2 1
 use std::str::FromStr;
3 2
 
4 3
 #[derive(Debug)]
@@ -22,21 +21,21 @@ impl FromStr for TextElement {
22 21
         if els.count() >= 2 {
23 22
             // Gophermap line
24 23
             if line.starts_with('0') || line.starts_with('1') {
25
-                Ok(TextElement::LinkItem(colors::cleanup(line)))
24
+                Ok(TextElement::LinkItem(String::from(line)))
26 25
             } else if line.starts_with('i') {
27 26
                 let mut els = line.split('\t');
28 27
                 let text = els.next().expect("");
29 28
                 let mut text = String::from(text);
30 29
                 text.remove(0);
31
-                Ok(TextElement::Text(colors::colorize(&text)))
30
+                Ok(TextElement::Text(text))
32 31
             } else if line.starts_with('h') {
33
-                Ok(TextElement::ExternalLinkItem(colors::cleanup(line)))
32
+                Ok(TextElement::ExternalLinkItem(String::from(line)))
34 33
             } else if line.starts_with('I') || line.starts_with('g') {
35
-                Ok(TextElement::Image(colors::cleanup(line)))
34
+                Ok(TextElement::Image(String::from(line)))
36 35
             } else if line.starts_with('9') {
37 36
                 Ok(TextElement::Binary(String::from(line)))
38 37
             } else {
39
-                Ok(TextElement::Text(colors::colorize(line)))
38
+                Ok(TextElement::Text(String::from(line)))
40 39
             }
41 40
         } else {
42 41
             // Text line
@@ -50,7 +49,7 @@ impl FromStr for TextElement {
50 49
             {
51 50
                 Ok(TextElement::ExternalLinkItem(String::from(line)))
52 51
             } else {
53
-                Ok(TextElement::Text(colors::colorize(line)))
52
+                Ok(TextElement::Text(String::from(line)))
54 53
             }
55 54
         }
56 55
     }

Loading…
Cancel
Save