def do_paragraph(self): start_linecol = int(self.code_window\ .subwidget("text").index("sel.first linestart")\ .split(".")[0]) self.mark_error_offset = start_linecol - 1 code = self.code_window.subwidget("text").get("sel.first linestart",\ "sel.last lineend") code = code.replace("\t"," "*8) starting_ws = "" for i in range(len(code)): if not code[i].isspace(): break starting_ws += code[i] deindented_code = [] for line in code.split("\n"): add_empty_line = True for i in range(len(line)): if i >= len(starting_ws) or (starting_ws[i] != line[i]): deindented_code.append(line[i:]) starting_ws = starting_ws[:i] add_empty_line = False break if add_empty_line: deindented_code.append("") self.runcode("\n".join(deindented_code))
def emit(template, **kwargs): path = os.path.dirname(os.path.abspath(__file__)) # Read the template text from the file with open(os.path.join(path, "Templates", template + ".template"), "r") as file: code = file.read() # Replace variables with their associated values for key, value in kwargs.items(): code = code.replace("$({})".format(key), value) return code
def OnEditExecClipboard(self, command, code): """ Executes python code directly from the clipboard.""" win32clipboard.OpenClipboard() try: code=win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT) finally: win32clipboard.CloseClipboard() code=code.replace('\r\n','\n')+'\n' try: o=compile(code, '<clipboard>', 'exec') exec(o, __main__.__dict__) except: traceback.print_exc()
def OnEditExecClipboard(self, command, code): """ Executes python code directly from the clipboard.""" win32clipboard.OpenClipboard() try: code = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT) finally: win32clipboard.CloseClipboard() code = code.replace("\r\n", "\n") + "\n" try: o = compile(code, "<clipboard>", "exec") exec o in __main__.__dict__ except: traceback.print_exc()
def OnEditExecClipboard(self, command, code): """ Executes python code directly from the clipboard.""" win32clipboard.OpenClipboard() try: code=win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT) finally: win32clipboard.CloseClipboard() code=code.replace('\r\n','\n')+'\n' try: o=compile(code, '<clipboard>', 'exec') exec o in __main__.__dict__ except: traceback.print_exc()
def OnEditExecClipboard(self, command, code): """Executes python code directly from the clipboard.""" win32clipboard.OpenClipboard() try: code = win32clipboard.GetClipboardData( win32clipboard.CF_UNICODETEXT) finally: win32clipboard.CloseClipboard() code = code.replace("\r\n", "\n") + "\n" try: o = compile(code, "<clipboard>", "exec") exec(o, __main__.__dict__) except: traceback.print_exc()
def parse(value, valid_tags=VALID_TAGS, valid_attrs=VALID_ATTRS): """Cleans non-allowed HTML from the input. Keyword arguments: value -- String valid_tags -- String valid_attrs -- String Returns: String """ valid_tags = valid_tags.split() valid_attrs = valid_attrs.split() value = value.replace('"', '"').replace('&', '&') soup = VeryBeautifulSoup(value) for tag in soup.findAll(True): if tag.name not in valid_tags: tag.hidden = True if tag.name == 'user': tag.replaceWith('<a class="user_tag user_tag_%s" href="/user/%s/">%s</a>' % (tag.string, tag.string, tag.string)) if tag.name == 'quote': tag.replaceWith('<div class="quote">%s</div>' % (tag.__unicode__().replace('<quote>', ' ').replace('</quote>', ' '), )) if tag.name == 'spoiler': tag.replaceWith('<div class="spoiler">%s</div>' % (tag.__unicode__().replace('<spoiler>',' ').replace('</spoiler>',' '), )) for attr, val in tag.attrs: if attr in ('src', 'href') and val.find('javascript') == 0: tag.hidden = True if tag.name == 'code' and attr == 'lang': try: lexer = get_lexer_by_name(val, encoding='utf-8', stripall=True, startinline=True) except ClassNotFound: lexer = get_lexer_by_name('text') formatter = HtmlFormatter(encoding='utf-8', style='colorful', linenos='table', cssclass='highlight', lineanchors="line") code = tag.__unicode__() code_model = Code() code_model.code = code code_model.lang = val code_model.save() code = highlight(code, lexer, formatter) code = code.replace('<table class="highlighttable">', '<table class="highlighttable" id="%d">' % (code_model.id,)) tag.replaceWith(code) if tag.name == 'iframe' and attr == 'src' and (val.find('http://www.youtube.com/embed/') != 0 and val.find('http://player.vimeo.com/video/') != 0): tag.hidden = True tag.attrs = [(attr, val) for attr, val in tag.attrs if attr in valid_attrs] return soup.renderContents().decode('utf8').replace('\n', '<br />')
def do_code(self): self.mark_error_offset = 0 code = self.code_window.subwidget("text").get(1.0,END) code = code.replace("\t"," "*8) self.runcode(code)