hgbook / en / fblinks


import errno
import os
import re
import sys

hg_id = sys.argv[1]

dest_dir = sys.argv[2]

empty_re = re.compile(r'^\s*$')
line_re = re.compile(r'^(\w+)(.*)')

except OSError, err:
    if err.errno != errno.EEXIST:

def feedback(name, text, ctx_id):
    return r'\marginpar{\scriptsize \href{http://demesne:8000/book/feedback/submit/%s/%s/%d/}{Feedback}}' % (hg_id, name, ctx_id)

ctxs = {}
    cfp = open(os.path.join(dest_dir, 'rev-' + hg_id + '.ctx'), 'r+')
    for line in cfp:
        f, l, c = line.split(':', 2)
        ctxs[(f, int(l))] = c.strip()
except IOError, err:
    if err.errno != errno.ENOENT: raise
    cfp = open(os.path.join(dest_dir, 'rev-' + hg_id + '.ctx'), 'w+')

changes = 0

for name in sys.argv[3:]:
    if not name.endswith('.tex'):
    dest_name = os.path.join(dest_dir, name)
    ifp = open(name)
    ofp = open(dest_name, 'w')
    new_par = True
    line_num = 0
    par_num = 0
    for line in ifp:
        line_num += 1
        if new_par:
            m = line_re.match(line)
            if m:
                par_num += 1
                ls = line.strip()
                if ctxs.get((name, par_num)) != ls:
                    ctxs[(name, par_num)] = ls
                    changes += 1
                line = + feedback(name, line, par_num) +
                new_par = False
        elif not line.strip():
            new_par = True

if changes:
    print '%s: %d changes' % (, changes)
    ctxs = ctxs.items()
    for ((file, line), content) in ctxs:
        cfp.write('%s:%d: %s\n' % (file, line, content))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.