Skip to content

Commit

Permalink
Merge branch 'pr/14'
Browse files Browse the repository at this point in the history
Conflicts:
	SublimeServer.py
  • Loading branch information
learning committed Oct 16, 2014
2 parents 1351c78 + 3a75168 commit a8b47d2
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions SublimeServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ def load_settings():
if not s.has('defaultExtension'):
s.set('defaultExtension', defaultExtension)

# Normalize base path.
if s.has('base'):
base = s.get('base')
base = base.replace('\\', '/')
if not base.endswith('/'):
base += '/'
s.set('base', base)

sublime.save_settings('SublimeServer.sublime-settings')

# Merge project and user settings.
Expand Down Expand Up @@ -136,11 +144,13 @@ class SublimeServerHandler(BaseHTTPRequestHandler):

extensions_map = {}
defaultExtension = None
base_path = None

def version_string(self):
'''overwrite HTTP server's version string'''
return 'SublimeServer/%s Sublime/%s' % (__VERSION__, sublime.version())


def do_GET(self):
"""Serve a GET request."""

Expand Down Expand Up @@ -343,6 +353,8 @@ def translate_path(self, path):
elif path == '/markdown.js':
return sublime.packages_path() + "/SublimeServer/markdown.js"

if SublimeServer.base_path:
path = SublimeServer.base_path + path

# else, deal with path...
words = path.split('/')
Expand Down Expand Up @@ -391,8 +403,10 @@ def __init__(self):
mimetypes.init() # try to read system mime.types
SublimeServerHandler.extensions_map = mimetypes.types_map.copy()
SublimeServerHandler.extensions_map.update(settings.get('mimetypes'))
SublimeServerHandler.base_path = settings.get('base')
SublimeServerHandler.defaultExtension = settings.get('defaultExtension')
self.httpd = SublimeServerThreadMixIn(('', settings.get('port')), SublimeServerHandler)

self.setName(self.__class__.__name__)

def run(self):
Expand Down Expand Up @@ -485,10 +499,17 @@ def run(self, edit):
dic = get_directories()
url = "http://localhost:{0}/{1}"
filename = self.view.file_name()
base = settings.get('base')
# Find the file.
for k in dic:
if filename.startswith(dic[k]):
url = url.format(
settings.get('port'), k + filename[len(dic[k]):])
path = k + filename[len(dic[k]):]
# Normalize path for Windows users.
path = path.replace('\\', '/')
# Remove base path from URL. It's assumed by server.
if base and path.startswith(base):
path = path[len(base):]
url = url.format(settings.get('port'), path)
return webbrowser.open(url)
rawname = filename.split(os.path.sep)[-1]
sublime.message_dialog(
Expand Down

0 comments on commit a8b47d2

Please sign in to comment.