Sublime Forum

Need simple plugin help

#1

Hello,

I am new to this community and I have moderate knowledge of python programming.

I was quickly referring some functions at sublimetext.com/docs/api-reference but couldn’t figure out how to grab entire file in variable. I need to create a plugin to convert xml file to text list.

I know how to read dom using python’s xml.dom.minidom module.

[1] Only the problem is to get entire xml file into variable.
After then it’s easy to get text nodes.

i.e.

from xml.dom.minidom import parse, parseString dom = parseString(xml_string) for entries in dom.getElementsByTagName('url'): ... # node processing

[2] And once processing is done, replace entire xml content with new text list.

Please guide me for these (point 1,2) missing functions.

0 Likes

#2
import sublime, sublimeplugin

class xml2listCommand(sublimeplugin.TextCommand):
	def run(self, view, args):
		region = sublime.Region(0L, view.size())
		xml_string = view.substr(region)
		# ...
		# process xml_string and put the result in list_output
		# ...
		view.replace(region, list_output)
0 Likes

#3

**wow done! ** Google XML sitemap => URL List.

import sublime, sublimeplugin from xml.dom.minidom import parse, parseString class xml2listCommand(sublimeplugin.TextCommand): def run(self, view, args): list_output = ] region = sublime.Region(0L, view.size()) xml_string = view.substr(region) dom = parseString(xml_string) for entries in dom.getElementsByTagName('url'): list_output.append(entries.getElementsByTagName('loc').item(0).firstChild.nodeValue) list_output = "\n".join(list_output) view.replace(region, list_output)

Just a little more help needed.

Any idea, after last line how to show “Save As” dialog box :question:

0 Likes

#4

I think you can do something like this:

view.runCommand("saveas")

See sublimetext.com/docs/commands

But instead of changing the current buffer and calling ‘save as’, you could put the output in a new buffer.

0 Likes

#5

I tried that but

view.runCommand("saveAs")

seems not working.

But on console ~ + CTRL],

window.runCommand("saveAs")

is working!

While adding that line in plugin throws following error

NameError: global name 'window' is not defined

Would you guide, how to import window object inside plugin code?

I tried following… but not working.

import window from sublime import window from sublimeplugin import window

0 Likes

#6

To get a Window from a TextCommand, call window() on the view passed to you

0 Likes

#7

Also tried this one.

cmd = sublime.Window() cmd.runCommand("saveAs")

Throwing Error

RuntimeError: This class cannot be instantiated from Python
0 Likes

#8

Great tip admin. :bulb:

[quote]cmd = view.window()
cmd.runCommand(“saveAs”)[/quote]

It’s working now. :smile:

0 Likes