mirror of
https://github.com/olivierkes/manuskript.git
synced 2024-05-13 17:32:24 +12:00
Writes outline
This commit is contained in:
parent
c31681a724
commit
05715a26e0
|
@ -3,10 +3,12 @@
|
|||
|
||||
# The loadSave file calls the propper functions to load and save file
|
||||
# trying to detect the proper file format if it comes from an older version
|
||||
import zipfile
|
||||
|
||||
import manuskript.load_save.version_0 as v0
|
||||
import manuskript.load_save.version_1 as v1
|
||||
|
||||
|
||||
def saveProject(version=None):
|
||||
|
||||
if version == 0:
|
||||
|
@ -18,9 +20,31 @@ def saveProject(version=None):
|
|||
def loadProject(project):
|
||||
|
||||
# Detect version
|
||||
# FIXME
|
||||
# Is it a zip?
|
||||
isZip = False
|
||||
version = 0
|
||||
|
||||
try:
|
||||
zf = zipfile.ZipFile(project)
|
||||
isZip = True
|
||||
except zipfile.BadZipFile:
|
||||
isZip = False
|
||||
|
||||
# Does it have a VERSION in zip root?
|
||||
if isZip and "VERSION" in zf.namelist():
|
||||
version = int(zf.read("VERSION"))
|
||||
|
||||
# Zip but no VERSION: oldest file format
|
||||
elif isZip:
|
||||
version = 0
|
||||
|
||||
# Not a zip
|
||||
else:
|
||||
# FIXME
|
||||
pass
|
||||
|
||||
print("Detected file format version:", version)
|
||||
|
||||
if version == 0:
|
||||
v0.loadProject(project)
|
||||
else:
|
||||
|
|
|
@ -37,8 +37,9 @@ def saveProject():
|
|||
|
||||
files.append((saveStandardItemModelXML(mw.mdlFlatData),
|
||||
"flatModel.xml"))
|
||||
files.append((saveStandardItemModelXML(mw.mdlCharacter),
|
||||
"perso.xml"))
|
||||
print("ERROR: file format 0 does not save characters !")
|
||||
# files.append((saveStandardItemModelXML(mw.mdlCharacter),
|
||||
# "perso.xml"))
|
||||
files.append((saveStandardItemModelXML(mw.mdlWorld),
|
||||
"world.xml"))
|
||||
files.append((saveStandardItemModelXML(mw.mdlLabels),
|
||||
|
@ -145,36 +146,35 @@ def loadProject(project):
|
|||
loadStandardItemModelXML(mw.mdlLabels,
|
||||
files["labels.xml"], fromString=True)
|
||||
else:
|
||||
errors.append("perso.xml")
|
||||
errors.append("labels.xml")
|
||||
|
||||
if "status.xml" in files:
|
||||
loadStandardItemModelXML(mw.mdlStatus,
|
||||
files["status.xml"], fromString=True)
|
||||
else:
|
||||
errors.append("perso.xml")
|
||||
errors.append("status.xml")
|
||||
|
||||
if "plots.xml" in files:
|
||||
loadStandardItemModelXML(mw.mdlPlots,
|
||||
files["plots.xml"], fromString=True)
|
||||
else:
|
||||
errors.append("perso.xml")
|
||||
errors.append("plots.xml")
|
||||
|
||||
if "outline.xml" in files:
|
||||
mw.mdlOutline.loadFromXML(files["outline.xml"], fromString=True)
|
||||
else:
|
||||
errors.append("perso.xml")
|
||||
errors.append("outline.xml")
|
||||
|
||||
if "settings.pickle" in files:
|
||||
settings.load(files["settings.pickle"], fromString=True)
|
||||
else:
|
||||
errors.append("perso.xml")
|
||||
errors.append("settings.pickle")
|
||||
|
||||
return errors
|
||||
|
||||
|
||||
def loadFilesFromZip(zipname):
|
||||
"""Returns the content of zipfile as a dict of filename:content."""
|
||||
print(zipname)
|
||||
zf = zipfile.ZipFile(zipname)
|
||||
files = {}
|
||||
for f in zf.namelist():
|
||||
|
@ -286,4 +286,4 @@ def loadStandardItemModelXMLForCharacters(mdl, xml):
|
|||
info.value = ccol.text
|
||||
char.infos.append(info)
|
||||
|
||||
mdl.characters.append(char)
|
||||
mdl.characters.append(char)
|
||||
|
|
|
@ -13,7 +13,7 @@ from PyQt5.QtCore import Qt, QModelIndex
|
|||
from PyQt5.QtGui import QColor
|
||||
|
||||
from manuskript import settings
|
||||
from manuskript.enums import Character, World, Plot, PlotStep
|
||||
from manuskript.enums import Character, World, Plot, PlotStep, Outline
|
||||
from manuskript.functions import mainWindow, iconColor
|
||||
from lxml import etree as ET
|
||||
|
||||
|
@ -163,7 +163,7 @@ def saveProject(zip=None):
|
|||
# Characters (self.mdlCharacter)
|
||||
# In a character folder
|
||||
|
||||
path = os.path.join("characters", "{name}.txt") # Not sure wheter os.path allows this
|
||||
path = os.path.join("characters", "{name}.txt")
|
||||
_map = [
|
||||
(Character.name, "Name"),
|
||||
(Character.ID, "ID"),
|
||||
|
@ -203,7 +203,9 @@ def saveProject(zip=None):
|
|||
# Texts
|
||||
# In an outline folder
|
||||
|
||||
# TODO
|
||||
mdl = mw.mdlOutline
|
||||
for filename, content in exportOutlineItem(mdl.rootItem):
|
||||
files.append((filename, content))
|
||||
|
||||
# World (mw.mdlWorld)
|
||||
# Either in an XML file, or in lots of plain texts?
|
||||
|
@ -371,6 +373,82 @@ def addPlotItem(root, mdl, parent=QModelIndex()):
|
|||
return root
|
||||
|
||||
|
||||
def exportOutlineItem(root):
|
||||
"""
|
||||
Takes an outline item, and returns an array of (`filename`, `content`) sets, representing the whole tree
|
||||
of items converted to mmd.
|
||||
|
||||
@param root: OutlineItem
|
||||
@return: (str, str)
|
||||
"""
|
||||
r = []
|
||||
path = "outline"
|
||||
|
||||
k=0
|
||||
for child in root.children():
|
||||
spath = os.path.join(path, *outlineItemPath(child))
|
||||
k += 1
|
||||
|
||||
if child.type() == "folder":
|
||||
fpath = os.path.join(spath, "folder.txt")
|
||||
content = outlineToMMD(child)
|
||||
r.append((fpath, content))
|
||||
|
||||
elif child.type() in ["txt", "t2t"]:
|
||||
content = outlineToMMD(child)
|
||||
r.append((spath, content))
|
||||
|
||||
elif child.type() in ["html"]:
|
||||
# Convert first
|
||||
pass
|
||||
|
||||
else:
|
||||
print("Unknown type")
|
||||
|
||||
r += exportOutlineItem(child)
|
||||
|
||||
return r
|
||||
|
||||
def outlineItemPath(item):
|
||||
# Root item
|
||||
if not item.parent():
|
||||
return []
|
||||
else:
|
||||
name = "{ID}-{name}{ext}".format(
|
||||
ID=item.row(),
|
||||
name=slugify(item.title()),
|
||||
ext="" if item.type() == "folder" else ".{}".format(item.type())
|
||||
)
|
||||
return outlineItemPath(item.parent()) + [name]
|
||||
|
||||
def outlineToMMD(item):
|
||||
content = ""
|
||||
|
||||
# We don't want to write some datas (computed)
|
||||
exclude = [Outline.wordCount, Outline.goal, Outline.goalPercentage, Outline.revisions, Outline.text]
|
||||
# We want to force some data even if they're empty
|
||||
force = [Outline.compile]
|
||||
|
||||
for attrib in Outline:
|
||||
if attrib in exclude: continue
|
||||
val = item.data(attrib.value)
|
||||
if val or attrib in force:
|
||||
content += formatMetaData(attrib.name, str(val), 15)
|
||||
|
||||
content += "\n\n"
|
||||
content += item.data(Outline.text.value)
|
||||
|
||||
# Saving revisions
|
||||
# TODO
|
||||
# rev = item.revisions()
|
||||
# for r in rev:
|
||||
# revItem = ET.Element("revision")
|
||||
# revItem.set("timestamp", str(r[0]))
|
||||
# revItem.set("text", r[1])
|
||||
# item.append(revItem)
|
||||
|
||||
return content
|
||||
|
||||
def loadProject(project):
|
||||
"""
|
||||
Loads a project.
|
||||
|
|
|
@ -854,8 +854,13 @@ class outlineItem():
|
|||
text = text.lower() if not caseSensitive else text
|
||||
for c in columns:
|
||||
|
||||
if c == Outline.POV.value:
|
||||
searchIn = mainWindow.mdlCharacter.getCharacterByID(self.POV()).name()
|
||||
if c == Outline.POV.value and self.POV():
|
||||
c = mainWindow.mdlCharacter.getCharacterByID(self.POV())
|
||||
if c:
|
||||
searchIn = c.name()
|
||||
else:
|
||||
searchIn = ""
|
||||
print("Character POV not found:", self.POV())
|
||||
|
||||
elif c == Outline.status.value:
|
||||
searchIn = mainWindow.mdlStatus.item(toInt(self.status()), 0).text()
|
||||
|
|
Loading…
Reference in a new issue