pushkin
pushkin

Reputation: 10227

Python script to substitute text in HTML file with user-supplied values

I have an hmtl file that looks like:

...
<!-- Special_ID -->
<p> stuff1 </p>
<p> stuff2 </p>
<!-- /Special_ID -->
...

I have an INI file:

[general]
param=stuff1
 stuff2

If the user edits the file and changes the param value to test, I want the html file to be changed to:

...
<!-- Special_ID -->
<p> test </p>
<!-- /Special_ID -->
...

Currently, what I'm doing is parsing the INI file (Python's ConfigParser) and then turning the section ("general") and option ("param") into a start and stop special id like in the examples above.

Then:

while we haven't found the start id:
    just write a line to some temporary file

write our start id to the temp file
write out new value ("test") to the temp file # surround with <p>

loop through original file until we find the stop id
then write the stop id and the rest of the file to temp

replace original file with tmp file

Is there a smarter way of doing this?

Perhaps a Python module that already does this.

I also don't particularly like requiring the <!-- Special_ID -->, but I'm not using a web framework (just a simple app), so I can't just do a fancy <p py:for ...>... like in TurboGears.

Upvotes: 1

Views: 664

Answers (1)

alecxe
alecxe

Reputation: 474131

Overall not sure about the current approach you've presented, but here is how you can replace all the p elements after a specific comment and insert a new p element instead (using BeautifulSoup HTML parser). The idea is to:

The working code:

from bs4 import BeautifulSoup, Comment

data = """
<!-- Special_ID -->
<p> stuff1 </p>
<p> stuff2 </p>
<!-- /Special_ID -->
"""
soup = BeautifulSoup(data, "html.parser")

# find "Special_ID" comment
special_id = soup.find(text=lambda text: isinstance(text, Comment) and "Special_ID" in text)

# find all sibling "p" elements
for p in special_id.find_next_siblings("p"):
    p.extract()

# create new "p" element
tag = soup.new_tag("p")
tag.string = "test"

# insert the new "p" element after the comment
special_id.insert_after(tag)

print(soup.prettify())

Prints:

<!-- Special_ID -->
<p>
 test
</p>
<!-- /Special_ID -->

Upvotes: 1

Related Questions