pinchflat/lib/pinchflat/rendered_string/parser.ex
Kieran 47f0fd533f
Refactor and document output path (#28)
* Updated output path builder to better use yt-dlp variables

* Added UI explaining output template options

* Fixed outdated reference in dockerfile
2024-02-20 13:40:13 -08:00

42 lines
1.3 KiB
Elixir

defmodule Pinchflat.RenderedString.Parser do
@moduledoc """
Parses liquid-ish-style strings into a rendered string
Used for turning filepath templates into real filepaths
"""
use Pinchflat.RenderedString.Base
@doc """
Parses a string into a rendered string, using the provided variables. Optionally
takes a custom fetcher function for handling missing variables.
Variable identifiers are surrounded by {{ and }}. The variable keys MUST be strings.
If an identifier is not found in the provided variables, it will be removed from the string.
Returns `{:ok, binary()}` or `{:error, binary()}`.
"""
def parse(string, variables, value_fetch_fn \\ &default_fetcher/2) do
# `do_parse` comes from `RenderedString.Base`
case do_parse(string) do
{:ok, parsed, _, _, _, _} ->
{:ok, build_string(parsed, variables, value_fetch_fn)}
{:error, message, _, _, _, _} ->
{:error, message}
end
end
defp build_string(parsed, variables, value_fetch_fn) do
Enum.reduce(parsed, "", fn element, acc ->
case element do
{:text, text} -> acc <> text
{:interpolation, {:identifier, identifier}} -> acc <> value_fetch_fn.(identifier, variables)
end
end)
end
def default_fetcher(identifier, variables) do
Map.get(variables, identifier, "")
end
end