En+emdash.py: Difference between revisions

From Scribus Wiki
Jump to navigation Jump to search
(Created page with "{{Scripting Index}} ==Creating en and em dashes from typewriter hyphens== This script is a stripped down and modified version of Autoquote.py. Instead of converting typewrite...")
 
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Scripting Index}}
{{Scripting Index}}
==Creating en and em dashes from typewriter hyphens==
==Creating en and em dashes from typewriter hyphens==
This script is a stripped down and modified version of Autoquote.py.
This script is a stripped down and modified version of Autoquote.py. It might be more accurate to say that it was inspired by Autoquote.py, since there is hardly anything left of the original in the script you see below.


Instead of converting typewriter quotes to typographic quotes, here we want to set up a system so that we can type and enter '-' when we just want a hyphen, '--' when we want an en-dash, and '---' when we want an em-dash. If you have something like '----' you'll probably end up with an em-dash followed by a hyphen.
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.
There are no dialogs except for the message box at the end.


==en+emdash.py==
==en+emdash.py==
<syntaxhighlight lang="python">
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: en+emdash.py - convert hyphens to en and em dashes
# © 2014.04.27 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 are no dialogs. The presumption is that you have encoded a single
hyphen to mean a single hyphen, two hyphens to mean an en-dash, and three
hyphens to mean an em-dash.
"""
import scribus
if scribus.haveDoc():
    c = 0
else:
    scribus.messageBox('Usage Error', 'You need a Document open', icon=scribus.ICON_NONE, button1=scribus.BUTTON_OK)
    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)
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
    if (char == '-'):
      if (prevchar == '-'):
if (nextchar == '-'):
  scribus.selectText(c-1, 3, textbox)
  scribus.deleteText(textbox)
  scribus.insertText(mdash, c-1, textbox)
  char = mdash
else:
  scribus.selectText(c-1, 2, textbox)
  scribus.deleteText(textbox)
  scribus.insertText(ndash, c-1, textbox)
  char = ndash
      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=scribus.ICON_NONE,button1=scribus.BUTTON_OK)
</syntaxhighlight>
==Usage of en dashes and em dashes==
There are many resources on usage of dashes. This [http://en.wikipedia.org/wiki/Dash Wikipedia article] is a reasonable resource, since it gives you a number of references so that you can get a broad sense of usage.
What you will find is that different publications or authorities have somewhat varying recommendations. In particular, there are differences between what is recommended in American versus British sources. To some extent, all agree that you should avoid overusage, and in addition use them in a consistent way.

Latest revision as of 13:34, 9 October 2016

This article is part of the Scripts series.

Creating en and em dashes from typewriter hyphens

This script is a stripped down and modified version of Autoquote.py. It might be more accurate to say that it was inspired by Autoquote.py, since there is hardly anything left of the original in the script you see below.

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

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# File: en+emdash.py - convert hyphens to en and em dashes
# © 2014.04.27 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 are no dialogs. The presumption is that you have encoded a single
hyphen to mean a single hyphen, two hyphens to mean an en-dash, and three
hyphens to mean an em-dash.
 
"""
import scribus
 
if scribus.haveDoc():
    c = 0
 
else:
    scribus.messageBox('Usage Error', 'You need a Document open', icon=scribus.ICON_NONE, button1=scribus.BUTTON_OK)
    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)
 
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

    if (char == '-'):

      if (prevchar == '-'):
	if (nextchar == '-'):
	  scribus.selectText(c-1, 3, textbox)
	  scribus.deleteText(textbox)
	  scribus.insertText(mdash, c-1, textbox)
	  char = mdash
	else:
	  scribus.selectText(c-1, 2, textbox)
	  scribus.deleteText(textbox)
	  scribus.insertText(ndash, c-1, textbox)
	  char = ndash
      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=scribus.ICON_NONE,button1=scribus.BUTTON_OK)

Usage of en dashes and em dashes

There are many resources on usage of dashes. This Wikipedia article is a reasonable resource, since it gives you a number of references so that you can get a broad sense of usage.

What you will find is that different publications or authorities have somewhat varying recommendations. In particular, there are differences between what is recommended in American versus British sources. To some extent, all agree that you should avoid overusage, and in addition use them in a consistent way.