|
17 | 17 | Rotate, |
18 | 18 | Uint1Array, |
19 | 19 | UUEncoderDecoder, |
20 | | - LZ77Compressor, |
21 | 20 | ) |
22 | 21 |
|
23 | 22 | yaml = lazy_import.lazy_module("yaml") |
@@ -176,7 +175,7 @@ def dict_get_items(self, *keys) -> DataFormatT: |
176 | 175 | return self |
177 | 176 |
|
178 | 177 | @ChepyDecorators.call_stack |
179 | | - def yaml_to_json(self) -> DataFormatT: |
| 178 | + def yaml_to_json(self) -> DataFormatT: # pragma: no cover |
180 | 179 | """Convert yaml to a json string |
181 | 180 |
|
182 | 181 | Returns: |
@@ -1034,19 +1033,36 @@ def from_octal(self, delimiter: str = None, join_by: str = "") -> DataFormatT: |
1034 | 1033 | return self |
1035 | 1034 |
|
1036 | 1035 | @ChepyDecorators.call_stack |
1037 | | - def to_html_entity(self) -> DataFormatT: |
| 1036 | + def to_html_entity(self, format="named", all_chars=False) -> DataFormatT: |
1038 | 1037 | """Encode html entities |
1039 | 1038 |
|
1040 | 1039 | Encode special html characters like & > < etc |
1041 | 1040 |
|
| 1041 | + Args: |
| 1042 | + format (str): Encoding format. Valid formats are named, numeric and hex. Defaults to named |
| 1043 | + all_chars (bool): If all chars should be encoded. By default a-ZA-Z0-9 are skipped. Defaults to False |
| 1044 | +
|
1042 | 1045 | Returns: |
1043 | 1046 | Chepy: The Chepy object. |
1044 | 1047 |
|
1045 | 1048 | Examples: |
1046 | 1049 | >>> Chepy('https://google.com&a="lol"').to_html_entity().o |
1047 | 1050 | "https://google.com&a="lol"" |
1048 | 1051 | """ |
1049 | | - self.state = html.escape(self._convert_to_str()) |
| 1052 | + data = self._convert_to_str() |
| 1053 | + chars = {k: 1 for k in string.ascii_letters + string.digits} |
| 1054 | + hold = "" |
| 1055 | + for d in data: |
| 1056 | + if not all_chars and chars.get(d) is not None: |
| 1057 | + hold += d |
| 1058 | + continue |
| 1059 | + if format == "named": |
| 1060 | + hold += Encoding.BYTE_TO_ENTITY.get(ord(d), f"&#{ord(d)};") |
| 1061 | + elif format == "hex": |
| 1062 | + hold += f"&#x{d.encode().hex()};" |
| 1063 | + elif format == "numeric": |
| 1064 | + hold += f"&#{ord(d)};" |
| 1065 | + self.state = hold |
1050 | 1066 | return self |
1051 | 1067 |
|
1052 | 1068 | @ChepyDecorators.call_stack |
|
0 commit comments