Sublime Forum

Change file path and open in localhost browser

#1

I have never used Python before. However I am trying to create a simple plugin that will allow me to open my PHP, HTML, HTM files in a web browser

I will show you the parts I have so far and hopefully someone with more knowledge can help me complete this.

FILE = Context.sublime-menu

{ "command": "openonserver", "caption": "Open on Server" } ]

FILE = openonserver.sublime-commands

{ "caption": "Open file on Server", "command": "openonserver" } ]

FILE= openonserver.sublime-settings { "server_url": "http://localhost/", "local_file_path": "E:\Server\htdocs\", }

So what I have is

[list=]
*]The code to add a context menu so I can just simply right click in file and click to open it on the server
*]Code to add it as a command
*]A setting file where I can set the file path to my dev server where the files I will be opening are located and a setting to set the url to where it should open the file in the browser
[/list]

What I need the .py file to do is take the full file path and if the value for setting local_file_path exist in the path, it should replace it with the value of setting server_url

So a file path of

E:\Server\htdocs\mytest_project_\some\folder_\test.php

would get replaced with a value of

http://localhost/mytest_project_/some/folder_/test.php

I then need it to launch the web browser with this modified path

I don’t think this should be too hard but I know nothing of Python, if someone could help me I would appreciate it and I think others will find this useful as well

0 Likes

#2

Ok I am sure, actually I am positive someone with more Python knowledge can improve this as this is my first time ever touching Python code (I never worked with a language where a space will kill the program)

Anyways feel free to improve.

The only issue I have ran into so far, is when I run this on any kind of file that does not have a default program set for opening it, it will give me the popup window asking to choose a program. In a perfect world this would open the file in my web browser regardless of what file extension it has.

What this plugin does?
It allows you to set a path to your local dev server and a path to where you access these files on your dev server on the browser. For me I have the path E:\Server\htdocs set to the URL http://localhost so if I am editing a file in Sublimetext2 that is located @ E:\Server\htdocs\coolproject\testing.php I can then run this from a hotket, a right click context menu, or the tools menu and have it open the file in my web browser at the location http://localhost/coolproject/testing.php

[code]#openserver.py

import sublime, sublime_plugin
import os
import webbrowser
import re
import os2emxpath
import logging
import sys

class OpenserverCommand(sublime_plugin.TextCommand):
def run(self,edit):
file_path = self.view.file_name()

settings = sublime.load_settings('Openserver.sublime-settings')

file = os2emxpath.normpath(file_path)

url = re.sub(settings.get('file_path_prefix'), settings.get('url_prefix'), file)
#logging.warning(url)

#webbrowser.open_new(url)
if sys.platform=='win32':
    os.startfile(url)
elif sys.platform=='darwin':
    subprocess.Popen('open', url])
else:
    try:
        subprocess.Popen('xdg-open', url])
    except OSError:
        logging.warning(url)[/code]

#Openserver.sublime-settings { "file_path_prefix": "E:/Server/htdocs", "url_prefix": "http://localhost" }

[code]#Openserver.sublime-command

{
    "caption": "Open file on Server in Browser",
    "command": "openserver"
}

][/code]

[code]#Main.sublime-menu

{
    "caption": "Tools",
    "mnemonic": "T",
    "id": "tools",
    "children":
    
        { "command": "openserver", "caption": "Open on Server" }
    ]
}

][/code]

[code]#Context.sublime-menu

{ "command": "openserver", "caption": "Open on Server" }

][/code]

0 Likes