Lexer for Multipurpose Internet Mail Extensions (MIME) data.
:copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
:license: BSD, see LICENSE for details. """
import re
from pygments.lexer import RegexLexer, include from pygments.lexers import get_lexer_for_mimetype from pygments.token import Text, Name, String, Operator, Comment, Other from pygments.util import get_int_opt, ClassNotFound
__all__ = ["MIMELexer"]
class MIMELexer(RegexLexer): """
Lexer for Multipurpose Internet Mail Extensions (MIME) data. This lexer is
designed to process nested multipart data.
It assumes that the given data contains both header and body (andis
split at an empty line). If no valid header is found, then the entire data
will be treated as body.
Additional options accepted:
`MIME-max-level`
Max recursion level for nested MIME structure. Any negative number
would treated as unlimited. (default: -1)
`Content-Type`
Treat the data as a specific content type. Useful when header is
missing, or this lexer would try to parse from header. (default:
`text/plain`)
`Multipart-Boundary`
Set the default multipart boundary delimiter. This option is only used
when `Content-Type` is `multipart` and header is missing. This lexer
would try to parse from header by default. (default: None)
`Content-Transfer-Encoding`
Treat the data as a specific encoding. Or this lexer would try to parse from header by default. (default: None) """
# skip first newline if entire_body[0] == '\n': yield pos_body_start, Text.Whitespace, '\n'
pos_body_start = pos_body_start + 1
entire_body = entire_body[1:]
# if it is not a multipart ifnot self.content_type.startswith("multipart") ornot self.boundary: for i, t, v in self.get_bodypart_tokens(entire_body): yield pos_body_start + i, t, v return
# some data has prefix text before first boundary
m = bdry_matcher.search(entire_body) if m:
pos_part_start = pos_body_start + m.end()
pos_iter_start = lpos_end = m.end() yield pos_body_start, Text, entire_body[:m.start()] yield pos_body_start + lpos_end, String.Delimiter, m.group() else:
pos_part_start = pos_body_start
pos_iter_start = 0
# process tokens of each body part for m in bdry_matcher.finditer(entire_body, pos_iter_start): # bodypart
lpos_start = pos_part_start - pos_body_start
lpos_end = m.start()
part = entire_body[lpos_start:lpos_end] for i, t, v in self.get_bodypart_tokens(part): yield pos_part_start + i, t, v
# some data has suffix text after last boundary
lpos_start = pos_part_start - pos_body_start if lpos_start != len(entire_body): yield pos_part_start, Text, entire_body[lpos_start:]
def get_bodypart_tokens(self, text): # return if: # * no content # * no content type specific # * content encoding is not readable # * max recurrsion exceed ifnot text.strip() ornot self.content_type: return [(0, Other, text)]
cte = self.content_transfer_encoding if cte and cte notin {"8bit", "7bit", "quoted-printable"}: return [(0, Other, text)]
if self.max_nested_level == 0: return [(0, Other, text)]
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.