Home Download Buy Blog Forum Support

Noobie question here...

Noobie question here...

Postby deafmetal on Tue Apr 19, 2011 1:31 pm

Hello,

I haven't worked with python or c++ before and i've run into an error I can't figure out in an html entities conversion plugin i'm trying to write.

here's the error:
Code: Select all
Traceback (most recent call last):
  File "./sublime_plugin.py", line 255, in run_
  File "./unicodeToEntities.py", line 19, in run
    self.view.replace(region, txt)
Boost.Python.ArgumentError: Python argument types in
    View.replace(View, Region, unicode)
did not match C++ signature:
    replace(SP<TextBufferView>, SP<Edit>, SelectionRegion, std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >)


and here's my code:

Code: Select all
import sublime, sublime_plugin

from htmlentitydefs import codepoint2name

class UnicodeToEntitiesCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      htmlentities = list()
      for region in self.view.sel(): 
         if not region.empty():
            s = self.view.substr(region)
            for c in s:
               if ord(c) < 128:
                  htmlentities.append(c)
               else:
                  htmlentities.append('&%s;' % codepoint2name[ord(c)])

      txt = ''.join(htmlentities)
      print txt, "txt"
      self.view.replace(region, txt)


Could any kind person help me out with this?

Thanks.
deafmetal
 
Posts: 2
Joined: Tue Apr 19, 2011 1:24 pm

Re: Noobie question here...

Postby bizoo on Tue Apr 19, 2011 2:45 pm

deafmetal wrote:
Code: Select all
import sublime, sublime_plugin

from htmlentitydefs import codepoint2name

class UnicodeToEntitiesCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      htmlentities = list()
      for region in self.view.sel(): 
         if not region.empty():
            s = self.view.substr(region)
            for c in s:
               if ord(c) < 128:
                  htmlentities.append(c)
               else:
                  htmlentities.append('&%s;' % codepoint2name[ord(c)])

      txt = ''.join(htmlentities)
      print txt, "txt"
      self.view.replace(region, txt)


Could any kind person help me out with this?

Thanks.

In SublimeText 2 you have to pass the edit object to the replace method, like the error message tell you:
Code: Select all
self.view.replace(edit, region, txt)

And it's probably not safe to use the region variable outside the for loop, you better use a new local variable.
By the way your region variable reference the last selected region, is it what you want to do or is this an indentation error ?

Good luck.
bizoo
 
Posts: 873
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Noobie question here...

Postby deafmetal on Tue Apr 19, 2011 2:56 pm

Thanks bizoo, that was exactly what i was looking for. Also yes, that was a mistake only using the last selected region.

Here is the revised plugin code:
Code: Select all
import sublime, sublime_plugin

from htmlentitydefs import codepoint2name

class UnicodeToEntitiesCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      for region in self.view.sel(): 
         htmlentities = list()
         if not region.empty():
            s = self.view.substr(region)
            for c in s:
               if ord(c) < 128:
                  htmlentities.append(c)
               else:
                  htmlentities.append('&%s;' % codepoint2name[ord(c)])

            txt = u''.join(htmlentities)
            self.view.replace(edit, region, txt)
deafmetal
 
Posts: 2
Joined: Tue Apr 19, 2011 1:24 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 4 guests