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