1 # ##### BEGIN GPL LICENSE BLOCK #####
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software Foundation,
15 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # ##### END GPL LICENSE BLOCK #####
21 "author": "Dalai Felinto (dfelinto)",
24 "location": "Text editor > Properties panel",
25 "description": "Send your selection or text to www.pasteall.org",
26 "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"\
27 "Scripts/Text_Editor/PasteAll",
28 "tracker_url": "https://projects.blender.org/tracker/index.php?"\
29 "func=detail&aid=23493",
30 "category": "Text Editor"}
32 # ########################################################
33 # PasteAll.org Text Sender Script
35 # Dalai Felinto (dfelinto)
36 # blenderecia.orgfree.com
38 # Rio de Janeiro - Brasil
41 # Original code: 23rd August 2010 (Blender 2.5.3 rev. 31525)
44 # This script is not official. I did it for fun and for my own usage.
45 # And please do not abuse of their generosity - use it wisely (a.k.a no flood).
47 # ########################################################
55 class TEXT_PT_pasteall(bpy.types.Panel):
56 bl_space_type = 'TEXT_EDITOR'
58 bl_label = "PasteAll.org"
60 def draw(self, context):
62 layout.operator("text.pasteall", icon='URL')
63 layout.prop(context.scene, "use_webbrowser")
65 class TEXT_OT_pasteall(bpy.types.Operator):
67 bl_idname = "text.pasteall"
68 bl_label = "PasteAll.org"
69 bl_description = "Send the current text or selection to www.pasteall.org"
72 def poll(cls, context):
73 if context.area.type != 'TEXT_EDITOR':
76 return context.space_data.text != None
78 def invoke(self, context, event):
80 st = context.space_data
82 # get the selected text
83 text = self.get_selected_text(st.text)
84 # if no text is selected send the whole file
85 if text is None: text = st.text.as_string()
87 # get the file type based on the extension
88 format = self.get_file_format(st.text)
90 # send the text and receive the returned page
91 html = self.send_text(text, format)
94 self.report({'ERROR'}, "Error in sending the text to the server.")
97 # get the link of the posted page
98 page = self.get_page(str(html))
100 if page is None or page == "":
101 self.report({'ERROR'}, "Error in retrieving the page.")
104 self.report({'INFO'}, page)
106 # store the link in the clipboard
107 bpy.context.window_manager.clipboard = page
109 if context.scene.use_webbrowser:
111 webbrowser.open_new_tab(page)
113 self.report({'WARNING'}, "Error in opening the page %s." % (page))
117 def send_text(self, text, format):
120 url = "http://www.pasteall.org/index.php"
121 values = { 'action' : 'savepaste',
123 'language_id': format,
127 data = urllib.parse.urlencode(values).encode()
128 req = urllib.request.Request(url, data)
129 response = urllib.request.urlopen(req)
130 page_source = response.read()
136 def get_page(self, html):
138 id = html.find('directlink')
139 id_begin = id + 12 # hardcoded: directlink">
140 id_end = html[id_begin:].find("</a>")
142 if id != -1 and id_end != -1:
143 return html[id_begin:id_begin + id_end]
147 def get_selected_text(self, text):
149 current_line = text.current_line
150 select_end_line = text.select_end_line
152 current_character = text.current_character
153 select_end_character = text.select_end_character
155 # if there is no selected text return None
156 if current_line == select_end_line:
157 if current_character == select_end_character:
160 return current_line.body[min(current_character,select_end_character):max(current_character,select_end_character)]
164 normal_order = True # selection from top to bottom
166 for line in text.lines:
168 if line == current_line:
169 text_return = current_line.body[current_character:] + "\n"
172 elif line == select_end_line:
173 text_return = select_end_line.body[select_end_character:] + "\n"
179 if line == select_end_line:
180 text_return += select_end_line.body[:select_end_character]
183 text_return += line.body + "\n"
186 if line == current_line:
187 text_return += current_line.body[:current_character]
190 text_return += line.body + "\n"
195 def get_file_format(self, text):
196 """Try to guess what is the format based on the file extension"""
197 extensions = {'diff':'24',
203 type = text.name.split(".")[-1]
204 return extensions.get(type, '0')
207 bpy.types.Scene.use_webbrowser = bpy.props.BoolProperty(
208 name='Launch Browser',
209 description='Opens the page with the submitted text.',
212 bpy.utils.register_module(__name__)
215 del bpy.types.Scene.use_webbrowser
216 bpy.utils.unregister_module(__name__)
218 if __name__ == "__main__":