Skip to content

Commit

Permalink
add telephone numers to text parsing
Browse files Browse the repository at this point in the history
also add element type to wasm bindings and demo
  • Loading branch information
Simon-Laux committed Nov 2, 2023
1 parent 5364d2d commit 038480a
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 4 deletions.
6 changes: 6 additions & 0 deletions message_parser_wasm/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ function renderElement(elm) {
);
return bcs;

case "TelephoneNumber":
let tn = document.createElement("a");
tn.innerText = elm.c.number;
tn.href = elm.c.tel_link;
return tn;

case "Linebreak":
return document.createElement("br");

Expand Down
3 changes: 2 additions & 1 deletion message_parser_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ export type ParsedElement =
| {
t: "LabeledLink";
c: { label: ParsedElement[]; destination: LinkDestination };
};
}
| {t: "TelephoneNumber", c: {number: string, tel_link: string}};
"#;
3 changes: 2 additions & 1 deletion message_parser_wasm/src/manual_typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ export type ParsedElement =
| {
t: "LabeledLink";
c: { label: ParsedElement[]; destination: LinkDestination };
};
}
| {t: "TelephoneNumber", c: {number: string, tel_link: string}};
2 changes: 1 addition & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub enum Element<'a> {
Link {
destination: LinkDestination<'a>,
},
TelephoneNumber{
TelephoneNumber {
/// number exactly how it was found in the input text
number: &'a str,
/// the tel: link (without special chars, but keeps the + in the beginning if it is present)
Expand Down
2 changes: 1 addition & 1 deletion src/parser/parse_from_text/phone_numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ mod test {
}

#[test]
fn test_not_enough_digits(){
fn test_not_enough_digits() {
telephone_number("(0)152 28").expect_err("fails because number is to short");
telephone_number("152 28").expect_err("fails because too short");
telephone_number("(152) 28").expect_err("fails because too short");
Expand Down
3 changes: 3 additions & 0 deletions src/parser/parse_from_text/text_elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::parser::link_url::LinkDestination;

use super::base_parsers::*;
use super::hashtag_content_char_ranges::hashtag_content_char;
use super::phone_numbers::telephone_number;
use super::Element;
use crate::nom::{Offset, Slice};
use nom::bytes::complete::take_while;
Expand Down Expand Up @@ -275,6 +276,8 @@ pub(crate) fn parse_text_element(
}
} {
Ok((i, elm))
} else if let Ok((i, elm)) = telephone_number(input) {
Ok((i, elm))
} else if let Ok((i, _)) = linebreak(input) {
Ok((i, Element::Linebreak))
} else {
Expand Down

0 comments on commit 038480a

Please sign in to comment.