##// END OF EJS Templates
highlight: add highlightfiles config option which takes a fileset (issue3005)...
av6 -
r26249:3166bcc0 default
parent child Browse files
Show More
@@ -1,68 +1,81
1 # highlight - syntax highlighting in hgweb, based on Pygments
1 # highlight - syntax highlighting in hgweb, based on Pygments
2 #
2 #
3 # Copyright 2008, 2009 Patrick Mezard <pmezard@gmail.com> and others
3 # Copyright 2008, 2009 Patrick Mezard <pmezard@gmail.com> and others
4 #
4 #
5 # This software may be used and distributed according to the terms of the
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
6 # GNU General Public License version 2 or any later version.
7 #
7 #
8 # The original module was split in an interface and an implementation
8 # The original module was split in an interface and an implementation
9 # file to defer pygments loading and speedup extension setup.
9 # file to defer pygments loading and speedup extension setup.
10
10
11 """syntax highlighting for hgweb (requires Pygments)
11 """syntax highlighting for hgweb (requires Pygments)
12
12
13 It depends on the Pygments syntax highlighting library:
13 It depends on the Pygments syntax highlighting library:
14 http://pygments.org/
14 http://pygments.org/
15
15
16 There is a single configuration option::
16 There are two configuration options::
17
17
18 [web]
18 [web]
19 pygments_style = <style>
19 pygments_style = <style> (default: colorful)
20
20 highlightfiles = <fileset> (default: size('<5M'))
21 The default is 'colorful'.
22 """
21 """
23
22
24 import highlight
23 import highlight
25 from mercurial.hgweb import webcommands, webutil, common
24 from mercurial.hgweb import webcommands, webutil, common
26 from mercurial import extensions, encoding
25 from mercurial import extensions, encoding, fileset
27 # Note for extension authors: ONLY specify testedwith = 'internal' for
26 # Note for extension authors: ONLY specify testedwith = 'internal' for
28 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
27 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
29 # be specifying the version(s) of Mercurial they are tested with, or
28 # be specifying the version(s) of Mercurial they are tested with, or
30 # leave the attribute unspecified.
29 # leave the attribute unspecified.
31 testedwith = 'internal'
30 testedwith = 'internal'
32
31
32 def checkfctx(fctx, expr):
33 ctx = fctx.changectx()
34 tree = fileset.parse(expr)
35 mctx = fileset.matchctx(ctx, subset=[fctx.path()], status=None)
36 repo = ctx.repo()
37 # To allow matching file names in the fileset in hgweb directory mode.
38 # See issue4568.
39 object.__setattr__(repo, 'getcwd', lambda: repo.root)
40 return fctx.path() in fileset.getset(mctx, tree)
41
33 def filerevision_highlight(orig, web, req, tmpl, fctx):
42 def filerevision_highlight(orig, web, req, tmpl, fctx):
34 mt = ''.join(tmpl('mimetype', encoding=encoding.encoding))
43 mt = ''.join(tmpl('mimetype', encoding=encoding.encoding))
35 # only pygmentize for mimetype containing 'html' so we both match
44 # only pygmentize for mimetype containing 'html' so we both match
36 # 'text/html' and possibly 'application/xhtml+xml' in the future
45 # 'text/html' and possibly 'application/xhtml+xml' in the future
37 # so that we don't have to touch the extension when the mimetype
46 # so that we don't have to touch the extension when the mimetype
38 # for a template changes; also hgweb optimizes the case that a
47 # for a template changes; also hgweb optimizes the case that a
39 # raw file is sent using rawfile() and doesn't call us, so we
48 # raw file is sent using rawfile() and doesn't call us, so we
40 # can't clash with the file's content-type here in case we
49 # can't clash with the file's content-type here in case we
41 # pygmentize a html file
50 # pygmentize a html file
42 if 'html' in mt:
51 if 'html' in mt:
43 style = web.config('web', 'pygments_style', 'colorful')
52 style = web.config('web', 'pygments_style', 'colorful')
53 expr = web.config('web', 'highlightfiles', "size('<5M')")
54 if checkfctx(fctx, expr):
44 highlight.pygmentize('fileline', fctx, style, tmpl)
55 highlight.pygmentize('fileline', fctx, style, tmpl)
45 return orig(web, req, tmpl, fctx)
56 return orig(web, req, tmpl, fctx)
46
57
47 def annotate_highlight(orig, web, req, tmpl):
58 def annotate_highlight(orig, web, req, tmpl):
48 mt = ''.join(tmpl('mimetype', encoding=encoding.encoding))
59 mt = ''.join(tmpl('mimetype', encoding=encoding.encoding))
49 if 'html' in mt:
60 if 'html' in mt:
50 fctx = webutil.filectx(web.repo, req)
61 fctx = webutil.filectx(web.repo, req)
51 style = web.config('web', 'pygments_style', 'colorful')
62 style = web.config('web', 'pygments_style', 'colorful')
63 expr = web.config('web', 'highlightfiles', "size('<5M')")
64 if checkfctx(fctx, expr):
52 highlight.pygmentize('annotateline', fctx, style, tmpl)
65 highlight.pygmentize('annotateline', fctx, style, tmpl)
53 return orig(web, req, tmpl)
66 return orig(web, req, tmpl)
54
67
55 def generate_css(web, req, tmpl):
68 def generate_css(web, req, tmpl):
56 pg_style = web.config('web', 'pygments_style', 'colorful')
69 pg_style = web.config('web', 'pygments_style', 'colorful')
57 fmter = highlight.HtmlFormatter(style=pg_style)
70 fmter = highlight.HtmlFormatter(style=pg_style)
58 req.respond(common.HTTP_OK, 'text/css')
71 req.respond(common.HTTP_OK, 'text/css')
59 return ['/* pygments_style = %s */\n\n' % pg_style,
72 return ['/* pygments_style = %s */\n\n' % pg_style,
60 fmter.get_style_defs('')]
73 fmter.get_style_defs('')]
61
74
62 def extsetup():
75 def extsetup():
63 # monkeypatch in the new version
76 # monkeypatch in the new version
64 extensions.wrapfunction(webcommands, '_filerevision',
77 extensions.wrapfunction(webcommands, '_filerevision',
65 filerevision_highlight)
78 filerevision_highlight)
66 extensions.wrapfunction(webcommands, 'annotate', annotate_highlight)
79 extensions.wrapfunction(webcommands, 'annotate', annotate_highlight)
67 webcommands.highlightcss = generate_css
80 webcommands.highlightcss = generate_css
68 webcommands.__all__.append('highlightcss')
81 webcommands.__all__.append('highlightcss')
@@ -1,624 +1,647
1 #require pygments serve
1 #require pygments serve
2
2
3 $ cat <<EOF >> $HGRCPATH
3 $ cat <<EOF >> $HGRCPATH
4 > [extensions]
4 > [extensions]
5 > highlight =
5 > highlight =
6 > [web]
6 > [web]
7 > pygments_style = friendly
7 > pygments_style = friendly
8 > highlightfiles = **.py and size('<100KB')
8 > EOF
9 > EOF
9 $ hg init test
10 $ hg init test
10 $ cd test
11 $ cd test
11
12
12 create random Python file to exercise Pygments
13 create random Python file to exercise Pygments
13
14
14 $ cat <<EOF > primes.py
15 $ cat <<EOF > primes.py
15 > #!/usr/bin/env python
16 > #!/usr/bin/env python
16 >
17 >
17 > """Fun with generators. Corresponding Haskell implementation:
18 > """Fun with generators. Corresponding Haskell implementation:
18 >
19 >
19 > primes = 2 : sieve [3, 5..]
20 > primes = 2 : sieve [3, 5..]
20 > where sieve (p:ns) = p : sieve [n | n <- ns, mod n p /= 0]
21 > where sieve (p:ns) = p : sieve [n | n <- ns, mod n p /= 0]
21 > """
22 > """
22 >
23 >
23 > from itertools import dropwhile, ifilter, islice, count, chain
24 > from itertools import dropwhile, ifilter, islice, count, chain
24 >
25 >
25 > def primes():
26 > def primes():
26 > """Generate all primes."""
27 > """Generate all primes."""
27 > def sieve(ns):
28 > def sieve(ns):
28 > p = ns.next()
29 > p = ns.next()
29 > # It is important to yield *here* in order to stop the
30 > # It is important to yield *here* in order to stop the
30 > # infinite recursion.
31 > # infinite recursion.
31 > yield p
32 > yield p
32 > ns = ifilter(lambda n: n % p != 0, ns)
33 > ns = ifilter(lambda n: n % p != 0, ns)
33 > for n in sieve(ns):
34 > for n in sieve(ns):
34 > yield n
35 > yield n
35 >
36 >
36 > odds = ifilter(lambda i: i % 2 == 1, count())
37 > odds = ifilter(lambda i: i % 2 == 1, count())
37 > return chain([2], sieve(dropwhile(lambda n: n < 3, odds)))
38 > return chain([2], sieve(dropwhile(lambda n: n < 3, odds)))
38 >
39 >
39 > if __name__ == "__main__":
40 > if __name__ == "__main__":
40 > import sys
41 > import sys
41 > try:
42 > try:
42 > n = int(sys.argv[1])
43 > n = int(sys.argv[1])
43 > except (ValueError, IndexError):
44 > except (ValueError, IndexError):
44 > n = 10
45 > n = 10
45 > p = primes()
46 > p = primes()
46 > print "The first %d primes: %s" % (n, list(islice(p, n)))
47 > print "The first %d primes: %s" % (n, list(islice(p, n)))
47 > EOF
48 > EOF
48 $ echo >> primes.py # to test html markup with an empty line just before EOF
49 $ echo >> primes.py # to test html markup with an empty line just before EOF
49 $ hg ci -Ama
50 $ hg ci -Ama
50 adding primes.py
51 adding primes.py
51
52
52 hg serve
53 hg serve
53
54
54 $ hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log
55 $ hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log
55 $ cat hg.pid >> $DAEMON_PIDS
56 $ cat hg.pid >> $DAEMON_PIDS
56
57
57 hgweb filerevision, html
58 hgweb filerevision, html
58
59
59 $ (get-with-headers.py localhost:$HGPORT 'file/tip/primes.py') \
60 $ (get-with-headers.py localhost:$HGPORT 'file/tip/primes.py') \
60 > | sed "s/class=\"k\"/class=\"kn\"/g" | sed "s/class=\"mf\"/class=\"mi\"/g"
61 > | sed "s/class=\"k\"/class=\"kn\"/g" | sed "s/class=\"mf\"/class=\"mi\"/g"
61 200 Script output follows
62 200 Script output follows
62
63
63 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
64 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
64 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
65 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
65 <head>
66 <head>
66 <link rel="icon" href="/static/hgicon.png" type="image/png" />
67 <link rel="icon" href="/static/hgicon.png" type="image/png" />
67 <meta name="robots" content="index, nofollow" />
68 <meta name="robots" content="index, nofollow" />
68 <link rel="stylesheet" href="/static/style-paper.css" type="text/css" />
69 <link rel="stylesheet" href="/static/style-paper.css" type="text/css" />
69 <script type="text/javascript" src="/static/mercurial.js"></script>
70 <script type="text/javascript" src="/static/mercurial.js"></script>
70
71
71 <link rel="stylesheet" href="/highlightcss" type="text/css" />
72 <link rel="stylesheet" href="/highlightcss" type="text/css" />
72 <title>test: 06824edf55d0 primes.py</title>
73 <title>test: 06824edf55d0 primes.py</title>
73 </head>
74 </head>
74 <body>
75 <body>
75
76
76 <div class="container">
77 <div class="container">
77 <div class="menu">
78 <div class="menu">
78 <div class="logo">
79 <div class="logo">
79 <a href="http://mercurial.selenic.com/">
80 <a href="http://mercurial.selenic.com/">
80 <img src="/static/hglogo.png" alt="mercurial" /></a>
81 <img src="/static/hglogo.png" alt="mercurial" /></a>
81 </div>
82 </div>
82 <ul>
83 <ul>
83 <li><a href="/shortlog/tip">log</a></li>
84 <li><a href="/shortlog/tip">log</a></li>
84 <li><a href="/graph/tip">graph</a></li>
85 <li><a href="/graph/tip">graph</a></li>
85 <li><a href="/tags">tags</a></li>
86 <li><a href="/tags">tags</a></li>
86 <li><a href="/bookmarks">bookmarks</a></li>
87 <li><a href="/bookmarks">bookmarks</a></li>
87 <li><a href="/branches">branches</a></li>
88 <li><a href="/branches">branches</a></li>
88 </ul>
89 </ul>
89 <ul>
90 <ul>
90 <li><a href="/rev/tip">changeset</a></li>
91 <li><a href="/rev/tip">changeset</a></li>
91 <li><a href="/file/tip/">browse</a></li>
92 <li><a href="/file/tip/">browse</a></li>
92 </ul>
93 </ul>
93 <ul>
94 <ul>
94 <li class="active">file</li>
95 <li class="active">file</li>
95 <li><a href="/file/tip/primes.py">latest</a></li>
96 <li><a href="/file/tip/primes.py">latest</a></li>
96 <li><a href="/diff/tip/primes.py">diff</a></li>
97 <li><a href="/diff/tip/primes.py">diff</a></li>
97 <li><a href="/comparison/tip/primes.py">comparison</a></li>
98 <li><a href="/comparison/tip/primes.py">comparison</a></li>
98 <li><a href="/annotate/tip/primes.py">annotate</a></li>
99 <li><a href="/annotate/tip/primes.py">annotate</a></li>
99 <li><a href="/log/tip/primes.py">file log</a></li>
100 <li><a href="/log/tip/primes.py">file log</a></li>
100 <li><a href="/raw-file/tip/primes.py">raw</a></li>
101 <li><a href="/raw-file/tip/primes.py">raw</a></li>
101 </ul>
102 </ul>
102 <ul>
103 <ul>
103 <li><a href="/help">help</a></li>
104 <li><a href="/help">help</a></li>
104 </ul>
105 </ul>
105 </div>
106 </div>
106
107
107 <div class="main">
108 <div class="main">
108 <h2 class="breadcrumb"><a href="/">Mercurial</a> </h2>
109 <h2 class="breadcrumb"><a href="/">Mercurial</a> </h2>
109 <h3>
110 <h3>
110 view primes.py @ 0:<a href="/rev/06824edf55d0">06824edf55d0</a>
111 view primes.py @ 0:<a href="/rev/06824edf55d0">06824edf55d0</a>
111 <span class="tag">tip</span>
112 <span class="tag">tip</span>
112 </h3>
113 </h3>
113
114
114 <form class="search" action="/log">
115 <form class="search" action="/log">
115
116
116 <p><input name="rev" id="search1" type="text" size="30" /></p>
117 <p><input name="rev" id="search1" type="text" size="30" /></p>
117 <div id="hint">Find changesets by keywords (author, files, the commit message), revision
118 <div id="hint">Find changesets by keywords (author, files, the commit message), revision
118 number or hash, or <a href="/help/revsets">revset expression</a>.</div>
119 number or hash, or <a href="/help/revsets">revset expression</a>.</div>
119 </form>
120 </form>
120
121
121 <div class="description">a</div>
122 <div class="description">a</div>
122
123
123 <table id="changesetEntry">
124 <table id="changesetEntry">
124 <tr>
125 <tr>
125 <th class="author">author</th>
126 <th class="author">author</th>
126 <td class="author">&#116;&#101;&#115;&#116;</td>
127 <td class="author">&#116;&#101;&#115;&#116;</td>
127 </tr>
128 </tr>
128 <tr>
129 <tr>
129 <th class="date">date</th>
130 <th class="date">date</th>
130 <td class="date age">Thu, 01 Jan 1970 00:00:00 +0000</td>
131 <td class="date age">Thu, 01 Jan 1970 00:00:00 +0000</td>
131 </tr>
132 </tr>
132 <tr>
133 <tr>
133 <th class="author">parents</th>
134 <th class="author">parents</th>
134 <td class="author"></td>
135 <td class="author"></td>
135 </tr>
136 </tr>
136 <tr>
137 <tr>
137 <th class="author">children</th>
138 <th class="author">children</th>
138 <td class="author"></td>
139 <td class="author"></td>
139 </tr>
140 </tr>
140 </table>
141 </table>
141
142
142 <div class="overflow">
143 <div class="overflow">
143 <div class="sourcefirst linewraptoggle">line wrap: <a class="linewraplink" href="javascript:toggleLinewrap()">on</a></div>
144 <div class="sourcefirst linewraptoggle">line wrap: <a class="linewraplink" href="javascript:toggleLinewrap()">on</a></div>
144 <div class="sourcefirst"> line source</div>
145 <div class="sourcefirst"> line source</div>
145 <pre class="sourcelines stripes4 wrap bottomline">
146 <pre class="sourcelines stripes4 wrap bottomline">
146 <span id="l1"><span class="c">#!/usr/bin/env python</span></span><a href="#l1"></a>
147 <span id="l1"><span class="c">#!/usr/bin/env python</span></span><a href="#l1"></a>
147 <span id="l2"></span><a href="#l2"></a>
148 <span id="l2"></span><a href="#l2"></a>
148 <span id="l3"><span class="sd">&quot;&quot;&quot;Fun with generators. Corresponding Haskell implementation:</span></span><a href="#l3"></a>
149 <span id="l3"><span class="sd">&quot;&quot;&quot;Fun with generators. Corresponding Haskell implementation:</span></span><a href="#l3"></a>
149 <span id="l4"></span><a href="#l4"></a>
150 <span id="l4"></span><a href="#l4"></a>
150 <span id="l5"><span class="sd">primes = 2 : sieve [3, 5..]</span></span><a href="#l5"></a>
151 <span id="l5"><span class="sd">primes = 2 : sieve [3, 5..]</span></span><a href="#l5"></a>
151 <span id="l6"><span class="sd"> where sieve (p:ns) = p : sieve [n | n &lt;- ns, mod n p /= 0]</span></span><a href="#l6"></a>
152 <span id="l6"><span class="sd"> where sieve (p:ns) = p : sieve [n | n &lt;- ns, mod n p /= 0]</span></span><a href="#l6"></a>
152 <span id="l7"><span class="sd">&quot;&quot;&quot;</span></span><a href="#l7"></a>
153 <span id="l7"><span class="sd">&quot;&quot;&quot;</span></span><a href="#l7"></a>
153 <span id="l8"></span><a href="#l8"></a>
154 <span id="l8"></span><a href="#l8"></a>
154 <span id="l9"><span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">dropwhile</span><span class="p">,</span> <span class="n">ifilter</span><span class="p">,</span> <span class="n">islice</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span> <span class="n">chain</span></span><a href="#l9"></a>
155 <span id="l9"><span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">dropwhile</span><span class="p">,</span> <span class="n">ifilter</span><span class="p">,</span> <span class="n">islice</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span> <span class="n">chain</span></span><a href="#l9"></a>
155 <span id="l10"></span><a href="#l10"></a>
156 <span id="l10"></span><a href="#l10"></a>
156 <span id="l11"><span class="kn">def</span> <span class="nf">primes</span><span class="p">():</span></span><a href="#l11"></a>
157 <span id="l11"><span class="kn">def</span> <span class="nf">primes</span><span class="p">():</span></span><a href="#l11"></a>
157 <span id="l12"> <span class="sd">&quot;&quot;&quot;Generate all primes.&quot;&quot;&quot;</span></span><a href="#l12"></a>
158 <span id="l12"> <span class="sd">&quot;&quot;&quot;Generate all primes.&quot;&quot;&quot;</span></span><a href="#l12"></a>
158 <span id="l13"> <span class="kn">def</span> <span class="nf">sieve</span><span class="p">(</span><span class="n">ns</span><span class="p">):</span></span><a href="#l13"></a>
159 <span id="l13"> <span class="kn">def</span> <span class="nf">sieve</span><span class="p">(</span><span class="n">ns</span><span class="p">):</span></span><a href="#l13"></a>
159 <span id="l14"> <span class="n">p</span> <span class="o">=</span> <span class="n">ns</span><span class="o">.</span><span class="n">next</span><span class="p">()</span></span><a href="#l14"></a>
160 <span id="l14"> <span class="n">p</span> <span class="o">=</span> <span class="n">ns</span><span class="o">.</span><span class="n">next</span><span class="p">()</span></span><a href="#l14"></a>
160 <span id="l15"> <span class="c"># It is important to yield *here* in order to stop the</span></span><a href="#l15"></a>
161 <span id="l15"> <span class="c"># It is important to yield *here* in order to stop the</span></span><a href="#l15"></a>
161 <span id="l16"> <span class="c"># infinite recursion.</span></span><a href="#l16"></a>
162 <span id="l16"> <span class="c"># infinite recursion.</span></span><a href="#l16"></a>
162 <span id="l17"> <span class="kn">yield</span> <span class="n">p</span></span><a href="#l17"></a>
163 <span id="l17"> <span class="kn">yield</span> <span class="n">p</span></span><a href="#l17"></a>
163 <span id="l18"> <span class="n">ns</span> <span class="o">=</span> <span class="n">ifilter</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">%</span> <span class="n">p</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">ns</span><span class="p">)</span></span><a href="#l18"></a>
164 <span id="l18"> <span class="n">ns</span> <span class="o">=</span> <span class="n">ifilter</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">%</span> <span class="n">p</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">,</span> <span class="n">ns</span><span class="p">)</span></span><a href="#l18"></a>
164 <span id="l19"> <span class="kn">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">sieve</span><span class="p">(</span><span class="n">ns</span><span class="p">):</span></span><a href="#l19"></a>
165 <span id="l19"> <span class="kn">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">sieve</span><span class="p">(</span><span class="n">ns</span><span class="p">):</span></span><a href="#l19"></a>
165 <span id="l20"> <span class="kn">yield</span> <span class="n">n</span></span><a href="#l20"></a>
166 <span id="l20"> <span class="kn">yield</span> <span class="n">n</span></span><a href="#l20"></a>
166 <span id="l21"></span><a href="#l21"></a>
167 <span id="l21"></span><a href="#l21"></a>
167 <span id="l22"> <span class="n">odds</span> <span class="o">=</span> <span class="n">ifilter</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">,</span> <span class="n">count</span><span class="p">())</span></span><a href="#l22"></a>
168 <span id="l22"> <span class="n">odds</span> <span class="o">=</span> <span class="n">ifilter</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="n">i</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">,</span> <span class="n">count</span><span class="p">())</span></span><a href="#l22"></a>
168 <span id="l23"> <span class="kn">return</span> <span class="n">chain</span><span class="p">([</span><span class="mi">2</span><span class="p">],</span> <span class="n">sieve</span><span class="p">(</span><span class="n">dropwhile</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">3</span><span class="p">,</span> <span class="n">odds</span><span class="p">)))</span></span><a href="#l23"></a>
169 <span id="l23"> <span class="kn">return</span> <span class="n">chain</span><span class="p">([</span><span class="mi">2</span><span class="p">],</span> <span class="n">sieve</span><span class="p">(</span><span class="n">dropwhile</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">3</span><span class="p">,</span> <span class="n">odds</span><span class="p">)))</span></span><a href="#l23"></a>
169 <span id="l24"></span><a href="#l24"></a>
170 <span id="l24"></span><a href="#l24"></a>
170 <span id="l25"><span class="kn">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span></span><a href="#l25"></a>
171 <span id="l25"><span class="kn">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span></span><a href="#l25"></a>
171 <span id="l26"> <span class="kn">import</span> <span class="nn">sys</span></span><a href="#l26"></a>
172 <span id="l26"> <span class="kn">import</span> <span class="nn">sys</span></span><a href="#l26"></a>
172 <span id="l27"> <span class="kn">try</span><span class="p">:</span></span><a href="#l27"></a>
173 <span id="l27"> <span class="kn">try</span><span class="p">:</span></span><a href="#l27"></a>
173 <span id="l28"> <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span></span><a href="#l28"></a>
174 <span id="l28"> <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span></span><a href="#l28"></a>
174 <span id="l29"> <span class="kn">except</span> <span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">IndexError</span><span class="p">):</span></span><a href="#l29"></a>
175 <span id="l29"> <span class="kn">except</span> <span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">IndexError</span><span class="p">):</span></span><a href="#l29"></a>
175 <span id="l30"> <span class="n">n</span> <span class="o">=</span> <span class="mi">10</span></span><a href="#l30"></a>
176 <span id="l30"> <span class="n">n</span> <span class="o">=</span> <span class="mi">10</span></span><a href="#l30"></a>
176 <span id="l31"> <span class="n">p</span> <span class="o">=</span> <span class="n">primes</span><span class="p">()</span></span><a href="#l31"></a>
177 <span id="l31"> <span class="n">p</span> <span class="o">=</span> <span class="n">primes</span><span class="p">()</span></span><a href="#l31"></a>
177 <span id="l32"> <span class="kn">print</span> <span class="s">&quot;The first </span><span class="si">%d</span><span class="s"> primes: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">islice</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">n</span><span class="p">)))</span></span><a href="#l32"></a>
178 <span id="l32"> <span class="kn">print</span> <span class="s">&quot;The first </span><span class="si">%d</span><span class="s"> primes: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">islice</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">n</span><span class="p">)))</span></span><a href="#l32"></a>
178 <span id="l33"></span><a href="#l33"></a></pre>
179 <span id="l33"></span><a href="#l33"></a></pre>
179 </div>
180 </div>
180 </div>
181 </div>
181 </div>
182 </div>
182
183
183 <script type="text/javascript">process_dates()</script>
184 <script type="text/javascript">process_dates()</script>
184
185
185
186
186 </body>
187 </body>
187 </html>
188 </html>
188
189
189
190
190 hgweb fileannotate, html
191 hgweb fileannotate, html
191
192
192 $ (get-with-headers.py localhost:$HGPORT 'annotate/tip/primes.py') \
193 $ (get-with-headers.py localhost:$HGPORT 'annotate/tip/primes.py') \
193 > | sed "s/class=\"k\"/class=\"kn\"/g" | sed "s/class=\"mi\"/class=\"mf\"/g"
194 > | sed "s/class=\"k\"/class=\"kn\"/g" | sed "s/class=\"mi\"/class=\"mf\"/g"
194 200 Script output follows
195 200 Script output follows
195
196
196 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
197 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
197 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
198 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US">
198 <head>
199 <head>
199 <link rel="icon" href="/static/hgicon.png" type="image/png" />
200 <link rel="icon" href="/static/hgicon.png" type="image/png" />
200 <meta name="robots" content="index, nofollow" />
201 <meta name="robots" content="index, nofollow" />
201 <link rel="stylesheet" href="/static/style-paper.css" type="text/css" />
202 <link rel="stylesheet" href="/static/style-paper.css" type="text/css" />
202 <script type="text/javascript" src="/static/mercurial.js"></script>
203 <script type="text/javascript" src="/static/mercurial.js"></script>
203
204
204 <link rel="stylesheet" href="/highlightcss" type="text/css" />
205 <link rel="stylesheet" href="/highlightcss" type="text/css" />
205 <title>test: primes.py annotate</title>
206 <title>test: primes.py annotate</title>
206 </head>
207 </head>
207 <body>
208 <body>
208
209
209 <div class="container">
210 <div class="container">
210 <div class="menu">
211 <div class="menu">
211 <div class="logo">
212 <div class="logo">
212 <a href="http://mercurial.selenic.com/">
213 <a href="http://mercurial.selenic.com/">
213 <img src="/static/hglogo.png" alt="mercurial" /></a>
214 <img src="/static/hglogo.png" alt="mercurial" /></a>
214 </div>
215 </div>
215 <ul>
216 <ul>
216 <li><a href="/shortlog/tip">log</a></li>
217 <li><a href="/shortlog/tip">log</a></li>
217 <li><a href="/graph/tip">graph</a></li>
218 <li><a href="/graph/tip">graph</a></li>
218 <li><a href="/tags">tags</a></li>
219 <li><a href="/tags">tags</a></li>
219 <li><a href="/bookmarks">bookmarks</a></li>
220 <li><a href="/bookmarks">bookmarks</a></li>
220 <li><a href="/branches">branches</a></li>
221 <li><a href="/branches">branches</a></li>
221 </ul>
222 </ul>
222
223
223 <ul>
224 <ul>
224 <li><a href="/rev/tip">changeset</a></li>
225 <li><a href="/rev/tip">changeset</a></li>
225 <li><a href="/file/tip/">browse</a></li>
226 <li><a href="/file/tip/">browse</a></li>
226 </ul>
227 </ul>
227 <ul>
228 <ul>
228 <li><a href="/file/tip/primes.py">file</a></li>
229 <li><a href="/file/tip/primes.py">file</a></li>
229 <li><a href="/file/tip/primes.py">latest</a></li>
230 <li><a href="/file/tip/primes.py">latest</a></li>
230 <li><a href="/diff/tip/primes.py">diff</a></li>
231 <li><a href="/diff/tip/primes.py">diff</a></li>
231 <li><a href="/comparison/tip/primes.py">comparison</a></li>
232 <li><a href="/comparison/tip/primes.py">comparison</a></li>
232 <li class="active">annotate</li>
233 <li class="active">annotate</li>
233 <li><a href="/log/tip/primes.py">file log</a></li>
234 <li><a href="/log/tip/primes.py">file log</a></li>
234 <li><a href="/raw-annotate/tip/primes.py">raw</a></li>
235 <li><a href="/raw-annotate/tip/primes.py">raw</a></li>
235 </ul>
236 </ul>
236 <ul>
237 <ul>
237 <li><a href="/help">help</a></li>
238 <li><a href="/help">help</a></li>
238 </ul>
239 </ul>
239 </div>
240 </div>
240
241
241 <div class="main">
242 <div class="main">
242 <h2 class="breadcrumb"><a href="/">Mercurial</a> </h2>
243 <h2 class="breadcrumb"><a href="/">Mercurial</a> </h2>
243 <h3>
244 <h3>
244 annotate primes.py @ 0:<a href="/rev/06824edf55d0">06824edf55d0</a>
245 annotate primes.py @ 0:<a href="/rev/06824edf55d0">06824edf55d0</a>
245 <span class="tag">tip</span>
246 <span class="tag">tip</span>
246 </h3>
247 </h3>
247
248
248 <form class="search" action="/log">
249 <form class="search" action="/log">
249
250
250 <p><input name="rev" id="search1" type="text" size="30" /></p>
251 <p><input name="rev" id="search1" type="text" size="30" /></p>
251 <div id="hint">Find changesets by keywords (author, files, the commit message), revision
252 <div id="hint">Find changesets by keywords (author, files, the commit message), revision
252 number or hash, or <a href="/help/revsets">revset expression</a>.</div>
253 number or hash, or <a href="/help/revsets">revset expression</a>.</div>
253 </form>
254 </form>
254
255
255 <div class="description">a</div>
256 <div class="description">a</div>
256
257
257 <table id="changesetEntry">
258 <table id="changesetEntry">
258 <tr>
259 <tr>
259 <th class="author">author</th>
260 <th class="author">author</th>
260 <td class="author">&#116;&#101;&#115;&#116;</td>
261 <td class="author">&#116;&#101;&#115;&#116;</td>
261 </tr>
262 </tr>
262 <tr>
263 <tr>
263 <th class="date">date</th>
264 <th class="date">date</th>
264 <td class="date age">Thu, 01 Jan 1970 00:00:00 +0000</td>
265 <td class="date age">Thu, 01 Jan 1970 00:00:00 +0000</td>
265 </tr>
266 </tr>
266 <tr>
267 <tr>
267 <th class="author">parents</th>
268 <th class="author">parents</th>
268 <td class="author"></td>
269 <td class="author"></td>
269 </tr>
270 </tr>
270 <tr>
271 <tr>
271 <th class="author">children</th>
272 <th class="author">children</th>
272 <td class="author"></td>
273 <td class="author"></td>
273 </tr>
274 </tr>
274 </table>
275 </table>
275
276
276 <div class="overflow">
277 <div class="overflow">
277 <table class="bigtable">
278 <table class="bigtable">
278 <thead>
279 <thead>
279 <tr>
280 <tr>
280 <th class="annotate">rev</th>
281 <th class="annotate">rev</th>
281 <th class="line">&nbsp;&nbsp;line source</th>
282 <th class="line">&nbsp;&nbsp;line source</th>
282 </tr>
283 </tr>
283 </thead>
284 </thead>
284 <tbody class="stripes2">
285 <tbody class="stripes2">
285
286
286 <tr id="l1">
287 <tr id="l1">
287 <td class="annotate">
288 <td class="annotate">
288 <a href="/annotate/06824edf55d0/primes.py#l1"
289 <a href="/annotate/06824edf55d0/primes.py#l1"
289 title="06824edf55d0: a">test@0</a>
290 title="06824edf55d0: a">test@0</a>
290 </td>
291 </td>
291 <td class="source"><a href="#l1"> 1</a> <span class="c">#!/usr/bin/env python</span></td>
292 <td class="source"><a href="#l1"> 1</a> <span class="c">#!/usr/bin/env python</span></td>
292 </tr>
293 </tr>
293 <tr id="l2">
294 <tr id="l2">
294 <td class="annotate">
295 <td class="annotate">
295 <a href="/annotate/06824edf55d0/primes.py#l2"
296 <a href="/annotate/06824edf55d0/primes.py#l2"
296 title="06824edf55d0: a">test@0</a>
297 title="06824edf55d0: a">test@0</a>
297 </td>
298 </td>
298 <td class="source"><a href="#l2"> 2</a> </td>
299 <td class="source"><a href="#l2"> 2</a> </td>
299 </tr>
300 </tr>
300 <tr id="l3">
301 <tr id="l3">
301 <td class="annotate">
302 <td class="annotate">
302 <a href="/annotate/06824edf55d0/primes.py#l3"
303 <a href="/annotate/06824edf55d0/primes.py#l3"
303 title="06824edf55d0: a">test@0</a>
304 title="06824edf55d0: a">test@0</a>
304 </td>
305 </td>
305 <td class="source"><a href="#l3"> 3</a> <span class="sd">&quot;&quot;&quot;Fun with generators. Corresponding Haskell implementation:</span></td>
306 <td class="source"><a href="#l3"> 3</a> <span class="sd">&quot;&quot;&quot;Fun with generators. Corresponding Haskell implementation:</span></td>
306 </tr>
307 </tr>
307 <tr id="l4">
308 <tr id="l4">
308 <td class="annotate">
309 <td class="annotate">
309 <a href="/annotate/06824edf55d0/primes.py#l4"
310 <a href="/annotate/06824edf55d0/primes.py#l4"
310 title="06824edf55d0: a">test@0</a>
311 title="06824edf55d0: a">test@0</a>
311 </td>
312 </td>
312 <td class="source"><a href="#l4"> 4</a> </td>
313 <td class="source"><a href="#l4"> 4</a> </td>
313 </tr>
314 </tr>
314 <tr id="l5">
315 <tr id="l5">
315 <td class="annotate">
316 <td class="annotate">
316 <a href="/annotate/06824edf55d0/primes.py#l5"
317 <a href="/annotate/06824edf55d0/primes.py#l5"
317 title="06824edf55d0: a">test@0</a>
318 title="06824edf55d0: a">test@0</a>
318 </td>
319 </td>
319 <td class="source"><a href="#l5"> 5</a> <span class="sd">primes = 2 : sieve [3, 5..]</span></td>
320 <td class="source"><a href="#l5"> 5</a> <span class="sd">primes = 2 : sieve [3, 5..]</span></td>
320 </tr>
321 </tr>
321 <tr id="l6">
322 <tr id="l6">
322 <td class="annotate">
323 <td class="annotate">
323 <a href="/annotate/06824edf55d0/primes.py#l6"
324 <a href="/annotate/06824edf55d0/primes.py#l6"
324 title="06824edf55d0: a">test@0</a>
325 title="06824edf55d0: a">test@0</a>
325 </td>
326 </td>
326 <td class="source"><a href="#l6"> 6</a> <span class="sd"> where sieve (p:ns) = p : sieve [n | n &lt;- ns, mod n p /= 0]</span></td>
327 <td class="source"><a href="#l6"> 6</a> <span class="sd"> where sieve (p:ns) = p : sieve [n | n &lt;- ns, mod n p /= 0]</span></td>
327 </tr>
328 </tr>
328 <tr id="l7">
329 <tr id="l7">
329 <td class="annotate">
330 <td class="annotate">
330 <a href="/annotate/06824edf55d0/primes.py#l7"
331 <a href="/annotate/06824edf55d0/primes.py#l7"
331 title="06824edf55d0: a">test@0</a>
332 title="06824edf55d0: a">test@0</a>
332 </td>
333 </td>
333 <td class="source"><a href="#l7"> 7</a> <span class="sd">&quot;&quot;&quot;</span></td>
334 <td class="source"><a href="#l7"> 7</a> <span class="sd">&quot;&quot;&quot;</span></td>
334 </tr>
335 </tr>
335 <tr id="l8">
336 <tr id="l8">
336 <td class="annotate">
337 <td class="annotate">
337 <a href="/annotate/06824edf55d0/primes.py#l8"
338 <a href="/annotate/06824edf55d0/primes.py#l8"
338 title="06824edf55d0: a">test@0</a>
339 title="06824edf55d0: a">test@0</a>
339 </td>
340 </td>
340 <td class="source"><a href="#l8"> 8</a> </td>
341 <td class="source"><a href="#l8"> 8</a> </td>
341 </tr>
342 </tr>
342 <tr id="l9">
343 <tr id="l9">
343 <td class="annotate">
344 <td class="annotate">
344 <a href="/annotate/06824edf55d0/primes.py#l9"
345 <a href="/annotate/06824edf55d0/primes.py#l9"
345 title="06824edf55d0: a">test@0</a>
346 title="06824edf55d0: a">test@0</a>
346 </td>
347 </td>
347 <td class="source"><a href="#l9"> 9</a> <span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">dropwhile</span><span class="p">,</span> <span class="n">ifilter</span><span class="p">,</span> <span class="n">islice</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span> <span class="n">chain</span></td>
348 <td class="source"><a href="#l9"> 9</a> <span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">dropwhile</span><span class="p">,</span> <span class="n">ifilter</span><span class="p">,</span> <span class="n">islice</span><span class="p">,</span> <span class="n">count</span><span class="p">,</span> <span class="n">chain</span></td>
348 </tr>
349 </tr>
349 <tr id="l10">
350 <tr id="l10">
350 <td class="annotate">
351 <td class="annotate">
351 <a href="/annotate/06824edf55d0/primes.py#l10"
352 <a href="/annotate/06824edf55d0/primes.py#l10"
352 title="06824edf55d0: a">test@0</a>
353 title="06824edf55d0: a">test@0</a>
353 </td>
354 </td>
354 <td class="source"><a href="#l10"> 10</a> </td>
355 <td class="source"><a href="#l10"> 10</a> </td>
355 </tr>
356 </tr>
356 <tr id="l11">
357 <tr id="l11">
357 <td class="annotate">
358 <td class="annotate">
358 <a href="/annotate/06824edf55d0/primes.py#l11"
359 <a href="/annotate/06824edf55d0/primes.py#l11"
359 title="06824edf55d0: a">test@0</a>
360 title="06824edf55d0: a">test@0</a>
360 </td>
361 </td>
361 <td class="source"><a href="#l11"> 11</a> <span class="kn">def</span> <span class="nf">primes</span><span class="p">():</span></td>
362 <td class="source"><a href="#l11"> 11</a> <span class="kn">def</span> <span class="nf">primes</span><span class="p">():</span></td>
362 </tr>
363 </tr>
363 <tr id="l12">
364 <tr id="l12">
364 <td class="annotate">
365 <td class="annotate">
365 <a href="/annotate/06824edf55d0/primes.py#l12"
366 <a href="/annotate/06824edf55d0/primes.py#l12"
366 title="06824edf55d0: a">test@0</a>
367 title="06824edf55d0: a">test@0</a>
367 </td>
368 </td>
368 <td class="source"><a href="#l12"> 12</a> <span class="sd">&quot;&quot;&quot;Generate all primes.&quot;&quot;&quot;</span></td>
369 <td class="source"><a href="#l12"> 12</a> <span class="sd">&quot;&quot;&quot;Generate all primes.&quot;&quot;&quot;</span></td>
369 </tr>
370 </tr>
370 <tr id="l13">
371 <tr id="l13">
371 <td class="annotate">
372 <td class="annotate">
372 <a href="/annotate/06824edf55d0/primes.py#l13"
373 <a href="/annotate/06824edf55d0/primes.py#l13"
373 title="06824edf55d0: a">test@0</a>
374 title="06824edf55d0: a">test@0</a>
374 </td>
375 </td>
375 <td class="source"><a href="#l13"> 13</a> <span class="kn">def</span> <span class="nf">sieve</span><span class="p">(</span><span class="n">ns</span><span class="p">):</span></td>
376 <td class="source"><a href="#l13"> 13</a> <span class="kn">def</span> <span class="nf">sieve</span><span class="p">(</span><span class="n">ns</span><span class="p">):</span></td>
376 </tr>
377 </tr>
377 <tr id="l14">
378 <tr id="l14">
378 <td class="annotate">
379 <td class="annotate">
379 <a href="/annotate/06824edf55d0/primes.py#l14"
380 <a href="/annotate/06824edf55d0/primes.py#l14"
380 title="06824edf55d0: a">test@0</a>
381 title="06824edf55d0: a">test@0</a>
381 </td>
382 </td>
382 <td class="source"><a href="#l14"> 14</a> <span class="n">p</span> <span class="o">=</span> <span class="n">ns</span><span class="o">.</span><span class="n">next</span><span class="p">()</span></td>
383 <td class="source"><a href="#l14"> 14</a> <span class="n">p</span> <span class="o">=</span> <span class="n">ns</span><span class="o">.</span><span class="n">next</span><span class="p">()</span></td>
383 </tr>
384 </tr>
384 <tr id="l15">
385 <tr id="l15">
385 <td class="annotate">
386 <td class="annotate">
386 <a href="/annotate/06824edf55d0/primes.py#l15"
387 <a href="/annotate/06824edf55d0/primes.py#l15"
387 title="06824edf55d0: a">test@0</a>
388 title="06824edf55d0: a">test@0</a>
388 </td>
389 </td>
389 <td class="source"><a href="#l15"> 15</a> <span class="c"># It is important to yield *here* in order to stop the</span></td>
390 <td class="source"><a href="#l15"> 15</a> <span class="c"># It is important to yield *here* in order to stop the</span></td>
390 </tr>
391 </tr>
391 <tr id="l16">
392 <tr id="l16">
392 <td class="annotate">
393 <td class="annotate">
393 <a href="/annotate/06824edf55d0/primes.py#l16"
394 <a href="/annotate/06824edf55d0/primes.py#l16"
394 title="06824edf55d0: a">test@0</a>
395 title="06824edf55d0: a">test@0</a>
395 </td>
396 </td>
396 <td class="source"><a href="#l16"> 16</a> <span class="c"># infinite recursion.</span></td>
397 <td class="source"><a href="#l16"> 16</a> <span class="c"># infinite recursion.</span></td>
397 </tr>
398 </tr>
398 <tr id="l17">
399 <tr id="l17">
399 <td class="annotate">
400 <td class="annotate">
400 <a href="/annotate/06824edf55d0/primes.py#l17"
401 <a href="/annotate/06824edf55d0/primes.py#l17"
401 title="06824edf55d0: a">test@0</a>
402 title="06824edf55d0: a">test@0</a>
402 </td>
403 </td>
403 <td class="source"><a href="#l17"> 17</a> <span class="kn">yield</span> <span class="n">p</span></td>
404 <td class="source"><a href="#l17"> 17</a> <span class="kn">yield</span> <span class="n">p</span></td>
404 </tr>
405 </tr>
405 <tr id="l18">
406 <tr id="l18">
406 <td class="annotate">
407 <td class="annotate">
407 <a href="/annotate/06824edf55d0/primes.py#l18"
408 <a href="/annotate/06824edf55d0/primes.py#l18"
408 title="06824edf55d0: a">test@0</a>
409 title="06824edf55d0: a">test@0</a>
409 </td>
410 </td>
410 <td class="source"><a href="#l18"> 18</a> <span class="n">ns</span> <span class="o">=</span> <span class="n">ifilter</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">%</span> <span class="n">p</span> <span class="o">!=</span> <span class="mf">0</span><span class="p">,</span> <span class="n">ns</span><span class="p">)</span></td>
411 <td class="source"><a href="#l18"> 18</a> <span class="n">ns</span> <span class="o">=</span> <span class="n">ifilter</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">%</span> <span class="n">p</span> <span class="o">!=</span> <span class="mf">0</span><span class="p">,</span> <span class="n">ns</span><span class="p">)</span></td>
411 </tr>
412 </tr>
412 <tr id="l19">
413 <tr id="l19">
413 <td class="annotate">
414 <td class="annotate">
414 <a href="/annotate/06824edf55d0/primes.py#l19"
415 <a href="/annotate/06824edf55d0/primes.py#l19"
415 title="06824edf55d0: a">test@0</a>
416 title="06824edf55d0: a">test@0</a>
416 </td>
417 </td>
417 <td class="source"><a href="#l19"> 19</a> <span class="kn">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">sieve</span><span class="p">(</span><span class="n">ns</span><span class="p">):</span></td>
418 <td class="source"><a href="#l19"> 19</a> <span class="kn">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">sieve</span><span class="p">(</span><span class="n">ns</span><span class="p">):</span></td>
418 </tr>
419 </tr>
419 <tr id="l20">
420 <tr id="l20">
420 <td class="annotate">
421 <td class="annotate">
421 <a href="/annotate/06824edf55d0/primes.py#l20"
422 <a href="/annotate/06824edf55d0/primes.py#l20"
422 title="06824edf55d0: a">test@0</a>
423 title="06824edf55d0: a">test@0</a>
423 </td>
424 </td>
424 <td class="source"><a href="#l20"> 20</a> <span class="kn">yield</span> <span class="n">n</span></td>
425 <td class="source"><a href="#l20"> 20</a> <span class="kn">yield</span> <span class="n">n</span></td>
425 </tr>
426 </tr>
426 <tr id="l21">
427 <tr id="l21">
427 <td class="annotate">
428 <td class="annotate">
428 <a href="/annotate/06824edf55d0/primes.py#l21"
429 <a href="/annotate/06824edf55d0/primes.py#l21"
429 title="06824edf55d0: a">test@0</a>
430 title="06824edf55d0: a">test@0</a>
430 </td>
431 </td>
431 <td class="source"><a href="#l21"> 21</a> </td>
432 <td class="source"><a href="#l21"> 21</a> </td>
432 </tr>
433 </tr>
433 <tr id="l22">
434 <tr id="l22">
434 <td class="annotate">
435 <td class="annotate">
435 <a href="/annotate/06824edf55d0/primes.py#l22"
436 <a href="/annotate/06824edf55d0/primes.py#l22"
436 title="06824edf55d0: a">test@0</a>
437 title="06824edf55d0: a">test@0</a>
437 </td>
438 </td>
438 <td class="source"><a href="#l22"> 22</a> <span class="n">odds</span> <span class="o">=</span> <span class="n">ifilter</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="n">i</span> <span class="o">%</span> <span class="mf">2</span> <span class="o">==</span> <span class="mf">1</span><span class="p">,</span> <span class="n">count</span><span class="p">())</span></td>
439 <td class="source"><a href="#l22"> 22</a> <span class="n">odds</span> <span class="o">=</span> <span class="n">ifilter</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="n">i</span> <span class="o">%</span> <span class="mf">2</span> <span class="o">==</span> <span class="mf">1</span><span class="p">,</span> <span class="n">count</span><span class="p">())</span></td>
439 </tr>
440 </tr>
440 <tr id="l23">
441 <tr id="l23">
441 <td class="annotate">
442 <td class="annotate">
442 <a href="/annotate/06824edf55d0/primes.py#l23"
443 <a href="/annotate/06824edf55d0/primes.py#l23"
443 title="06824edf55d0: a">test@0</a>
444 title="06824edf55d0: a">test@0</a>
444 </td>
445 </td>
445 <td class="source"><a href="#l23"> 23</a> <span class="kn">return</span> <span class="n">chain</span><span class="p">([</span><span class="mf">2</span><span class="p">],</span> <span class="n">sieve</span><span class="p">(</span><span class="n">dropwhile</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mf">3</span><span class="p">,</span> <span class="n">odds</span><span class="p">)))</span></td>
446 <td class="source"><a href="#l23"> 23</a> <span class="kn">return</span> <span class="n">chain</span><span class="p">([</span><span class="mf">2</span><span class="p">],</span> <span class="n">sieve</span><span class="p">(</span><span class="n">dropwhile</span><span class="p">(</span><span class="kn">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mf">3</span><span class="p">,</span> <span class="n">odds</span><span class="p">)))</span></td>
446 </tr>
447 </tr>
447 <tr id="l24">
448 <tr id="l24">
448 <td class="annotate">
449 <td class="annotate">
449 <a href="/annotate/06824edf55d0/primes.py#l24"
450 <a href="/annotate/06824edf55d0/primes.py#l24"
450 title="06824edf55d0: a">test@0</a>
451 title="06824edf55d0: a">test@0</a>
451 </td>
452 </td>
452 <td class="source"><a href="#l24"> 24</a> </td>
453 <td class="source"><a href="#l24"> 24</a> </td>
453 </tr>
454 </tr>
454 <tr id="l25">
455 <tr id="l25">
455 <td class="annotate">
456 <td class="annotate">
456 <a href="/annotate/06824edf55d0/primes.py#l25"
457 <a href="/annotate/06824edf55d0/primes.py#l25"
457 title="06824edf55d0: a">test@0</a>
458 title="06824edf55d0: a">test@0</a>
458 </td>
459 </td>
459 <td class="source"><a href="#l25"> 25</a> <span class="kn">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span></td>
460 <td class="source"><a href="#l25"> 25</a> <span class="kn">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span></td>
460 </tr>
461 </tr>
461 <tr id="l26">
462 <tr id="l26">
462 <td class="annotate">
463 <td class="annotate">
463 <a href="/annotate/06824edf55d0/primes.py#l26"
464 <a href="/annotate/06824edf55d0/primes.py#l26"
464 title="06824edf55d0: a">test@0</a>
465 title="06824edf55d0: a">test@0</a>
465 </td>
466 </td>
466 <td class="source"><a href="#l26"> 26</a> <span class="kn">import</span> <span class="nn">sys</span></td>
467 <td class="source"><a href="#l26"> 26</a> <span class="kn">import</span> <span class="nn">sys</span></td>
467 </tr>
468 </tr>
468 <tr id="l27">
469 <tr id="l27">
469 <td class="annotate">
470 <td class="annotate">
470 <a href="/annotate/06824edf55d0/primes.py#l27"
471 <a href="/annotate/06824edf55d0/primes.py#l27"
471 title="06824edf55d0: a">test@0</a>
472 title="06824edf55d0: a">test@0</a>
472 </td>
473 </td>
473 <td class="source"><a href="#l27"> 27</a> <span class="kn">try</span><span class="p">:</span></td>
474 <td class="source"><a href="#l27"> 27</a> <span class="kn">try</span><span class="p">:</span></td>
474 </tr>
475 </tr>
475 <tr id="l28">
476 <tr id="l28">
476 <td class="annotate">
477 <td class="annotate">
477 <a href="/annotate/06824edf55d0/primes.py#l28"
478 <a href="/annotate/06824edf55d0/primes.py#l28"
478 title="06824edf55d0: a">test@0</a>
479 title="06824edf55d0: a">test@0</a>
479 </td>
480 </td>
480 <td class="source"><a href="#l28"> 28</a> <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span></td>
481 <td class="source"><a href="#l28"> 28</a> <span class="n">n</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">1</span><span class="p">])</span></td>
481 </tr>
482 </tr>
482 <tr id="l29">
483 <tr id="l29">
483 <td class="annotate">
484 <td class="annotate">
484 <a href="/annotate/06824edf55d0/primes.py#l29"
485 <a href="/annotate/06824edf55d0/primes.py#l29"
485 title="06824edf55d0: a">test@0</a>
486 title="06824edf55d0: a">test@0</a>
486 </td>
487 </td>
487 <td class="source"><a href="#l29"> 29</a> <span class="kn">except</span> <span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">IndexError</span><span class="p">):</span></td>
488 <td class="source"><a href="#l29"> 29</a> <span class="kn">except</span> <span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="ne">IndexError</span><span class="p">):</span></td>
488 </tr>
489 </tr>
489 <tr id="l30">
490 <tr id="l30">
490 <td class="annotate">
491 <td class="annotate">
491 <a href="/annotate/06824edf55d0/primes.py#l30"
492 <a href="/annotate/06824edf55d0/primes.py#l30"
492 title="06824edf55d0: a">test@0</a>
493 title="06824edf55d0: a">test@0</a>
493 </td>
494 </td>
494 <td class="source"><a href="#l30"> 30</a> <span class="n">n</span> <span class="o">=</span> <span class="mf">10</span></td>
495 <td class="source"><a href="#l30"> 30</a> <span class="n">n</span> <span class="o">=</span> <span class="mf">10</span></td>
495 </tr>
496 </tr>
496 <tr id="l31">
497 <tr id="l31">
497 <td class="annotate">
498 <td class="annotate">
498 <a href="/annotate/06824edf55d0/primes.py#l31"
499 <a href="/annotate/06824edf55d0/primes.py#l31"
499 title="06824edf55d0: a">test@0</a>
500 title="06824edf55d0: a">test@0</a>
500 </td>
501 </td>
501 <td class="source"><a href="#l31"> 31</a> <span class="n">p</span> <span class="o">=</span> <span class="n">primes</span><span class="p">()</span></td>
502 <td class="source"><a href="#l31"> 31</a> <span class="n">p</span> <span class="o">=</span> <span class="n">primes</span><span class="p">()</span></td>
502 </tr>
503 </tr>
503 <tr id="l32">
504 <tr id="l32">
504 <td class="annotate">
505 <td class="annotate">
505 <a href="/annotate/06824edf55d0/primes.py#l32"
506 <a href="/annotate/06824edf55d0/primes.py#l32"
506 title="06824edf55d0: a">test@0</a>
507 title="06824edf55d0: a">test@0</a>
507 </td>
508 </td>
508 <td class="source"><a href="#l32"> 32</a> <span class="kn">print</span> <span class="s">&quot;The first </span><span class="si">%d</span><span class="s"> primes: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">islice</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">n</span><span class="p">)))</span></td>
509 <td class="source"><a href="#l32"> 32</a> <span class="kn">print</span> <span class="s">&quot;The first </span><span class="si">%d</span><span class="s"> primes: </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="nb">list</span><span class="p">(</span><span class="n">islice</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">n</span><span class="p">)))</span></td>
509 </tr>
510 </tr>
510 <tr id="l33">
511 <tr id="l33">
511 <td class="annotate">
512 <td class="annotate">
512 <a href="/annotate/06824edf55d0/primes.py#l33"
513 <a href="/annotate/06824edf55d0/primes.py#l33"
513 title="06824edf55d0: a">test@0</a>
514 title="06824edf55d0: a">test@0</a>
514 </td>
515 </td>
515 <td class="source"><a href="#l33"> 33</a> </td>
516 <td class="source"><a href="#l33"> 33</a> </td>
516 </tr>
517 </tr>
517 </tbody>
518 </tbody>
518 </table>
519 </table>
519 </div>
520 </div>
520 </div>
521 </div>
521 </div>
522 </div>
522
523
523 <script type="text/javascript">process_dates()</script>
524 <script type="text/javascript">process_dates()</script>
524
525
525
526
526 </body>
527 </body>
527 </html>
528 </html>
528
529
529
530
530 hgweb fileannotate, raw
531 hgweb fileannotate, raw
531
532
532 $ (get-with-headers.py localhost:$HGPORT 'annotate/tip/primes.py?style=raw') \
533 $ (get-with-headers.py localhost:$HGPORT 'annotate/tip/primes.py?style=raw') \
533 > | sed "s/test@//" > a
534 > | sed "s/test@//" > a
534 $ echo "200 Script output follows" > b
535 $ echo "200 Script output follows" > b
535 $ echo "" >> b
536 $ echo "" >> b
536 $ echo "" >> b
537 $ echo "" >> b
537 $ hg annotate "primes.py" >> b
538 $ hg annotate "primes.py" >> b
538 $ echo "" >> b
539 $ echo "" >> b
539 $ echo "" >> b
540 $ echo "" >> b
540 $ echo "" >> b
541 $ echo "" >> b
541 $ echo "" >> b
542 $ echo "" >> b
542 $ cmp b a || diff -u b a
543 $ cmp b a || diff -u b a
543
544
544 hgweb filerevision, raw
545 hgweb filerevision, raw
545
546
546 $ (get-with-headers.py localhost:$HGPORT 'file/tip/primes.py?style=raw') \
547 $ (get-with-headers.py localhost:$HGPORT 'file/tip/primes.py?style=raw') \
547 > > a
548 > > a
548 $ echo "200 Script output follows" > b
549 $ echo "200 Script output follows" > b
549 $ echo "" >> b
550 $ echo "" >> b
550 $ hg cat primes.py >> b
551 $ hg cat primes.py >> b
551 $ cmp b a || diff -u b a
552 $ cmp b a || diff -u b a
552
553
553 hgweb highlightcss friendly
554 hgweb highlightcss friendly
554
555
555 $ get-with-headers.py localhost:$HGPORT 'highlightcss' > out
556 $ get-with-headers.py localhost:$HGPORT 'highlightcss' > out
556 $ head -n 4 out
557 $ head -n 4 out
557 200 Script output follows
558 200 Script output follows
558
559
559 /* pygments_style = friendly */
560 /* pygments_style = friendly */
560
561
561 $ rm out
562 $ rm out
562
563
563 errors encountered
564 errors encountered
564
565
565 $ cat errors.log
566 $ cat errors.log
566 $ killdaemons.py
567 $ killdaemons.py
567
568
568 Change the pygments style
569 Change the pygments style
569
570
570 $ cat > .hg/hgrc <<EOF
571 $ cat > .hg/hgrc <<EOF
571 > [web]
572 > [web]
572 > pygments_style = fruity
573 > pygments_style = fruity
573 > EOF
574 > EOF
574
575
575 hg serve again
576 hg serve again
576
577
577 $ hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log
578 $ hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log
578 $ cat hg.pid >> $DAEMON_PIDS
579 $ cat hg.pid >> $DAEMON_PIDS
579
580
580 hgweb highlightcss fruity
581 hgweb highlightcss fruity
581
582
582 $ get-with-headers.py localhost:$HGPORT 'highlightcss' > out
583 $ get-with-headers.py localhost:$HGPORT 'highlightcss' > out
583 $ head -n 4 out
584 $ head -n 4 out
584 200 Script output follows
585 200 Script output follows
585
586
586 /* pygments_style = fruity */
587 /* pygments_style = fruity */
587
588
588 $ rm out
589 $ rm out
589
590
590 errors encountered
591 errors encountered
591
592
592 $ cat errors.log
593 $ cat errors.log
594 $ killdaemons.py
595
596 only highlight C source files
597
598 $ cat > .hg/hgrc <<EOF
599 > [web]
600 > highlightfiles = **.c
601 > EOF
602
603 hg serve again
604
605 $ hg serve -p $HGPORT -d -n test --pid-file=hg.pid -A access.log -E errors.log
606 $ cat hg.pid >> $DAEMON_PIDS
607
608 test that fileset in highlightfiles works and primes.py is not highlighted
609
610 $ get-with-headers.py localhost:$HGPORT 'file/tip/primes.py' | grep 'id="l11"'
611 <span id="l11">def primes():</span><a href="#l11"></a>
612
613 errors encountered
614
615 $ cat errors.log
593 $ cd ..
616 $ cd ..
594 $ hg init eucjp
617 $ hg init eucjp
595 $ cd eucjp
618 $ cd eucjp
596 $ $PYTHON -c 'print("\265\376")' >> eucjp.txt # Japanese kanji "Kyo"
619 $ $PYTHON -c 'print("\265\376")' >> eucjp.txt # Japanese kanji "Kyo"
597 $ hg ci -Ama
620 $ hg ci -Ama
598 adding eucjp.txt
621 adding eucjp.txt
599 $ hgserveget () {
622 $ hgserveget () {
600 > killdaemons.py
623 > killdaemons.py
601 > echo % HGENCODING="$1" hg serve
624 > echo % HGENCODING="$1" hg serve
602 > HGENCODING="$1" hg serve -p $HGPORT -d -n test --pid-file=hg.pid -E errors.log
625 > HGENCODING="$1" hg serve -p $HGPORT -d -n test --pid-file=hg.pid -E errors.log
603 > cat hg.pid >> $DAEMON_PIDS
626 > cat hg.pid >> $DAEMON_PIDS
604 >
627 >
605 > echo % hgweb filerevision, html
628 > echo % hgweb filerevision, html
606 > get-with-headers.py localhost:$HGPORT "file/tip/$2" \
629 > get-with-headers.py localhost:$HGPORT "file/tip/$2" \
607 > | grep '<div class="parity0 source">'
630 > | grep '<div class="parity0 source">'
608 > echo % errors encountered
631 > echo % errors encountered
609 > cat errors.log
632 > cat errors.log
610 > }
633 > }
611 $ hgserveget euc-jp eucjp.txt
634 $ hgserveget euc-jp eucjp.txt
612 % HGENCODING=euc-jp hg serve
635 % HGENCODING=euc-jp hg serve
613 % hgweb filerevision, html
636 % hgweb filerevision, html
614 % errors encountered
637 % errors encountered
615 $ hgserveget utf-8 eucjp.txt
638 $ hgserveget utf-8 eucjp.txt
616 % HGENCODING=utf-8 hg serve
639 % HGENCODING=utf-8 hg serve
617 % hgweb filerevision, html
640 % hgweb filerevision, html
618 % errors encountered
641 % errors encountered
619 $ hgserveget us-ascii eucjp.txt
642 $ hgserveget us-ascii eucjp.txt
620 % HGENCODING=us-ascii hg serve
643 % HGENCODING=us-ascii hg serve
621 % hgweb filerevision, html
644 % hgweb filerevision, html
622 % errors encountered
645 % errors encountered
623
646
624 $ cd ..
647 $ cd ..
General Comments 0
You need to be logged in to leave comments. Login now