partial_tokens
Implementation of partial tokens.
A partial token is a callable that wraps text with a given string. It can also be used with pipe operators to allow composition with other tokens.
To create your own partial tokens, use the create_partial_token
function.
InvalidPartialTokenCompositionError
Bases: Exception
Raised when a partial token is constructed in an invalid way.
Source code in src/pymarkdown_builder/partial_tokens.py
13 14 |
|
PartialToken
Partial token representation.
A partial token is a callable that wraps text with a given string. It can also be used with pipe operators to allow composition with other tokens.
Examples:
>>> bold = PartialToken("**")
>>> italic = PartialToken("_")
>>> bold | "hello" | italic | "world" | italic | bold
'**hello_world_**'
Source code in src/pymarkdown_builder/partial_tokens.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
close_tag: str = close_tag
instance-attribute
String that will be appended to the text.
open_tag: str = open_tag
instance-attribute
String that will be prepended to the text.
__call__(text)
Wraps the text with the tags.
Source code in src/pymarkdown_builder/partial_tokens.py
85 86 87 |
|
__init__(open_tag, close_tag=None)
Initializes the partial token.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_tag |
str
|
The string that will be prepended to the text. |
required |
close_tag |
Optional[str]
|
The string that will be appended to the text. If not provided, will use the |
None
|
Source code in src/pymarkdown_builder/partial_tokens.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
__or__(value)
Executed when on the left side of the pipe operator, opening the tag.
The right side of the pipe operator can be either a str
or a
PartialToken
.
Always returns a PartialTokenContent
.
Source code in src/pymarkdown_builder/partial_tokens.py
89 90 91 92 93 94 95 96 97 98 99 100 |
|
__ror__(value)
Executed when on the right side of the pipe operator, closing the tag.
The left side of the pipe operator must be a
PartialTokenContent
.
Always returns a PartialTokenContent
.
Source code in src/pymarkdown_builder/partial_tokens.py
102 103 104 105 106 107 108 109 110 111 112 |
|
PartialTokenContent
Bases: str
An "overloaded" string to support pipe operations with PartialToken
.
Source code in src/pymarkdown_builder/partial_tokens.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
__new__(string)
Creates a new partial token content.
Source code in src/pymarkdown_builder/partial_tokens.py
20 21 22 |
|
__or__(value)
Executed when on the left side of the pipe operator.
The right side of the pipe operator can be either a str
or a
PartialToken
.
Always returns a PartialToken
.
Source code in src/pymarkdown_builder/partial_tokens.py
24 25 26 27 28 29 30 31 32 33 34 35 |
|
__ror__(value)
Executed when on the right side of the pipe operator.
The left side of the pipe operator can be either a str
or a
PartialToken
.
Always returns a PartialToken
.
Source code in src/pymarkdown_builder/partial_tokens.py
37 38 39 40 41 42 43 44 45 46 47 48 |
|
create_partial_token(open_tag, close_tag=None)
Creates a partial token.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
open_tag |
str
|
The string that will be prepended to the text. |
required |
close_tag |
Optional[str]
|
The string that will be appended to the text. If not provided, will use the |
None
|
Source code in src/pymarkdown_builder/partial_tokens.py
115 116 117 118 119 120 121 122 123 124 125 |
|