code: def urlize(
      text: str,
      trim_url_limit: t.Optional[int] = None,
      rel: t.Optional[str] = None,
      target: t.Optional[str] = None,
      extra_schemes: t.Optional[t.Iterable[str]] = None,
  ) -> str:
      """Convert URLs in text into clickable links.
  
      This may not recognize links in some situations. Usually, a more
      comprehensive formatter, such as a Markdown library, is a better
      choice.
  
      Works on ``http://``, ``https://``, ``www.``, ``mailto:``, and email
      addresses. Links with trailing punctuation (periods, commas, closing
      parentheses) and leading punctuation (opening parentheses) are
      recognized excluding the punctuation. Email addresses that include
      header fields are not recognized (for example,
      ``mailto:address@example.com?cc=copy@example.com``).
  
      :param text: Original text containing URLs to link.
      :param trim_url_limit: Shorten displayed URL values to this length.
      :param target: Add the ``target`` attribute to links.
      :param rel: Add the ``rel`` attribute to links.
      :param extra_schemes: Recognize URLs that start with these schemes
          in addition to the default behavior.
  
      .. versionchanged:: 3.0
          The ``extra_schemes`` parameter was added.
  
      .. versionchanged:: 3.0
          Generate ``https://`` links for URLs without a scheme.
  
      .. versionchanged:: 3.0
          The parsing rules were updated. Recognize email addresses with
          or without the ``mailto:`` scheme. Validate IP addresses. Ignore
          parentheses and brackets in more cases.
      """
      if trim_url_limit is not None:
  
          def trim_url(x: str) -> str:
              if len(x) > trim_url_limit:  # type: ignore
                  return f"{x[:trim_url_limit]}..."
  
              return x
  
      else:
  
          def trim_url(x: str) -> str:
              return x
  
      words = re.split(r"(\s+)", str(markupsafe.escape(text)))
      rel_attr = f' rel="{markupsafe.escape(rel)}"' if rel else ""
      target_attr = (
          f' target="{markupsafe.escape(target)}"' if target else ""
      )
  
      for i, word in enumerate(words):
          head, middle, tail = "", word, ""
          match = re.match(r"^([(<]|&lt;)+", middle)
  
          if match:
              head = match.group()
              middle = middle[match.end() :]
  
          # Unlike lead, which is anchored to the start of the string,
          # need to check that the string ends with any of the characters
          # before trying to match all of them, to avoid backtracking.
          if middle.endswith((")", ">", ".", ",", "\n", "&gt;")):
              match = re.search(r"([)>.,\n]|&gt;)+$", middle)
  
              if match:
                  tail = match.group()
                  middle = middle[: match.start()]
  
          # Prefer balancing parentheses in URLs instead of ignoring a
          # trailing character.
          for start_char, end_char in (
              ("(", ")"),
              ("<", ">"),
              ("&lt;", "&gt;"),
          ):
              start_count = middle.count(start_char)
  
              if start_count <= middle.count(end_char):
                  # Balanced, or lighter on the left
                  continue
  
              # Move as many as possible from the tail to balance
              for _ in range(min(start_count, tail.count(end_char))):
                  end_index = tail.index(end_char) + len(end_char)
                  # Move anything in the tail before the end char too
                  middle += tail[:end_index]
                  tail = tail[end_index:]
  
          if _http_re.match(middle):
              if middle.startswith("https://") or middle.startswith(
                  "http://"
              ):
                  middle = f'<a href="{middle}"{rel_attr}{target_attr}>{trim_url(middle)}</a>'
              else:
                  middle = (
                      f'<a href="https://{middle}"{rel_attr}{target_attr}>'
                      f"{trim_url(middle)}</a>"
                  )
  
          elif middle.startswith("mailto:") and _email_re.match(
              middle[7:]
          ):
              middle = f'<a href="{middle}">{middle[7:]}</a>'
  
          elif (
              "@" in middle
              and not middle.startswith("www.")
              and ":" not in middle
              and _email_re.match(middle)
          ):
              middle = f'<a href="mailto:{middle}">{middle}</a>'
  
          elif extra_schemes is not None:
              for scheme in extra_schemes:
                  if middle != scheme and middle.startswith(scheme):
                      middle = f'<a href="{middle}"{rel_attr}{target_attr}>{middle}</a>'
  
          words[i] = f"{head}{middle}{tail}"
  
      return "".join(words)
  
imports: import enum
  import json
  import os
  import re
  import typing
  from collections import abc
  from collections import deque
  from random import choice
  from random import randrange
  from threading import Lock
  from types import CodeType
  from urllib.parse import quote_from_bytes
  import markupsafe
label_desc: Use image node_id 12 for calling a global function with example usage: urlize(text, trim_url_limit, rel, target, extra_schemes) and returns: str, x, x, x, str