code: def ansi2html(text, palette="solarized"):
      # pylint: disable=too-many-branches, too-many-statements
      def _ansi2html(m):
          if m.group(2) != "m":
              return ""
          state = None
          sub = ""
          cs = m.group(1)
          cs = cs.strip() if cs else ""
          for c in cs.split(";"):
              c = c.strip().lstrip("0") or "0"
              if c == "0":
                  while stack:
                      sub += "</span>"
                      stack.pop()
              elif c in ("38", "48"):
                  extra = [c]
                  state = "extra"
              elif state == "extra":
                  if c == "5":
                      state = "idx"
                  elif c == "2":
                      state = "r"
              elif state:
                  if state == "idx":
                      extra.append(c)
                      state = None
                      color = indexed_style.get(c)  # 256 colors
                      if color is not None:
                          sub += '<span style="%s:%s">' % (
                              "color"
                              if extra[0] == "38"
                              else "background-color",
                              color,
                          )
                          stack.append(extra)
                  elif state in ("r", "g", "b"):
                      extra.append(c)
                      if state == "r":
                          state = "g"
                      elif state == "g":
                          state = "b"
                      else:
                          state = None
                          try:
                              color = "#" + "".join(
                                  "%02X" % c if 0 <= c <= 255 else None
                                  for x in extra
                                  for c in [int(x)]
                              )
                          except (ValueError, TypeError):
                              pass
                          else:
                              sub += '<span style="%s:%s">' % (
                                  "color"
                                  if extra[0] == "38"
                                  else "background-color",
                                  color,
                              )
                              stack.append(extra)
              else:
                  if "1" in stack:
                      style = bold_style.get(c)
                  else:
                      style = regular_style.get(c)
                  if style is not None:
                      sub += '<span style="%s">' % style
                      stack.append(
                          c
                      )  # Still needs to be added to the stack even if style is empty
                      #  (so it can check '1' in stack above, for example)
          return sub
  
      stack = []
      regular_style, bold_style, indexed_style = _ansi2html_get_styles(
          palette
      )
      sub = ANSI2HTML_CODES_RE.sub(_ansi2html, text)
      while stack:
          sub += "</span>"
          stack.pop()
      return sub
  
imports: from __future__ import print_function
  import re
  import sys
label_desc: Use image node_id 2 for calling a global function with example usage: ansi2html(text, palette) and returns: sub, sub, str