# HG changeset patch # User Constantine Linnick # Date 1381055781 -25200 # Node ID ade5266185db547798cc32d380ea6f17ec5cac4a # Parent 34d537e83b7ae88dcebeb098921dc44e18d2eb09 Add win compatibility diff -r 34d537e83b7a -r ade5266185db ReadMe.markdown --- a/ReadMe.markdown Sat Jun 01 17:42:46 2013 +0700 +++ b/ReadMe.markdown Sun Oct 06 17:36:21 2013 +0700 @@ -19,7 +19,7 @@ 1. Clone this repository to /example/hgext.markdown 1. Add the following to your hgweb config -config: +###config: [web] # Must be absolute path @@ -27,19 +27,22 @@ style = markdown #optional; 'tip' is default value. markdown.changeid = tip + #optional; use in with TortoiseHG or Windows + #markdown.egg = Markdown-2.3.1 [extensions] hgext.markdown=/example/hgext.markdown -##Usage with TortoiseHg## +##Usage with TortoiseHg or Windows## -If you want to use this extension with TortoiseHg, you will need to -obtain the python markdown package (http://packages.python.org/Markdown/) -and uncomment the section in [\_\_init\_\_.py](\_\_init\_\_.py) that adjusts the python module path. +If you want to use this extension with TortoiseHg or Windows, you will need to +obtain the python markdown package [http://packages.python.org/Markdown/](http://packages.python.org/Markdown/), +put Markdown-X.Y.Z.zip into extension directory and uncomment markdown.egg in config file ##Preview## -You can preview changes in your working copy before committing them by browsing to e.g. `http://localhost:8000/preview/ReadMe.markdown`. +You can preview changes in your working copy before committing them by browsing to e.g. +[http://localhost:8000/preview/ReadMe.markdown](http://localhost:8000/preview/ReadMe.markdown). ##Security## diff -r 34d537e83b7a -r ade5266185db __init__.py --- a/__init__.py Sat Jun 01 17:42:46 2013 +0700 +++ b/__init__.py Sun Oct 06 17:36:21 2013 +0700 @@ -1,10 +1,8 @@ import os import sys -# If using TortoiseHg, obtain Python-Markdown and tell Python where to find it: -#sys.path.append("c:/python27/Lib/site-packages/markdown-2.2.0-py2.7.egg") - -import logging, markdown, mimetypes, codecs +# markdown import is in extsetup function +import logging, 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 @@ -13,185 +11,196 @@ logging.basicConfig() def preview_markdown(web, req, tmpl): - f = req.form.get('node', [''])[0] - path = req.form.get('file', [''])[0] - if path: - f = f + '/' + path + f = req.form.get('node', [''])[0] + path = req.form.get('file', [''])[0] + if path: + f = f + '/' + path - if not f: - f = find_working_copy_readme(web.repo.root) + if not f: + f = find_working_copy_readme(web.repo.root) - parts = os.path.splitext(f) + parts = os.path.splitext(f) - try: - text = codecs.open(web.repo.root + "/" + f, "rb", "utf-8").read() - except IOError: - raise ErrorResponse(HTTP_NOT_FOUND, 'path not found: ' + f) + try: + 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) + if not parts[-1] == '.markdown' and not parts[-1] == '.md': + return preview_sendraw(web, req, f, text) - base_url = tmpl.defaults['url'] + 'preview/' - base_raw_url = tmpl.defaults['url'] + 'preview/' + base_url = tmpl.defaults['url'] + 'preview/' + base_raw_url = tmpl.defaults['url'] + 'preview/' - def rebase(proc, e, attr): - uri = e.get(attr, '') - if '://' in uri or uri.startswith('/') or uri.startswith('#'): - return - base = base_url - if attr == 'src': - base = base_raw_url - e.set(attr, proc.rebase(base, uri)) + def rebase(proc, e, attr): + uri = e.get(attr, '') + if '://' in uri or uri.startswith('/') 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','toc','headerid','attr_list'], - extension_configs={ - 'wikilinks' : [('base_url', ""), ('end_url', parts[-1])]}) - html = md.convert(text).encode("utf-8") + ext = mdx_urlrebase.UrlRebaseExtension(configs=[('rebase', rebase)]) + md = Markdown( + extensions=[ext, 'wikilinks','toc','headerid','attr_list'], + extension_configs={ + 'wikilinks' : [('base_url', ""), ('end_url', parts[-1])]}) + html = md.convert(text).encode("utf-8") - args = {'file':f, - 'readmefilename':parts[0].split('/')[-1], - 'path':webutil.up(f), - 'readme':html, - 'rev':'tip', - 'node':''} + args = {'file':f, + 'readmefilename':parts[0].split('/')[-1], + 'path':webutil.up(f), + 'readme':html, + 'rev':'tip', + 'node':''} - return tmpl("markdown", **args) + return tmpl("markdown", **args) def preview_sendraw(web, req, path, data): - guessmime = web.configbool('web', 'guessmime', False) + guessmime = web.configbool('web', 'guessmime', False) - if util.binary(data): - mt = 'application/binary' - else: - mt = 'text/plain' + if util.binary(data): + mt = 'application/binary' + else: + mt = 'text/plain' - if guessmime: - mt = mimetypes.guess_type(path)[0] - if mt is None: - mt = binary(text) and 'application/binary' or 'text/plain' - if mt.startswith('text/'): - mt += '; charset="%s"' % encoding.encoding + if guessmime: + mt = mimetypes.guess_type(path)[0] + if mt is None: + mt = binary(text) and 'application/binary' or 'text/plain' + if mt.startswith('text/'): + mt += '; charset="%s"' % encoding.encoding - mt += '; Content-length="%s"' % len(data) - req.respond(HTTP_OK, mt, path, data) - return data + mt += '; Content-length="%s"' % len(data) + req.respond(HTTP_OK, mt, path, data) + return data def file_markdown(orig, web, req, tmpl): - f = req.form.get('file', [''])[0] - parts = os.path.splitext(f) + f = req.form.get('file', [''])[0] + parts = os.path.splitext(f) - if 'file' not in req.form: - return webcommands.manifest(web, req, tmpl) + if 'file' not in req.form: + return webcommands.manifest(web, req, tmpl) - if not parts[-1] == '.markdown' and not parts[-1] == '.md': - return orig(web, req, tmpl) + if not parts[-1] == '.markdown' and not parts[-1] == '.md': + return orig(web, req, tmpl) - try: - fctx = webutil.filectx(web.repo, req) - changeid = fctx.hex()[0:12] - text = fctx.data().decode("utf-8") - except LookupError, inst: - try: - return webcommands.manifest(web, req, tmpl) - except ErrorResponse: - raise inst + try: + fctx = webutil.filectx(web.repo, req) + changeid = fctx.hex()[0:12] + text = fctx.data().decode("utf-8") + except LookupError, inst: + try: + return webcommands.manifest(web, req, tmpl) + except ErrorResponse: + raise inst - if util.binary(text): - return webcommands.rawfile(web, req, tmpl) + if util.binary(text): + return webcommands.rawfile(web, req, tmpl) - base_url = tmpl.defaults['url'] + 'file/' + changeid + "/" - base_raw_url = tmpl.defaults['url'] + 'rawfile/' + changeid + "/" + 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('/') or uri.startswith('#'): - return - base = base_url - if attr == 'src': - base = base_raw_url - e.set(attr, proc.rebase(base, uri)) + def rebase(proc, e, attr): + uri = e.get(attr, '') + if '://' in uri or uri.startswith('/') 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','toc','headerid','attr_list'], - extension_configs={ - 'wikilinks' : [('base_url', ""), ('end_url', parts[-1])]}) - html = md.convert(text).encode("utf-8") + ext = mdx_urlrebase.UrlRebaseExtension(configs=[('rebase', rebase)]) + md = markdown.Markdown( + extensions=[ext, 'wikilinks','toc','headerid','attr_list'], + extension_configs={ + 'wikilinks' : [('base_url', ""), ('end_url', parts[-1])]}) + html = md.convert(text).encode("utf-8") - args = {'file':f, - 'readmefilename':parts[0].split('/')[-1], - 'path':webutil.up(f), - 'readme':html} + args = {'file':f, + 'readmefilename':parts[0].split('/')[-1], + 'path':webutil.up(f), + 'readme':html} - args.update({'rev':fctx.rev(), - 'node':fctx.hex(), - 'author':fctx.user(), - 'date':fctx.date(), - 'desc':fctx.description(), - 'branch':webutil.nodebranchnodefault(fctx), - 'parent':webutil.parents(fctx), - 'child':webutil.children(fctx)}) + args.update({'rev':fctx.rev(), + 'node':fctx.hex(), + 'author':fctx.user(), + 'date':fctx.date(), + 'desc':fctx.description(), + 'branch':webutil.nodebranchnodefault(fctx), + 'parent':webutil.parents(fctx), + 'child':webutil.children(fctx)}) - return tmpl("markdown", **args) + return tmpl("markdown", **args) def summary_markdown(orig, web, req, tmpl): - """ - Decorates the default summary view by adding 'readme' and 'readmefile' content - to the template. - """ + """ + Decorates the default summary view by adding 'readme' and 'readmefile' content + to the template. + """ - changeid = web.config('web', 'markdown.changeid', 'tip') - text = None + 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().decode("utf-8") - filesuffix = '.' + filename.split('.')[-1] - readmefile = filename - break + 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().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 + "/" + 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('/') or uri.startswith('#'): - return - base = base_url - if attr == 'src': - base = base_raw_url - e.set(attr, proc.rebase(base, uri)) + def rebase(proc, e, attr): + uri = e.get(attr, '') + if '://' in uri or uri.startswith('/') 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','toc','headerid','attr_list'], - extension_configs={ - 'wikilinks' : [('base_url', base_url), ('end_url', filesuffix)]}) - readme = md.convert(text).encode("utf-8") - else: - readmefile = "ReadMe" - readme = "Add ReadMe.md or ReadMe.markdown to this repository to display it here." + ext = mdx_urlrebase.UrlRebaseExtension(configs=[('rebase', rebase)]) + md = Markdown( + extensions=[ext, 'wikilinks','toc','headerid','attr_list'], + extension_configs={ + 'wikilinks' : [('base_url', base_url), ('end_url', filesuffix)]}) + 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 + tmpl.defaults['readmefilename'] = readmefile + tmpl.defaults['readme'] = readme - return orig(web, req, tmpl) + return orig(web, req, tmpl) def find_working_copy_readme(dir): - for filename in os.listdir(dir): - if filename.lower() == 'readme.md' or filename.lower() == 'readme.markdown': - return filename + for filename in os.listdir(dir): + if filename.lower() == 'readme.md' or filename.lower() == 'readme.markdown': + return filename + +def extsetup(ui): + extensions.wrapfunction(webcommands, 'file', file_markdown) + extensions.wrapfunction(webcommands, 'summary', summary_markdown) + webcommands.preview = preview_markdown + webcommands.__all__.append('preview') -def extsetup(): - extensions.wrapfunction(webcommands, 'file', file_markdown) - extensions.wrapfunction(webcommands, 'summary', summary_markdown) - webcommands.preview = preview_markdown - webcommands.__all__.append('preview') + try: + from markdown import Markdown + global Markdown + except ImportError: + dir = os.path.dirname(os.path.realpath(__file__)) + md = ui.config("web", "markdown.egg", "Markdown-2.3.1") + ui.debug("Markdown not found search for egg in local dir %s for %s.zip\n" % (dir, md)) + sys.path.append(os.path.join(dir, "%s.zip\%s" % (md, md))) + from markdown import Markdown + global Markdown