Skip to content

Commit

Permalink
Merge pull request #79 from cmason3/v1.18.1
Browse files Browse the repository at this point in the history
V1.18.1
  • Loading branch information
cmason3 authored Oct 25, 2023
2 parents f419423 + 8b691bb commit 50374ca
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,31 @@ This function is used to access all the row and column data that JinjaFx is curr

This function is used to expand a string that contains static character classes (i.e. `[0-9]`), static groups (i.e. `(a|b)`) or active counters (i.e. `{ start-end:increment }`) into a list of all the different permutations. You are permitted to use as many classes, groups or counters within the same string - if it doesn't detect any classes, groups or counters within the string then the "string" will be returned as the only list element. Character classes support "A-Z", "a-z" and "0-9" characters, whereas static groups allow any string of characters (including static character classes). If you wish to include "[", "]", "(", ")", "{" or "}" literals within the string then they will need to be escaped.

- <code><b>jinjafx.counter(key</b>: Optional[String]<b>, increment</b>: Optional[Integer]<b>=1, start</b>: Optional[Integer]<b>=1)</b> -> Integer</code>
- <code><b>jinjafx.counter(key</b>: Optional[String]<b>, increment</b>: Optional[Integer]<b>=1, start</b>: Optional[Integer]<b>=1)</b> -> Integer | String</code>

This function is used to provide a persistent counter within a row or between rows. If you specify a `key` then it is a global counter that will persist between rows, but if you don't or you include `jinjafx.row` within the `key`, then the counter only persists within the template of the current row.
This function is used to provide a persistent counter within a row or between rows. If you specify a case insensitive `key` then it is a global counter that will persist between rows, but if you don't or you include `jinjafx.row` within the `key`, then the counter only persists within the template of the current row.

This function also supports a hierarchical counter which can be used for numbering of headings, e.g:

```
1.
1.1.
1.1.1.
1.1.2.
2.
```

This is achieved by using a special syntax for the `key` value:

```jinja2
{{ jinjafx.counter('A.') }}
{{ jinjafx.counter('A.A.') }}
{{ jinjafx.counter('A.A.A.') }}
{{ jinjafx.counter('A.A.A.') }}
{{ jinjafx.counter('A.') }}
```

When this function detects a `key` which consists of single letters preceded by a dot, then it will act as a hierarchical counter and will produce the above output. Using different letters will reset the counter value back to 1 (or a different number if you have specified `start`). You aren't able to skip levels (i.e. 'A.A.A.' isn't valid if 'A.A.' and 'A.' haven't been used previously, etc).

- <code><b>jinjafx.now(format</b>: Optional[String]<b>, tz</b>: Optional[String]<b>="UTC")</b> -> String</code>

Expand Down
25 changes: 23 additions & 2 deletions jinjafx.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from cryptography.hazmat.primitives.ciphers.modes import CTR
from cryptography.exceptions import InvalidSignature

__version__ = '1.18.0'
__version__ = '1.18.1'

__all__ = ['JinjaFx', 'Vault']

Expand Down Expand Up @@ -458,6 +458,7 @@ def jinjafx(self, template, data, gvars, output, exts_dirs=None, sandbox=False):
self.__g_vars = {}
self.__g_warnings = []
self.__g_xlimit = 5000 if sandbox else 0
self.__g_hcounter = re.compile(r'(?:[A-Z]\.)+$', re.IGNORECASE)

outputs = {}
delim = None
Expand Down Expand Up @@ -1164,8 +1165,28 @@ def __jfx_data(self, row, col=None):
def __jfx_counter(self, key=None, increment=1, start=1):
if key is None:
key = '_cnt_r_' + str(self.__g_row)

elif str(key).endswith('.') and self.__g_hcounter.match(key):
nkey = '_cnt_hk'
kelements = key[:-1].lower().split('.')

for i, v in enumerate(kelements[:-1]):
nkey += '_' + v

if nkey in self.__g_dict:
nkey = '_'.join(nkey.split('_')[:-1]) + '_' + str(self.__g_dict[nkey])

else:
return None

nkey += '_' + kelements[-1]
n = self.__g_dict.get(nkey, int(start) - int(increment))
self.__g_dict[nkey] = int(n) + int(increment)
rv = nkey.split('_')[3:-1] + [str(self.__g_dict[nkey])]
return '.'.join(rv) + '.'

else:
key = '_cnt_k_' + str(key)
key = '_cnt_k_' + str(key).lower()

n = self.__g_dict.get(key, int(start) - int(increment))
self.__g_dict[key] = int(n) + int(increment)
Expand Down

0 comments on commit 50374ca

Please sign in to comment.