code: def parse_chart(chart, convert):
      
      out = []
      for match in re.finditer(ATTR_RE, chart):
          if match.group('whitespace'):
              out.append(match.group('whitespace'))
          entry = match.group('entry')
          entry = entry.replace("_", " ")
          while entry:
              # try the first four characters
              attrtext = convert(entry[:SHORT_ATTR])
              if attrtext:
                  elen = SHORT_ATTR
                  entry = entry[SHORT_ATTR:].strip()
              else: # try the whole thing
                  attrtext = convert(entry.strip())
                  assert attrtext, "Invalid palette entry: %r" % entry
                  elen = len(entry)
                  entry = ""
              attr, text = attrtext
              out.append((attr, text.ljust(elen)))
      return out
docs: Convert string chart into text markup with the correct attributes.
  
      chart -- palette chart as a string
      convert -- function that converts a single palette entry to an
          (attr, text) tuple, or None if no match is found