Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Hacking configurable indent width into BeautifulSoup
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  1 message - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Helio Perroni Filho  
View profile  
 More options Mar 9 2011, 7:53 pm
From: Helio Perroni Filho <xperr...@gmail.com>
Date: Wed, 9 Mar 2011 21:53:42 -0300
Local: Wed, Mar 9 2011 7:53 pm
Subject: Hacking configurable indent width into BeautifulSoup

Many a time I have been saved by the HTML parsing prowess of BeautifulSoup.
When it comes to beautifying HTML dumps for easier analysis it is king;
however I have always been bothered by its unorthodox choice of indentation
width (just one space per level). So the other day I took the time to hack
configurable indent width into the code for version 3.2.0.

The complete modified file is
here<https://sites.google.com/site/machineawakeningsvault/home/BeautifulSo...>;
so long you're using version 3.2.0, you can just paste it over the original
file at your installation. For details on the changes see the diff below:

***************
*** 544 ****
--- 545,547 ----
+         # Adjustable indentation patch
+         self.indentWidth = parser.indentWidth
+
***************
*** 752 ****
!             space = (' ' * (indentTag-1))
--- 755 ----
!             space = (self.indentWidth * (indentTag-1))
***************
*** 813 ****
!                     s.append(" " * (indentLevel-1))
--- 816 ----
!                     s.append(self.indentWidth * (indentLevel-1))
***************
*** 1080,1082 ****
!     def __init__(self, markup="", parseOnlyThese=None, fromEncoding=None,
!                  markupMassage=True, smartQuotesTo=XML_ENTITIES,
!                  convertEntities=None, selfClosingTags=None,
isHTML=False):
--- 1083,1088 ----
!     def __init__(
!         self, markup="", parseOnlyThese=None, fromEncoding=None,
!         markupMassage=True, smartQuotesTo=XML_ENTITIES,
!         convertEntities=None, selfClosingTags=None, isHTML=False,
!         indentWidth=' '
!     ):
***************
*** 1111 ****
--- 1118,1121 ----
+
+         # Adjustable indentation patch
+         self.indentWidth = indentWidth
+

Now setting a custom indent width is as simple as doing:

soup = BeautifulSoup(html_string.encode('utf-8'), indentWidth='    ')
beautiful_html_string = soup.prettify()

--
Ja ne,
Helio Perroni Filho
http://machineawakening.blogspot.com/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »