hgbook

diff en/examples/run-example @ 564:0d5935744f87

Switch from LaTeX to XML for examples.
author Bryan O'Sullivan <bos@serpentine.com>
date Mon Mar 09 21:39:23 2009 -0700 (2009-03-09)
parents d8913b7869b5
children 8a9c66da6fcb
line diff
     1.1 --- a/en/examples/run-example	Thu Jan 29 22:11:10 2009 -0800
     1.2 +++ b/en/examples/run-example	Mon Mar 09 21:39:23 2009 -0700
     1.3 @@ -18,23 +18,23 @@
     1.4  import tempfile
     1.5  import time
     1.6  
     1.7 -tex_subs = {
     1.8 -    '\\': '\\textbackslash{}',
     1.9 -    '{': '\\{',
    1.10 -    '}': '\\}',
    1.11 +xml_subs = {
    1.12 +    '<': '&lt;',
    1.13 +    '>': '&gt;',
    1.14 +    '&': '&amp;',
    1.15      }
    1.16  
    1.17  def gensubs(s):
    1.18      start = 0
    1.19      for i, c in enumerate(s):
    1.20 -        sub = tex_subs.get(c)
    1.21 +        sub = xml_subs.get(c)
    1.22          if sub:
    1.23              yield s[start:i]
    1.24              start = i + 1
    1.25              yield sub
    1.26      yield s[start:]
    1.27  
    1.28 -def tex_escape(s):
    1.29 +def xml_escape(s):
    1.30      return ''.join(gensubs(s))
    1.31          
    1.32  def maybe_unlink(name):
    1.33 @@ -53,6 +53,10 @@
    1.34              return p
    1.35      return None
    1.36          
    1.37 +def result_name(name):
    1.38 +    dirname, basename = os.path.split(name)
    1.39 +    return os.path.join(dirname, 'results', basename)
    1.40 +
    1.41  class example:
    1.42      shell = '/usr/bin/env bash'
    1.43      ps1 = '__run_example_ps1__ '
    1.44 @@ -231,12 +235,15 @@
    1.45                                  return 1
    1.46                              assert os.sep not in out
    1.47                              if ofp is not None:
    1.48 +                                ofp.write('</screen>\n')
    1.49                                  ofp.close()
    1.50                                  err |= self.rename_output(ofp_basename, ignore)
    1.51                              if out:
    1.52                                  ofp_basename = '%s.%s' % (self.name, out)
    1.53                                  read_hint = ofp_basename + ' '
    1.54 -                                ofp = open(ofp_basename + '.tmp', 'w')
    1.55 +                                ofp = open(result_name(ofp_basename + '.tmp'),
    1.56 +                                           'w')
    1.57 +                                ofp.write('<screen>')
    1.58                              else:
    1.59                                  ofp = None
    1.60                          elif pi == 'ignore':
    1.61 @@ -249,13 +256,13 @@
    1.62                          # first, print the command we ran
    1.63                          if not hunk.startswith('#'):
    1.64                              nl = hunk.endswith('\n')
    1.65 -                            hunk = ('%s \\textbf{%s}' %
    1.66 +                            hunk = ('<prompt>%s</prompt> <userinput>%s</userinput>' %
    1.67                                      (prompts[ps],
    1.68 -                                     tex_escape(hunk.rstrip('\n'))))
    1.69 +                                     xml_escape(hunk.rstrip('\n'))))
    1.70                              if nl: hunk += '\n'
    1.71                          ofp.write(hunk)
    1.72                          # then its output
    1.73 -                        ofp.write(tex_escape(output))
    1.74 +                        ofp.write(xml_escape(output))
    1.75                      ps = newps
    1.76                  self.status('\n')
    1.77              except:
    1.78 @@ -268,6 +275,7 @@
    1.79                      ps, output = self.sendreceive('exit\n', read_hint)
    1.80                      if ofp is not None:
    1.81                          ofp.write(output)
    1.82 +                        ofp.write('</screen>\n')
    1.83                          ofp.close()
    1.84                          err |= self.rename_output(ofp_basename, ignore)
    1.85                      os.close(self.cfd)
    1.86 @@ -282,7 +290,7 @@
    1.87                      elif os.WIFSIGNALED(rc):
    1.88                          print >> sys.stderr, '(signal %s)' % os.WTERMSIG(rc)
    1.89                  else:
    1.90 -                    open(self.name + '.run', 'w')
    1.91 +                    open(result_name(self.name + '.run'), 'w')
    1.92                  return err
    1.93          finally:
    1.94              shutil.rmtree(tmpdir)
    1.95 @@ -301,13 +309,13 @@
    1.96                      return True
    1.97              return False
    1.98  
    1.99 -        oldname = base + '.out'
   1.100 -        tmpname = base + '.tmp'
   1.101 -        errname = base + '.err'
   1.102 +        oldname = result_name(base + '.out')
   1.103 +        tmpname = result_name(base + '.tmp')
   1.104 +        errname = result_name(base + '.err')
   1.105          errfp = open(errname, 'w+')
   1.106          for line in open(tmpname):
   1.107              errfp.write(mangle_re.sub('', line))
   1.108 -        os.rename(tmpname, base + '.lxo')
   1.109 +        os.rename(tmpname, result_name(base + '.lxo'))
   1.110          errfp.seek(0)
   1.111          try:
   1.112              oldfp = open(oldname)
   1.113 @@ -374,8 +382,7 @@
   1.114          names.sort()
   1.115          for name in names:
   1.116              if name == 'run-example' or name.startswith('.'): continue
   1.117 -            if name.endswith('.out') or name.endswith('~'): continue
   1.118 -            if name.endswith('.run'): continue
   1.119 +            if name.endswith('~'): continue
   1.120              pathname = os.path.join(path, name)
   1.121              try:
   1.122                  st = os.lstat(pathname)