|
1 | 1 | import json |
2 | 2 |
|
3 | 3 | from django.http import Http404, HttpResponse |
4 | | -from django.template.loader import get_template |
| 4 | +from django.template.loader import get_template, render_to_string |
5 | 5 | from django.utils.decorators import method_decorator |
6 | 6 | from django.utils.html import escape |
7 | 7 | from django.utils.safestring import mark_safe |
8 | 8 | from django.views.decorators.clickjacking import xframe_options_sameorigin |
9 | 9 | from django.views.decorators.csrf import csrf_exempt |
10 | 10 | from django.views.generic.base import TemplateView |
11 | 11 |
|
| 12 | +from bs4 import BeautifulSoup |
| 13 | +from bs4.formatter import HTMLFormatter |
| 14 | + |
12 | 15 | from pattern_library import get_base_template_names, get_pattern_base_template_name |
13 | 16 | from pattern_library.exceptions import PatternLibraryEmpty, TemplateIsNotPattern |
14 | 17 | from pattern_library.utils import ( |
@@ -72,6 +75,18 @@ def get(self, request, pattern_template_name=None): |
72 | 75 | context["pattern_config"] = escape( |
73 | 76 | get_pattern_config_str(pattern_template_name) |
74 | 77 | ) |
| 78 | + template_context = get_pattern_context(pattern_template_name) |
| 79 | + try: |
| 80 | + soup = BeautifulSoup( |
| 81 | + render_to_string( |
| 82 | + pattern_template_name, template_context, request=request |
| 83 | + ), |
| 84 | + "html.parser", |
| 85 | + ) |
| 86 | + formatter = HTMLFormatter(indent=4) |
| 87 | + context["pattern_html_source"] = escape(soup.prettify(formatter=formatter)) |
| 88 | + except Exception as e: |
| 89 | + context["pattern_html_source"] = f"Error rendering pattern: {e}" |
75 | 90 | context["pattern_name"] = pattern_config.get("name", pattern_template_name) |
76 | 91 | context["pattern_markdown"] = get_pattern_markdown(pattern_template_name) |
77 | 92 |
|
|
0 commit comments