Skip to content

Store twine passage position information in modified .twee file #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ def loadPrefs(self):
'displayArrows' : True,
'createPassagePrompt' : True,
'importImagePrompt' : True,
'passageWarnings' : True
'passageWarnings' : True,
'saveTwinePosition' : False
}.iteritems():
if not sc.HasEntry(k):
if type(v) == str:
Expand Down
5 changes: 4 additions & 1 deletion prefframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, app, parent = None):
panel = wx.Panel(parent = self, id = wx.ID_ANY)
borderSizer = wx.BoxSizer(wx.VERTICAL)
panel.SetSizer(borderSizer)
panelSizer = wx.FlexGridSizer(14, 2, metrics.size('relatedControls'), metrics.size('relatedControls'))
panelSizer = wx.FlexGridSizer(15, 2, metrics.size('relatedControls'), metrics.size('relatedControls'))
borderSizer.Add(panelSizer, flag = wx.ALL, border = metrics.size('windowBorder'))

self.editorFont = wx.FontPickerCtrl(panel, style = wx.FNTP_FONTDESC_AS_LABEL)
Expand Down Expand Up @@ -67,6 +67,7 @@ def checkbox(self, name, label, panel=panel):
checkbox(self, "createPassagePrompt", 'Offer to create new passages for broken links')
checkbox(self, "importImagePrompt", 'Offer to import externally linked images')
checkbox(self, "passageWarnings", 'Warn about possible passage code errors')
checkbox(self, "saveTwinePosition", 'Save twine position data in twee source')

panelSizer.Add(wx.StaticText(panel, label = 'Normal Font'), flag = wx.ALIGN_CENTER_VERTICAL)
panelSizer.Add(self.editorFont)
Expand Down Expand Up @@ -96,6 +97,8 @@ def checkbox(self, name, label, panel=panel):
panelSizer.Add(self.importImagePrompt) # pylint: disable=no-member
panelSizer.Add((1,2))
panelSizer.Add(self.passageWarnings) # pylint: disable=no-member
panelSizer.Add((1,2))
panelSizer.Add(self.saveTwinePosition) # pylint: disable=no-member

panelSizer.Fit(self)
borderSizer.Fit(self)
Expand Down
6 changes: 4 additions & 2 deletions storyframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,12 @@ def exportSource(self, event=None):
path = dialog.GetPath()
tw = TiddlyWiki()

for widget in self.storyPanel.widgetDict.itervalues(): tw.addTiddler(widget.passage)
for widget in self.storyPanel.widgetDict.itervalues():
widget.passage.pos = widget.pos
tw.addTiddler(widget.passage)
dest = codecs.open(path, 'w', 'utf-8-sig', 'replace')
order = [widget.passage.title for widget in self.storyPanel.sortedWidgets()]
dest.write(tw.toTwee(order))
dest.write(tw.toTwee(order, self.app.config.ReadBool('saveTwinePosition')))
dest.close()
except:
self.app.displayError('exporting your source code')
Expand Down
41 changes: 36 additions & 5 deletions tiddlywiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ def __init__(self, author = 'twee'):
def hasTiddler(self, name):
return name in self.tiddlers

def toTwee(self, order = None):
def toTwee(self, order = None, saveTwinePosition=False):
"""Returns Twee source code for this TiddlyWiki.
The 'order' argument is a sequence of passage titles specifying the order
in which passages should appear in the output string; by default passages
are returned in arbitrary order.
If saveTwinePosition is True the twee source code will include metadata
with the location of the passage in the twine storypanel.
"""
tiddlers = self.tiddlers
if order is None:
order = tiddlers.keys()
return u''.join(tiddlers[i].toTwee() for i in order)
return u''.join(tiddlers[i].toTwee(saveTwinePosition) for i in order)

def read(self, filename):
try:
Expand Down Expand Up @@ -390,7 +392,30 @@ def initTwee(self, source):
tag_bits = meta_bits[1].split(' ')

for tag in tag_bits:
self.tags.append(tag.strip('[]'))
stripped_tag = tag.strip('[] ')
if not stripped_tag:
continue
self.tags.append(stripped_tag)

if len(meta_bits) > 2:
pos_bits = meta_bits[2].split(',')

for pos_key in pos_bits:
stripped_pos = pos_key.strip('[]')
keyval_bits = stripped_pos.split(':')
if len(keyval_bits) == 2:
key = keyval_bits[0].strip()
val = keyval_bits[1].strip()
if key == 'twine-pos-x':
try:
self.pos[0] = float(val)
except ValueError:
continue
elif key == 'twine-pos-y':
try:
self.pos[1] = float(val)
except ValueError:
continue

# and then the body text

Expand Down Expand Up @@ -511,8 +536,10 @@ def applyRot13(text):
)


def toTwee(self):
"""Returns a Twee representation of this tiddler."""
def toTwee(self, saveTwinePosition=False):
"""Returns a Twee representation of this tiddler. If saveTwinePosition is
True the location of the passagewidget will be stored in the twee source.
"""
output = u':: ' + self.title

if len(self.tags) > 0:
Expand All @@ -521,6 +548,10 @@ def toTwee(self):
output += tag + ' '
output = output.strip()
output += u']'
elif saveTwinePosition:
output += u' []'
if saveTwinePosition:
output += u'[twine-pos-x:%s, twine-pos-y:%s]' % (self.pos[0], self.pos[1])

output += u"\n" + self.text + u"\n\n\n"
return output
Expand Down