Skip to content

Commit 34a6ff9

Browse files
feat: Rendered code view in Template output (#2)
1 parent 13e6632 commit 34a6ff9

File tree

9 files changed

+458
-101
lines changed

9 files changed

+458
-101
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [1.2.1dev1](https://github.com/lincolnloop/django-pattern-library/releases/tag/v1.2.1dev1) - 2024-04-02
4+
5+
### Added
6+
7+
- Add HTML rendered code view in `Template output` column ([#2](https://github.com/lincolnloop/django-pattern-library/pull/2))
8+
39
## [1.2.0](https://github.com/torchbox/django-pattern-library/releases/tag/v1.2.0) - 2024-01-16
410

511
### Added

docker-compose.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ services:
77
PYTHONDONTWRITEBYTECODE: 1
88
ports:
99
- "8000:8000"
10+
stdin_open: true
11+
tty: true
1012
volumes:
1113
- type: bind
1214
source: .

pattern_library/static/pattern_library/src/js/app.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import '../scss/main.scss';
2-
import persistMenu from './components/persist-menu';
3-
import patternSearch from './components/pattern-search';
4-
import tabbedContent from './components/tabbed-content';
5-
import syntaxHighlighting from './components/syntax-highlighting';
6-
import hideMenuMobile from './components/hide-menu-mobile';
7-
import {setIframeSize, resizeIframe} from './components/iframe';
8-
import {toggleNav, toggleNavItems} from './components/navigation';
1+
import "../scss/main.scss";
2+
import persistMenu from "./components/persist-menu";
3+
import patternSearch from "./components/pattern-search";
4+
import tabbedContent from "./components/tabbed-content";
5+
import syntaxHighlighting from "./components/syntax-highlighting";
6+
import hideMenuMobile from "./components/hide-menu-mobile";
7+
import { setIframeSize, resizeIframe } from "./components/iframe";
8+
import { toggleNav, toggleNavItems } from "./components/navigation";
99

10-
document.addEventListener('DOMContentLoaded', () => {
10+
document.addEventListener("DOMContentLoaded", () => {
1111
syntaxHighlighting();
1212
toggleNavItems();
1313
resizeIframe();
Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import hljs from 'highlight.js/lib/core';
2-
import django from 'highlight.js/lib/languages/django';
3-
import yaml from 'highlight.js/lib/languages/yaml';
1+
import hljs from "highlight.js/lib/core";
2+
import django from "highlight.js/lib/languages/django";
3+
import yaml from "highlight.js/lib/languages/yaml";
4+
import md from "highlight.js/lib/languages/markdown";
5+
import xml from "highlight.js/lib/languages/xml";
46

5-
export default function() {
6-
hljs.registerLanguage('django', django);
7-
hljs.registerLanguage('yaml', yaml);
7+
export default function () {
8+
hljs.registerLanguage("django", django);
9+
hljs.registerLanguage("yaml", yaml);
10+
hljs.registerLanguage("md", md);
11+
hljs.registerLanguage("xml", xml);
812
hljs.highlightAll();
913
}

pattern_library/templates/pattern_library/index.html

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,19 @@ <h4 class="heading heading--iframe-size js-iframe-size">
2727
<li class="tabbed-content__heading tabbed-content__heading--active">
2828
<a href="#tab-1">Template source</a>
2929
</li>
30+
3031
<li class="tabbed-content__heading">
31-
<a href="#tab-2">Template config</a>
32+
<a href="#tab-2">Template output</a>
3233
</li>
3334

3435
<li class="tabbed-content__heading">
35-
<a href="#tab-3">Template docs</a>
36+
<a href="#tab-3">Template config</a>
3637
</li>
38+
39+
<li class="tabbed-content__heading">
40+
<a href="#tab-4">Template docs</a>
41+
</li>
42+
3743
</ul>
3844

3945
<div class="tabbed-content__tabs">
@@ -42,10 +48,14 @@ <h4 class="heading heading--iframe-size js-iframe-size">
4248
</div>
4349

4450
<div id="tab-2" class="tabbed-content__item">
45-
<pre><code class="code yaml">{{ pattern_config }}</code></pre>
51+
<pre><code class="code xml">{{ pattern_html_source }}</code></pre>
4652
</div>
4753

4854
<div id="tab-3" class="tabbed-content__item">
55+
<pre><code class="code yaml">{{ pattern_config }}</code></pre>
56+
</div>
57+
58+
<div id="tab-4" class="tabbed-content__item">
4959
<div class="md">{{ pattern_markdown|safe }}</div>
5060
</div>
5161
</div>

pattern_library/views.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import json
22

33
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
55
from django.utils.decorators import method_decorator
66
from django.utils.html import escape
77
from django.utils.safestring import mark_safe
88
from django.views.decorators.clickjacking import xframe_options_sameorigin
99
from django.views.decorators.csrf import csrf_exempt
1010
from django.views.generic.base import TemplateView
1111

12+
from bs4 import BeautifulSoup
13+
from bs4.formatter import HTMLFormatter
14+
1215
from pattern_library import get_base_template_names, get_pattern_base_template_name
1316
from pattern_library.exceptions import PatternLibraryEmpty, TemplateIsNotPattern
1417
from pattern_library.utils import (
@@ -72,6 +75,15 @@ def get(self, request, pattern_template_name=None):
7275
context["pattern_config"] = escape(
7376
get_pattern_config_str(pattern_template_name)
7477
)
78+
template_context = get_pattern_context(pattern_template_name)
79+
try:
80+
soup = BeautifulSoup(
81+
render_to_string(pattern_template_name, template_context), "html.parser"
82+
)
83+
formatter = HTMLFormatter(indent=4)
84+
context["pattern_html_source"] = escape(soup.prettify(formatter=formatter))
85+
except Exception as e:
86+
context["pattern_html_source"] = f"Error rendering pattern: {e}"
7587
context["pattern_name"] = pattern_config.get("name", pattern_template_name)
7688
context["pattern_markdown"] = get_pattern_markdown(pattern_template_name)
7789

0 commit comments

Comments
 (0)