def getHtmlFromClipboard(): """ Retrieve HTML source from clipboard. Returns a tuple (source, URL) where source is the HTML sourcecode and URL is the URL where it came from. Both or one of the items may be None. """ from StringOps import lineendToInternal, mbcsDec cb = wx.TheClipboard cb.Open() try: df = wx.CustomDataFormat("HTML Format") dataob = wx.CustomDataObject(df) if cb.GetData(dataob): if dataob.GetSize() > 0: raw = dataob.GetData() # Windows HTML clipboard format contains a header with additional # information start = None end = None sourceUrl = None canBreak = lambda : start is not None and end is not None \ and sourceUrl is not None pos = 0 try: for line in raw.split("\r\n"): if line.startswith("StartFragment:"): start = int(line[14:]) if canBreak(): break elif line.startswith("EndFragment:"): end = int(line[14:]) if canBreak(): break elif line.startswith("SourceURL:"): sourceUrl = line[10:] if canBreak(): break pos += len(line) + 2 if start is not None and pos >= start: break except ValueError: return (None, None) if start is None or end is None: return (None, None) return (lineendToInternal( dataob.GetData()[start:end]).decode( "utf-8", "replace"), sourceUrl) return (None, None) finally: cb.Close()
def getHtmlFromClipboard(): """ Retrieve HTML source from clipboard. Returns a tuple (source, URL) where source is the HTML sourcecode and URL is the URL where it came from. Both or one of the items may be None. """ from StringOps import lineendToInternal, mbcsDec cb = wx.TheClipboard cb.Open() try: df = wx.CustomDataFormat("HTML Format") dataob = wx.CustomDataObject(df) if cb.GetData(dataob): if dataob.GetSize() > 0: raw = dataob.GetData() # Windows HTML clipboard format contains a header with additional # information start = None end = None sourceUrl = None canBreak = lambda : start is not None and end is not None \ and sourceUrl is not None pos = 0 try: for line in raw.split("\r\n"): if line.startswith("StartFragment:"): start = int(line[14:]) if canBreak(): break elif line.startswith("EndFragment:"): end = int(line[14:]) if canBreak(): break elif line.startswith("SourceURL:"): sourceUrl = line[10:] if canBreak(): break pos += len(line) + 2 if start is not None and pos >= start: break except ValueError: return (None, None) if start is None or end is None: return (None, None) return (lineendToInternal(dataob.GetData()[start:end]).decode( "utf-8", "replace"), sourceUrl) return (None, None) finally: cb.Close()
def getTextFromClipboard(): """ Retrieve text or unicode text from clipboard """ from StringOps import lineendToInternal, mbcsDec cb = wx.TheClipboard cb.Open() try: dataob = textToDataObject() if cb.GetData(dataob): if dataob.GetTextLength() > 0: return lineendToInternal(dataob.GetText()) else: return u"" return None finally: cb.Close()
def getHtmlFromClipboard(): """ Retrieve HTML source from clipboard. Returns a tuple (source, URL) where source is the HTML sourcecode and URL is the URL where it came from. Both or one of the items may be None. For GTK second item is always None. """ from StringOps import lineendToInternal, mbcsDec cb = wx.TheClipboard cb.Open() try: dataob = wx.CustomDataObject(wx.CustomDataFormat("text/html")) if cb.GetData(dataob): if dataob.GetSize() > 0: raw = dataob.GetData() return (lineendToInternal(StringOps.fileContentToUnicode( raw)), None) finally: cb.Close() return (None, None)
def getHtmlFromClipboard(): """ Retrieve HTML source from clipboard. Returns a tuple (source, URL) where source is the HTML sourcecode and URL is the URL where it came from. Both or one of the items may be None. For GTK second item is always None. """ from StringOps import lineendToInternal, mbcsDec cb = wx.TheClipboard cb.Open() try: dataob = wx.CustomDataObject(wx.CustomDataFormat("text/html")) if cb.GetData(dataob): if dataob.GetSize() > 0: raw = dataob.GetData() return (lineendToInternal( StringOps.fileContentToUnicode(raw)), None) finally: cb.Close() return (None, None)