nya-codeblock 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ from .codeblock import codeblock, codeblocks, codeblocks_from_exception
2
+ from .uncodeblock import uncodeblock
3
+
4
+ __all__ = [
5
+ "codeblock",
6
+ "codeblocks",
7
+ "codeblocks_from_exception",
8
+ "uncodeblock",
9
+ ]
File without changes
@@ -0,0 +1 @@
1
+ __version__: str = "0.1.0"
@@ -0,0 +1,120 @@
1
+ from collections.abc import Callable
2
+
3
+ BACKTICK = "`"
4
+ BACKTICKS = 3 * BACKTICK
5
+ NEWLINE = "\n"
6
+
7
+
8
+ def _default_cut_at(text: str, max_length: int) -> str:
9
+ """Cut text at max_length, if text is longer than max_length.
10
+
11
+ Args:
12
+ text: The text to cut.
13
+ max_length: The maximum length of the output str.
14
+ Returns:
15
+ str: The text cut at max_length.
16
+ """
17
+ return text[:max_length]
18
+
19
+
20
+ def codeblock(
21
+ text: str,
22
+ *,
23
+ langcode: str = "",
24
+ max_length: int = 1984,
25
+ fn_cut_at: Callable[[str, int], str] = _default_cut_at,
26
+ smart_empty: bool = True,
27
+ convert_three_backticks_to_apostrophes: bool = False,
28
+ ) -> str:
29
+ r"""Pack text into a Discord codeblock. This will cut it to a max_length with a default for Discord, if you dont want this, set max_length to your desired length or -1 means text's length.
30
+
31
+ Note: May return str with len > max_length if max_length is ridicously low such that not even `'''\nlangcode'''` fits
32
+
33
+ Args:
34
+ text: The text to put in the codeblock body.
35
+ langcode: The language code to use in discord codeblock, for example "py".
36
+ max_length: The maximum length of the output str.
37
+ fn_cut_at: A Callable[[str, int], str] which's returned string is of or less than the passed in int in length.
38
+ smart_empty: If true, if `text` is '' (an empty string) it will be changed to ' ' (a space). This prevents the codeblock to think langcode is the body of the codeblock if no text is supplied.
39
+ convert_three_backticks_to_apostrophes: If true, any backticks in the text will be converted to apostrophes. This prevents the codeblock from being broken by backticks in the text.
40
+
41
+ Returns:
42
+ str: A str of the form ```langcode\ntext``` with a length of or less than max_length.
43
+ """
44
+ if smart_empty and not text:
45
+ text = " "
46
+
47
+ if convert_three_backticks_to_apostrophes:
48
+ text = text.replace(BACKTICKS, "'''")
49
+
50
+ length_of_parts = len(BACKTICKS) + len(langcode) + len(NEWLINE) + len(BACKTICKS)
51
+
52
+ return "".join((
53
+ BACKTICKS,
54
+ langcode,
55
+ NEWLINE,
56
+ fn_cut_at(
57
+ text,
58
+ (max_length - length_of_parts if max_length != -1 else len(text)),
59
+ ),
60
+ BACKTICKS,
61
+ ))
62
+
63
+
64
+ def codeblocks(
65
+ text0: str,
66
+ *texts: str,
67
+ langcodes: tuple[str, ...] | None = None,
68
+ max_length: int = 1984,
69
+ fn_cut_at: Callable[[str, int], str] = _default_cut_at,
70
+ smart_empty: bool = True,
71
+ convert_three_backticks_to_apostrophes: bool = False,
72
+ ) -> str:
73
+ r"""Mutliple-`codeblock()`. For more info read codeblock() docstring.
74
+
75
+ Args:
76
+ text0: The first text to put in the codeblock body.
77
+ *texts: The rest of the texts to put in the codeblock body.
78
+ langcodes: The language codes to use in discord codeblock, for example "py". If None, all langcodes will be empty strings. If provided, must be of same length as the total amount of texts passed in including text0.
79
+ max_length: The maximum length of the output str.
80
+ fn_cut_at: A Callable[[str, int], str] which's returned string is of or less than the passed in int in length.
81
+ smart_empty: If true, if `text` is '' (an empty string) it will be changed to ' ' (a space). This prevents the codeblock to think langcode is the body of the codeblock if no text is supplied.
82
+
83
+ Returns:
84
+ str: A str of the form ```langcode\ntext``` with a length of or less than max_length.
85
+ """
86
+ texts = text0, *texts
87
+
88
+ if langcodes is None:
89
+ langcodes: tuple[str, ...] = ("",) * len(texts)
90
+
91
+ out = ""
92
+
93
+ for text, langcode in zip(texts, langcodes, strict=True):
94
+ out += codeblock(
95
+ text,
96
+ langcode=langcode,
97
+ max_length=max_length - len(out),
98
+ fn_cut_at=fn_cut_at,
99
+ smart_empty=smart_empty,
100
+ convert_three_backticks_to_apostrophes=convert_three_backticks_to_apostrophes,
101
+ )
102
+
103
+ return out
104
+
105
+
106
+ def codeblocks_from_exception(
107
+ e: BaseException,
108
+ *,
109
+ max_length: int = 1800,
110
+ ) -> str:
111
+ return codeblocks(
112
+ extract_error(e),
113
+ extract_traceback(e),
114
+ langcodes=(
115
+ "txt",
116
+ "py",
117
+ ),
118
+ max_length=max_length,
119
+ smart_empty=True,
120
+ )
@@ -0,0 +1,19 @@
1
+ from .codeblock import BACKTICKS, NEWLINE
2
+
3
+
4
+ def uncodeblock(text: str) -> str:
5
+ """*Try to* remove any markdown/discord codeblocks along with their langcodes if any are found.
6
+
7
+ Args:
8
+ text: The text to remove markdown/discord codeblocks from.
9
+
10
+ Returns:
11
+ str: The text with any markdown/discord codeblocks removed.
12
+ """
13
+ if text[-3:] == BACKTICKS and text[:3] == BACKTICKS:
14
+ code_start = 3
15
+ code_end = -3
16
+ if NEWLINE in text[3:]: # Check if there is a language code specified
17
+ code_start = text.index(NEWLINE) + 1
18
+ return text[code_start:code_end].strip()
19
+ return text
@@ -0,0 +1,10 @@
1
+ Metadata-Version: 2.4
2
+ Name: nya-codeblock
3
+ Version: 0.1.0
4
+ Summary: Provide helpful functions to convert str into a markdown/discord codeblock. Ported from my own package, tcrutils.
5
+ Requires-Python: >=3.11
6
+ Description-Content-Type: text/markdown
7
+
8
+ # nya-codeblock
9
+
10
+ Provide helpful functions to convert str into a markdown/discord codeblock. Ported from my own package, tcrutils.
@@ -0,0 +1,8 @@
1
+ nya_codeblock/__init__.py,sha256=6izyXLG495houwqs_p-FvYOW6fj5LWYkUPHQul0nPho,199
2
+ nya_codeblock/__main__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ nya_codeblock/_version.py,sha256=5HYXODBNk5J9vVbUrfu716lCND-BNE2LCfb2Jp8jsXs,26
4
+ nya_codeblock/codeblock.py,sha256=FXoD_aZyS5RolAlM3TNX7CxkzFLVWnHc_Ueu_IWxIGA,3856
5
+ nya_codeblock/uncodeblock.py,sha256=V-Xt8yTFABOLHKeBomw2pa6xfXWFttTmkrL4peMqpMo,579
6
+ nya_codeblock-0.1.0.dist-info/METADATA,sha256=Ktexi5zv6E_doTSALNfwZBKJz91_GtHDWnU8MDwj7hI,375
7
+ nya_codeblock-0.1.0.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
8
+ nya_codeblock-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.31.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any