Double2emspace.py: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
No edit summary
Line 1: Line 1:
[[Category:Scripts]]
[[Category:Scripts]]
{{Scripting Index}}
{{Scripting Index}}
Here is a script coming out of a discussion on the mailing list. The question was how to search and replace double spaces with single spaces. This led to various suggestions, a simple one being to use the Search/Replace function in Scribus.
Some side comments in the thread were about the propriety of double versus single spaces between sentences. What you can easily find in a web search is that, prior to the invention of typewriters, publishers came with their own conventions or styles as to dealing with spacing and other typographic features. One convention was to use an em space between sentences. When typewriters came along, presumably in a form of emulation, it was recommended to put two spaces between sentences in a paragraph.
Currently, it has become a much more common practice, especially outside the US, and certainly on the internet to only use single spaces, thus there is some utility of the following script. If you truly wanted to have em spaces between sentences, you might want to add logic that also includes converting single spaces between sentences to em spaces. This is a bit tricky, since then you must begin with the ends of sentences, which would include periods, question marks, exclamation points, and maybe even quotation marks. So a period followed by a space converts the space to em space, then any subsequent space would be deleted.
Here is the script, and after it you can see the outcome after using it.
==double2emspace.py==
==double2emspace.py==
<syntaxhighlight lang='python'>
<syntaxhighlight lang='python'>
Line 90: Line 97:
scribus.messageBox("Finished", "That should do it!",icon=0,button1=1)
scribus.messageBox("Finished", "That should do it!",icon=0,button1=1)
</syntaxhighlight>
</syntaxhighlight>
==Discussion==

Revision as of 02:35, 9 November 2014

This article is part of the Scripts series.

Here is a script coming out of a discussion on the mailing list. The question was how to search and replace double spaces with single spaces. This led to various suggestions, a simple one being to use the Search/Replace function in Scribus.

Some side comments in the thread were about the propriety of double versus single spaces between sentences. What you can easily find in a web search is that, prior to the invention of typewriters, publishers came with their own conventions or styles as to dealing with spacing and other typographic features. One convention was to use an em space between sentences. When typewriters came along, presumably in a form of emulation, it was recommended to put two spaces between sentences in a paragraph.

Currently, it has become a much more common practice, especially outside the US, and certainly on the internet to only use single spaces, thus there is some utility of the following script. If you truly wanted to have em spaces between sentences, you might want to add logic that also includes converting single spaces between sentences to em spaces. This is a bit tricky, since then you must begin with the ends of sentences, which would include periods, question marks, exclamation points, and maybe even quotation marks. So a period followed by a space converts the space to em space, then any subsequent space would be deleted.

Here is the script, and after it you can see the outcome after using it.

double2emspace.py

# -*- coding: utf-8 -*-
# File: double2emspace.py - convert double spaces to emspace or single space
# © 2014.11.08 Gregory Pittman
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
"""
USAGE
 
You must have a document open, and a text frame selected.
There is one dialog, which asks you if you wish to replace double spaces
with an em space. The default choice is Yes, any other input causes a double
space to convert to a single space.

"""
import scribus
 
if scribus.haveDoc():
    c = 0
 
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)


emspace = u"\u2003"
replace_char = " "

selection = scribus.valueDialog("Replace Character", 'Replace with em space?\n Anything but Yes will use normal space', 'Yes')
if (selection == 'Yes'):
  replace_char = emspace
  
prevchar = ''
 
while 1:
    if ((c == contents) or (c > contents)): break
    if ((c + 1) > contents - 1):
        nextchar = 'emspace'
    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

    if (char == ' '):

	if (prevchar == ' '):
	    scribus.selectText(c-1, 2, textbox)
	    scribus.deleteText(textbox)
	    scribus.insertText(replace_char, c-1, textbox)
	    char = replace_char
	  
	else:
	    c += 1

    else:
	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)

Discussion