Add [if][/if] and [if][/if][else][/else] tags
This commit is contained in:
parent
0afcc2fbdc
commit
c913a34c9e
2 changed files with 45 additions and 3 deletions
|
|
@ -61,9 +61,11 @@ There are also special tags to link to yourself or other users automatically. Th
|
||||||
[fa=Ipsum]Dolor[/fa] shows Ipsum's username on FurAffinity, and Dolor everywhere else as a link to Ipsum's FA userpage.
|
[fa=Ipsum]Dolor[/fa] shows Ipsum's username on FurAffinity, and Dolor everywhere else as a link to Ipsum's FA userpage.
|
||||||
|
|
||||||
[weasyl=Sit][ib=Amet][/ib][/weasyl] will show the two user links on Weasyl and Inkbunny as expected. For other websites, the innermost tag is prioritized - Inkbunny, in this case.
|
[weasyl=Sit][ib=Amet][/ib][/weasyl] will show the two user links on Weasyl and Inkbunny as expected. For other websites, the innermost tag is prioritized - Inkbunny, in this case.
|
||||||
[ib=Amet][weasyl=Sit][/weasyl][/ib] is the same as above, but the Weasyl is prioritized instead.
|
[ib=Amet][weasyl=Sit][/weasyl][/ib] is the same as above, but the Weasyl link is prioritized instead.
|
||||||
|
|
||||||
[ib=Amet][weasyl=Sit]Consectetur[/weasyl][/ib] is the same as above, but Consectetur is displayed as the username for websites other than Inkbunny and Weasyl. The Weasyl gallery is linked to in those websites.
|
[ib=Amet][weasyl=Sit]Consectetur[/weasyl][/ib] is the same as above, but Consectetur is displayed as the username for websites other than Inkbunny and Weasyl, with a link to the Weasyl gallery.
|
||||||
|
|
||||||
[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 that get used for intra-linking only.
|
[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]`).
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ DESCRIPTION_GRAMMAR = r"""
|
||||||
| u_tag
|
| u_tag
|
||||||
| url_tag
|
| url_tag
|
||||||
| self_tag
|
| self_tag
|
||||||
|
| if_tag
|
||||||
| user_tag_root
|
| user_tag_root
|
||||||
| TEXT
|
| TEXT
|
||||||
|
|
||||||
|
|
@ -29,6 +30,8 @@ DESCRIPTION_GRAMMAR = r"""
|
||||||
url_tag: "[url" ["=" [URL]] "]" [document_list] "[/url]"
|
url_tag: "[url" ["=" [URL]] "]" [document_list] "[/url]"
|
||||||
|
|
||||||
self_tag: "[self][/self]"
|
self_tag: "[self][/self]"
|
||||||
|
if_tag: "[if=" CONDITION "]" [document_list] "[/if]" [ "[else]" document_list "[/else]" ]
|
||||||
|
|
||||||
user_tag_root: user_tag
|
user_tag_root: user_tag
|
||||||
user_tag: generic_tag | """
|
user_tag: generic_tag | """
|
||||||
|
|
||||||
|
|
@ -41,6 +44,7 @@ DESCRIPTION_GRAMMAR += r"""
|
||||||
USERNAME: /[a-zA-Z0-9][a-zA-Z0-9 _-]*/
|
USERNAME: /[a-zA-Z0-9][a-zA-Z0-9 _-]*/
|
||||||
URL: /(https?:\/\/)?[^\]]+/
|
URL: /(https?:\/\/)?[^\]]+/
|
||||||
TEXT: /([^\[]|[ \t\r\n])+/
|
TEXT: /([^\[]|[ \t\r\n])+/
|
||||||
|
CONDITION: / *[a-z]+ *(==|!=) *[a-zA-Z0-9]+ */
|
||||||
"""
|
"""
|
||||||
|
|
||||||
DESCRIPTION_PARSER = lark.Lark(DESCRIPTION_GRAMMAR, parser='lalr')
|
DESCRIPTION_PARSER = lark.Lark(DESCRIPTION_GRAMMAR, parser='lalr')
|
||||||
|
|
@ -112,6 +116,27 @@ class UploadTransformer(lark.Transformer):
|
||||||
def self_tag(self, _):
|
def self_tag(self, _):
|
||||||
raise NotImplementedError('UploadTransformer.self_tag is abstract')
|
raise NotImplementedError('UploadTransformer.self_tag is abstract')
|
||||||
|
|
||||||
|
def transformer_matches_site(self, site: str) -> bool:
|
||||||
|
raise NotImplementedError('UploadTransformer.transformer_matches_site is abstract')
|
||||||
|
|
||||||
|
def if_tag(self, data: typing.Tuple[str, str, str]):
|
||||||
|
condition, truthy_document, falsy_document = data
|
||||||
|
equality_condition = condition.split('==')
|
||||||
|
if len(equality_condition) == 2 and equality_condition[1].strip():
|
||||||
|
conditional_test = f'transformer_matches_{equality_condition[0].strip()}'
|
||||||
|
if hasattr(self, conditional_test):
|
||||||
|
if getattr(self, conditional_test)(equality_condition[1].strip()):
|
||||||
|
return truthy_document or ''
|
||||||
|
return falsy_document or ''
|
||||||
|
inequality_condition = condition.split('!=')
|
||||||
|
if len(inequality_condition) == 2 and inequality_condition[1].strip():
|
||||||
|
conditional_test = f'transformer_matches_{inequality_condition[0].strip()}'
|
||||||
|
if hasattr(self, conditional_test):
|
||||||
|
if not getattr(self, conditional_test)(inequality_condition[1].strip()):
|
||||||
|
return truthy_document or ''
|
||||||
|
return falsy_document or ''
|
||||||
|
raise ValueError(f'Invalid [if][/if] tag condition: {condition}')
|
||||||
|
|
||||||
def user_tag_root(self, data):
|
def user_tag_root(self, data):
|
||||||
user_data: UserTag = data[0]
|
user_data: UserTag = data[0]
|
||||||
for site in user_data.sites:
|
for site in user_data.sites:
|
||||||
|
|
@ -229,6 +254,9 @@ class AryionTransformer(BbcodeTransformer):
|
||||||
return self.user_tag_root((UserTag(eka=self_user),))
|
return self.user_tag_root((UserTag(eka=self_user),))
|
||||||
self.self_tag = self_tag
|
self.self_tag = self_tag
|
||||||
|
|
||||||
|
def transformer_matches_site(self, site: str) -> bool:
|
||||||
|
return site in ('eka', 'aryion')
|
||||||
|
|
||||||
def user_tag_root(self, data):
|
def user_tag_root(self, data):
|
||||||
user_data = data[0]
|
user_data = data[0]
|
||||||
if user_data['eka']:
|
if user_data['eka']:
|
||||||
|
|
@ -242,6 +270,9 @@ class FuraffinityTransformer(BbcodeTransformer):
|
||||||
return self.user_tag_root((UserTag(fa=self_user),))
|
return self.user_tag_root((UserTag(fa=self_user),))
|
||||||
self.self_tag = self_tag
|
self.self_tag = self_tag
|
||||||
|
|
||||||
|
def transformer_matches_site(self, site: str) -> bool:
|
||||||
|
return site in ('fa', 'furaffinity')
|
||||||
|
|
||||||
def user_tag_root(self, data):
|
def user_tag_root(self, data):
|
||||||
user_data = data[0]
|
user_data = data[0]
|
||||||
if user_data['fa']:
|
if user_data['fa']:
|
||||||
|
|
@ -255,6 +286,9 @@ class WeasylTransformer(MarkdownTransformer):
|
||||||
return self.user_tag_root((UserTag(weasyl=self_user),))
|
return self.user_tag_root((UserTag(weasyl=self_user),))
|
||||||
self.self_tag = self_tag
|
self.self_tag = self_tag
|
||||||
|
|
||||||
|
def transformer_matches_site(self, site: str) -> bool:
|
||||||
|
return site == 'weasyl'
|
||||||
|
|
||||||
def user_tag_root(self, data):
|
def user_tag_root(self, data):
|
||||||
user_data = data[0]
|
user_data = data[0]
|
||||||
if user_data['weasyl']:
|
if user_data['weasyl']:
|
||||||
|
|
@ -276,6 +310,9 @@ class InkbunnyTransformer(BbcodeTransformer):
|
||||||
return self.user_tag_root((UserTag(ib=self_user),))
|
return self.user_tag_root((UserTag(ib=self_user),))
|
||||||
self.self_tag = self_tag
|
self.self_tag = self_tag
|
||||||
|
|
||||||
|
def transformer_matches_site(self, site: str) -> bool:
|
||||||
|
return site in ('ib', 'inkbunny')
|
||||||
|
|
||||||
def user_tag_root(self, data):
|
def user_tag_root(self, data):
|
||||||
user_data = data[0]
|
user_data = data[0]
|
||||||
if user_data['ib']:
|
if user_data['ib']:
|
||||||
|
|
@ -297,6 +334,9 @@ class SoFurryTransformer(BbcodeTransformer):
|
||||||
return self.user_tag_root((UserTag(sf=self_user),))
|
return self.user_tag_root((UserTag(sf=self_user),))
|
||||||
self.self_tag = self_tag
|
self.self_tag = self_tag
|
||||||
|
|
||||||
|
def transformer_matches_site(self, site: str) -> bool:
|
||||||
|
return site in ('sf', 'sofurry')
|
||||||
|
|
||||||
def user_tag_root(self, data):
|
def user_tag_root(self, data):
|
||||||
user_data = data[0]
|
user_data = data[0]
|
||||||
if user_data['sf']:
|
if user_data['sf']:
|
||||||
|
|
|
||||||
Reference in a new issue