我正在尝试为程序Sublimetext2构建一个插件。
它使用Python编码的插件。我根本没有Python知识,但是通过查看现有的插件,我的PHP知识就是我需要帮助的...
到目前为止,这是Python文件的开头
import sublime, sublime_plugin
import webbrowser
settings = sublime.load_settings('openonserver.sublime-settings')
settings.get('file_path_prefix')
settings.get('server_url')
class OpenonServerCommand(sublime_plugin.TextCommand):
def run(self,edit):
file_path = self.view.file_name()
虽然需要设置值
,但我需要做什么 file_path
将是我运行此文件的路径,所以我们说...
E:\Server\htdocs\mytest_project_\some\folder_\test.php
设置
file_path_prefix
将为E:\Server\htdocs\
和
server_url
将为http://localhost/
我需要查看file_path_prefix
是否存在file_path
,如果有,
我需要将E:\Server\htdocs\
替换为http://localhost/
并将所有\
替换为/
,然后将此新路径存储在变量中
...这样
E:\Server\htdocs\mytest_project_\some\folder_\test.php
将成为
http://localhost/mytest_project_/some/folder_/test.php
然后我需要将其发送到浏览器。
非常感谢任何帮助
答案 0 :(得分:1)
答案 1 :(得分:0)
好几个小时之后好了(我现在讨厌Python)我的解决方案(我没有留下深刻的印象)但部分工作
#Context.sublime-menu
[
{ "command": "openserver", "caption": "Open on Server" }
]
#Default (Windows).sublime-keymap
[
{ "keys": ["ctrl+shift+b"], "command": "openserver" }
]
#Main.sublime-menu
[
{
"caption": "Tools",
"mnemonic": "T",
"id": "tools",
"children":
[
{ "command": "openserver", "caption": "Open on Server" }
]
}
]
#Openserver.sublime-commands
[
{
"caption": "Open file on Server in Browser",
"command": "openserver"
}
]
#Openserver.sublime-settings
{
"file_path_prefix": "E:/Server/htdocs",
"url_prefix": "http://localhost"
}
主档
#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)
现在,当我说它有效但部分没有时,它确实需要文件名,从设置文件中替换我的路径和服务器URL,然后启动带有正确URL的浏览器
除了在Sublimetext2中运行.py文件或任何你没有设置为能够在Web浏览器中打开的文件,然后在Web浏览器中打开文件时它将给出窗口弹出窗口要求设置默认程序来打开文件,非常烦人!