# HG changeset patch # User Anton Shestakov # Date 2015-07-22 02:19:17 # Node ID a74e9806d17d777595f02bef912da25b876cb56f # Parent 8c1d7a0e737b2304bc919d0f276848874c40f3ca highlight: produce correct markup when there's a blank line just before EOF Due to how the colorized output from pygments was stripped of
 elements,
when there was an empty line at the end of a file, highlight extension produced
an incorrect markup (no closing tags from the fileline/annotateline template).
It wasn't usually noticeable, because browsers were smart enough to see where
the missing tags should've been, but in monoblue style it resulted in the last
line having twice the normal height.

Instead of awkwardly trying to strip outer 
 tags, let's make the
formatter with nowrap=True, which should do what we need in pygments since at
least 0.5 (2006-10-30).

Example from monoblue style:

Before:

    
Now: (Notice the missing
now in place) diff --git a/hgext/highlight/highlight.py b/hgext/highlight/highlight.py --- a/hgext/highlight/highlight.py +++ b/hgext/highlight/highlight.py @@ -51,12 +51,9 @@ def pygmentize(field, fctx, style, tmpl) except (ClassNotFound, ValueError): lexer = TextLexer(stripnl=False) - formatter = HtmlFormatter(style=style) + formatter = HtmlFormatter(nowrap=True, style=style) colorized = highlight(text, lexer, formatter) - # strip wrapping div - colorized = colorized[:colorized.find('\n')] - colorized = colorized[colorized.find('
') + 5:]
     coloriter = (s.encode(encoding.encoding, 'replace')
                  for s in colorized.splitlines())
 
diff --git a/tests/test-highlight.t b/tests/test-highlight.t
--- a/tests/test-highlight.t
+++ b/tests/test-highlight.t
@@ -45,6 +45,7 @@ create random Python file to exercise Py
   >     p = primes()
   >     print "The first %d primes: %s" % (n, list(islice(p, n)))
   > EOF
+  $ echo >> primes.py  # to test html markup with an empty line just before EOF
   $ hg ci -Ama
   adding primes.py
 
@@ -68,7 +69,7 @@ hgweb filerevision, html
   
   
   
-  test: 853dcd4de2a6 primes.py
+  test: 06824edf55d0 primes.py
   
   
   
@@ -106,7 +107,7 @@ hgweb filerevision, html
   

- view primes.py @ 0:853dcd4de2a6 + view primes.py @ 0:06824edf55d0 tip

@@ -173,7 +174,8 @@ hgweb filerevision, html except (ValueError, IndexError): n = 10 p = primes() - print "The first %d primes: %s" % (n, list(islice(p, n)))
+ print "The first %d primes: %s" % (n, list(islice(p, n))) +
@@ -240,7 +242,7 @@ hgweb fileannotate, html

- annotate primes.py @ 0:853dcd4de2a6 + annotate primes.py @ 0:06824edf55d0 tip

@@ -284,228 +286,235 @@ hgweb fileannotate, html - test@0 + test@0 1 #!/usr/bin/env python - test@0 + test@0 2 - test@0 + test@0 3 """Fun with generators. Corresponding Haskell implementation: - test@0 + test@0 4 - test@0 + test@0 5 primes = 2 : sieve [3, 5..] - test@0 + test@0 6 where sieve (p:ns) = p : sieve [n | n <- ns, mod n p /= 0] - test@0 + test@0 7 """ - test@0 + test@0 8 - test@0 + test@0 9 from itertools import dropwhile, ifilter, islice, count, chain - test@0 + test@0 10 - test@0 + test@0 11 def primes(): - test@0 + test@0 12 """Generate all primes.""" - test@0 + test@0 13 def sieve(ns): - test@0 + test@0 14 p = ns.next() - test@0 + test@0 15 # It is important to yield *here* in order to stop the - test@0 + test@0 16 # infinite recursion. - test@0 + test@0 17 yield p - test@0 + test@0 18 ns = ifilter(lambda n: n % p != 0, ns) - test@0 + test@0 19 for n in sieve(ns): - test@0 + test@0 20 yield n - test@0 + test@0 21 - test@0 + test@0 22 odds = ifilter(lambda i: i % 2 == 1, count()) - test@0 + test@0 23 return chain([2], sieve(dropwhile(lambda n: n < 3, odds))) - test@0 + test@0 24 - test@0 + test@0 25 if __name__ == "__main__": - test@0 + test@0 26 import sys - test@0 + test@0 27 try: - test@0 + test@0 28 n = int(sys.argv[1]) - test@0 + test@0 29 except (ValueError, IndexError): - test@0 + test@0 30 n = 10 - test@0 + test@0 31 p = primes() - test@0 + test@0 32 print "The first %d primes: %s" % (n, list(islice(p, n))) + + + test@0 + + 33 +