Skip to content

Instantly share code, notes, and snippets.

@liamcain
Created January 15, 2012 18:30
Show Gist options
  • Save liamcain/1616696 to your computer and use it in GitHub Desktop.
Save liamcain/1616696 to your computer and use it in GitHub Desktop.
Cloze Deletion? on ST2???
import sublime, sublime_plugin, re
class ClozeDeletionCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.show_input_panel("Cloze Deletion", "", self.on_done, self.on_change, self.on_cancel)
def on_change(self, inp):
view = self.window.active_view()
sel = view.sel()[0];
if inp:
regex = view.find_all(inp)
view.add_regions("cloze", regex, "comment" )
else:
biggest_words = []
for l in view.lines(view.visible_region()):
line = view.substr(l)
biggest_word = ""
for w in re.findall("\\b[A-Za-z]*\\b", line):
word = w.strip()
if len(word) > len(biggest_word):
biggest_word = word
biggest_word_reg = view.find(biggest_word, l.begin())
biggest_words.append(biggest_word_reg)
view.add_regions("cloze", biggest_words, "comment")
def on_done(self, inp):
view = self.window.active_view()
words = view.get_regions("cloze")
view.fold(words)
view.erase_regions("cloze")
def on_cancel(self):
view = self.window.active_view()
view.erase_regions("cloze")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment