Skip to content

Commit

Permalink
TxtBlock: standardize newline behavior, less convenient but less conf…
Browse files Browse the repository at this point in the history
…using
  • Loading branch information
ProducerMatt committed Mar 7, 2024
1 parent d8477f1 commit c9ce43f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 27 deletions.
22 changes: 12 additions & 10 deletions lib/txt_block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ defmodule TxtBlock do

def plain_indent_io(str, prefix) when is_binary(prefix) do
IO.iodata_to_binary(str)
|> String.split("\n")
|> String.split("\n", trim: true)
|> Enum.flat_map(&[prefix, &1, "\n"])
end
end
Expand All @@ -78,17 +78,19 @@ defmodule TxtBlock.Debugging do
[
"Testing formats.\n\n",
"Quoted\n",
{:quote_block, "Quoted line 1\nQuoted line 2 with newline\n"},
{:quote_block, "Quoted line 1\nQuoted line 2 without newline"},
{:source_block, "source(1)\nsource(2, \"with_newline\")\n"},
{:source_block, "source(1)\nsource(2, \"without_newline\")"},
{:quote_block, "Quoted line 1\nQuoted line 2\n"},
"\n",
{:source_block, "source(1)\nsource(2)\n"},
"\n",
["Inline source quote ", {:source, "foobar"}, "\n"],
{{:indent, "><> "}, ["school\n", "\nof", "\nfishies"]},
"\n",
"Dotted list",
{{:list, :dotted}, ["Item 1", "Item 2 with newline\n", "Item 3"]},
"Numbered list",
{{:list, :numbered}, ["Item 1", "Item 2 with newline", "Item 3"]}
{{:indent, "><> "}, ["school\n", "of\n", "fishies\n"]},
"\n",
"Dotted list\n",
{{:list, :dotted}, ["Item 1", "Item 2", "Item 3"]},
"\n",
"Numbered list\n",
{{:list, :numbered}, ["Item 1", "Item 2", "Item 3"]}
]
end
end
26 changes: 11 additions & 15 deletions lib/txt_block/md.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule TxtBlock.Md do
[
"```\n",
txt,
"\n```\n"
"```\n"
]
end

Expand All @@ -31,17 +31,17 @@ defmodule TxtBlock.Md do
end

def format(items, {:list, :dotted}) when is_list(items) do
Enum.map(items, fn blk ->
Enum.flat_map(items, fn blk ->
["- ", blk, "\n"]
end)
end

def format(items, {:list, :numbered}) when is_list(items) do
Enum.reduce(items, {[], 0}, fn blk, {ls, i} ->
Enum.map_reduce(items, 0, fn blk, i ->
j = i + 1

{
[j |> Integer.to_string(), ". ", blk, "\n" | ls],
[j |> Integer.to_string(), ". ", blk, "\n"],
j
}
end)
Expand All @@ -55,32 +55,28 @@ defmodule TxtBlock.Md do
Quoted
> Quoted line 1
> Quoted line 2 with newline
> Quoted line 2
> Quoted line 1
> Quoted line 2 without newline
```
source(1)
source(2, "with_newline")
```
```
source(1)
source(2, "without_newline")
source(2)
```
Inline source quote `foobar`
><> school
><> of
><> fishies
Dotted list
- Item 1
- Item 2
- Improper list item 3
- Item 3
Numbered list
1. Item 1
2. Item 2
3. Improper list item 3
Improper end
3. Item 3
"""
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/stampede_stateless_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,14 @@ defmodule StampedeStatelessTest do

one =
TxtBlock.to_str_list(
{:source_block, "foo"},
{:source_block, "foo\n"},
Service.Dummy
)
|> IO.iodata_to_binary()

two =
TxtBlock.to_str_list(
{:source_block, [["f"], [], "o", [["o"]]]},
{:source_block, [["f"], [], "o", [["o"], "\n"]]},
Service.Dummy
)
|> IO.iodata_to_binary()
Expand Down

0 comments on commit c9ce43f

Please sign in to comment.