diff --git a/lib/diff_web/controllers/page_controller.ex b/lib/diff_web/controllers/page_controller.ex index f63b45d..e78f1a2 100644 --- a/lib/diff_web/controllers/page_controller.ex +++ b/lib/diff_web/controllers/page_controller.ex @@ -28,18 +28,13 @@ defmodule DiffWeb.PageController do end end - def expand_context(conn, %{"file_name" => file_name} = params) do - %{ - "version" => version, - "package" => package, - "from_line" => from_line, - "direction" => direction - } = params - - case parse_version(version) do + def expand_context(conn, %{"file_name" => _file_name} = params) do + case parse_version(params["version"]) do {:ok, version} -> version = to_string(version) - do_expand_context(conn, package, version, file_name, from_line, direction) + params = Map.update!(params, "version", fn _ -> version end) + + do_expand_context(conn, params) :error -> conn @@ -96,10 +91,22 @@ defmodule DiffWeb.PageController do end end - defp do_expand_context(conn, package, version, file_name, from_line, direction) do - case Diff.Hex.get_chunk(package, version, file_name, from_line, direction) do + defp do_expand_context(conn, params) do + chunk_extractor_params = + for {key, val} <- params, into: %{} do + {String.to_existing_atom(key), val} + end + + case Diff.Hex.get_chunk(chunk_extractor_params) do {:ok, chunk} -> - json(conn, %{chunk: chunk}) + rendered_chunk = + Phoenix.View.render_to_string(DiffWeb.RenderView, "render_context_chunk.html", + chunk: chunk + ) + + conn + |> put_status(200) + |> json(%{chunk: rendered_chunk, lines: length(chunk)}) {:error, %{errors: errors}} -> conn diff --git a/lib/diff_web/router.ex b/lib/diff_web/router.ex index 191e07e..b9427d0 100644 --- a/lib/diff_web/router.ex +++ b/lib/diff_web/router.ex @@ -15,7 +15,11 @@ defmodule DiffWeb.Router do pipe_through :browser live "/", SearchLiveView - get "/diff/:package/:version/expand/:from_line/:direction", PageController, :expand_context + + get "/diff/:package/:version/expand/:from_line/:to_line/:right_line/", + PageController, + :expand_context + get "/diff/:package/:versions", PageController, :diff get "/diffs", PageController, :diffs end diff --git a/lib/diff_web/templates/render/render_context_chunk.html.eex b/lib/diff_web/templates/render/render_context_chunk.html.eex new file mode 100644 index 0000000..90396ee --- /dev/null +++ b/lib/diff_web/templates/render/render_context_chunk.html.eex @@ -0,0 +1,15 @@ +<%= for line <- @chunk do %> +