OpenOffice.org ODF, Python and XML
Let's take fix1.py and make an easy modification. Whenever two hyphens appear, replace them with the em dash. Then, when we're done, write the XML to stdout—that's exactly what the shell script (fixit.sh) expects.
We'll specify the em dash by giving its hex value; to find it, locate the em dash in OpenOffice.org Writer's Insert→Special Character dialog (Figure 8).
When I select the long dash (the em dash), its Unicode value appears in the lower-right corner, where I've put a purple ellipse; that's the value to put into the string in place of the double hyphens. Let's call this script fix2.py:
#!/usr/bin/python -tt import xml.dom.minidom import sys DEBUG = 1 def dprint(what): if DEBUG == 0: return sys.stderr.write(what.encode('ascii', 'replace') + '\n') emDash=u'\u2014' def fixdata(td, depth): dprint("depth=%d: childNode: %s" % (depth, td.data)) # OK, so '--' becomes em dash everywhere td.data = td.data.replace('--', emDash) def handle_xml_tree(aNode, depth): if aNode.hasChildNodes(): for kid in aNode.childNodes: handle_xml_tree(kid, depth+1) else: if 'data' in dir(aNode): fixdata(aNode, depth) def doit(argv): doc = xml.dom.minidom.parse(argv) handle_xml_tree(doc, 0) sys.stdout.write(doc.toxml('utf-8')) if __name__ == "__main__": doit(sys.argv)
Notice how easy Python makes it to replace a pattern in a string. Strings in recent Python versions have a built-in method, replace, that causes one substring to be replaced by another:
td.data = td.data.replace('--', emDash)
Let's plug fix2.py into fixit.sh to see how well it works:
% ln -sf fix2.py fixit.py % ./fixit.sh ex3.odt ex3-1.odt depth=5: childNode: The ?en? dash ? is too short depth=5: childNode: The ?em? dash ? which is longer ? is what we need. depth=5: childNode: And two hyphens -- ugly -- should be turned into ?em? dashes. depth=5: childNode: This line has "straight double quotes" depth=5: childNode: These 'single quotes' aren't pretty. % oowriter ex3-1.odt %
Success! Now for the rest. Besides the double hyphen, we want to change the en dash into an em dash. That syntax is just like the double hyphen replacement.
Replacing straight quotes with curly ones is more complicated though, because we have to decide between a starting double quote and an ending double-quote character. How to tell? Well, if the quote character is at the start of the string, and there's a nonspace character afterward, it's a left (or start of quote) curly quote. Ditto if there's a blank before it and a nonspace afterward.
That's the easy way to describe it. We could code it like that, or we could simply write a regular expression. I looked at the section titled “re -- Regular expression operations” in Chapter 4 of Python's library documentation and eventually came up with this:
sDpat = re.compile(r'(\A|(?<=\s))"(?=\S)', re.U)
Let me explain this left to right. We are creating sDpat, the pattern for a starting double quote or Starting Double-quote PATtern. We do that by calling the method compile in the re module (for regular expressions). That analyzes the pattern once and creates a regular expression object. We'll use sDpat to match straight double quotes that should be turned into nice curly quotes at the start of a quotation.
Now, about the pattern—the pattern contains a double-quote character (") so we delimit it with single quotes, 'like this'. Also, we'll pass some escapes (such as \A and \s) to re.compile, so let's make this a raw string by putting an r in front of it.
(A little explanation for Perl users: in Python, \ escapes are interpolated except in raw strings, whether single-quoted or double-quoted; the delimiters don't affect interpolation as they do in Perl.)
We can see how raw strings work by using Python's shell:
>>> print 'normal string: \n is a newline' normal string: is a newline >>> print r'raw string: \n is not a newline' raw string: \n is not a newline >>>
So, what's in that raw string? It consists of three parts:
The part before the quote character (\A|(?<=\s)). What we are doing is matching something (the '"' in this case), but only if it occurs at the beginning of the string or if it's preceded by a whitespace character. The \A means “match beginning of the string”, the | means “or” and (?<=\s) means “match if immediately preceded by whitespace (a blank, tab or newline), but don't include that whitespace itself in the match”. The enclosing parentheses denote grouping.
The straight double quote itself: ". That's what we're matching.
The part after the '"': (?=\S). What we're doing is adding another condition—that the quote character be followed by a non-whitespace character.
If all three conditions are met—that is, if a quote is there (condition 2), and it's either at the start of the string or preceded by whitespace (condition 1), and it's followed by some non-whitespace character (condition 3), we want to replace it by an opening double-quote character.
Besides the pattern, you also can pass flags to re.compile. We pass re.U to make certain escapes dependent on the Unicode character database. Because we're parsing a Unicode string, I think we want that.
Let's call this fix3.py:
#!/usr/bin/python -tt import xml.dom.minidom import sys import re # new in fix3.py DEBUG = 1 def dprint(what): if DEBUG == 0: return sys.stderr.write(what.encode('ascii', 'replace') + '\n') emDash=u'\u2014' enDash=u'\u2013' # new in fix3.py sDquote=u'\u201c' # new in fix3.py # sDpat: pattern for starting dbl quote, as # "Go! <-- the quote there # We look for it either at start (\A) or # after a space (\s), and we want it to be # followed by a non-space sDpat = re.compile(r'(\A|(?<=\s))"(?=\S)', re.U) # new in fix3.py def fixdata(td, depth): dprint("depth=%d: childNode: %s" % (depth, td.data)) # OK, so '--' becomes em dash everywhere td.data = td.data.replace('--', emDash) # Change 'en' dash to 'em' dash td.data = td.data.replace(enDash , emDash) # new in fix3.py # Make a nice starting curly-quote # new in fix3.py td.data = sDpat.sub(sDquote, td.data) # new in fix3.py def handle_xml_tree(aNode, depth): if aNode.hasChildNodes(): for kid in aNode.childNodes: handle_xml_tree(kid, depth+1) else: if 'data' in dir(aNode): fixdata(aNode, depth) def doit(argv): doc = xml.dom.minidom.parse(argv) handle_xml_tree(doc, 0) sys.stdout.write(doc.toxml('utf-8')) if __name__ == "__main__": doit(sys.argv)
Note that the syntax for replacing a regular expression differs from that of substring replacement: we use the sub (substitute) method of the regular expression object (sDpat in this case):
td.data = sDpat.sub(sDquote, td.data)
Here we're taking td.data, the data in this particular node in the XML tree, looking for the regular expression specified by sDpat, and replacing whatever matched it (the straight " character in the appropriate context) with the starting double quote, sDquote.
Now, if we try fixit.sh with fix3.py as the lower-level program:
% ln -sf fix3.py fixit.py % ./fixit.sh ex3.odt ex3-2.odt depth=5: childNode: The ?en? dash ? is too short depth=5: childNode: The ?em? dash ? which is longer ? is what we need. depth=5: childNode: And two hyphens -- ugly -- should be turned into ?em? dashes. depth=5: childNode: This line has "straight double quotes" depth=5: childNode: These 'single quotes' aren't pretty. % oowriter ex3-2.odt %
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- The Death of RoboVM
- Server Hardening
- The Humble Hacker?
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- EnterpriseDB's EDB Postgres Advanced Server and EDB Postgres Enterprise Manager
- The US Government and Open-Source Software
- ACI Worldwide's UP Retail Payments
- Open-Source Project Secretly Funded by CIA
- Varnish Software's Hitch
- New Container Image Standard Promises More Portable Apps
In modern computer systems, privacy and security are mandatory. However, connections from the outside over public networks automatically imply risks. One easily available solution to avoid eavesdroppers’ attempts is SSH. But, its wide adoption during the past 21 years has made it a target for attackers, so hardening your system properly is a must.
Additionally, in highly regulated markets, you must comply with specific operational requirements, proving that you conform to standards and even that you have included new mandatory authentication methods, such as two-factor authentication. In this ebook, I discuss SSH and how to configure and manage it to guarantee that your network is safe, your data is secure and that you comply with relevant regulations.Get the Guide