En+emdash.py
Jump to navigation
Jump to search
Creating en and em dashes from typewriter hyphens
This script is a stripped down and modified version of Autoquote.py.
Instead of converting typewriter quotes to typographic quotes, here we want to set up a system so that we can type and enter '-' (a single hyphen) when we just want a hyphen, '--' (2 hyphens) when we want an en-dash, and '---' (3 hyphens) when we want an em-dash. If you have something like '----' (4 hyphens) you'll probably end up with an em-dash followed by a hyphen.
There are no dialogs except for the message box at the end.
en+emdash.py
1:
scribus.messageBox('Scribus - Usage Error',
"You have more than one object selected.\nPlease select one text frame and try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
sys.exit(2)
textbox = scribus.getSelectedObject()
pageitems = scribus.getPageItems()
boxcount = 1
for item in pageitems:
if (item[0] == textbox):
if (item[1] != 4):
scribus.messageBox('Scribus - Usage Error', "This is not a textframe. Try again.", scribus.ICON_WARNING, scribus.BUTTON_OK)
sys.exit(2)
contents = scribus.getTextLength(textbox)
ndash = u"\u2013"
mdash = u"\u2014"
prevchar = ''
while 1:
if ((c == contents) or (c > contents)): break
if ((c + 1) > contents - 1):
nextchar = ' '
else:
scribus.selectText(c+1, 1, textbox)
nextchar = scribus.getText(textbox)
scribus.selectText(c, 1, textbox)
char = scribus.getText(textbox)
if (len(char) != 1):
c += 1
continue
if (prevchar == chr(45)):
if (ord(char) == 45):
scribus.deleteText(textbox)
char = ndash
c -= 1
else:
scribus.deleteText(textbox)
scribus.insertText(chr(45) + char, c, textbox)
c += 1
elif (prevchar == ndash):
if (ord(char) == 45):
scribus.deleteText(textbox)
scribus.insertText(mdash, c, textbox)
char = mdash
else:
scribus.deleteText(textbox)
scribus.insertText(ndash, c, textbox)
c += 1
scribus.insertText(char, c, textbox)
else:
if (ord(char) == 45):
scribus.deleteText(textbox)
c -= 1
else:
scribus.deleteText(textbox)
scribus.insertText(char, c, textbox)
c += 1
prevchar = char
contents = scribus.getTextLength(textbox)
scribus.setRedraw(1)
scribus.docChanged(1)
scribus.messageBox("Finished", "That should do it!",icon=0,button1=1)