defmodule NLdoc.Validation.Content do @moduledoc """ This module provides helper functions for validating the content of NLdoc spec objects. """ @doc """ Returns true if the given text is visible, i.e. consists anything else than whitespace or invisible characters. If the given text is an `NLdoc.Spec` object, this function will recursively check whether any of its children contain visible text. """ @spec visible_text?(NLdoc.Spec.object() | String.t() | nil) :: boolean() def visible_text?(text) when is_binary(text), # \s matches standard whitespace characters. # \b matches a backspace character. # \x{0085} matches Unicode character U+0085 (Next Line). # See the tests for this module for a complete list of all these Unicode characters and what they are. # See also https://en.wikipedia.org/wiki/Whitespace_character#Unicode do: String.replace( text, ~r/[\s\b\x{0085}\x{00A0}\x{1680}\x{180E}\x{2000}\x{2001}\x{2002}\x{2003}\x{2004}\x{2005}\x{2006}\x{2007}\x{2008}\x{2009}\x{200A}\x{200B}\x{200C}\x{200D}\x{2028}\x{2029}\x{202F}\x{205F}\x{2060}\x{3000}\x{FEFF}]/u, "" ) != "" def visible_text?(%{text: text}), do: visible_text?(text) def visible_text?(%{children: children}), do: Enum.any?(children, &visible_text?/1) def visible_text?(_), do: false end