Vitavonni

Wed, 28 Dec 2005

Automagic fixing quotation marks for LyX

My mother uses LyX (and thus, LaTeX) to layout her books. She doesn't need much help, but does almost everything herself. Except she was lacking an editor which could replace the quotation marks appropriately, because they are handled somewhat special in LyX (you need an editor which can replace by multiline text, such as VIM).

So every now and then, mostly when she's started layouting a new book, she'll ask me to fix the quotation marks in a lyx file for her.

I decided to write a small python script for it, since she has python installed on her box anyway (also on Windows, although she also has Linux on it).

Maybe someone else has the same problems, so here you go:

#!/usr/bin/python
import re, sys
 
# Use full regexp power to detect as many " properly as possible...
# Note that this is a lot more sophisticated than just \b" and "\b
# and will be able to handle all the 'something ...", said foo' cases, too!
# But yeah, you can do even more, and maybe separate it into more regexps...
qrp1 = re.compile(r'(^\s*|\b[:]?\s+[.:,;?!\-\(]*)"(\b)', re.M)
qrp2 = re.compile(r'(\b[.:,;?!\-\)]*|\b \.\.\.)"([.:,;?!\-\)]*(\s+\b|\s*$))', re.M)
 
# These are for >>-style quotes, german bracketing. If you want other
# quotes, adjust to your style yourself (hint: look at your .lyx file)
q1 = r"""
\\begin_inset Quotes ald
\\end_inset
"""
q2 = r"""
\\begin_inset Quotes ard
\\end_inset
"""
 
# Iterate over all input files...
for name in sys.argv[1:]:
        # Load input file
        fi = file(name, "r")
        data = fi.read()
        fi.close()
        # Replace quotes
        data = qrp1.sub("\\1"+q1+"\\2", data)
        data = qrp2.sub("\\1"+q2+"\\2", data)
        # Write output file with new name
        newname = name + ".quotes.lyx"
        fi2 = file(newname, "w")
        fi2.write(data)
        fi2.close()

[category: /en | Permalink]
Menu
[planet.debian]
[planet.xmlhack]
[planet SELinux]
[munichblogs]
[email]
[RSS 2 feed]
[English RSS 2]
Categories
< December 2005 >
SuMoTuWeThFrSa
     1 2 3
4 5 6 7 8 910
11121314151617
18192021222324
25262728293031
Archives
2010-Feb
2010-Jan
2009-Dec
2009-Nov
2009-Oct
2009-Sep
2009-Aug
2009-Jul
2009-Jun
2009-May
2009-Apr
2009-Mar
2009-Feb
2009-Jan
2008-Dec
2008-Nov
2008-Oct
2008-Sep
2008-Aug
2008-Jul
2008-May
2008-Apr
2008-Mar
2008-Feb
2008-Jan
2007-Dec
2007-Nov
2007-Oct
2007-Sep
2007-Aug
2007-Jul
2007-Jun
2007-May
2007-Apr
2007-Mar
2007-Feb
2007-Jan
2006-Dec
2006-Nov
2006-Oct
2006-Sep
2006-Aug
2006-Jul
2006-Jun
2006-May
2006-Apr
2006-Mar
2006-Feb
2006-Jan
2005-Dec
2005-Nov
2005-Oct
2005-Sep
2005-Aug
2005-Jul
2005-Jun
2005-May
2005-Apr
2005-Mar
2005-Feb
2005-Jan
2004-Dec
2004-Nov
2004-Oct
2004-Sep
2004-Aug
2004-Jul
Other links:
Swing and the City - Lindy Hop in Munich