Skip to content

Commit 9d211ee

Browse files
committed
Do not build regexes at compile-time
1 parent 7383e3e commit 9d211ee

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

lib/ex_doc/autolink.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ defmodule ExDoc.Autolink do
163163
end
164164
end
165165

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

168168
def custom_link(attrs, config) do
169169
case Keyword.fetch(attrs, :href) do
170170
{:ok, href} ->
171-
case Regex.scan(@ref_regex, href) do
171+
case Regex.scan(ref_regex(), href) do
172172
[[_, custom_link]] ->
173173
custom_link
174174
|> url(:custom_link, config)
@@ -214,7 +214,7 @@ defmodule ExDoc.Autolink do
214214

215215
defp build_extra_link(link, config) do
216216
with %{scheme: nil, host: nil, path: path} = uri <- URI.parse(link),
217-
true <- is_binary(path) and path != "" and not (path =~ @ref_regex),
217+
true <- is_binary(path) and path != "" and not (path =~ ref_regex()),
218218
true <- Path.extname(path) in @builtin_ext do
219219
if file = config.extras[Path.basename(path)] do
220220
append_fragment(file <> config.ext, uri.fragment)

lib/ex_doc/formatter/html/templates.ex

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,8 @@ defmodule ExDoc.Formatter.HTML.Templates do
182182
end
183183

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

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

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

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

lib/ex_doc/utils.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ defmodule ExDoc.Utils do
8585
end)
8686
end
8787

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

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

9797
@doc """

0 commit comments

Comments
 (0)