#require pygments serve $ cat <> $HGRCPATH > [extensions] > highlight = > [web] > pygments_style = friendly > highlightfiles = **.py and size('<100KB') > EOF $ hg init test $ cd test $ filterhtml () { > sed -e "s/class=\"k\"/class=\"kn\"/g" \ > -e "s/class=\"mf\"/class=\"mi\"/g" \ > -e "s/class=\"\([cs]\)[h12]\"/class=\"\1\"/g" > } create random Python file to exercise Pygments $ cat < primes.py > #!/usr/bin/env python > > """Fun with generators. Corresponding Haskell implementation: > > primes = 2 : sieve [3, 5..] > where sieve (p:ns) = p : sieve [n | n <- ns, mod n p /= 0] > """ > > from itertools import dropwhile, ifilter, islice, count, chain > > def primes(): > """Generate all primes.""" > def sieve(ns): > p = ns.next() > # It is important to yield *here* in order to stop the > # infinite recursion. > yield p > ns = ifilter(lambda n: n % p != 0, ns) > for n in sieve(ns): > yield n > > odds = ifilter(lambda i: i % 2 == 1, count()) > return chain([2], sieve(dropwhile(lambda n: n < 3, odds))) > > if __name__ == "__main__": > import sys > try: > n = int(sys.argv[1]) > except (ValueError, IndexError): > n = 10 > p = primes() > print "The first %d primes: %s" % (n, list(islice(p, n))) > EOF $ echo >> primes.py # to test html markup with an empty line just before EOF $ hg ci -Ama adding primes.py hg serve $ hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log $ cat hg.pid >> $DAEMON_PIDS hgweb filerevision, html $ (get-with-headers.py localhost:$HGPORT 'file/tip/primes.py') | filterhtml 200 Script output follows test: 06824edf55d0 primes.py

view primes.py @ 0:06824edf55d0 tip

a
author test
date Thu, 01 Jan 1970 00:00:00 +0000
parents
children
line wrap: on
line source
  #!/usr/bin/env python
  
  """Fun with generators. Corresponding Haskell implementation:
  
  primes = 2 : sieve [3, 5..]
      where sieve (p:ns) = p : sieve [n | n <- ns, mod n p /= 0]
  """
  
  from itertools import dropwhile, ifilter, islice, count, chain
  
  def primes():
      """Generate all primes."""
      def sieve(ns):
          p = ns.next()
          # It is important to yield *here* in order to stop the
          # infinite recursion.
          yield p
          ns = ifilter(lambda n: n % p != 0, ns)
          for n in sieve(ns):
              yield n
  
      odds = ifilter(lambda i: i % 2 == 1, count())
      return chain([2], sieve(dropwhile(lambda n: n < 3, odds)))
  
  if __name__ == "__main__":
      import sys
      try:
          n = int(sys.argv[1])
      except (ValueError, IndexError):
          n = 10
      p = primes()
      print "The first %d primes: %s" % (n, list(islice(p, n)))
  
hgweb fileannotate, html $ (get-with-headers.py localhost:$HGPORT 'annotate/tip/primes.py') | filterhtml 200 Script output follows test: primes.py annotate

annotate primes.py @ 0:06824edf55d0 tip

a
author test
date Thu, 01 Jan 1970 00:00:00 +0000
parents
children
rev   line source
test@0 1 #!/usr/bin/env python
test@0 2
test@0 3 """Fun with generators. Corresponding Haskell implementation:
test@0 4
test@0 5 primes = 2 : sieve [3, 5..]
test@0 6 where sieve (p:ns) = p : sieve [n | n <- ns, mod n p /= 0]
test@0 7 """
test@0 8
test@0 9 from itertools import dropwhile, ifilter, islice, count, chain
test@0 10
test@0 11 def primes():
test@0 12 """Generate all primes."""
test@0 13 def sieve(ns):
test@0 14 p = ns.next()
test@0 15 # It is important to yield *here* in order to stop the
test@0 16 # infinite recursion.
test@0 17 yield p
test@0 18 ns = ifilter(lambda n: n % p != 0, ns)
test@0 19 for n in sieve(ns):
test@0 20 yield n
test@0 21
test@0 22 odds = ifilter(lambda i: i % 2 == 1, count())
test@0 23 return chain([2], sieve(dropwhile(lambda n: n < 3, odds)))
test@0 24
test@0 25 if __name__ == "__main__":
test@0 26 import sys
test@0 27 try:
test@0 28 n = int(sys.argv[1])
test@0 29 except (ValueError, IndexError):
test@0 30 n = 10
test@0 31 p = primes()
test@0 32 print "The first %d primes: %s" % (n, list(islice(p, n)))
test@0 33
hgweb fileannotate, raw $ (get-with-headers.py localhost:$HGPORT 'annotate/tip/primes.py?style=raw') \ > | sed "s/test@//" > a $ echo "200 Script output follows" > b $ echo "" >> b $ echo "" >> b $ hg annotate "primes.py" >> b $ echo "" >> b $ echo "" >> b $ echo "" >> b $ echo "" >> b $ cmp b a || diff -u b a hgweb filerevision, raw $ (get-with-headers.py localhost:$HGPORT 'file/tip/primes.py?style=raw') \ > > a $ echo "200 Script output follows" > b $ echo "" >> b $ hg cat primes.py >> b $ cmp b a || diff -u b a hgweb highlightcss friendly $ get-with-headers.py localhost:$HGPORT 'highlightcss' > out $ head -n 4 out 200 Script output follows /* pygments_style = friendly */ $ rm out errors encountered $ cat errors.log $ killdaemons.py Change the pygments style $ cat > .hg/hgrc < [web] > pygments_style = fruity > EOF hg serve again $ hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log $ cat hg.pid >> $DAEMON_PIDS hgweb highlightcss fruity $ get-with-headers.py localhost:$HGPORT 'highlightcss' > out $ head -n 4 out 200 Script output follows /* pygments_style = fruity */ $ rm out errors encountered $ cat errors.log $ killdaemons.py only highlight C source files $ cat > .hg/hgrc < [web] > highlightfiles = **.c > EOF hg serve again $ hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log $ cat hg.pid >> $DAEMON_PIDS test that fileset in highlightfiles works and primes.py is not highlighted $ get-with-headers.py localhost:$HGPORT 'file/tip/primes.py' | grep 'id="l11"' def primes(): errors encountered $ cat errors.log $ cd .. $ hg init eucjp $ cd eucjp $ $PYTHON -c 'print("\265\376")' >> eucjp.txt # Japanese kanji "Kyo" $ hg ci -Ama adding eucjp.txt $ hgserveget () { > killdaemons.py > echo % HGENCODING="$1" hg serve > HGENCODING="$1" hg serve -p $HGPORT -d -n test --pid-file=hg.pid -E errors.log > cat hg.pid >> $DAEMON_PIDS > > echo % hgweb filerevision, html > get-with-headers.py localhost:$HGPORT "file/tip/$2" \ > | grep '
' > echo % errors encountered > cat errors.log > } $ hgserveget euc-jp eucjp.txt % HGENCODING=euc-jp hg serve % hgweb filerevision, html % errors encountered $ hgserveget utf-8 eucjp.txt % HGENCODING=utf-8 hg serve % hgweb filerevision, html % errors encountered $ hgserveget us-ascii eucjp.txt % HGENCODING=us-ascii hg serve % hgweb filerevision, html % errors encountered We attempt to highlight unknown files by default $ killdaemons.py $ cat > .hg/hgrc << EOF > [web] > highlightfiles = ** > EOF $ cat > unknownfile << EOF > #!/usr/bin/python > def foo(): > pass > EOF $ hg add unknownfile $ hg commit -m unknown unknownfile $ hg serve -p $HGPORT -d -n test --pid-file=hg.pid $ cat hg.pid >> $DAEMON_PIDS $ get-with-headers.py localhost:$HGPORT 'file/tip/unknownfile' | grep l2 def foo(): We can prevent Pygments from falling back to a non filename-based detection mode $ cat > .hg/hgrc << EOF > [web] > highlightfiles = ** > highlightonlymatchfilename = true > EOF $ killdaemons.py $ hg serve -p $HGPORT -d -n test --pid-file=hg.pid $ cat hg.pid >> $DAEMON_PIDS $ get-with-headers.py localhost:$HGPORT 'file/tip/unknownfile' | grep l2 def foo(): $ cd ..