-
Notifications
You must be signed in to change notification settings - Fork 7
/
hipsteripsum.py
146 lines (126 loc) · 5.59 KB
/
hipsteripsum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import sublime
import sublime_plugin
import threading
import urllib
import json
if int(sublime.version()) >= 3000:
from . import requests
else:
import requests
def err(theError):
print("[Hipster Ipsum: " + theError + "]")
class HipsterIpsumCommand(sublime_plugin.TextCommand):
def run(self, edit):
s = sublime.load_settings("Hipster Ipsum.sublime-settings")
defaultParas = s.get("paragraphs", 1)
ipsumType = s.get("ipsum_type", "hipster-centric")
useHTML = "false" if s.get("html", False) == False else "true"
selections = self.view.sel()
threads = []
passedThreads = 0
skippedThreads = 0
for theSelection in selections:
theSubstring = self.view.substr(theSelection)
if len(theSubstring) == 0:
newThread = HipsterIpsumAPICall(theSelection, defaultParas, ipsumType, useHTML, "")
threads.append(newThread)
newThread.start()
passedThreads += 1
else:
try:
parasHere = int(theSubstring)
except ValueError:
newThread = HipsterIpsumAPICall(theSelection, defaultParas, ipsumType, useHTML, theSubstring)
threads.append(newThread)
newThread.start()
passedThreads += 1
else:
if parasHere < 1:
err("%i is too few paragraphs." % parasHere)
err("Select a number between 1 and 99.")
sublime.status_message("Hipster Ipsum: Too few paragraphs (%i)." % parasHere)
skippedThreads += 1
elif parasHere > 99:
err("%i is too many paragraphs." % parasHere)
err("Select a number between 1 and 99.")
sublime.status_message("Hipster Ipsum: Too many paragraphs (%i)." % parasHere)
skippedThreads += 1
else:
newThread = HipsterIpsumAPICall(theSelection, parasHere, ipsumType, useHTML, theSubstring)
threads.append(newThread)
newThread.start()
passedThreads += 1
if passedThreads > 0:
self.view.sel().clear()
self.manageThreads(threads)
else:
sublime.status_message("Hipster Ipsum: No authentic selections.")
err("Skipped %i selections." % skippedThreads)
def manageThreads(self, theThreads, offset=0, i=0, direction=1):
next_threads = []
for thread in theThreads:
if thread.is_alive():
next_threads.append(thread)
continue
if thread.result == False:
continue
offset = self.replace(thread, offset)
theThreads = next_threads
if len(theThreads):
before = i % 8
after = 7 - before
if not after:
direction = -1
if not before:
direction = 1
i += direction
self.view.set_status("hipster_ipsum", "Gentrifying... [%s=%s]" % (" " * before, " " * after))
sublime.set_timeout(lambda: self.manageThreads(theThreads, offset, i, direction), 100)
return
self.view.erase_status("hipster_ipsum")
selections = len(self.view.sel())
sublime.status_message("%s area%s gentrified." % (selections, '' if selections == 1 else 's'))
def replace(self, theThread, offset):
selection = theThread.selection
original = theThread.original
result = theThread.result
if offset:
selection = sublime.Region(selection.begin() + offset, selection.end() + offset)
result = self.normalize_line_endings(result)
self.view.run_command("hipster_ipsum_replace", {"begin": selection.begin(), "end": selection.end(), "data": result})
endpoint = selection.begin() + len(result)
self.view.sel().add(sublime.Region(endpoint, endpoint))
return offset + len(result) - len(original)
def normalize_line_endings(self, string):
string = string.replace('\n', '\n\n')
string = string.replace('\r\n', '\n').replace('\r', '\n')
line_endings = self.view.settings().get('default_line_ending')
if line_endings == 'windows':
string = string.replace('\n', '\r\n')
elif line_endings == 'mac':
string = string.replace('\n', '\r')
return string
class HipsterIpsumAPICall(threading.Thread):
def __init__(self, theSelection, numParagraphs, ipsumType, useHTML, originalString):
self.selection = theSelection
self.paragraphs = numParagraphs
self.ipsumType = ipsumType
self.useHTML = useHTML
self.original = originalString
self.result = None
threading.Thread.__init__(self)
def run(self):
params = {"paras": self.paragraphs, "type": self.ipsumType, "html": self.useHTML}
try:
r = requests.get("http://hipsterjesus.com/api/", params=params)
except Exception as e:
err("Exception: %s" % e)
self.result = False
data = r.json()
self.result = data["text"]
class HipsterIpsumReplaceCommand(sublime_plugin.TextCommand):
def run(self, edit, begin, end, data):
a = long(begin) if int(sublime.version()) < 3000 else begin
b = long(end) if int(sublime.version()) < 3000 else end
region = sublime.Region(a, b)
self.view.replace(edit, region, data)