0

我正在尝试为 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\用 thehttp://localhost/替换并全部替换\/,然后将这个新路径存储在一个变量中

所以…… E:\Server\htdocs\mytest_project_\some\folder_\test.php会变成

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

然后我需要将其发送到浏览器。

任何帮助是极大的赞赏

4

2 回答 2

1

采用

os.system("path_to_browser url")

运行任何外部程序。我也建议看看这个评论

于 2011-12-15T12:24:45.947 回答
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 浏览器中打开文件,而是会弹出窗口询问设置默认程序打开文件,很烦人!

于 2011-12-15T15:32:41.227 回答