From 61def278b520201a660ff19cc0d064670884875c Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sun, 3 Nov 2019 19:10:05 -0500 Subject: [PATCH] add links --- lib/textile/lexer.ex | 58 ++++++++++++++++++++++++++++++++++++- lib/textile/markup_lexer.ex | 5 ++-- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/lib/textile/lexer.ex b/lib/textile/lexer.ex index e9272737..297c1509 100644 --- a/lib/textile/lexer.ex +++ b/lib/textile/lexer.ex @@ -116,5 +116,61 @@ defmodule Textile.Lexer do image_without_link ]) - defparsec :image, image + + # Links + + + {link_markup_start, link_markup_element} = markup_ending_in(string("\"")) + + link_contents_start = + choice([ + image, + link_markup_start, + ]) + + link_contents_element = + choice([ + image, + link_markup_element + ]) + + link_contents = + optional(link_contents_start) + |> repeat(link_contents_element) + + bracketed_link_end = + string("\":") + |> unwrap_and_tag(:link_end) + |> concat( + url_ending_in(string("]")) + |> unwrap_and_tag(:link_url) + ) + + bracketed_link = + string("[\"") + |> unwrap_and_tag(:link_start) + |> concat(link_contents) + |> concat(bracketed_link_end) + + unbracketed_link_end = + string("\":") + |> unwrap_and_tag(:link_end) + |> concat( + url_ending_in(space()) + |> unwrap_and_tag(:link_url) + ) + + unbracketed_link = + string("\"") + |> unwrap_and_tag(:link_start) + |> concat(link_contents) + |> concat(unbracketed_link_end) + + link = + choice([ + bracketed_link, + unbracketed_link + ]) + + defparsec :link, link end \ No newline at end of file diff --git a/lib/textile/markup_lexer.ex b/lib/textile/markup_lexer.ex index de3a20f1..968ca83a 100644 --- a/lib/textile/markup_lexer.ex +++ b/lib/textile/markup_lexer.ex @@ -4,7 +4,7 @@ defmodule Textile.MarkupLexer do # Markup tags - def markup_segment(ending_sequence) do + def markup_ending_in(ending_sequence) do # The literal tag is special, because # 1. It needs to capture everything inside it as a distinct token. @@ -157,7 +157,6 @@ defmodule Textile.MarkupLexer do utf8_char([]) ]) - optional(markup_at_start) - |> repeat(markup_element) + {markup_at_start, markup_element} end end \ No newline at end of file