0
# Text Formatting
1
2
Comprehensive text styling capabilities including bold, italics, colors, alignment, inline code, and advanced formatting combinations. The TextUtils class provides static methods for applying various text formatting options that can be used independently or combined for complex styling needs.
3
4
## Capabilities
5
6
### Basic Text Styling
7
8
Apply fundamental text formatting including bold, italics, and inline code styling.
9
10
```python { .api }
11
class TextUtils:
12
@staticmethod
13
def bold(text: str) -> str:
14
"""
15
Apply bold formatting to text.
16
17
Parameters:
18
- text (str): Text to make bold
19
20
Returns:
21
str: Text wrapped in markdown bold syntax (**text**)
22
"""
23
24
@staticmethod
25
def italics(text: str) -> str:
26
"""
27
Apply italic formatting to text.
28
29
Parameters:
30
- text (str): Text to make italic
31
32
Returns:
33
str: Text wrapped in markdown italic syntax (*text*)
34
"""
35
36
@staticmethod
37
def inline_code(text: str) -> str:
38
"""
39
Apply inline code formatting to text.
40
41
Parameters:
42
- text (str): Text to format as inline code
43
44
Returns:
45
str: Text wrapped in markdown code syntax (`text`)
46
"""
47
```
48
49
**Usage Example:**
50
51
```python
52
from mdutils import MdUtils
53
from mdutils.tools import TextUtils
54
55
md = MdUtils(file_name='formatting_example')
56
57
# Basic styling
58
bold_text = TextUtils.bold('This is bold text')
59
italic_text = TextUtils.italics('This is italic text')
60
code_text = TextUtils.inline_code('print("Hello")')
61
62
md.write(f"{bold_text}, {italic_text}, and {code_text}")
63
64
# Or use directly in document methods
65
md.new_paragraph('Important note', bold_italics_code='b') # 'b' for bold
66
md.new_paragraph('Emphasized text', bold_italics_code='i') # 'i' for italics
67
md.new_paragraph('Code example', bold_italics_code='c') # 'c' for code
68
```
69
70
### Text Color and Alignment
71
72
Apply color styling and text alignment for enhanced visual presentation.
73
74
```python { .api }
75
class TextUtils:
76
@staticmethod
77
def text_color(text: str, color: str = "black") -> str:
78
"""
79
Apply color formatting to text using HTML/CSS color names.
80
81
Parameters:
82
- text (str): Text to colorize
83
- color (str): Color name or specification (e.g., 'red', 'blue', '#FF0000')
84
85
Returns:
86
str: Text wrapped in HTML color tags
87
"""
88
89
@staticmethod
90
def center_text(text: str) -> str:
91
"""
92
Center-align text using HTML alignment.
93
94
Parameters:
95
- text (str): Text to center
96
97
Returns:
98
str: Text wrapped in HTML center alignment tags
99
"""
100
```
101
102
**Usage Example:**
103
104
```python
105
from mdutils import MdUtils
106
from mdutils.tools import TextUtils
107
108
md = MdUtils(file_name='color_alignment_example')
109
110
# Colored text
111
red_text = TextUtils.text_color('Error message', 'red')
112
blue_text = TextUtils.text_color('Information', 'blue')
113
green_text = TextUtils.text_color('Success', 'green')
114
115
md.write(f"{red_text} | {blue_text} | {green_text}")
116
117
# Centered text
118
centered = TextUtils.center_text('This text is centered')
119
md.new_paragraph(centered)
120
121
# Using color and alignment in document methods
122
md.new_paragraph('Red centered text', color='red', align='center')
123
md.new_line('Blue right-aligned text', color='blue', align='right')
124
```
125
126
### Advanced Text Formatting
127
128
Comprehensive text formatting with multiple style combinations and external link integration.
129
130
```python { .api }
131
class TextUtils:
132
@staticmethod
133
def text_format(text: str, bold_italics_code: str = "", color: str = "black", align: str = "") -> str:
134
"""
135
Apply comprehensive text formatting with multiple style options.
136
137
Parameters:
138
- text (str): Text to format
139
- bold_italics_code (str): Combination of format codes:
140
- 'b' for bold
141
- 'i' for italics
142
- 'c' for inline code
143
- Can combine: 'bi' for bold italic, 'bc' for bold code, etc.
144
- color (str): Text color specification
145
- align (str): Text alignment ('left', 'center', 'right')
146
147
Returns:
148
str: Text with all specified formatting applied
149
"""
150
151
@staticmethod
152
def text_external_link(text: str, link: str = "") -> str:
153
"""
154
Create an external link with specified text.
155
156
Parameters:
157
- text (str): Display text for the link
158
- link (str): URL for the external link
159
160
Returns:
161
str: Markdown-formatted external link
162
"""
163
164
@staticmethod
165
def add_tooltip(link: str, tip: str) -> str:
166
"""
167
Add tooltip to a link.
168
169
Parameters:
170
- link (str): The link URL
171
- tip (str): Tooltip text to display
172
173
Returns:
174
str: Link with tooltip attribute
175
"""
176
```
177
178
**Usage Example:**
179
180
```python
181
from mdutils import MdUtils
182
from mdutils.tools import TextUtils
183
184
md = MdUtils(file_name='advanced_formatting')
185
186
# Complex formatting combinations
187
bold_italic = TextUtils.text_format('Bold and Italic', bold_italics_code='bi')
188
bold_code = TextUtils.text_format('Bold Code', bold_italics_code='bc', color='blue')
189
centered_red_bold = TextUtils.text_format('Centered Red Bold',
190
bold_italics_code='b',
191
color='red',
192
align='center')
193
194
md.write(bold_italic)
195
md.new_line(bold_code)
196
md.new_paragraph(centered_red_bold)
197
198
# External links
199
github_link = TextUtils.text_external_link('Visit GitHub', 'https://github.com')
200
md.new_paragraph(f'Check out the project: {github_link}')
201
202
# Links with tooltips
203
tooltip_link = TextUtils.add_tooltip('https://example.com', 'Click to visit example site')
204
md.write(f'Link with tooltip: {tooltip_link}')
205
```
206
207
### Code Block Formatting
208
209
Format code blocks with syntax highlighting and language specification.
210
211
```python { .api }
212
class TextUtils:
213
@staticmethod
214
def insert_code(code: str, language: str = "") -> str:
215
"""
216
Format code as a markdown code block with optional syntax highlighting.
217
218
Parameters:
219
- code (str): Code content to format
220
- language (str, optional): Programming language for syntax highlighting
221
222
Returns:
223
str: Formatted code block with markdown syntax
224
"""
225
```
226
227
**Usage Example:**
228
229
```python
230
from mdutils import MdUtils
231
from mdutils.tools import TextUtils
232
233
md = MdUtils(file_name='code_formatting')
234
235
# Python code block
236
python_code = '''
237
def calculate_fibonacci(n):
238
if n <= 1:
239
return n
240
return calculate_fibonacci(n-1) + calculate_fibonacci(n-2)
241
'''
242
formatted_python = TextUtils.insert_code(python_code, 'python')
243
md.write(formatted_python)
244
245
# JSON code block
246
json_code = '''
247
{
248
"name": "example",
249
"version": "1.0.0",
250
"dependencies": {
251
"mdutils": "^1.8.0"
252
}
253
}
254
'''
255
formatted_json = TextUtils.insert_code(json_code, 'json')
256
md.write(formatted_json)
257
258
# Generic code block (no language)
259
bash_code = 'pip install mdutils'
260
formatted_bash = TextUtils.insert_code(bash_code)
261
md.write(formatted_bash)
262
```
263
264
### Format Code Combinations
265
266
Understanding how to combine different formatting codes for complex text styling.
267
268
**Format Code Reference:**
269
270
- `'b'` - Bold text (**text**)
271
- `'i'` - Italic text (*text*)
272
- `'c'` - Inline code (`text`)
273
- `'bi'` or `'ib'` - Bold italic (***text***)
274
- `'bc'` or `'cb'` - Bold code (**`text`**)
275
- `'ic'` or `'ci'` - Italic code (*`text`*)
276
- `'bic'` or combinations - Bold italic code (***`text`***)
277
278
**Usage Example:**
279
280
```python
281
from mdutils import MdUtils
282
283
md = MdUtils(file_name='format_combinations')
284
285
# Using format codes in document methods
286
md.new_paragraph('This is bold text', bold_italics_code='b')
287
md.new_paragraph('This is italic text', bold_italics_code='i')
288
md.new_paragraph('This is inline code', bold_italics_code='c')
289
md.new_paragraph('This is bold italic text', bold_italics_code='bi')
290
md.new_paragraph('This is bold code text', bold_italics_code='bc')
291
292
# Combining with colors and alignment
293
md.new_paragraph('Bold red centered text',
294
bold_italics_code='b',
295
color='red',
296
align='center')
297
298
md.new_paragraph('Italic blue right-aligned code',
299
bold_italics_code='ic',
300
color='blue',
301
align='right')
302
```