brendonh / pyth

Python text markup and conversion
MIT License
89 stars 79 forks source link

Added callback function to xhtml reader, which could be used to make absolute paths #18

Closed Sets88 closed 10 years ago

Sets88 commented 11 years ago

Sometimes you need to dump html page and then save it to pdf, but it'll raise an exception, because of relative paths in urls i added additional callback function, which user can use to make absolute paths in urls, same thing i've seen in pisa(https://pypi.python.org/pypi/pisa/) here is simple example:

from pyth.plugins.xhtml.reader import XHTMLReader

content = """
text<br>
<a href="/relative/path/to/something">link</a><br>
text
"""

def link_callback(url):
    return "http://domain.com" + url

doc=XHTMLReader.read(content, link_callback=link_callback)
print str(doc.content[1].content)

That, what i needed:

[Text('[link]' {'url': u'http://domain.com/relative/path/to/something'})]
brendonh commented 11 years ago

Why is this better as a callback than just a string base URL?

Sets88 commented 11 years ago

because there much more flexibility, for example: you have on your page incorect paths like: google.com/test/, without "http://" with string base url, it'll be understood as local relative link, but in call back you can operate whith such links as you like

brendonh commented 10 years ago

Okay, that makes sense. Pretty great how I forgot about this for eight months, right?