View Source TextParser (text_parser v0.1.0)
Summary
Functions
When used, defines a custom parser module.
Returns tokens of the specified type from a Text struct.
Parses the given text and returns a Text struct with extracted tokens.
Functions
When used, defines a custom parser module.
Example
defmodule MyParser do
use TextParser
def validate(%TextParser.Tokens.Tag{value: value} = tag) do
if String.length(value) >= 66, do: {:error, "tag too long"}, else: {:ok, tag}
end
end
@spec get(TextParser.Text.t(), module()) :: [struct()]
Returns tokens of the specified type from a Text struct.
Examples
iex> text = TextParser.parse("Check out https://example.com #elixir")
iex> TextParser.get(text, URL)
[%TextParser.Tokens.URL{value: "https://example.com", position: {10, 29}}]
@spec parse(String.t()) :: TextParser.Text.t()
@spec parse( String.t(), keyword() ) :: TextParser.Text.t()
Parses the given text and returns a Text struct with extracted tokens.
Options
:extract
- A list of token extractor modules that implement theTextParser.Token
behaviour.
Examples
# Extract URLs only
iex> TextParser.parse("Check out https://example.com", extract: [URL])
%TextParser.Text{
value: "Check out https://example.com",
tokens: [
%TextParser.Tokens.URL{
value: "https://example.com",
position: {10, 29}
}
]
}
# Extract URLs and tags
iex> TextParser.parse("Check out https://example.com #elixir", extract: [URL, Tag])
%TextParser.Text{
value: "Check out https://example.com #elixir",
tokens: [
%TextParser.Tokens.URL{
value: "https://example.com",
position: {10, 29}
},
%TextParser.Tokens.Tag{
value: "#elixir",
position: {30, 37}
}
]
}