9ebbaae19100

Fix unicode problem
[view raw] [browse files]
author theaspect@gmail.com
date Mon, 13 May 2013 19:13:41 +0700
parents 09bb5670eec6
children c49ba42e5f0e
branches/tags (none)
files __init__.py

Changes

--- a/__init__.py	Mon Sep 24 21:46:22 2012 +0100
+++ b/__init__.py	Mon May 13 19:13:41 2013 +0700
@@ -4,7 +4,7 @@
 # If using TortoiseHg, obtain Python-Markdown and tell Python where to find it:
 sys.path.append("c:/Program Files (x86)/MSBuild/The Motley Fool/Fool.Build/Resources/hgext.markdown/markdown-2.2.0-py2.7.egg")
 
-import logging, markdown, mimetypes
+import logging, markdown, mimetypes, codecs
 from mercurial import extensions, encoding, util
 from mercurial.hgweb import webcommands, webutil, common
 from mercurial.hgweb.common import ErrorResponse, HTTP_OK, HTTP_FORBIDDEN, HTTP_NOT_FOUND
@@ -17,23 +17,23 @@
 	path = req.form.get('file', [''])[0]
 	if path:
 		f = f + '/' + path
-	
+
 	if not f:
 		f = find_working_copy_readme(web.repo.root)
-	
+
 	parts = os.path.splitext(f)
-	
+
 	try:
-		text = file(web.repo.root + "/" + f, "rb").read()
+		text = codecs.open(web.repo.root + "/" + f, "rb", "utf-8").read()
 	except IOError:
 		raise ErrorResponse(HTTP_NOT_FOUND, 'path not found: ' + f)
-	
+
 	if not parts[1] == '.markdown' and not parts[1] == '.md':
 		return preview_sendraw(web, req, f, text)
-	
-	md = markdown.Markdown(extensions=['wikilinks(base_url={0},end_url={1})'.format('', parts[1])])	
-	html = md.convert(text)
-	
+
+	md = markdown.Markdown(extensions=['wikilinks(base_url={0},end_url={1})'.format('', parts[1])])
+	html = md.convert(text).encode("utf-8")
+
 	args = {'file':f,
 			'readmefilename':parts[0].split('/')[-1],
 			'path':webutil.up(f),
@@ -50,7 +50,7 @@
 		mt = 'application/binary'
 	else:
 		mt = 'text/plain'
-	
+
 	if guessmime:
 		mt = mimetypes.guess_type(path)[0]
 		if mt is None:
@@ -60,14 +60,14 @@
 
 	req.respond(HTTP_OK, mt, path, len(data))
 	return [data]
-	
+
 def file_markdown(orig, web, req, tmpl):
 	f = req.form.get('file', [''])[0]
 	parts = os.path.splitext(f)
-	
+
 	try:
 		fctx = webutil.filectx(web.repo, req)
-		text = fctx.data()
+		text = fctx.data().decode("utf-8")
 	except ErrorResponse, inst:
 		try:
 			return webcommands.manifest(web, req, tmpl)
@@ -80,9 +80,9 @@
 	if not parts[1] == '.markdown' and not parts[1] == '.md':
 		return orig(web, req, tmpl)
 
-	md = markdown.Markdown(extensions=['wikilinks(base_url={0},end_url={1})'.format('', parts[1])])	
-	html = md.convert(text)
-	
+	md = markdown.Markdown(extensions=['wikilinks(base_url={0},end_url={1})'.format('', parts[1])])
+	html = md.convert(text).encode("utf-8")
+
 	args = {'file':f,
 			'readmefilename':parts[0].split('/')[-1],
 			'path':webutil.up(f),
@@ -104,25 +104,25 @@
 	Decorates the default summary view by adding 'readme' and 'readmefile' content
 	to the template.
 	"""
-	
+
 	changeid = web.config('web', 'markdown.changeid', 'tip')
 	text = None
-	
+
 	cctx = web.repo[changeid]
 	changeid = cctx.hex()[0:12]
 	for filename in cctx:
 		if filename.lower() == 'readme.md' or filename.lower() == 'readme.markdown':
 			fctx = cctx.filectx(filename)
-			text = fctx.data()
+			text = fctx.data().decode("utf-8")
 			filesuffix = '.' + filename.split('.')[-1]
 			readmefile = filename
 			break
-	
+
 	if text:
 		ext = os.path.splitext(readmefile)[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('/'):
@@ -131,20 +131,20 @@
 			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', base_url), ('end_url', filesuffix)]})
-		readme = md.convert(text)
+		readme = md.convert(text).encode("utf-8")
 	else:
 		readmefile = "ReadMe"
 		readme = "Add ReadMe.md or ReadMe.markdown to this repository to display it here."
-		
+
 	tmpl.defaults['readmefilename'] = readmefile
 	tmpl.defaults['readme'] = readme
-	
+
 	return orig(web, req, tmpl)
 
 def find_working_copy_readme(dir):