Document Rendering Module¶
Markdown-based document rendering system using QTextBrowser with automatic theme support.
Quick Start¶
from src.shared_services.rendering.documents.api import MarkdownView
view = MarkdownView(parent=self, object_name="NewsView")
view.set_markdown("# Titel\n\nAbsatz mit **fettem** Text.")
MarkdownView Widget¶
Theme-aware QTextBrowser wrapper that renders Markdown with GitHub-style alerts.
from src.shared_services.rendering.documents.api import MarkdownView
# Render Markdown content
view = MarkdownView(parent=self)
view.set_markdown("# Hello\n\n> [!NOTE]\n> This is a note.")
# Set pre-built HTML (still gets theme stylesheet)
view.set_html_content("<h1>Titel</h1><p>Some HTML content.</p>")
Supported Markdown Features¶
- CommonMark (headings, bold, italic, links, code blocks, etc.)
- GFM tables
- Task lists
- Strikethrough
- GitHub-style alert blocks:
[!NOTE],[!TIP],[!IMPORTANT],[!WARNING],[!CAUTION]
Theme Integration¶
MarkdownView automatically re-renders when the application theme changes. No manual theme callback registration is needed.
Fun Facts¶
Daily rotating facts injected at placeholder locations.
from src.shared_services.rendering.documents.fun_facts import inject_fun_fact
md = "# Welcome\n\n{{FUN_FACT}}"
md = inject_fun_fact(md)
Files¶
| File | Purpose |
|---|---|
api.py |
Public API exports |
markdown_view.py |
MarkdownView widget (QTextBrowser + markdown-it-py) |
fun_facts.py |
Daily rotating facts |
API Reference¶
src.shared_services.rendering.documents.api
¶
Document rendering public API.
Provides the MarkdownView widget for displaying Markdown content with automatic theme support and GitHub-style alert blocks.
USAGE::
from src.shared_services.rendering.documents.api import MarkdownView
view = MarkdownView(parent=self)
view.set_markdown("# Willkommen\n\nEin Absatz mit **fettem** Text.")
FunFactsManager
¶
Manager for daily rotating fun facts.
Uses date-based selection to ensure the same fact is shown throughout a single day, with caching to persist across restarts.
Source code in src\shared_services\rendering\documents\fun_facts.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
__init__(cache_dir=None)
¶
Initialize the fun facts manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
Optional[str]
|
Directory to store the cache file. If None, uses the application data directory. |
None
|
Source code in src\shared_services\rendering\documents\fun_facts.py
get_daily_fact()
¶
Get the fact for today.
Returns:
| Type | Description |
|---|---|
str
|
A fun fact string, prefixed with a lightbulb indicator. |
Example::
manager = FunFactsManager()
fact = manager.get_daily_fact()
# Returns something like "[*] Honey never spoils..."
Source code in src\shared_services\rendering\documents\fun_facts.py
MarkdownView
¶
Bases: QTextBrowser
Theme-aware Markdown rendering widget.
Renders Markdown content as GitHub-styled HTML inside a QTextBrowser. Supports GFM alert blocks, tables, task lists, and automatic re-rendering on theme changes.
Usage::
view = MarkdownView(parent=self)
view.set_markdown("# Hello\n\nSome **bold** text.")
Source code in src\shared_services\rendering\documents\markdown_view.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 | |
set_html_content(html)
¶
Set pre-built HTML content directly.
Use this for programmatically generated HTML (complete documents or body fragments). Complete documents (containing tag) are passed through as-is; fragments are wrapped with the MarkdownView theme stylesheet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
html
|
str
|
HTML string (complete document or body fragment). |
required |
Source code in src\shared_services\rendering\documents\markdown_view.py
set_markdown(text)
¶
Render Markdown text into the view.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Raw Markdown string. |
required |
set_theme(theme)
¶
Force a specific theme and re-render.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theme
|
str
|
"light" or "dark". |
required |
Source code in src\shared_services\rendering\documents\markdown_view.py
get_daily_fact()
¶
Get today's fun fact.
Convenience function that uses the singleton manager.
Returns:
| Type | Description |
|---|---|
str
|
A fun fact string with lightbulb prefix. |
Example::
fact = get_daily_fact()
# "[*] Octopuses have three hearts and blue blood"
Source code in src\shared_services\rendering\documents\fun_facts.py
get_fun_facts_manager()
¶
Get the singleton FunFactsManager instance.
Returns:
| Type | Description |
|---|---|
FunFactsManager
|
The shared FunFactsManager instance. |
Source code in src\shared_services\rendering\documents\fun_facts.py
inject_fun_fact(text)
¶
Replace the fun fact placeholder in text with today's fact.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text that may contain {{FUN_FACT}} placeholder. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Text with placeholder replaced, or original text if no placeholder. |
Example::
text = "Here's a fact: {{FUN_FACT}}"
result = inject_fun_fact(text)
# "Here's a fact: [*] Honey never spoils..."