group_matcher.ex (1774B)
1 defmodule ExDoc.GroupMatcher do 2 @moduledoc false 3 4 @type pattern :: Regex.t() | module() | String.t() 5 @type patterns :: pattern | [pattern] 6 @type group_patterns :: keyword(patterns) 7 8 @doc """ 9 Finds the index of a given group. 10 """ 11 def group_index(groups, group) do 12 Enum.find_index(groups, fn {k, _v} -> k == group end) || -1 13 end 14 15 @doc """ 16 Finds a matching group for the given function. 17 """ 18 @spec match_function(group_patterns, map) :: atom() | nil 19 def match_function(group_patterns, metadata) do 20 match_group_patterns(group_patterns, fn pattern -> pattern.(metadata) end) 21 end 22 23 @doc """ 24 Finds a matching group for the given module name, id, and metadata. 25 """ 26 @spec match_module(group_patterns, module, binary, map) :: atom() | nil 27 def match_module(group_patterns, module, id, metadata) do 28 match_group_patterns(group_patterns, fn pattern -> 29 case pattern do 30 %Regex{} = regex -> Regex.match?(regex, id) 31 string when is_binary(string) -> id == string 32 atom when is_atom(atom) -> atom == module 33 function when is_function(function) -> function.(metadata) 34 end 35 end) 36 end 37 38 @doc """ 39 Finds a matching group for the given extra filename 40 """ 41 @spec match_extra(group_patterns, binary) :: atom() | nil 42 def match_extra(group_patterns, filename) do 43 match_group_patterns(group_patterns, fn pattern -> 44 case pattern do 45 %Regex{} = regex -> Regex.match?(regex, filename) 46 string when is_binary(string) -> filename == string 47 end 48 end) 49 end 50 51 defp match_group_patterns(group_patterns, matcher) do 52 Enum.find_value(group_patterns, fn {group, patterns} -> 53 patterns = List.wrap(patterns) 54 Enum.any?(patterns, matcher) && group 55 end) 56 end 57 end