Skip to content

Commit acde807

Browse files
author
José Valim
committed
Clean up IO.ANSI.Docs handling of `
Signed-off-by: José Valim <[email protected]>
1 parent 487ac9c commit acde807

File tree

1 file changed

+60
-58
lines changed

1 file changed

+60
-58
lines changed

lib/elixir/lib/io/ansi/docs.ex

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ defmodule IO.ANSI.Docs do
194194
lines
195195
|> Enum.join(" ")
196196
|> handle_links
197-
|> handle_inline(true, nil, [], [], options)
197+
|> handle_inline(options)
198198
|> String.split(~r{\s})
199199
|> write_with_wrap(options[:width] - byte_size(indent), indent, no_wrap)
200200

@@ -262,7 +262,7 @@ defmodule IO.ANSI.Docs do
262262
col = col
263263
|> String.replace(~r/\\ \|/x, "|")
264264
|> handle_links
265-
|> handle_inline(true, nil, [], [], options)
265+
|> handle_inline(options)
266266
{col, length_without_escape(col, 0)}
267267
end
268268

@@ -392,108 +392,110 @@ defmodule IO.ANSI.Docs do
392392
Regex.replace(~r{\[(.*?)\]\((.*?)\)}, text, "\\1 (\\2)")
393393
end
394394

395-
# Single inline quotes.
396-
@single [?`, ?_, ?*]
397395

398-
# ` does not require space in between
399-
@spaced [?_, ?*]
396+
# We have four entries: **, *, _ and `.
397+
#
398+
# The first three behave the same while the last one is simpler
399+
# when it comes to delimiters. But, since the first has two
400+
# characters, we need to handle 3 cases:
401+
#
402+
# 1. **
403+
# 2. _ and *
404+
# 3. `
405+
#
406+
# Where the first two should have the same code but match differently.
407+
@single [?_, ?*]
400408

401409
# Characters that can mark the beginning or the end of a word.
402410
# Only support the most common ones at this moment.
403411
@delimiters [?\s, ?', ?", ?!, ?@, ?#, ?$, ?%, ?^, ?&, ?-, ?+, ?(, ?), ?[, ?], ?{, ?}, ?<, ?>]
404412

405-
# Clauses for handling spaces
406-
defp handle_inline(<<?*, ?*, ?\s, rest :: binary>>, _line_starter, nil, buffer, acc, options) do
407-
handle_inline(rest, false, nil, [?\s, ?*, ?*|buffer], acc, options)
408-
end
413+
# Inline start
409414

410-
defp handle_inline(<<mark, ?\s, rest :: binary>>, _line_starter, nil, buffer, acc, options) when mark in @spaced do
411-
handle_inline(rest, false, nil, [?\s, mark|buffer], acc, options)
415+
defp handle_inline(<<?*, ?*, rest :: binary>>, options) do
416+
handle_inline(rest, ?d, ["**"], [], options)
412417
end
413418

414-
# Inline start
415-
defp handle_inline(<<delimiter, ?*, ?*, rest :: binary>>, _line_starter, nil, buffer, acc, options)
416-
when rest != "" and delimiter in @delimiters do
417-
handle_inline(rest, false, ?d, ["**"], [delimiter, Enum.reverse(buffer)|acc], options)
419+
defp handle_inline(<<mark, rest :: binary>>, options) when mark in @single do
420+
handle_inline(rest, mark, [<<mark>>], [], options)
418421
end
419422

420-
defp handle_inline(<<delimiter, mark, rest :: binary>>, _line_starter, nil, buffer, acc, options)
421-
when rest != "" and delimiter in @delimiters and mark in @single do
422-
handle_inline(rest, false, mark, [<<mark>>], [delimiter, Enum.reverse(buffer)|acc], options)
423+
defp handle_inline(rest, options) do
424+
handle_inline(rest, nil, [], [], options)
423425
end
424426

425-
defp handle_inline(<<?*, ?*, rest :: binary>>, true, nil, buffer, acc, options) do
426-
handle_inline(rest, false, ?d, ["**"], [Enum.reverse(buffer)|acc], options)
427-
end
427+
# Inline delimiters
428428

429-
defp handle_inline(<<mark, rest :: binary>>, true, nil, buffer, acc, options) when mark in @single do
430-
handle_inline(rest, false, mark, [<<mark>>], [Enum.reverse(buffer)|acc], options)
429+
defp handle_inline(<<delimiter, ?*, ?*, rest :: binary>>, nil, buffer, acc, options)
430+
when rest != "" and delimiter in @delimiters do
431+
handle_inline(rest, ?d, ["**"], [delimiter, Enum.reverse(buffer)|acc], options)
431432
end
432433

433-
# Clauses for handling spaces
434-
defp handle_inline(<<?\s, ?*, ?*, rest :: binary>>, _line_starter, limit, buffer, acc, options) do
435-
handle_inline(rest, false, limit, [?*, ?*, ?\s|buffer], acc, options)
434+
defp handle_inline(<<delimiter, mark, rest :: binary>>, nil, buffer, acc, options)
435+
when rest != "" and delimiter in @delimiters and mark in @single do
436+
handle_inline(rest, mark, [<<mark>>], [delimiter, Enum.reverse(buffer)|acc], options)
436437
end
437438

438-
defp handle_inline(<<?\s, mark, rest :: binary>>, _line_starter, limit, buffer, acc, options) when mark in @spaced do
439-
handle_inline(rest, false, limit, [mark, ?\s|buffer], acc, options)
439+
defp handle_inline(<<?`, rest :: binary>>, nil, buffer, acc, options)
440+
when rest != "" do
441+
handle_inline(rest, ?`, ["`"], [Enum.reverse(buffer)|acc], options)
440442
end
441443

442444
# Clauses for handling escape
443-
defp handle_inline(<<?\\, ?\\, ?*, ?*, rest :: binary>>, _line_starter, nil, buffer, acc, options)
445+
446+
defp handle_inline(<<?\\, ?\\, ?*, ?*, rest :: binary>>, nil, buffer, acc, options)
444447
when rest != "" do
445-
handle_inline(rest, false, ?d, ["**"], [?\\, Enum.reverse(buffer)|acc], options)
448+
handle_inline(rest, ?d, ["**"], [?\\, Enum.reverse(buffer)|acc], options)
446449
end
447450

448-
defp handle_inline(<<?\\, ?\\, mark, rest :: binary>>, _line_starter, nil, buffer, acc, options)
451+
defp handle_inline(<<?\\, ?\\, mark, rest :: binary>>, nil, buffer, acc, options)
449452
when rest != "" and mark in @single do
450-
handle_inline(rest, false, mark, [<<mark>>], [?\\, Enum.reverse(buffer)|acc], options)
451-
end
452-
453-
defp handle_inline(<<?\\, ?\\, rest :: binary>>, _line_starter, limit, buffer, acc, options) do
454-
handle_inline(rest, false, limit, [?\\|buffer], acc, options)
453+
handle_inline(rest, mark, [<<mark>>], [?\\, Enum.reverse(buffer)|acc], options)
455454
end
456455

457-
defp handle_inline(<<?\\, ?*, ?*, rest :: binary>>, _line_starter, limit, buffer, acc, options) do
458-
handle_inline(rest, false, limit, [?*, ?*|buffer], acc, options)
456+
defp handle_inline(<<?\\, ?\\, rest :: binary>>, limit, buffer, acc, options) do
457+
handle_inline(rest, limit, [?\\|buffer], acc, options)
459458
end
460459

461460
# An escape is not valid inside `
462-
defp handle_inline(<<?\\, mark, rest :: binary>>, _line_starter, limit, buffer, acc, options)
463-
when mark in [?_, ?*, ?`] and not(mark == limit and mark == ?`) do
464-
handle_inline(rest, false, limit, [mark|buffer], acc, options)
461+
defp handle_inline(<<?\\, mark, rest :: binary>>, limit, buffer, acc, options)
462+
when not(mark == limit and mark == ?`) do
463+
handle_inline(rest, limit, [mark|buffer], acc, options)
465464
end
466465

467466
# Inline end
468-
defp handle_inline(<<?*, ?*, delimiter, rest :: binary>>, _line_starter, ?d, buffer, acc, options)
467+
468+
defp handle_inline(<<?*, ?*, delimiter, rest :: binary>>, ?d, buffer, acc, options)
469469
when delimiter in @delimiters do
470-
handle_inline(<<delimiter, rest :: binary>>, false, nil, [], [inline_buffer(buffer, options)|acc], options)
470+
handle_inline(<<delimiter, rest :: binary>>, nil, [], [inline_buffer(buffer, options)|acc], options)
471471
end
472472

473-
defp handle_inline(<<mark, delimiter, rest :: binary>>, _line_starter, mark, buffer, acc, options)
473+
defp handle_inline(<<mark, delimiter, rest :: binary>>, mark, buffer, acc, options)
474474
when delimiter in @delimiters and mark in @single do
475-
handle_inline(<<delimiter, rest :: binary>>, false, nil, [], [inline_buffer(buffer, options)|acc], options)
475+
handle_inline(<<delimiter, rest :: binary>>, nil, [], [inline_buffer(buffer, options)|acc], options)
476476
end
477-
478-
defp handle_inline(<<?\s, ?`, rest :: binary>>, _line_starter, ?`, buffer, acc, options) do
479-
handle_inline(rest, false, nil, [], [inline_buffer([?\s|buffer], options)|acc], options)
477+
478+
defp handle_inline(<<?*, ?*, rest:: binary>>, ?d, buffer, acc, options)
479+
when rest == "" do
480+
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options)|acc], options)
480481
end
481482

482-
defp handle_inline(<<?*, ?*, rest:: binary>>, _line_starter, ?d, buffer, acc, options)
483-
when rest in @delimiters or rest == "" do
484-
handle_inline(<<>>, false, nil, [], [inline_buffer(buffer, options)|acc], options)
483+
defp handle_inline(<<mark, rest :: binary>>, mark, buffer, acc, options)
484+
when rest == "" and mark in @single do
485+
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options)|acc], options)
485486
end
486487

487-
defp handle_inline(<<mark, rest :: binary>>, _line_starter, mark, buffer, acc, options)
488-
when (rest in @delimiters or rest == "") and mark in @single do
489-
handle_inline(<<>>, false, nil, [], [inline_buffer(buffer, options)|acc], options)
488+
defp handle_inline(<<?`, rest :: binary>>, ?`, buffer, acc, options) do
489+
handle_inline(rest, nil, [], [inline_buffer(buffer, options)|acc], options)
490490
end
491491

492-
defp handle_inline(<<char, rest :: binary>>, _line_starter, mark, buffer, acc, options) do
493-
handle_inline(rest, false, mark, [char|buffer], acc, options)
492+
# Catch all
493+
494+
defp handle_inline(<<char, rest :: binary>>, mark, buffer, acc, options) do
495+
handle_inline(rest, mark, [char|buffer], acc, options)
494496
end
495497

496-
defp handle_inline(<<>>, _line_starter, _mark, buffer, acc, _options) do
498+
defp handle_inline(<<>>, _mark, buffer, acc, _options) do
497499
IO.iodata_to_binary Enum.reverse([Enum.reverse(buffer)|acc])
498500
end
499501

0 commit comments

Comments
 (0)