Created
January 15, 2012 18:30
-
-
Save liamcain/1616696 to your computer and use it in GitHub Desktop.
Cloze Deletion? on ST2???
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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