From 038480a50695432d30ed2a706196086c13c22ca4 Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Thu, 2 Nov 2023 03:47:15 +0100 Subject: [PATCH] add telephone numers to text parsing also add element type to wasm bindings and demo --- message_parser_wasm/example.js | 6 ++++++ message_parser_wasm/src/lib.rs | 3 ++- message_parser_wasm/src/manual_typings.ts | 3 ++- src/parser/mod.rs | 2 +- src/parser/parse_from_text/phone_numbers.rs | 2 +- src/parser/parse_from_text/text_elements.rs | 3 +++ 6 files changed, 15 insertions(+), 4 deletions(-) diff --git a/message_parser_wasm/example.js b/message_parser_wasm/example.js index e248493..a17aeb3 100644 --- a/message_parser_wasm/example.js +++ b/message_parser_wasm/example.js @@ -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"); diff --git a/message_parser_wasm/src/lib.rs b/message_parser_wasm/src/lib.rs index cb6d75f..5bf2696 100644 --- a/message_parser_wasm/src/lib.rs +++ b/message_parser_wasm/src/lib.rs @@ -57,5 +57,6 @@ export type ParsedElement = | { t: "LabeledLink"; c: { label: ParsedElement[]; destination: LinkDestination }; - }; + } + | {t: "TelephoneNumber", c: {number: string, tel_link: string}}; "#; diff --git a/message_parser_wasm/src/manual_typings.ts b/message_parser_wasm/src/manual_typings.ts index 2a7b7a0..3ff2e60 100644 --- a/message_parser_wasm/src/manual_typings.ts +++ b/message_parser_wasm/src/manual_typings.ts @@ -25,4 +25,5 @@ export type ParsedElement = | { t: "LabeledLink"; c: { label: ParsedElement[]; destination: LinkDestination }; - }; + } + | {t: "TelephoneNumber", c: {number: string, tel_link: string}}; diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 0d76d92..ed1243b 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -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) diff --git a/src/parser/parse_from_text/phone_numbers.rs b/src/parser/parse_from_text/phone_numbers.rs index ed72acf..39ad1be 100644 --- a/src/parser/parse_from_text/phone_numbers.rs +++ b/src/parser/parse_from_text/phone_numbers.rs @@ -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"); diff --git a/src/parser/parse_from_text/text_elements.rs b/src/parser/parse_from_text/text_elements.rs index 161d8c6..a88dbfa 100644 --- a/src/parser/parse_from_text/text_elements.rs +++ b/src/parser/parse_from_text/text_elements.rs @@ -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; @@ -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 {