Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Labelled hashtag #69

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ pub enum Element<'a> {
Text(&'a str),
/// #hashtag
Tag(&'a str),
/// [label](#tag)
LabelledTag {
label: Box<Element<'a>>,
tag: &'a str
},
farooqkz marked this conversation as resolved.
Show resolved Hide resolved
/// Represents a linebreak - \n
Linebreak,
Link {
Expand Down
18 changes: 17 additions & 1 deletion src/parser/parse_from_text/text_elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
streaming::take_till1,
},
character::complete::char,
combinator::{peek, recognize, verify},
combinator::{peek, recognize, verify, consumed},
sequence::tuple,
AsChar, IResult, Offset, Slice,
};
Expand Down Expand Up @@ -253,6 +253,20 @@
}
}

fn labelled_tag(input: &str) -> IResult<&str, Element, CustomError<&str>> {
let (input, label) = delimited(char('['), is_not("["), char(']'))(input)?;

Check failure on line 257 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

cannot find function `is_not` in this scope

error[E0425]: cannot find function `is_not` in this scope --> src/parser/parse_from_text/text_elements.rs:257:47 | 257 | let (input, label) = delimited(char('['), is_not("["), char(']'))(input)?; | ^^^^^^ not found in this scope | help: consider importing one of these items | 1 + use nom::bytes::complete::is_not; | 1 + use nom::bytes::streaming::is_not; |

Check failure on line 257 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

cannot find function `delimited` in this scope

error[E0425]: cannot find function `delimited` in this scope --> src/parser/parse_from_text/text_elements.rs:257:26 | 257 | let (input, label) = delimited(char('['), is_not("["), char(']'))(input)?; | ^^^^^^^^^ not found in this scope | help: consider importing this function | 1 + use nom::sequence::delimited; |

Check failure on line 257 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, 1.77.2)

cannot find function `delimited` in this scope

Check failure on line 257 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, 1.77.2)

cannot find function `is_not` in this scope
let (_, label) = parse_text_element(label, None)?;
let (input, tag) = delimited(char('('), is_not("("), char(')'))(input)?;

Check failure on line 259 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

cannot find function `is_not` in this scope

error[E0425]: cannot find function `is_not` in this scope --> src/parser/parse_from_text/text_elements.rs:259:45 | 259 | let (input, tag) = delimited(char('('), is_not("("), char(')'))(input)?; | ^^^^^^ not found in this scope | help: consider importing one of these items | 1 + use nom::bytes::complete::is_not; | 1 + use nom::bytes::streaming::is_not; |

Check failure on line 259 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

cannot find function `delimited` in this scope

error[E0425]: cannot find function `delimited` in this scope --> src/parser/parse_from_text/text_elements.rs:259:24 | 259 | let (input, tag) = delimited(char('('), is_not("("), char(')'))(input)?; | ^^^^^^^^^ not found in this scope | help: consider importing this function | 1 + use nom::sequence::delimited; |

Check failure on line 259 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, 1.77.2)

cannot find function `delimited` in this scope

Check failure on line 259 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, 1.77.2)

cannot find function `is_not` in this scope
let (_, tag) = consumed(hashtag)(tag)?;
Ok((
input,
Element::LabelledTag {
label: Box::new(label),
tag

Check failure on line 265 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / clippy

mismatched types

error[E0308]: mismatched types --> src/parser/parse_from_text/text_elements.rs:265:17 | 265 | tag | ^^^ expected `&str`, found `(&str, Element<'_>)` | = note: expected reference `&str` found tuple `(&str, parser::Element<'_>)`

Check failure on line 265 in src/parser/parse_from_text/text_elements.rs

View workflow job for this annotation

GitHub Actions / Build and test (ubuntu-latest, 1.77.2)

mismatched types
}
))
}

pub(crate) fn parse_text_element(
input: &str,
prev_char: Option<char>,
Expand All @@ -266,6 +280,8 @@

if let Ok((i, elm)) = hashtag(input) {
Ok((i, elm))
} else if let Ok((i, elm)) = labelled_tag(input) {
Ok((i, elm))
} else if let Ok((i, elm)) = {
if prev_char == Some(' ') || prev_char.is_none() {
bot_command_suggestion(input)
Expand Down
63 changes: 0 additions & 63 deletions tests/text_to_ast/mod.rs.orig

This file was deleted.

28 changes: 28 additions & 0 deletions tests/text_to_ast/text_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,34 @@ fn persian_hashtag_with_underline() {
);
}

#[test]
fn labelled_hashtag() {
let input = "[Hello](#hello) #world";

assert_eq!(
parse_only_text(input),
vec![
LabelledTag {
label: vec![Text("Hello")],
tag: "#hello",
},
Tag("#world"),
]
);

let input_bold = "[**Hello**](#hi) ";
assert_eq!(
parse_only_text(input_bold),
vec![
LaballedTag {
label: vec![Bold(vec![Text(Hello)])],
tag: "#hi",
},
Text(" ")
]
);
}

#[test]
fn email_address_standalone() {
let test_cases = vec![
Expand Down
Loading