#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=\"vm\"/class=\"n\"/g" \ > -e "s/class=\"\([cs]\)[h12]\"/class=\"\1\"/g" > } create random Python file to exercise Pygments $ cat < primes.py > """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: f4fca47b67e6 primes.py

view primes.py @ 0:f4fca47b67e6 draft default tip

a
author test
date Thu, 01 Jan 1970 00:00:00 +0000
parents
children
line wrap: on
line source
  
  """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:f4fca47b67e6 draft default tip

a
author test
date Thu, 01 Jan 1970 00:00:00 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
test
parents:
diff changeset
1 """Fun with generators. Corresponding Haskell implementation:
test
parents:
diff changeset
2
test
parents:
diff changeset
3 primes = 2 : sieve [3, 5..]
test
parents:
diff changeset
4 where sieve (p:ns) = p : sieve [n | n <- ns, mod n p /= 0]
test
parents:
diff changeset
5 """
test
parents:
diff changeset
6
test
parents:
diff changeset
7 from itertools import dropwhile, ifilter, islice, count, chain
test
parents:
diff changeset
8
test
parents:
diff changeset
9 def primes():
test
parents:
diff changeset
10 """Generate all primes."""
test
parents:
diff changeset
11 def sieve(ns):
test
parents:
diff changeset
12 p = ns.next()
test
parents:
diff changeset
13 # It is important to yield *here* in order to stop the
test
parents:
diff changeset
14 # infinite recursion.
test
parents:
diff changeset
15 yield p
test
parents:
diff changeset
16 ns = ifilter(lambda n: n % p != 0, ns)
test
parents:
diff changeset
17 for n in sieve(ns):
test
parents:
diff changeset
18 yield n
test
parents:
diff changeset
19
test
parents:
diff changeset
20 odds = ifilter(lambda i: i % 2 == 1, count())
test
parents:
diff changeset
21 return chain([2], sieve(dropwhile(lambda n: n < 3, odds)))
test
parents:
diff changeset
22
test
parents:
diff changeset
23 if __name__ == "__main__":
test
parents:
diff changeset
24 import sys
test
parents:
diff changeset
25 try:
test
parents:
diff changeset
26 n = int(sys.argv[1])
test
parents:
diff changeset
27 except (ValueError, IndexError):
test
parents:
diff changeset
28 n = 10
test
parents:
diff changeset
29 p = primes()
test
parents:
diff changeset
30 print("The first %d primes: %s" % (n, list(islice(p, n))))
test
parents:
diff changeset
31
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 sieve(ns): 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 > #!$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 ..