Use enums for build formats
This commit is contained in:
+20
-10
@@ -25,7 +25,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
from PyQt5.QtCore import QCoreApplication, QT_TRANSLATE_NOOP
|
||||
|
||||
from novelwriter.enum import nwItemClass, nwItemLayout, nwOutline
|
||||
from novelwriter.enum import nwBuildFmt, nwItemClass, nwItemLayout, nwOutline
|
||||
|
||||
|
||||
def trConst(tString):
|
||||
@@ -205,15 +205,25 @@ class nwLabels:
|
||||
nwOutline.CUSTOM: KEY_NAME[nwKeyWords.CUSTOM_KEY],
|
||||
nwOutline.SYNOP: QT_TRANSLATE_NOOP("Constant", "Synopsis"),
|
||||
}
|
||||
BUILD_FORMATS = {
|
||||
"odt": (".odt", QT_TRANSLATE_NOOP("Constant", "Open Document (.odt)")),
|
||||
"fodt": (".fodt", QT_TRANSLATE_NOOP("Constant", "Flat Open Document (.fodt)")),
|
||||
"html": (".html", QT_TRANSLATE_NOOP("Constant", "novelWriter HTML (.html)")),
|
||||
"nwd": (".nwd", QT_TRANSLATE_NOOP("Constant", "novelWriter Markdown (.nwd)")),
|
||||
"md": (".md", QT_TRANSLATE_NOOP("Constant", "Standard Markdown (.md)")),
|
||||
"md+": (".md", QT_TRANSLATE_NOOP("Constant", "Extended Markdown (.md)")),
|
||||
"jhtml": (".json", QT_TRANSLATE_NOOP("Constant", "JSON + novelWriter HTML (.json)")),
|
||||
"jnwd": (".json", QT_TRANSLATE_NOOP("Constant", "JSON + novelWriter Markdown (.json)")),
|
||||
BUILD_FMT = {
|
||||
nwBuildFmt.ODT: QT_TRANSLATE_NOOP("Constant", "Open Document (.odt)"),
|
||||
nwBuildFmt.FODT: QT_TRANSLATE_NOOP("Constant", "Flat Open Document (.fodt)"),
|
||||
nwBuildFmt.HTML: QT_TRANSLATE_NOOP("Constant", "novelWriter HTML (.html)"),
|
||||
nwBuildFmt.NWD: QT_TRANSLATE_NOOP("Constant", "novelWriter Markup (.txt)"),
|
||||
nwBuildFmt.STD_MD: QT_TRANSLATE_NOOP("Constant", "Standard Markdown (.md)"),
|
||||
nwBuildFmt.EXT_MD: QT_TRANSLATE_NOOP("Constant", "Extended Markdown (.md)"),
|
||||
nwBuildFmt.J_HTML: QT_TRANSLATE_NOOP("Constant", "JSON + novelWriter HTML (.json)"),
|
||||
nwBuildFmt.J_NWD: QT_TRANSLATE_NOOP("Constant", "JSON + novelWriter Markup (.json)"),
|
||||
}
|
||||
BUILD_EXT = {
|
||||
nwBuildFmt.ODT: ".odt",
|
||||
nwBuildFmt.FODT: ".fodt",
|
||||
nwBuildFmt.HTML: ".html",
|
||||
nwBuildFmt.NWD: ".txt",
|
||||
nwBuildFmt.STD_MD: ".md",
|
||||
nwBuildFmt.EXT_MD: ".md",
|
||||
nwBuildFmt.J_HTML: ".json",
|
||||
nwBuildFmt.J_NWD: ".json",
|
||||
}
|
||||
|
||||
# END Class nwLabels
|
||||
|
||||
@@ -34,11 +34,12 @@ from pathlib import Path
|
||||
|
||||
from PyQt5.QtCore import QT_TRANSLATE_NOOP
|
||||
|
||||
from novelwriter.enum import nwBuildFmt
|
||||
from novelwriter.error import logException
|
||||
from novelwriter.common import checkUuid, isHandle, jsonEncode
|
||||
from novelwriter.constants import nwFiles, nwHeadFmt, nwLabels
|
||||
from novelwriter.constants import nwFiles, nwHeadFmt
|
||||
from novelwriter.core.item import NWItem
|
||||
from novelwriter.core.project import NWProject
|
||||
from novelwriter.error import logException
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -140,7 +141,7 @@ class BuildSettings:
|
||||
self._uuid = str(uuid.uuid4())
|
||||
self._path = Path.home()
|
||||
self._build = ""
|
||||
self._format = "odt"
|
||||
self._format = nwBuildFmt.ODT
|
||||
self._skipRoot = set()
|
||||
self._excluded = set()
|
||||
self._included = set()
|
||||
@@ -175,7 +176,7 @@ class BuildSettings:
|
||||
return self._build
|
||||
|
||||
@property
|
||||
def lastFormat(self) -> str:
|
||||
def lastFormat(self) -> nwBuildFmt:
|
||||
"""The last used build format."""
|
||||
return self._format
|
||||
|
||||
@@ -252,10 +253,10 @@ class BuildSettings:
|
||||
self._changed = True
|
||||
return
|
||||
|
||||
def setLastFormat(self, key: str):
|
||||
def setLastFormat(self, value: nwBuildFmt):
|
||||
"""Set the last used build format."""
|
||||
if key in nwLabels.BUILD_FORMATS:
|
||||
self._format = key
|
||||
if isinstance(value, nwBuildFmt):
|
||||
self._format = value
|
||||
self._changed = True
|
||||
return
|
||||
|
||||
@@ -377,7 +378,7 @@ class BuildSettings:
|
||||
"uuid": self._uuid,
|
||||
"path": str(self._path),
|
||||
"build": self._build,
|
||||
"format": self._format,
|
||||
"format": self._format.name,
|
||||
"settings": self._settings.copy(),
|
||||
"content": {
|
||||
"included": list(self._included),
|
||||
@@ -398,7 +399,11 @@ class BuildSettings:
|
||||
self.setBuildID(data.get("uuid", ""))
|
||||
self.setLastPath(data.get("path", None))
|
||||
self.setLastBuildName(data.get("build", ""))
|
||||
self.setLastFormat(data.get("format", "odt"))
|
||||
|
||||
buildFmt = str(data.get("build", ""))
|
||||
if buildFmt in nwBuildFmt.__members__:
|
||||
self.setLastFormat(nwBuildFmt[buildFmt])
|
||||
|
||||
if isinstance(included, list):
|
||||
self._included = set([h for h in included if isHandle(h)])
|
||||
if isinstance(excluded, list):
|
||||
|
||||
@@ -31,6 +31,7 @@ from pathlib import Path
|
||||
from PyQt5.QtGui import QFont, QFontInfo
|
||||
|
||||
from novelwriter import CONFIG
|
||||
from novelwriter.enum import nwBuildFmt
|
||||
from novelwriter.error import formatException
|
||||
from novelwriter.core.tomd import ToMarkdown
|
||||
from novelwriter.core.toodt import ToOdt
|
||||
@@ -106,15 +107,15 @@ class NWBuildDocument:
|
||||
self._queue.append(item.itemHandle)
|
||||
return
|
||||
|
||||
def iterBuild(self, path: Path, bFormat: str) -> Iterable[tuple[int, bool]]:
|
||||
def iterBuild(self, path: Path, bFormat: nwBuildFmt) -> Iterable[tuple[int, bool]]:
|
||||
"""Wrapper for builders based on format."""
|
||||
if bFormat in ("odt", "fodt"):
|
||||
if bFormat in (nwBuildFmt.ODT, nwBuildFmt.FODT):
|
||||
yield from self.iterBuildOpenDocument(path, bFormat == "fodt")
|
||||
elif bFormat in ("html", "jhtml"):
|
||||
elif bFormat in (nwBuildFmt.HTML, nwBuildFmt.J_HTML):
|
||||
yield from self.iterBuildHTML(path if bFormat == "html" else None)
|
||||
elif bFormat in ("md", "md+"):
|
||||
elif bFormat in (nwBuildFmt.STD_MD, nwBuildFmt.EXT_MD):
|
||||
yield from self.iterBuildMarkdown(path, bFormat == "md+")
|
||||
elif bFormat in ("nwd", "jnwd"):
|
||||
elif bFormat in (nwBuildFmt.NWD, nwBuildFmt.J_NWD):
|
||||
yield from self.iterBuildNovelWriter(path if bFormat == "nwd" else None)
|
||||
return
|
||||
|
||||
|
||||
@@ -171,3 +171,17 @@ class nwOutline(Enum):
|
||||
SYNOP = 16
|
||||
|
||||
# END Enum nwOutline
|
||||
|
||||
|
||||
class nwBuildFmt(Enum):
|
||||
|
||||
ODT = 0
|
||||
FODT = 1
|
||||
HTML = 2
|
||||
NWD = 3
|
||||
STD_MD = 4
|
||||
EXT_MD = 5
|
||||
J_HTML = 6
|
||||
J_NWD = 7
|
||||
|
||||
# END Enum nwBuildFormat
|
||||
|
||||
@@ -36,10 +36,11 @@ from PyQt5.QtWidgets import (
|
||||
)
|
||||
|
||||
from novelwriter import CONFIG
|
||||
from novelwriter.enum import nwBuildFmt
|
||||
from novelwriter.common import makeFileNameSafe
|
||||
from novelwriter.constants import nwLabels
|
||||
from novelwriter.core.docbuild import NWBuildDocument
|
||||
from novelwriter.core.item import NWItem
|
||||
from novelwriter.core.docbuild import NWBuildDocument
|
||||
from novelwriter.core.buildsettings import BuildSettings
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@@ -91,9 +92,9 @@ class GuiManuscriptBuild(QDialog):
|
||||
self.listFormats = QListWidget()
|
||||
self.listFormats.setIconSize(QSize(iPx, iPx))
|
||||
current = None
|
||||
for key, (_, label) in nwLabels.BUILD_FORMATS.items():
|
||||
for key in nwBuildFmt:
|
||||
item = QListWidgetItem()
|
||||
item.setText(label)
|
||||
item.setText(nwLabels.BUILD_FMT[key])
|
||||
item.setData(self.D_KEY, key)
|
||||
self.listFormats.addItem(item)
|
||||
if key == self._build.lastFormat:
|
||||
@@ -275,7 +276,7 @@ class GuiManuscriptBuild(QDialog):
|
||||
def _runBuild(self) -> bool:
|
||||
"""Run the currently selected build."""
|
||||
selFormat = self._getSelectedFormat()
|
||||
if not selFormat or selFormat not in nwLabels.BUILD_FORMATS:
|
||||
if not isinstance(selFormat, nwBuildFmt):
|
||||
return False
|
||||
|
||||
lastName = self.buildName.text().strip()
|
||||
@@ -283,7 +284,7 @@ class GuiManuscriptBuild(QDialog):
|
||||
self._doResetBuildName()
|
||||
|
||||
lastPath = self._build.lastPath
|
||||
selExt = nwLabels.BUILD_FORMATS[selFormat][0]
|
||||
selExt = nwLabels.BUILD_EXT[selFormat]
|
||||
selName = Path(makeFileNameSafe(lastName)).with_suffix(selExt)
|
||||
|
||||
self.buildProgress.setValue(0)
|
||||
@@ -308,11 +309,11 @@ class GuiManuscriptBuild(QDialog):
|
||||
|
||||
return True
|
||||
|
||||
def _getSelectedFormat(self) -> str | None:
|
||||
def _getSelectedFormat(self) -> nwBuildFmt | None:
|
||||
"""Get the currently selected format."""
|
||||
items = self.listFormats.selectedItems()
|
||||
if items and isinstance(items[0], QListWidgetItem):
|
||||
return str(items[0].data(self.D_KEY))
|
||||
return items[0].data(self.D_KEY)
|
||||
return None
|
||||
|
||||
def _saveSettings(self):
|
||||
|
||||
Reference in New Issue
Block a user