rouge-ruby / rouge

A pure Ruby code highlighter that is compatible with Pygments
https://rouge.jneen.net/
Other
3.3k stars 732 forks source link

Elixir triple-quoted strings after a sigil sign produce unexpected results #2051

Open bsmithgall opened 4 weeks ago

bsmithgall commented 4 weeks ago

Name of the lexer Elixir

Code sample

This is taken from the elixir documentation about sigils and character escaping:

~S"""
Converts double-quotes to single-quotes.

## Examples

    iex> convert("\"foo\"")
    "'foo'"

"""

Link to jneen example: rouge.jneen.net.

Additional context

I wasn't sure whether to file this as a bug or feature request, but the primary driver here is adding support for HEEx templates, which were added to the Phoenix web framework somewhat recently. Those embed what is something like eex syntax directly inside of an Elixir .ex file.

Here is an example from the Phoenix LiveView getting started documentation:

defmodule MyAppWeb.ThermostatLive do
  # In Phoenix v1.6+ apps, the line is typically: use MyAppWeb, :live_view
  use Phoenix.LiveView

  def render(assigns) do
    ~H"""
    Current temperature: <%= @temperature %>°F
    <button phx-click="inc_temperature">+</button>
    """
  end

  def mount(_params, _session, socket) do
    temperature = 70 # Let's assume a fixed temperature for now
    {:ok, assign(socket, :temperature, temperature)}
  end

  def handle_event("inc_temperature", _params, socket) do
    {:noreply, update(socket, :temperature, &(&1 + 1))}
  end
end

(and relevant jneen link)

However, when I was exploring further, I realized that in general sigils that use """ delimiters also don't work as expected. I think adding support for HEEx completely (like, for example, in tree-sitter-heex) would be nice but difficult. In the short term, doing what GitHub does and ensuring that """-enclosed sigils are treated as strings would be great.