home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 November / CPNL0711.ISO / beeld / teken / scribus-1.3.3.9-win32-install.exe / share / samples / quote.py < prev    next >
Text File  |  2005-02-22  |  2KB  |  76 lines

  1. #!/usr/bin/env python
  2. # -*- coding: iso-8859-1 -*-
  3.  
  4. """ This script changes quotation marks from " " to french style """
  5.  
  6. import sys
  7.  
  8. try:
  9.     from scribus import *
  10. except ImportError:
  11.     print "This script only runs from within Scribus."
  12.     sys.exit(1)
  13.  
  14. import re
  15.  
  16. TITLE = "Text quoting"
  17.  
  18. # These need to be declared as unicode strings until some
  19. # charset issues in the scripter are worked out.
  20. QUOTE_START = u""
  21. QUOTE_END = u""
  22.  
  23. def quote(textobj):
  24.     quoted_re = re.compile('"[^"]*"')
  25.     try:
  26.         text = getText(textobj)
  27.     except WrongFrameTypeError:
  28.         messageBox("quote.py", "Cannot quote text in a non-text frame", ICON_INFORMATION);
  29.         sys.exit(1)
  30.     if len(text) == 0:
  31.         return 0    # We can't very well change anything in an empty frame
  32.     count = 0
  33.     i = 0
  34.     selectText(0, 0, textobj)
  35.     while i < len(text):
  36.         match = quoted_re.match(text[i:])
  37.         if match:
  38.             end = match.end()
  39.             selectText(i, 1, textobj)
  40.             deleteText(textobj)
  41.             insertText(QUOTE_START, i, textobj)
  42.             selectText(i + end - 1, 1, textobj)
  43.             deleteText(textobj)
  44.             insertText(QUOTE_END, i + end - 1, textobj)
  45.             count += 1
  46.             i = i + end
  47.         else:
  48.             i = i + 1
  49.     return count
  50.  
  51.  
  52. def main():
  53.     changed = 0
  54.     sel_count = selectionCount()
  55.     if sel_count:
  56.         for i in range(sel_count):
  57.             changed += quote(getSelectedObject(i))
  58.     else:
  59.         for page in range(pageCount()):
  60.             gotoPage(page)
  61.             for obj in getAllObjects():
  62.                 changed += quote(obj)
  63.     messageBox(TITLE, "%s quotations changed" % changed,
  64.                ICON_INFORMATION, BUTTON_OK)
  65.  
  66. if __name__ == '__main__':
  67.     if haveDoc():
  68.         try:
  69.             setRedraw(False)
  70.             main()
  71.         finally:
  72.             setRedraw(True)
  73.             redrawAll()
  74.     else:
  75.         messageBox(TITLE, "No document open", ICON_WARNING, BUTTON_OK)
  76.