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