Improve documentation on [if][/if][else][/else] tags

This commit is contained in:
Bad Manners 2023-11-15 17:04:37 -03:00
parent 83cd4d7119
commit 468e219ca8
2 changed files with 3 additions and 3 deletions

View file

@ -68,4 +68,4 @@ There are also special tags to link to yourself or other users automatically. Th
[generic=https://github.com/BadMannersXYZ]Bad Manners[/generic] can be used as the innermost tag with a mandatory URL attribute and default username, and is similar to the URL tag, but it can be nested within other profile links. Those other profile links get used only at their respective websites. [generic=https://github.com/BadMannersXYZ]Bad Manners[/generic] can be used as the innermost tag with a mandatory URL attribute and default username, and is similar to the URL tag, but it can be nested within other profile links. Those other profile links get used only at their respective websites.
``` ```
Another special set of tags is `[if][/if]` and `[else][/else]`. The if tag receives a parameter for the condition (i.e. `[if=parameter==value]...[/if]` or `[if=parameter!=value]...[/if]`) to check on the current transformer, and lets you show or omit generated content respectively. The else tag is optional but must appear immediately after an if tag (no whitespace in-between), and displays whenever the condition is false instead. For now, the if tag only accepts the `site` parameter (eg. `[if=site==fa]...[/if][else]...[/else]` or `[if=site!=furaffinity]...[/if]`). Another special set of tags is `[if][/if]` or `[if][/if][else][/else]`. The if tag receives a parameter for the condition (i.e. `[if=parameter==value]...[/if]` or `[if=parameter!=value]...[/if]`) to check on the current transformer, and lets you show or omit generated content respectively. The else tag is optional but must appear immediately after an if tag (no characters or whitespace in-between), and displays whenever the condition is false instead. For now, the if tag only accepts the `site` parameter (eg. `[if=site==fa]...[/if][else]...[/else]` or `[if=site!=furaffinity]...[/if][else]...[/else]`).

View file

@ -121,14 +121,14 @@ class UploadTransformer(lark.Transformer):
def if_tag(self, data: typing.Tuple[str, str, str]): def if_tag(self, data: typing.Tuple[str, str, str]):
condition, truthy_document, falsy_document = data condition, truthy_document, falsy_document = data
equality_condition = condition.split('==') equality_condition = condition.split('==', 1)
if len(equality_condition) == 2 and equality_condition[1].strip(): if len(equality_condition) == 2 and equality_condition[1].strip():
conditional_test = f'transformer_matches_{equality_condition[0].strip()}' conditional_test = f'transformer_matches_{equality_condition[0].strip()}'
if hasattr(self, conditional_test): if hasattr(self, conditional_test):
if getattr(self, conditional_test)(equality_condition[1].strip()): if getattr(self, conditional_test)(equality_condition[1].strip()):
return truthy_document or '' return truthy_document or ''
return falsy_document or '' return falsy_document or ''
inequality_condition = condition.split('!=') inequality_condition = condition.split('!=', 1)
if len(inequality_condition) == 2 and inequality_condition[1].strip(): if len(inequality_condition) == 2 and inequality_condition[1].strip():
conditional_test = f'transformer_matches_{inequality_condition[0].strip()}' conditional_test = f'transformer_matches_{inequality_condition[0].strip()}'
if hasattr(self, conditional_test): if hasattr(self, conditional_test):