| 918 | |
| 919 | |
| 920 | class Template(BaseTemplate): |
| 921 | CONTENT_TYPES = { |
| 922 | ".html": "text/html; charset=utf-8", |
| 923 | ".xhtml": "application/xhtml+xml; charset=utf-8", |
| 924 | ".txt": "text/plain", |
| 925 | } |
| 926 | FILTERS = {".html": websafe, ".xhtml": websafe, ".xml": websafe} |
| 927 | globals = {} |
| 928 | |
| 929 | def __init__( |
| 930 | self, |
| 931 | text, |
| 932 | filename="<template>", |
| 933 | filter=None, |
| 934 | globals=None, |
| 935 | builtins=None, |
| 936 | extensions=None, |
| 937 | ): |
| 938 | self.extensions = extensions or [] |
| 939 | text = Template.normalize_text(text) |
| 940 | code = self.compile_template(text, filename) |
| 941 | |
| 942 | _, ext = os.path.splitext(filename) |
| 943 | filter = filter or self.FILTERS.get(ext, None) |
| 944 | self.content_type = self.CONTENT_TYPES.get(ext, None) |
| 945 | |
| 946 | if globals is None: |
| 947 | globals = self.globals |
| 948 | if builtins is None: |
| 949 | builtins = TEMPLATE_BUILTINS |
| 950 | |
| 951 | BaseTemplate.__init__( |
| 952 | self, |
| 953 | code=code, |
| 954 | filename=filename, |
| 955 | filter=filter, |
| 956 | globals=globals, |
| 957 | builtins=builtins, |
| 958 | ) |
| 959 | |
| 960 | def __repr__(self): |
| 961 | """ |
| 962 | >>> Template(text='Template text', filename='burndown_chart.html') |
| 963 | <Template burndown_chart.html> |
| 964 | """ |
| 965 | return f"<{self.__class__.__name__} {self.filename}>" |
| 966 | |
| 967 | def normalize_text(text): |
| 968 | """Normalizes template text by correcting \r\n, tabs and BOM chars.""" |
| 969 | text = text.replace("\r\n", "\n").replace("\r", "\n").expandtabs() |
| 970 | if not text.endswith("\n"): |
| 971 | text += "\n" |
| 972 | |
| 973 | # ignore BOM chars at the beginning of template |
| 974 | BOM = "\xef\xbb\xbf" |
| 975 | if isinstance(text, str) and text.startswith(BOM): |
| 976 | text = text[len(BOM) :] |
| 977 |
no outgoing calls