From fa6be88874701555f08f21efa035675c7e80e3cf Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Wed, 27 Nov 2019 16:27:31 -0500 Subject: [PATCH] fix link parsing --- lib/textile/lexer.ex | 30 ++++++++++++++++-------------- lib/textile/parser.ex | 2 +- lib/textile/url_lexer.ex | 19 +++++++++++++++---- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/lib/textile/lexer.ex b/lib/textile/lexer.ex index 3364455f..b9f2705c 100644 --- a/lib/textile/lexer.ex +++ b/lib/textile/lexer.ex @@ -123,20 +123,22 @@ defmodule Textile.Lexer do {link_markup_start, link_markup_element} = markup_ending_in(string("\"")) link_stop = - choice([ - space(), - string("*"), - string("_"), - string("@"), - string("+"), - string("^"), - string("-"), - string("~"), - string("."), - string("?"), - string("!"), - string(",") - ]) + repeat( + choice([ + string("*"), + string("_"), + string("@"), + string("+"), + string("^"), + string("-"), + string("~"), + string("."), + string("?"), + string("!"), + string(","), + ]) + ) + |> choice([space(), eos()]) link_contents_start = choice([ diff --git a/lib/textile/parser.ex b/lib/textile/parser.ex index 6bcebb4a..2c890d6f 100644 --- a/lib/textile/parser.ex +++ b/lib/textile/parser.ex @@ -179,7 +179,7 @@ defmodule Textile.Parser do # link_start well_formed_including_paragraphs link_end link_url; # defp link(parser, [{:link_start, start} | r_tokens]) do - case well_formed_including_paragraphs(parser, nil, r_tokens) do + case well_formed_including_paragraphs(parser, :link_end, r_tokens) do {:ok, tree, [{:link_end, _end}, {:link_url, url} | r2_tokens]} -> {:ok, [{:markup, ~s||}, tree, {:markup, ~s||}], r2_tokens} diff --git a/lib/textile/url_lexer.ex b/lib/textile/url_lexer.ex index 443e74f1..212224de 100644 --- a/lib/textile/url_lexer.ex +++ b/lib/textile/url_lexer.ex @@ -2,13 +2,24 @@ defmodule Textile.UrlLexer do import NimbleParsec def url_ending_in(ending_sequence) do - protocol = + domain = + repeat( + choice([ + ascii_char([?a..?z]) |> string(".") |> ascii_char([?a..?z]), + ascii_char([?a..?z]) + ]) + ) + + scheme_and_domain = choice([ - string("/"), string("https://"), string("http://"), string("data:image/") + string("/"), + string("data:image/"), + string("https://") |> concat(domain), + string("http://") |> concat(domain) ]) - protocol - |> repeat(lookahead_not(ending_sequence) |> utf8_char([])) + scheme_and_domain + |> repeat(utf8_char([]) |> lookahead_not(ending_sequence)) |> reduce({List, :to_string, []}) end end \ No newline at end of file