Skip to content

Commit 3964348

Browse files
authored
Merge pull request #130 from bckohan/v2.1.0
V2.1.0
2 parents 224f0a4 + f30dbfa commit 3964348

File tree

35 files changed

+2486
-385
lines changed

35 files changed

+2486
-385
lines changed

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ Single-sourcing these structures by transpiling client side code from the server
4545
- Plain data define-like structures in Python classes and modules
4646
(`defines_to_js`)
4747

48+
Transpilation is extremely flexible and may be customized by using override blocks or extending the provided
49+
transpilers.
50+
4851
`django-render-static` also formalizes the concept of a package-time or deployment-time
4952
static file rendering step. It piggybacks off the existing templating engines and configurations
5053
and should therefore be familiar to Django developers. It supports both standard Django templating

doc/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ sphinxcontrib-jsmath==1.0.1; python_version >= "3.5"
77
sphinxcontrib-qthelp==1.0.3; python_version >= "3.5"
88
sphinxcontrib-serializinghtml==1.1.5; python_version >= "3.5"
99
sphinx-js==3.2.2; python_version >= "3.5"
10-
django-render-static==2.0.3
10+
django-render-static==2.1.0

doc/source/changelog.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
Change Log
33
==========
44

5+
v2.1.0
6+
======
7+
* Implemented `Support templating of destination paths. <https://github.com/bckohan/django-render-static/issues/129>`_
8+
* Implemented `Support configurable case insensitive property mapping on enum transpilation. <https://github.com/bckohan/django-render-static/issues/128>`_
9+
* Implemented `Add a pass through getter for enums_to_js transpilation. <https://github.com/bckohan/django-render-static/issues/126>`_
10+
* Implemented `enum transpilation should iterate through value properties instead of hardcoding a switch statement. <https://github.com/bckohan/django-render-static/issues/125>`
11+
* Implemented `Add type check and return to getter on transpiled enum classes.. <https://github.com/bckohan/django-render-static/issues/122>`_
12+
* Implemented `Provide switch to turn off toString() transpilation on enums_to_js <https://github.com/bckohan/django-render-static/issues/121>`_
13+
* Implemented `Allow include_properties to be a list of properties on enums_to_js <https://github.com/bckohan/django-render-static/issues/119>`_
14+
* Implemented `Extension points for transpiled code. <https://github.com/bckohan/django-render-static/issues/104>`_
15+
516
v2.0.3
617
======
718
* Fixed `Invalid URL generation for urls with default arguments. <https://github.com/bckohan/django-render-static/issues/124>`_

doc/source/configuration.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ single template, if the ``dest`` parameter is not an existing directory, it will
209209
the full path including the file name where the template will be rendered. When rendering in batch
210210
mode, ``dest`` will be treated as a directory and created if it does not exist.
211211

212+
The ``dest`` parameter may include template variables that will be replaced with the value of the
213+
variable in the context. For example, if ``dest`` is ``'js/{{ app_name }}.js'`` and the context
214+
contains ``{'app_name': 'my_app'}`` then the template will be rendered to ``js/my_app.js``.
215+
212216
``context``
213217
~~~~~~~~~~~
214218

doc/source/reference.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ transpilers
120120
.. autofunction:: to_js_datetime
121121
.. autoclass:: CodeWriter
122122
.. autoclass:: Transpiler
123+
.. autoproperty:: Transpiler.context
123124

124125

125126
.. _transpilers_defines_to_js:
@@ -131,6 +132,7 @@ transpilers.defines_to_js
131132
.. automodule:: render_static.transpilers.defines_to_js
132133

133134
.. autoclass:: DefaultDefineTranspiler
135+
.. autoproperty:: DefaultDefineTranspiler.context
134136

135137

136138
.. _transpilers_urls_to_js:
@@ -141,8 +143,11 @@ transpilers.urls_to_js
141143
.. automodule:: render_static.transpilers.urls_to_js
142144

143145
.. autoclass:: URLTreeVisitor
146+
.. autoproperty:: URLTreeVisitor.context
144147
.. autoclass:: SimpleURLWriter
148+
.. autoproperty:: SimpleURLWriter.context
145149
.. autoclass:: ClassURLWriter
150+
.. autoproperty:: ClassURLWriter.context
146151
.. autoclass:: Substitute
147152
.. autofunction:: normalize_ns
148153
.. autofunction:: build_tree
@@ -155,8 +160,10 @@ transpilers.enums_to_js
155160

156161
.. automodule:: render_static.transpilers.enums_to_js
157162

163+
.. autoclass:: UnrecognizedBehavior
158164
.. autoclass:: EnumTranspiler
159165
.. autoclass:: EnumClassWriter
166+
.. autoproperty:: EnumClassWriter.context
160167

161168

162169
.. _context:

doc/source/templatetags.rst

Lines changed: 187 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,25 @@ The generated source would look like:
144144
parent classes and add them to the JavaScript.
145145

146146

147+
Overrides
148+
*********
149+
150+
The ``DefaultDefineTranspiler`` supports the :ref:`override` block. The context available to
151+
override blocks is detailed here:
152+
:py:attr:`render_static.transpilers.defines_to_js.DefaultDefineTranspiler.context`. More code
153+
can be added to define variables or specific defines can be overridden by using their python
154+
path:
155+
156+
.. code-block:: js+django
157+
158+
{% defines_to_js defines='myapp' %}
159+
160+
{% override 'myapp.defines.TestDefines.DEFINE1' %}
161+
"OVERRIDE"
162+
{% endoverride %}
163+
164+
{% enddefines_to_js %}
165+
147166
.. _urls_to_js:
148167

149168
``urls_to_js``
@@ -265,15 +284,36 @@ Placeholders are the price paid for that reliability. Common default placeholder
265284
after all registered placeholders fail, and all of Django's native path converters are
266285
supported. This should allow most urls to work out of the box.
267286

287+
Overrides
288+
*********
289+
290+
Both the ``ClassURLWriter`` and ``SimpleURLWriter`` transpilers support the :ref:`override`
291+
block. The contexts available to override blocks for each transpiler are detailed here:
292+
293+
- :py:attr:`render_static.transpilers.urls_to_js.SimpleURLWriter.context`
294+
- :py:attr:`render_static.transpilers.urls_to_js.ClassURLWriter.context`
295+
296+
Any function on ``ClassURLWriter`` including the constructor can be overridden and both
297+
transpilers allow adding to the class or object and overriding the reversal code for
298+
specific url names. For instance:
299+
300+
.. code-block:: js+django
301+
302+
{% urls_to_js transpiler='render_static.SimpleURLWriter' %}
303+
304+
{% override 'namespace:path_name' %}
305+
return "/an/overridden/path";
306+
{% endoverride %}
307+
308+
{% endurls_to_js %}
268309

269310
`ClassURLWriter` (default)
270311
**************************
271312

272313
A transpiler class that produces ES6 JavaScript class is now included. As of version 2 This
273-
class is used by default. It is the preferred transpiler for larger, more complex URL trees
274-
because it minifies better than the ``SimpleURLWriter`` and it handles default kwargs
275-
appropriately. **The** ``ClassURLWriter`` **is guaranteed to produce output identical to Django's
276-
reverse function**. If it does not please report a bug. To use the class writer:
314+
class is used by default. **The** ``ClassURLWriter`` **is guaranteed to produce output
315+
identical to Django's reverse function**. If it does not please report a bug. To use the
316+
class writer:
277317

278318
.. code-block:: htmldjango
279319
@@ -532,13 +572,14 @@ like this:
532572
}
533573
534574
static get(value) {
535-
switch(value) {
536-
case "R":
537-
return Color.RED;
538-
case "G":
539-
return Color.GREEN;
540-
case "B":
541-
return Color.BLUE;
575+
if (value instanceof this) {
576+
return value;
577+
}
578+
579+
for (const en of this) {
580+
if (en.value === value) {
581+
return en;
582+
}
542583
}
543584
throw new TypeError(`No Color enumeration maps to value ${value}`);
544585
}
@@ -556,3 +597,138 @@ We can now use our enumeration like so:
556597
for (const color of Color) {
557598
console.log(color);
558599
}
600+
601+
Overrides
602+
*********
603+
604+
You may add additional code to the class or :ref:`override` the following functions:
605+
606+
- constructor
607+
- toString
608+
- get
609+
- ciCompare
610+
- [Symbol.iterator]
611+
612+
See :py:attr:`render_static.transpilers.enums_to_js.EnumClassWriter.context` for the
613+
context made available by the transpiler to override blocks.
614+
615+
.. _override:
616+
617+
``override``
618+
~~~~~~~~~~~~
619+
620+
All of the transpilation tags accept child override blocks to override default transpilation
621+
of functions or objects or be used to add additional code to an object block or class. For
622+
example, if we wanted to override the default transpilation of the Color class above to allow
623+
instantiation off a cmyk value we could do so by adapting the get function and adding a new
624+
static utility function called cmykToRgb. We would do so like this:
625+
626+
627+
.. code:: js+django
628+
629+
{% enums_to_js enums="examples.models.ExampleModel.Color" %}
630+
631+
{# to override a function we must pass its name as the argument #}
632+
{% override 'get' %}
633+
static get(value) {
634+
if (Array.isArray(value) && value.length === 4) {
635+
value = Color.cmykToRgb(...value);
636+
}
637+
638+
if (Array.isArray(value) && value.length === 3) {
639+
for (const en of this) {
640+
let i = 0;
641+
for (; i < 3; i++) {
642+
if (en.rgb[i] !== value[i]) break;
643+
}
644+
if (i === 3) return en;
645+
}
646+
}
647+
{{ default_impl }}
648+
}
649+
{% endoverride %}
650+
651+
{# additions do not require a name argument #}
652+
{% override %}
653+
static cmykToRgb(c, m, y, k) {
654+
655+
let r = 255 * (1 - c / 100) * (1 - k / 100);
656+
let g = 255 * (1 - m / 100) * (1 - k / 100);
657+
let b = 255 * (1 - y / 100) * (1 - k / 100);
658+
659+
return [Math.round(r), Math.round(g), Math.round(b)]
660+
}
661+
{% endoverride %}
662+
{% endenums_to_js %}
663+
664+
When a function is overridden, the default implementation is available in the template context
665+
as the ``default_impl`` variable. This allows you to add the default implementation from
666+
code to your override. The context available to an override block varies depending on the
667+
transpiler. See the individual tag sections for details.
668+
669+
The above example will generate code that looks like this:
670+
671+
.. code:: javascript
672+
673+
class Color {
674+
675+
static RED = new Color("R", "RED", "Red", [1, 0, 0], "ff0000");
676+
static GREEN = new Color("G", "GREEN", "Green", [0, 1, 0], "00ff00");
677+
static BLUE = new Color("B", "BLUE", "Blue", [0, 0, 1], "0000ff");
678+
679+
constructor (value, name, label, rgb, hex) {
680+
this.value = value;
681+
this.name = name;
682+
this.label = label;
683+
this.rgb = rgb;
684+
this.hex = hex;
685+
}
686+
687+
toString() {
688+
return this.value;
689+
}
690+
691+
static get(value) {
692+
if (Array.isArray(value) && value.length === 4) {
693+
value = Color.cmykToRgb(...value);
694+
}
695+
696+
if (Array.isArray(value) && value.length === 3) {
697+
for (const en of this) {
698+
let i = 0;
699+
for (; i < 3; i++) {
700+
if (en.rgb[i] !== value[i]) break;
701+
}
702+
if (i === 3) return en;
703+
}
704+
}
705+
if (value instanceof this) {
706+
return value;
707+
}
708+
709+
for (const en of this) {
710+
if (en.value === value) {
711+
return en;
712+
}
713+
}
714+
throw new TypeError(`No Color enumeration maps to value ${value}`);
715+
}
716+
717+
static [Symbol.iterator]() {
718+
return [Color.RED, Color.GREEN, Color.BLUE][Symbol.iterator]();
719+
}
720+
721+
static cmykToRgb(c, m, y, k) {
722+
723+
let r = (1 - c / 100) * (1 - k / 100);
724+
let g = (1 - m / 100) * (1 - k / 100);
725+
let b = (1 - y / 100) * (1 - k / 100);
726+
727+
return [Math.round(r), Math.round(g), Math.round(b)]
728+
}
729+
}
730+
731+
732+
.. note::
733+
734+
The Jinja2 tags do not currently support overrides.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "django-render-static"
3-
version = "2.0.3"
3+
version = "2.1.0"
44
description = "Use Django's template engine to render static files at deployment or package time. Includes transpilers for extending Django's url reversal and enums to JavaScript."
55
authors = ["Brian Kohan <bckohan@gmail.com>"]
66
license = "MIT"

render_static/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .transpilers.enums_to_js import EnumClassWriter
1515
from .transpilers.urls_to_js import ClassURLWriter, SimpleURLWriter
1616

17-
VERSION = (2, 0, 3)
17+
VERSION = (2, 1, 0)
1818

1919
__title__ = 'Django Render Static'
2020
__version__ = '.'.join(str(i) for i in VERSION)

render_static/backends.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def default_env(**options):
120120
env = Environment(**options)
121121
env.globals.update(render_static.register.filters)
122122
env.globals.update({
123-
name: tag.__wrapped__
123+
name: getattr(tag, '__wrapped__', tag)
124124
for name, tag in render_static.register.tags.items()
125125
})
126126
return env
@@ -211,7 +211,6 @@ def select_templates(
211211
loader specific and documented on the loader.
212212
:return: The list of resolved template names
213213
"""
214-
215214
template_names = set()
216215
if callable(getattr(self.env.loader, 'select_templates', None)):
217216
for templates in self.env.loader.select_templates(selector):
@@ -223,6 +222,7 @@ def select_templates(
223222
else:
224223
self.get_template(selector)
225224
template_names.add(selector)
225+
226226
if first_loader and template_names:
227227
return list(template_names)
228228

0 commit comments

Comments
 (0)