Skip to content

Commit

Permalink
TxtBlock: moving and merging text formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ProducerMatt committed Mar 3, 2024
1 parent 89f51dc commit 1a487fe
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 79 deletions.
3 changes: 2 additions & 1 deletion lib/plugin/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ defmodule Plugin.Test do
{"a", "(shows channel locks work)"},
{"timeout", "(shows that plugins which time out won't disrupt other plugins)"},
{"raise", "(raises an error which should be reported)"},
{"throw", "(causes a throw which should be reported)"}
{"throw", "(causes a throw which should be reported)"},
{"formatting", "(tests plugin text formatting)"}
]
end

Expand Down
14 changes: 3 additions & 11 deletions lib/service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ defmodule Service do
@callback reload_configs() :: :ok | {:error, any()}
@callback author_is_privileged(server_id :: any(), author_id :: any()) :: boolean()

@callback txt_source_block(txt :: S.io_list()) :: S.io_list()
@callback txt_source(txt :: S.io_list()) :: S.io_list()
@callback txt_quote_block(txt :: S.io_list()) :: S.io_list()
@callback txt_format(blk :: TxtBlock.t(), type :: TxtBlock.type()) :: S.io_list()

@callback start_link(Keyword.t()) :: :ignore | {:error, any} | {:ok, pid}

Expand Down Expand Up @@ -53,12 +51,6 @@ defmodule Service do
apply(service_name, func_name, args)
end

def txt_source_block(text, cfg_or_service),
do: apply_service_function(cfg_or_service, :txt_source_block, [text])

def txt_source(text, cfg_or_service),
do: apply_service_function(cfg_or_service, :txt_source, [text])

def txt_quote_block(text, cfg_or_service),
do: apply_service_function(cfg_or_service, :txt_quote_block, [text])
def txt_format(blk, type, cfg_or_service),
do: apply_service_function(cfg_or_service, :txt_format, [blk, type])
end
16 changes: 4 additions & 12 deletions lib/service/discord.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ defmodule Service.Discord do
"Erlang-level error ",
inspect(level),
"\n",
message |> S.pp() |> txt_source_block()
message |> S.pp() |> Service.Discord.txt_format(:source_block)
]

_ = send_msg(channel_id, log)
Expand Down Expand Up @@ -146,16 +146,8 @@ defmodule Service.Discord do
end

@impl Service
def txt_source_block(txt),
do: S.markdown_source_block_io(txt)

@impl Service
def txt_source(txt),
do: S.markdown_source_io(txt)

@impl Service
def txt_quote_block(txt),
do: S.markdown_quote_io(txt)
def txt_format(blk, kind),
do: TxtBlock.Md.format(blk, kind)

def is_dm(msg), do: msg.guild_id == nil

Expand Down Expand Up @@ -292,7 +284,7 @@ defmodule Service.Discord.Handler do
discord_msg.author |> Nostrum.Struct.User.full_name() |> inspect(),
" \\\n",
"Message:\n",
discord_msg.content |> Discord.txt_quote_block()
discord_msg.content |> Service.Discord.txt_format(:quote_block)
]
end)
end
Expand Down
9 changes: 2 additions & 7 deletions lib/service/dummy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,8 @@ defmodule Service.Dummy do
end

@impl Service
def txt_source_block(txt), do: S.markdown_source_block_io(txt)

@impl Service
def txt_source(txt), do: S.markdown_source_io(txt)

@impl Service
def txt_quote_block(txt), do: S.markdown_quote_io(txt)
def txt_format(blk, kind),
do: TxtBlock.Md.format(blk, kind)

@spec! channel_history(dummy_server_id(), dummy_channel_id()) :: channel()
def channel_history(server_id, channel) do
Expand Down
29 changes: 0 additions & 29 deletions lib/stampede.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,35 +58,6 @@ defmodule Stampede do
end
end

@spec! txt_indent_io(io_list(), String.t() | non_neg_integer()) :: io_list()
def txt_indent_io(str, n) when is_integer(n),
do: str |> txt_indent_io(String.duplicate(" ", n))

def txt_indent_io(str, prefix) when is_binary(prefix) do
IO.iodata_to_binary(str)
|> String.split("\n")
|> Enum.map(&[prefix, &1, "\n"])
end

@spec! markdown_quote_io(io_list()) :: io_list()
def markdown_quote_io(str) do
txt_indent_io(str, "> ")
end

@spec! markdown_source_block_io(io_list()) :: io_list()
def markdown_source_block_io(txt) do
[
"```\n",
txt,
"\n```\n"
]
end

@spec! markdown_source_io(io_list()) :: io_list()
def markdown_source_io(txt) do
["`", txt, "`"]
end

def quick_task_via() do
{:via, PartitionSupervisor, {Stampede.QuickTaskSupers, self()}}
end
Expand Down
64 changes: 45 additions & 19 deletions lib/txt_block.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,31 @@ defmodule TxtBlock do
- quote_block (greater-than signs '>')
- source_block (triple backticks)
- source (single backticks)
- list, dotted (list starting with '-')
- list, numbered (list starting with numbers)
"""

use TypeCheck
alias Stampede, as: S

@type! modes :: :quote_block | :source_block | :source | {:indent, pos_integer() | String.t()}
@type! t :: [] | maybe_improper_list(lazy(t()), lazy(t())) | String.t() | {modes(), lazy(t())}
@type! t_formatted ::
[] | String.t() | maybe_improper_list(lazy(t_formatted()), lazy(t_formatted()))
@type! block :: {type(), lazy(t())}
@type! type ::
:quote_block
| :source_block
| :source
| {:indent, pos_integer() | String.t()}
| {:list, :dotted | :numbered}
@type! t :: [] | maybe_improper_list(lazy(t()), lazy(t())) | String.t() | lazy(block)

@spec! to_iolist(t(), module()) :: t_formatted()
@spec! to_iolist(t(), module()) :: S.io_list()
def to_iolist(item, service_name) when not is_list(item) do
case item do
txt when is_binary(txt) ->
txt

{:source_block, blk} ->
{type, blk} ->
to_iolist(blk, service_name)
|> Service.txt_source_block(service_name)

{:source, blk} ->
to_iolist(blk, service_name)
|> Service.txt_source(service_name)

{:quote_block, blk} ->
to_iolist(blk, service_name)
|> Service.txt_quote_block(service_name)

{{:indent, n}, blk} ->
to_iolist(blk, service_name)
|> Stampede.txt_indent_io(n)
|> Service.txt_format(type, service_name)
end
end

Expand Down Expand Up @@ -69,4 +63,36 @@ defmodule TxtBlock do
other
end
end

@spec! plain_indent_io(S.io_list(), String.t() | non_neg_integer()) :: S.io_list()
def plain_indent_io(str, n) when is_integer(n),
do: str |> plain_indent_io(String.duplicate(" ", n))

def plain_indent_io(str, prefix) when is_binary(prefix) do
IO.iodata_to_binary(str)
|> String.split("\n")
|> Enum.map(&[prefix, &1, "\n"])
end
end

defmodule TxtBlock.Debugging do
use TypeCheck

@spec! all_formats_example() :: TxtBlock.t()
def all_formats_example() do
[
"Testing formats.\n",
"Quoted\n",
{:quote_block, "Quoted line 1\nQuoted line 2\nQuoted line 3\n"},
{:source_block, "source(1)\nsource(2)\nsource(3)\n"},
["Inline source quote ", {:source, "foobar"}, "\n"],
{{:indent, "><> "}, ["school\n", "\nof", "\nfishies"]},
"\n",
"Dotted list",
{{:list, :dotted}, ["Item 1", "Item 2", ["Improper list item " | "3"]]},
"Numbered list",
{{:list, :numbered}, ["Item 1", "Item 2", ["Improper list item " | "3"]]}
| "Improper end"
]
end
end
50 changes: 50 additions & 0 deletions lib/txt_block/md.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
defmodule TxtBlock.Md do
use TypeCheck
alias Stampede, as: S

@spec! format(TxtBlock.t(), TxtBlock.type()) :: S.io_list()
def format(input, type)

def format(txt, {:indent, n}),
do: TxtBlock.plain_indent_io(txt, n)

def format(txt, :quote_block) do
TypeCheck.conforms!(txt, S.io_list())

TxtBlock.plain_indent_io(txt, "> ")
end

def format(txt, :source_block) do
TypeCheck.conforms!(txt, S.io_list())

[
"```\n",
txt,
"\n```\n"
]
end

def format(txt, :source) do
TypeCheck.conforms!(txt, S.io_list())

["`", txt, "`"]
end

def format(items, {:list, :dotted}) when is_list(items) do
Enum.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} ->
j = i + 1

{
[j |> Integer.to_string(), ". ", blk, "\n" | ls],
j
}
end)
|> elem(0)
end
end

0 comments on commit 1a487fe

Please sign in to comment.