philomena/lib/textile/url_lexer.ex

34 lines
757 B
Elixir
Raw Normal View History

2019-11-04 00:58:11 +01:00
defmodule Textile.UrlLexer do
import NimbleParsec
def url_ending_in(ending_sequence) do
2019-11-28 01:19:55 +01:00
domain_character =
choice([
ascii_char([?a..?z]),
ascii_char([?A..?Z]),
ascii_char([?0..?9]),
string("-")
])
2019-11-27 22:27:31 +01:00
domain =
repeat(
choice([
2019-11-28 01:19:55 +01:00
domain_character |> string(".") |> concat(domain_character),
domain_character
2019-11-27 22:27:31 +01:00
])
)
scheme_and_domain =
2019-11-04 00:58:11 +01:00
choice([
2019-12-08 15:43:47 +01:00
string("#"),
2019-11-27 22:27:31 +01:00
string("/"),
string("data:image/"),
string("https://") |> concat(domain),
string("http://") |> concat(domain)
2019-11-04 00:58:11 +01:00
])
2019-11-27 22:27:31 +01:00
scheme_and_domain
|> repeat(lookahead_not(ending_sequence) |> utf8_char([]))
2019-11-04 00:58:11 +01:00
|> reduce({List, :to_string, []})
end
end