Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 5 | |
michael@0 | 6 | |
michael@0 | 7 | """ |
michael@0 | 8 | Fix references to source files of the form [LOCpath] |
michael@0 | 9 | so that they are relative to a given source directory. |
michael@0 | 10 | |
michael@0 | 11 | Substitute the DOT-generated image map into the document. |
michael@0 | 12 | """ |
michael@0 | 13 | |
michael@0 | 14 | import os, sys, re |
michael@0 | 15 | |
michael@0 | 16 | (srcdir, ) = sys.argv[1:] |
michael@0 | 17 | srcdir = os.path.realpath(srcdir) |
michael@0 | 18 | |
michael@0 | 19 | f = re.compile(r'\[LOC(.*?)\]') |
michael@0 | 20 | |
michael@0 | 21 | def replacer(m): |
michael@0 | 22 | file = m.group(1) |
michael@0 | 23 | file = os.path.realpath(file) |
michael@0 | 24 | if not file.startswith(srcdir): |
michael@0 | 25 | raise Exception("File %s doesn't start with %s" % (file, srcdir)) |
michael@0 | 26 | |
michael@0 | 27 | file = file[len(srcdir) + 1:] |
michael@0 | 28 | return file |
michael@0 | 29 | |
michael@0 | 30 | s = re.compile(r'\[MAP(.*?)\]') |
michael@0 | 31 | |
michael@0 | 32 | def mapreplace(m): |
michael@0 | 33 | file = m.group(1) |
michael@0 | 34 | c = open(file).read() |
michael@0 | 35 | return c |
michael@0 | 36 | |
michael@0 | 37 | for line in sys.stdin: |
michael@0 | 38 | line = f.sub(replacer, line) |
michael@0 | 39 | line = s.sub(mapreplace, line) |
michael@0 | 40 | |
michael@0 | 41 | sys.stdout.write(line) |