Back to index

scribus-ng  1.3.4.dfsg+svn20071115
quote.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # -*- coding: iso-8859-1 -*-
00003 
00004 """ This script changes quotation marks from " " to french style """
00005 
00006 import sys
00007 
00008 try:
00009     from scribus import *
00010 except ImportError:
00011     print "This script only runs from within Scribus."
00012     sys.exit(1)
00013 
00014 import re
00015 
00016 TITLE = "Text quoting"
00017 
00018 # These need to be declared as unicode strings until some
00019 # charset issues in the scripter are worked out.
00020 QUOTE_START = u""
00021 QUOTE_END = u""
00022 
00023 def quote(textobj):
00024     quoted_re = re.compile('"[^"]*"')
00025     try:
00026         text = getText(textobj)
00027     except WrongFrameTypeError:
00028         messageBox("quote.py", "Cannot quote text in a non-text frame", ICON_INFORMATION);
00029         sys.exit(1)
00030     if len(text) == 0:
00031         return 0    # We can't very well change anything in an empty frame
00032     count = 0
00033     i = 0
00034     selectText(0, 0, textobj)
00035     while i < len(text):
00036         match = quoted_re.match(text[i:])
00037         if match:
00038             end = match.end()
00039             selectText(i, 1, textobj)
00040             deleteText(textobj)
00041             insertText(QUOTE_START, i, textobj)
00042             selectText(i + end - 1, 1, textobj)
00043             deleteText(textobj)
00044             insertText(QUOTE_END, i + end - 1, textobj)
00045             count += 1
00046             i = i + end
00047         else:
00048             i = i + 1
00049     return count
00050 
00051 
00052 def main():
00053     changed = 0
00054     sel_count = selectionCount()
00055     if sel_count:
00056         for i in range(sel_count):
00057             changed += quote(getSelectedObject(i))
00058     else:
00059         for page in range(pageCount()):
00060             gotoPage(page)
00061             for obj in getAllObjects():
00062                 changed += quote(obj)
00063     messageBox(TITLE, "%s quotations changed" % changed,
00064                ICON_INFORMATION, BUTTON_OK)
00065 
00066 if __name__ == '__main__':
00067     if haveDoc():
00068         try:
00069             setRedraw(False)
00070             main()
00071         finally:
00072             setRedraw(True)
00073             redrawAll()
00074     else:
00075         messageBox(TITLE, "No document open", ICON_WARNING, BUTTON_OK)