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

Proposed by alanjds
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 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>

Version marked with branch owner

44. By Alan Justino <alanjds@lgpill>

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
=== modified file 'BeautifulSoup.py'
--- BeautifulSoup.py 2010-11-21 13:35:35 +0000
+++ BeautifulSoup.py 2010-12-29 22:58:46 +0000
@@ -79,7 +79,7 @@
79from __future__ import generators79from __future__ import generators
8080
81__author__ = "Leonard Richardson (leonardr@segfault.org)"81__author__ = "Leonard Richardson (leonardr@segfault.org)"
82__version__ = "3.2.0"82__version__ = "3.2.0-ajs"
83__copyright__ = "Copyright (c) 2004-2010 Leonard Richardson"83__copyright__ = "Copyright (c) 2004-2010 Leonard Richardson"
84__license__ = "New-style BSD"84__license__ = "New-style BSD"
8585
@@ -691,7 +691,7 @@
691 return "&" + self.XML_SPECIAL_CHARS_TO_ENTITIES[x.group(0)[0]] + ";"691 return "&" + self.XML_SPECIAL_CHARS_TO_ENTITIES[x.group(0)[0]] + ";"
692692
693 def __str__(self, encoding=DEFAULT_OUTPUT_ENCODING,693 def __str__(self, encoding=DEFAULT_OUTPUT_ENCODING,
694 prettyPrint=False, indentLevel=0):694 prettyPrint=False, indentLevel=0, spacesPerLevel=1):
695 """Returns a string or Unicode representation of this tag and695 """Returns a string or Unicode representation of this tag and
696 its contents. To get Unicode, pass None for encoding.696 its contents. To get Unicode, pass None for encoding.
697697
@@ -751,7 +751,10 @@
751 indentTag = indentLevel751 indentTag = indentLevel
752 space = (' ' * (indentTag-1))752 space = (' ' * (indentTag-1))
753 indentContents = indentTag + 1753 indentContents = indentTag + 1
754 contents = self.renderContents(encoding, prettyPrint, indentContents)754 if indentLevel > 0:
755 indentContents += spacesPerLevel - 1
756
757 contents = self.renderContents(encoding, prettyPrint, indentContents, spacesPerLevel)
755 if self.hidden:758 if self.hidden:
756 s = contents759 s = contents
757 else:760 else:
@@ -792,11 +795,11 @@
792 current.nextSibling = None795 current.nextSibling = None
793 current = next796 current = next
794797
795 def prettify(self, encoding=DEFAULT_OUTPUT_ENCODING):798 def prettify(self, encoding=DEFAULT_OUTPUT_ENCODING, spacesPerLevel=1):
796 return self.__str__(encoding, True)799 return self.__str__(encoding, True, spacesPerLevel=spacesPerLevel)
797800
798 def renderContents(self, encoding=DEFAULT_OUTPUT_ENCODING,801 def renderContents(self, encoding=DEFAULT_OUTPUT_ENCODING,
799 prettyPrint=False, indentLevel=0):802 prettyPrint=False, indentLevel=0, spacesPerLevel=1):
800 """Renders the contents of this tag as a string in the given803 """Renders the contents of this tag as a string in the given
801 encoding. If encoding is None, returns a Unicode string.."""804 encoding. If encoding is None, returns a Unicode string.."""
802 s=[]805 s=[]
@@ -805,7 +808,7 @@
805 if isinstance(c, NavigableString):808 if isinstance(c, NavigableString):
806 text = c.__str__(encoding)809 text = c.__str__(encoding)
807 elif isinstance(c, Tag):810 elif isinstance(c, Tag):
808 s.append(c.__str__(encoding, prettyPrint, indentLevel))811 s.append(c.__str__(encoding, prettyPrint, indentLevel, spacesPerLevel))
809 if text and prettyPrint:812 if text and prettyPrint:
810 text = text.strip()813 text = text.strip()
811 if text:814 if text:

Subscribers

People subscribed via source and target branches

to all changes: