Merge lp:~alanjds/beautifulsoup/prettify-improved into lp:beautifulsoup/3.2

Proposed by alanjds on 2010-12-29
Status: Needs review
Proposed branch: lp:~alanjds/beautifulsoup/prettify-improved
Merge into: lp:beautifulsoup/3.2
Diff against target: 57 lines (+10/-7)
1 file modified
BeautifulSoup.py (+10/-7)
To merge this branch: bzr merge lp:~alanjds/beautifulsoup/prettify-improved
Reviewer Review Type Date Requested Status
Leonard Richardson 2010-12-29 Pending
Review via email: mp+44877@code.launchpad.net

Description of the change

Hi.

I added an option to allow one to choose how many spaces to add per level, so I can use .prettify(spacesPerLevel=4), who I like more. Default remains to 1 more space per level.

Tests keep passing, and I tried to make the code easy to merge.

Best, Alan.

To post a comment you must log in.

Unmerged revisions

45. By Alan Justino <alanjds@lgpill> on 2010-12-29

Version marked with branch owner

44. By Alan Justino <alanjds@lgpill> on 2010-12-29

Option 'spacesPerLevel' added to .prettify()

- It allows one to set number of spaces who indent the tags

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'BeautifulSoup.py'
2--- BeautifulSoup.py 2010-11-21 13:35:35 +0000
3+++ BeautifulSoup.py 2010-12-29 22:58:46 +0000
4@@ -79,7 +79,7 @@
5 from __future__ import generators
6
7 __author__ = "Leonard Richardson (leonardr@segfault.org)"
8-__version__ = "3.2.0"
9+__version__ = "3.2.0-ajs"
10 __copyright__ = "Copyright (c) 2004-2010 Leonard Richardson"
11 __license__ = "New-style BSD"
12
13@@ -691,7 +691,7 @@
14 return "&" + self.XML_SPECIAL_CHARS_TO_ENTITIES[x.group(0)[0]] + ";"
15
16 def __str__(self, encoding=DEFAULT_OUTPUT_ENCODING,
17- prettyPrint=False, indentLevel=0):
18+ prettyPrint=False, indentLevel=0, spacesPerLevel=1):
19 """Returns a string or Unicode representation of this tag and
20 its contents. To get Unicode, pass None for encoding.
21
22@@ -751,7 +751,10 @@
23 indentTag = indentLevel
24 space = (' ' * (indentTag-1))
25 indentContents = indentTag + 1
26- contents = self.renderContents(encoding, prettyPrint, indentContents)
27+ if indentLevel > 0:
28+ indentContents += spacesPerLevel - 1
29+
30+ contents = self.renderContents(encoding, prettyPrint, indentContents, spacesPerLevel)
31 if self.hidden:
32 s = contents
33 else:
34@@ -792,11 +795,11 @@
35 current.nextSibling = None
36 current = next
37
38- def prettify(self, encoding=DEFAULT_OUTPUT_ENCODING):
39- return self.__str__(encoding, True)
40+ def prettify(self, encoding=DEFAULT_OUTPUT_ENCODING, spacesPerLevel=1):
41+ return self.__str__(encoding, True, spacesPerLevel=spacesPerLevel)
42
43 def renderContents(self, encoding=DEFAULT_OUTPUT_ENCODING,
44- prettyPrint=False, indentLevel=0):
45+ prettyPrint=False, indentLevel=0, spacesPerLevel=1):
46 """Renders the contents of this tag as a string in the given
47 encoding. If encoding is None, returns a Unicode string.."""
48 s=[]
49@@ -805,7 +808,7 @@
50 if isinstance(c, NavigableString):
51 text = c.__str__(encoding)
52 elif isinstance(c, Tag):
53- s.append(c.__str__(encoding, prettyPrint, indentLevel))
54+ s.append(c.__str__(encoding, prettyPrint, indentLevel, spacesPerLevel))
55 if text and prettyPrint:
56 text = text.strip()
57 if text:

Subscribers

People subscribed via source and target branches

to all changes: