Support interpolation in diary page titles

This commit is contained in:
Gouvernathor 2024-03-29 23:04:54 +01:00
parent 2111e5da5f
commit 994e9121bd

View File

@ -66,7 +66,7 @@ init python:
`dictionary` is a dict containing two kinds of entries: `dictionary` is a dict containing two kinds of entries:
- the first kind is to store the possible pages which can be added to the dict. - the first kind is to store the possible pages which can be added to the dict.
These entries are the form {str page_id : (str|None page_title, str page_text)}. These entries are the form {str page_id : (str|None page_title, str page_text)}.
`page_text` may contain interpolation fields, such as {code}. `page_title` and `page_text` may contain interpolation fields, such as {code}.
- the second kind is to store the for interpolation codes to fill in those blanks. - the second kind is to store the for interpolation codes to fill in those blanks.
These entries are of the form {str interpo_id : str interpo_text}. These entries are of the form {str interpo_id : str interpo_text}.
`interpo_text` is the text to be used for the interpolation. `interpo_text` is the text to be used for the interpolation.
@ -78,8 +78,8 @@ init python:
""" """
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.dictionary_ = dictionary # str id : either (title, text) or text self.dictionary_ = dictionary # type: dict[str, tuple[str|None, str]|str]|str
self.entry_ids = set() # str id self.entry_ids = set() # set[str]
@property @property
def dictionary(self): def dictionary(self):
@ -97,7 +97,7 @@ init python:
`branches` is a dict of {code : interpo_id} for every happened sub-event specializing the `id` event. `branches` is a dict of {code : interpo_id} for every happened sub-event specializing the `id` event.
The specified interpolation ids will be looked for in the dictionary, and the original entry of id `id` The specified interpolation ids will be looked for in the dictionary, and the original entry of id `id`
will be formatted by replacing {code} with the text found in dictionary[interpo_id]. will be formatted by replacing {code} with the page title and page text found in dictionary[interpo_id].
If `interpo_id` is not a valid entry in the dict, the passed value itself will be interpolated instead, If `interpo_id` is not a valid entry in the dict, the passed value itself will be interpolated instead,
or nothing if it is a false value (like None). or nothing if it is a false value (like None).
@ -106,8 +106,8 @@ init python:
"Today I met Alice and did nothing. I liked it a lot." to the diary, with the title "Tittle". "Today I met Alice and did nothing. I liked it a lot." to the diary, with the title "Tittle".
Passing c="" or c=None or c=False will all result in "Today I met Alice and did nothing. I liked it.". Passing c="" or c=None or c=False will all result in "Today I met Alice and did nothing. I liked it.".
If the page for the key `id` contains interpolation fields, it is a mistake If the page and title for the key `id` contain interpolation fields, it is a mistake
not to pass all interpolation fields. not to pass all interpolation fields as kwargs.
It is benign to specify keys which are not interpolation fields in the entry : in the previous example, It is benign to specify keys which are not interpolation fields in the entry : in the previous example,
passing d=whatever will not change anything to the result. passing d=whatever will not change anything to the result.
@ -124,6 +124,8 @@ init python:
if branches: if branches:
branches = {k : dictionary.get(v, v or "") for k, v in branches.items()} branches = {k : dictionary.get(v, v or "") for k, v in branches.items()}
page_text = page_text.format(**branches) page_text = page_text.format(**branches)
if page_title:
page_title = page_title.format(**branches)
if day is None: if day is None:
day = game.day day = game.day