Autoquote2

I have changed the logic of the original, Autoquote.py, so that it is much simpler, and therefore probably easier to tweak to your needs. Here is the basic process for each kind of typographic quotation:


 * I have created a couple of flags, doublequote_flag and singlequote_flag, which merely denote whether you are outside or inside a set of quotation marks.
 * If you are outside, the next quote is a leading quote, if inside, the next is a following quote. Since the single and double flags are separate, nested quotes are possible, but only one layer. In other words, for example, you will not get a good result if you put double quotes inside single quotes inside double quotes. I think this is an unlikely occurrence, and suggests you're overusing quotes.
 * There is also the addition of an apostrophe, for contractions like don't or d'etat. The apostrophe is a single follow quote. The test is whether the previous and next character are both alphanumeric.

Something not handled is a contraction like  'twas or  'tween. Also not handled at this time is the single guillemet, if this is used in nested quotes, the script can be modified. Also note that in French, a space is added after the leading double guillemets and before following double guillemets.

The Script
""" USAGE
 * 1) !/usr/bin/env python
 * 2) -*- coding: utf-8 -*-
 * 3) File: Autoquote2.py - changes typewriter quotes to typographic quotes
 * 4) © 2010.12.25 Gregory Pittman
 * 5) This program is free software; you can redistribute it and/or modify
 * 6) it under the terms of the GNU General Public License as published by
 * 7) the Free Software Foundation; either version 2 of the License, or
 * 8) (at your option) any later version.

You must have a document open, and a text frame selected. There will be a valueDialog asking for your language for the quotes, the default is 'en', but change the default to suit your needs. Detected errors shut down the script with an appropriate message.

""" import scribus import string

if scribus.haveDoc: c = 0 lang = scribus.valueDialog("Choose by language or country", 'Language: af, be, ch, de, en, es, et, fi, fr,\n hu, is, lt, mk, nl, pl, ru, se, sk, sl, sq and uk\n are current choices','en') if (lang == 'en'): lead_double = u"\u201c" follow_double = u"\u201d" lead_single = u"\u2018" follow_single = u"\u2019" elif (lang == 'de'): lead_double = u"\u201e" follow_double = u"\u201c" lead_single = u"\u2019" follow_single = u"\u201a" elif (lang == 'fr'): lead_double = u"\u00ab" + " " follow_double = " " + u"\u00bb" lead_single = u"\u2018" follow_single = u"\u2019" elif (lang == 'pl'): lead_double = u"\u201e" follow_double = u"\u201d" lead_single = u"\u201a" follow_single = u"\u2019" elif ((lang == 'se') or (lang == 'fi')): lead_double = u"\u201d" follow_double = u"\u201d" lead_single = u"\u2019" follow_single = u"\u2019" elif (lang == 'af'): lead_double = u"\u201c" follow_double = u"\u201d" lead_single = u"\u2018" follow_single = u"\u2019" elif (lang == 'sq'): lead_double = u"\u201e" follow_double = u"\u201c" lead_single = u"\u2018" follow_single = u"\u2019" elif ((lang == 'be') or (lang == 'ch') or (lang == 'uk') or (lang == 'ru')): lead_double = u"\u00ab" follow_double = u"\u00bb" lead_single = u"\u2039" follow_single = u"\u203a" elif (lang == 'uk'): lead_double = u"\u00ab" follow_double = u"\u00bb" lead_single = u"\u2039" follow_single = u"\u203a" elif (lang == 'es'): lead_double = u"\u00ab" follow_double = u"\u00bb" follow_double = u"\u201d" lead_single = u"\u2018" elif ((lang == 'lt') or (lang == 'mk') or (lang == 'is') or (lang == 'sk') or (lang == 'sl') or (lang == 'et')): lead_double = u"\u201e" follow_double = u"\u201c" lead_single = u"\u2019" follow_single = u"\u201a" elif ((lang == 'hu') or (lang == 'nl')): lead_double = u"\u201e" follow_double = u"\u201d" lead_single = u"\u00bb" follow_single = u"\u00ab" else: scribus.messageBox('Language Error', 'You need to choose an available language', icon=0, button1=1) sys.exit(2) else: scribus.messageBox('Usage Error', 'You need a Document open', icon=0, button1=1) sys.exit(2)

if scribus.selectionCount == 0: scribus.messageBox('Scribus - Usage Error',       "There is no object selected.\nPlease select a text frame and try again.",        scribus.ICON_WARNING, scribus.BUTTON_OK) sys.exit(2) if scribus.selectionCount > 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) doublequote_flag = scribus.valueDialog("Beginning Double Quotation State", "'outside' is normal. If you are running on a linked frame,\n enter 'inside' if this was indicated at last script end",'outside') singlequote_flag = scribus.valueDialog("Beginning Single Quotation State", "'outside' is normal. If you are running on a linked frame,\n enter 'inside' if this was indicated at last script end",'outside') if ((doublequote_flag != 'outside') and (doublequote_flag != 'inside')): doublequote_flag = 'outside' if ((singlequote_flag != 'outside') and (singlequote_flag != 'inside')): singlequote_flag = 'outside' apostrophe = u"\u2019" # we are using a follow single quote for apostrophe while c <= (contents -1): 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 ((ord(char) == 34) and (doublequote_flag == 'outside')): scribus.deleteText(textbox) scribus.insertText(lead_double, c, textbox) doublequote_flag = 'inside' elif ((ord(char) == 34) and (doublequote_flag == 'inside')): scribus.deleteText(textbox) scribus.insertText(follow_double, c, textbox) doublequote_flag = 'outside' if ((ord(char) == 39) and (c == 0)): scribus.deleteText(textbox) scribus.insertText(lead_single, c, textbox) singlequote_flag = 'inside' elif (ord(char) == 39): if ((prevchar.isalnum) and (nextchar.isalnum)): scribus.deleteText(textbox) scribus.insertText(apostrophe, c, textbox) elif (singlequote_flag == 'inside'): scribus.deleteText(textbox) scribus.insertText(follow_single, c, textbox) singlequote_flag = 'outside' elif (singlequote_flag == 'outside'): scribus.deleteText(textbox) scribus.insertText(lead_single, c, textbox) singlequote_flag = 'inside' c += 1 prevchar = char

scribus.setRedraw(1) scribus.docChanged(1) endmessage = 'Successfully ran script\nThe final state of Double Quotes: '+doublequote_flag+ '\nThe final state of Single Quotes: '+singlequote_flag # Change this message to your liking scribus.messageBox("Finished", endmessage,icon=0,button1=1)