Skip to content

Commit

Permalink
Do not build regexes at compile-time
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jan 21, 2025
1 parent 7383e3e commit 9d211ee
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
6 changes: 3 additions & 3 deletions lib/ex_doc/autolink.ex
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,12 @@ defmodule ExDoc.Autolink do
end
end

@ref_regex ~r/^`(.+)`$/
defp ref_regex, do: ~r/^`(.+)`$/

def custom_link(attrs, config) do
case Keyword.fetch(attrs, :href) do
{:ok, href} ->
case Regex.scan(@ref_regex, href) do
case Regex.scan(ref_regex(), href) do
[[_, custom_link]] ->
custom_link
|> url(:custom_link, config)
Expand Down Expand Up @@ -214,7 +214,7 @@ defmodule ExDoc.Autolink do

defp build_extra_link(link, config) do
with %{scheme: nil, host: nil, path: path} = uri <- URI.parse(link),
true <- is_binary(path) and path != "" and not (path =~ @ref_regex),
true <- is_binary(path) and path != "" and not (path =~ ref_regex()),
true <- Path.extname(path) in @builtin_ext do
if file = config.extras[Path.basename(path)] do
append_fragment(file <> config.ext, uri.fragment)
Expand Down
9 changes: 3 additions & 6 deletions lib/ex_doc/formatter/html/templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,8 @@ defmodule ExDoc.Formatter.HTML.Templates do
end

# TODO: split into sections in Formatter.HTML instead (possibly via DocAST)
@h2_regex ~r/<h2.*?>(.*?)<\/h2>/m
defp extract_headers(content) do
@h2_regex
~r/<h2.*?>(.*?)<\/h2>/m
|> Regex.scan(content, capture: :all_but_first)
|> List.flatten()
|> Enum.filter(&(&1 != ""))
Expand Down Expand Up @@ -222,13 +221,12 @@ defmodule ExDoc.Formatter.HTML.Templates do
We only link `h2` and `h3` headers. This is kept consistent in ExDoc.SearchData.
"""
@heading_regex ~r/<(h[23]).*?>(.*?)<\/\1>/m
@spec link_headings(String.t() | nil, String.t()) :: String.t() | nil
def link_headings(content, prefix \\ "")
def link_headings(nil, _), do: nil

def link_headings(content, prefix) do
@heading_regex
~r/<(h[23]).*?>(.*?)<\/\1>/m
|> Regex.scan(content)
|> Enum.reduce({content, %{}}, fn [match, tag, title], {content, occurrences} ->
possible_id = text_to_id(title)
Expand All @@ -243,7 +241,6 @@ defmodule ExDoc.Formatter.HTML.Templates do
|> elem(0)
end

@class_regex ~r/<h[23].*?(\sclass="(?<class>[^"]+)")?.*?>/
@class_separator " "
defp link_heading(match, _tag, _title, "", _prefix), do: match

Expand Down Expand Up @@ -273,7 +270,7 @@ defmodule ExDoc.Formatter.HTML.Templates do
# it was setting `#{section_header_class_name}` as the only CSS class
# associated with the given header.
class_attribute =
case Regex.named_captures(@class_regex, match) do
case Regex.named_captures(~r/<h[23].*?(\sclass="(?<class>[^"]+)")?.*?>/, match) do
%{"class" => ""} ->
section_header_class_name

Expand Down
4 changes: 2 additions & 2 deletions lib/ex_doc/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ defmodule ExDoc.Utils do
end)
end

@clean_html_regex ~r/<\/?\s*[a-zA-Z]+(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*>/

@doc """
Strips HTML tags from text leaving their text content
"""
def strip_tags(text, replace_with \\ "") when is_binary(text) do
String.replace(text, @clean_html_regex, replace_with)
clean_html_regex = ~r/<\/?\s*[a-zA-Z]+(?:[^>=]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*>/
String.replace(text, clean_html_regex, replace_with)
end

@doc """
Expand Down

0 comments on commit 9d211ee

Please sign in to comment.