Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/outlines/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def __getattr__(self, name):
"ipv6",
"semver",
"mac_address",
"hex_color",
"slug",
# Document-specific types
"sentence",
"paragraph",
Expand Down Expand Up @@ -160,6 +162,18 @@ def __getattr__(self, name):
r"(?:\.(?:25[0-5]|2[0-4][0-9]|1?[0-9]{1,2})){3}"
)

# CSS hexadecimal color notation: https://www.w3.org/TR/css-color-4/#hex-notation
# This matches only the ubiquitous three-digit (#rgb) and six-digit (#rrggbb)
# forms. The four- and eight-digit forms that encode an alpha channel are
# intentionally excluded to keep the type to the canonical opaque-color syntax.
hex_color = Regex(r"#(?:[0-9A-Fa-f]{3}|[0-9A-Fa-f]{6})")

# URL slugs in the lowercase-hyphenated form produced by common web framework
# slugify helpers: ASCII lowercase alphanumeric groups separated by single
# hyphens. Uppercase letters, underscores, and leading, trailing, or
# consecutive hyphens are excluded, as is the empty string.
slug = Regex(r"[a-z0-9]+(?:-[a-z0-9]+)*")

# Document-specific types
sentence = Regex(r"[A-Z].*\s*[.!?]")
paragraph = Regex(rf"{sentence.pattern}(?:\s+{sentence.pattern})*\n+")
Expand Down
18 changes: 18 additions & 0 deletions tests/types/test_custom_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@
(types.mac_address, "00:1A:2B:3C:4D:5E", True),
(types.mac_address, "00-1A-2B-3C-4D-5E", False),
(types.mac_address, "00:1A:2B:3C:4D", False),
(types.hex_color, "#1a2b3c", True),
(types.hex_color, "#abc", True),
(types.hex_color, "#ABC123", True),
(types.hex_color, "1a2b3c", False),
(types.hex_color, "#12345", False),
(types.hex_color, "#gggggg", False),
(types.hex_color, "#abcd", False),
(types.hex_color, "#aabbccdd", False),
(types.hex_color, " #abc", False),
(types.slug, "my-post-title", True),
(types.slug, "post", True),
(types.slug, "a1-b2", True),
(types.slug, "My-Post", False),
(types.slug, "-leading-hyphen", False),
(types.slug, "trailing-hyphen-", False),
(types.slug, "double--hyphen", False),
(types.slug, "under_score", False),
(types.slug, "", False),
],
)
def test_type_regex(custom_type, test_string, should_match):
Expand Down
Loading