Support - lists in ANSIDocs, closes #1937

This commit is contained in:
José Valim
2013-12-15 22:02:57 +01:00
parent 11ab91c0fb
commit c1bcf5dab2
2 changed files with 36 additions and 9 deletions
+6 -4
View File
@@ -1,6 +1,8 @@
defmodule IEx.ANSIDocs do
@moduledoc false
@bullets [?*, ?-]
@doc """
Prints the head of the documentation (i.e. the function signature)
"""
@@ -64,7 +66,7 @@ defmodule IEx.ANSIDocs do
defp process([line | rest], indent) do
{ stripped, count } = strip_spaces(line, 0)
case stripped do
"* " <> item ->
<<bullet, ?\s, item :: binary >> when bullet in @bullets ->
process_list(item, rest, count, indent)
_ ->
process_text(rest, [line], indent, false)
@@ -117,7 +119,7 @@ defmodule IEx.ANSIDocs do
end
end
defp process_list_next(["* " <> _ | _] = rest, _count, _done, acc) do
defp process_list_next([<<bullet, ?\s, _ :: binary>> | _] = rest, _count, _done, acc) when bullet in @bullets do
{ Enum.reverse(acc), rest, false }
end
@@ -129,7 +131,7 @@ defmodule IEx.ANSIDocs do
{ Enum.reverse(acc), rest, done }
end
defp list_next("* " <> _, 0), do: :done
defp list_next(<<bullet, ?\s, _ :: binary>>, 0) when bullet in @bullets, do: :done
defp list_next(line, 0), do: chop(line, 2)
defp list_next(" " <> line, acc), do: list_next(line, acc - 1)
defp list_next(line, _acc), do: line
@@ -151,7 +153,7 @@ defmodule IEx.ANSIDocs do
defp process_text([line | rest], para, indent, true) do
{ stripped, count } = strip_spaces(line, 0)
case stripped do
"* " <> item ->
<<bullet, ?\s, item :: binary>> when bullet in @bullets ->
write_text(Enum.reverse(para), indent, true)
process_list(item, rest, count, indent)
_ ->
+30 -5
View File
@@ -49,31 +49,56 @@ defmodule IEx.AnsiDocsTest do
assert result == "line\n\e[0m\n\e[36m\e[1m┃ code\n┃ code2\e[0m\n\e[0m\nline2\n\e[0m"
end
test "list is converted" do
test "* list is converted" do
result = format("* one\n* two\n* three\n")
assert result == "• one\n• two\n• three\n\e[0m"
end
test "list surrounded by text is converted" do
test "* list surrounded by text is converted" do
result = format("Count:\n\n* one\n* two\n* three\n\nDone")
assert result == "Count:\n\e[0m\n• one\n• two\n• three\n\e[0m\nDone\n\e[0m"
end
test "list with continuation is converted" do
test "* list with continuation is converted" do
result = format("* one\n two\n three\n* four")
assert result == "• one two three\n• four"
end
test "nested lists are converted" do
test "* nested lists are converted" do
result = format("* one\n * one.one\n * one.two\n* two")
assert result == "• one\n • one.one\n • one.two\n• two"
end
test "lists with spaces are converted" do
test "* lists with spaces are converted" do
result = format(" * one\n * two\n * three")
assert result == "• one\n• two\n• three"
end
test "- list is converted" do
result = format("- one\n- two\n- three\n")
assert result == "• one\n• two\n• three\n\e[0m"
end
test "- list surrounded by text is converted" do
result = format("Count:\n\n- one\n- two\n- three\n\nDone")
assert result == "Count:\n\e[0m\n• one\n• two\n• three\n\e[0m\nDone\n\e[0m"
end
test "- list with continuation is converted" do
result = format("- one\n two\n three\n- four")
assert result == "• one two three\n• four"
end
test "- nested lists are converted" do
result = format("- one\n - one.one\n - one.two\n- two")
assert result == "• one\n • one.one\n • one.two\n• two"
end
test "- lists with spaces are converted" do
result = format(" - one\n - two\n - three")
assert result == "• one\n• two\n• three"
end
test "paragraphs are split" do
result = format("para1\n\npara2")
assert result == "para1\n\e[0m\npara2\n\e[0m"