Changeset 219:854608cc314c
- Timestamp:
- 10/26/09 21:48:37
(11 months ago)
- Author:
- Daniele Varrazzo <piro@develer.com>
- Tags:
tip
- branch:
- default
- Message:
Convert relative urls into absolute in rendered ht pages.
Only convert links in the frame, leave links in the body untouched.
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r198 |
r219 |
|
| 9 | 9 | import sys |
|---|
| 10 | 10 | import os |
|---|
| | 11 | import re |
|---|
| 11 | 12 | from optparse import make_option |
|---|
| 12 | 13 | from os.path import abspath, dirname, join |
|---|
| … | … | |
| 77 | 78 | metadata = get_page_metadata(msg) |
|---|
| 78 | 79 | body = get_page_body(msg) |
|---|
| | 80 | # hack to convert relative links into absolute in the page frame |
|---|
| | 81 | body = re.sub(r"""(?i)(href)(=['"]/)""", r'\1XXX\2', body) |
|---|
| 79 | 82 | |
|---|
| 80 | 83 | class FakeRequest: |
|---|
| … | … | |
| 89 | 92 | |
|---|
| 90 | 93 | page = render_to_string('htpage.html', ctx) |
|---|
| | 94 | |
|---|
| | 95 | # convert relative links into absolute and revert hacked links |
|---|
| | 96 | page = re.sub(r"""(?i)(href=['"])/""", r'\1http://www.python.it/', page) |
|---|
| | 97 | page = re.sub(r"""(?i)(href)XXX(=['"]/)""", r'\1\2', page) |
|---|
| 91 | 98 | outfile = options['out'] and open(options['out'], 'w') or sys.stdout |
|---|
| 92 | 99 | outfile.write(page.encode('utf-8')) |
|---|