68c3007cd59d

Add url rebase of images in files
[view raw] [browse files]
author theaspect@gmail.com
date Tue, 14 May 2013 11:44:47 +0700
parents c49ba42e5f0e
children af6d0c8414f2
branches/tags (none)
files __init__.py

Changes

--- a/__init__.py	Mon May 13 21:22:16 2013 +0700
+++ b/__init__.py	Tue May 14 11:44:47 2013 +0700
@@ -70,6 +70,7 @@
 
 	try:
 		fctx = webutil.filectx(web.repo, req)
+		changeid = fctx.hex()[0:12]
 		text = fctx.data().decode("utf-8")
 	except ErrorResponse, inst:
 		try:
@@ -80,7 +81,23 @@
 	if util.binary(text):
 		return webcommands.rawfile(web, req, tmpl)
 
-	md = markdown.Markdown(extensions=['wikilinks(base_url={0},end_url={1})'.format('', parts[1])])
+	base_url = tmpl.defaults['url'] + 'file/' + changeid + "/"
+	base_raw_url = tmpl.defaults['url'] + 'rawfile/' + changeid + "/"
+
+	def rebase(proc, e, attr):
+		uri = e.get(attr, '')
+		if '://' in uri or uri.startswith('/'):
+			return
+		base = base_url
+		if attr == 'src':
+			base = base_raw_url
+		e.set(attr, proc.rebase(base, uri))
+
+	ext = mdx_urlrebase.UrlRebaseExtension(configs=[('rebase', rebase)])
+	md = markdown.Markdown(
+			extensions=[ext, 'wikilinks'],
+			extension_configs={
+				'wikilinks' : [('base_url', ""), ('end_url', parts[-1])]})
 	html = md.convert(text).encode("utf-8")
 
 	args = {'file':f,