Add key generator to text index

This commit is contained in:
Veronica Berglyd Olsen
2024-04-14 22:17:00 +02:00
parent ba42285172
commit 5568b43b22
3 changed files with 25 additions and 4 deletions
+16 -4
View File
@@ -30,6 +30,7 @@ from __future__ import annotations
import json
import logging
from random import randint
from time import time
from typing import TYPE_CHECKING
from pathlib import Path
@@ -1309,6 +1310,9 @@ class IndexHeading:
# The Text Index Object
# =============================================================================================== #
KEY_SOURCE = "0123456789bcdfghjklmnopqrstvwxyz"
class TextIndex:
"""Core: Text Index Wrapper Class
@@ -1318,8 +1322,8 @@ class TextIndex:
__slots__ = ("_comments", "_footnotes")
def __init__(self) -> None:
self._comments = TextRegistry()
self._footnotes = TextRegistry()
self._comments = TextRegistry("c_")
self._footnotes = TextRegistry("f_")
return
@property
@@ -1374,11 +1378,12 @@ class TextRegistry:
A wrapper class that holds a category of text entries.
"""
__slots__ = ("_map", "_text")
__slots__ = ("_map", "_text", "_prefix")
def __init__(self) -> None:
def __init__(self, prefix: str) -> None:
self._map: dict[str, str] = {}
self._text: dict[str, str] = {}
self._prefix = prefix
return
def __len__(self) -> int:
@@ -1416,6 +1421,13 @@ class TextRegistry:
del self._text[key]
return
def newKey(self) -> str:
"""Generate a new key."""
key = self._prefix + "".join([KEY_SOURCE[randint(0, 31)] for _ in range(4)])
if key in self._text:
key = self.newKey()
return key
##
# Pack/Unpack
##
+3
View File
@@ -69,6 +69,8 @@ class nwComment(Enum):
SHORT = 2
NOTE = 3
FOOTNOTE = 4
COMMENT = 5
STORY = 6
# END Enum nwComment
@@ -147,6 +149,7 @@ class nwDocInsert(Enum):
VSPACE_S = 8
VSPACE_M = 9
LIPSUM = 10
FOOTNOTE = 11
# END Enum nwDocInsert
+6
View File
@@ -597,6 +597,12 @@ class GuiMainMenu(QMenuBar):
lambda: self.requestDocInsert.emit(nwDocInsert.LIPSUM)
)
# Insert > Footnote
self.aFootnote = self.insMenu.addAction(self.tr("Footnote"))
self.aFootnote.triggered.connect(
lambda: self.requestDocInsert.emit(nwDocInsert.FOOTNOTE)
)
return
def _buildFormatMenu(self) -> None: