def handleMatch(self, m): node = ImagePattern.handleMatch(self, m) image_src = node.attrib["src"] if not image_src.startswith("http"): if image_src.startswith("/"): node.attrib["src"] = "{0}{1}".format(self.config.root_path, image_src[1:]) return node
class BaseUrlImagePattern(Pattern): """ Adds base url to images which have relative path. """ url_pattern = re.compile( r'^(?:http|ftp)s?://' # http:// or https:// r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain... r'localhost|' # localhost... r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip r'(?::\d+)?' # optional port r'(?:/?|[/?]\S+)$', re.IGNORECASE) def __init__(self, images_dir, *args): super().__init__(*args) if images_dir: self.images_dir = images_dir.strip('/') else: self.images_dir = '' self.image_pattern = ImagePattern(*args) def _is_url(self, text): url = text.strip().strip('/').split(' ')[0] return self.url_pattern.match(url) def handleMatch(self, m): if self._is_url(m.group(10)): image = m.string else: image = IMAGE_PATTERN.format(m.group(2), self.images_dir, m.group(10).strip('/')) pattern = re.compile("^(.*?)%s(.*?)$" % self.image_pattern.pattern, re.DOTALL | re.UNICODE) match = re.match(pattern, ' ' + image + ' ') return self.image_pattern.handleMatch(match)
def handleMatch(self, match): node = ImagePattern.handleMatch(self, match) # check 'src' to ensure it is local src = node.attrib.get('src') storage_class = get_storage_class(STATICBLOG_STORAGE) storage = storage_class() # otherwise we need to do some downloading! if 'http://' in src or 'https://' in src: img_data = urllib2.urlopen(src).read() md5 = hashlib.md5() md5.update(img_data) name = md5.hexdigest() + '/' + os.path.basename(src) else: with open(STATICBLOG_POST_DIRECTORY + src, 'r') as fhandle: img_data = fhandle.read() name = src print >> sys.stderr, 'Uploading ' + src try: storage.save(name, ContentFile(img_data)) node.attrib['src'] = storage.url(name) print >> sys.stderr, 'Uploaded ' + src + ' to ' + storage.url(name) except Exception as e: print >> sys.stderr, str(e) print >> sys.stderr, '\033[01;31mUpload of %s failed\033[0m' % src return node
def handleMatch(self, match): node = ImagePattern.handleMatch(self, match) # check 'src' to ensure it is local src = node.attrib.get('src') storage_class = get_storage_class(STATICBLOG_STORAGE) storage = storage_class() # otherwise we need to do some downloading! if 'http://' in src or 'https://' in src: img_data = urllib2.urlopen(src).read() md5 = hashlib.md5() md5.update(img_data) name = md5.hexdigest() + '/' + os.path.basename(src) else: with open(STATICBLOG_POST_DIRECTORY + src, 'r') as fhandle: img_data = fhandle.read() name = src print >> sys.stderr, 'Uploading ' + src try: storage.save(name, ContentFile(img_data)) node.attrib['src'] = storage.url(name) print >> sys.stderr, 'Uploaded ' + src + ' to ' + storage.url(name) except Exception as e: print >> sys.stderr, str(e) print >> sys.stderr, '\033[01;31mUpload of %s failed\033[0m' % src return node
def handleMatch(self, m): image = ImagePattern.handleMatch(self, m) title = image.get('title') if title: figure = etree.Element('figure') figure.append(image) etree.SubElement(figure, 'figcaption').text = title return figure else: return image
def handleMatch(self, m): image = ImagePattern.handleMatch(self, m) title = image.get('title') if title: figure = etree.Element('figure') figure.append(image) etree.SubElement(figure, 'figcaption').text = title return figure else: return image
def handleMatch(self, m): # get node node = ImagePattern.handleMatch(self, m) # get image src src = node.attrib.get('src') # parse image url output_asset = self.asset_processor.asset_urlparse(src) if output_asset: node.attrib['src'] = output_asset return node
def handleMatch(self, m): node = ImagePattern.handleMatch(self, m) src = node.attrib.get('src') if src.endswith('.gif'): node.attrib.pop('src') node.set('data-gifffer', src) node.set('data-gifffer-width', '240') return node else: anode = util.etree.Element("a") anode.set('data-lightbox', str(uuid.uuid4())) anode.set('href', src) anode.append(node) return anode
class ImageLinkPattern(Pattern): def __init__(self, mdi): Pattern.__init__(self, IMAGE_LINK_RE, mdi) self.link = LinkPattern(LINK_RE, mdi) self.image = ImagePattern(IMAGE_LINK_RE, mdi) def handleMatch(self, m): img = self.image.handleMatch(m) img.set('style', 'max-width:100%;') anchor = util.etree.Element("a") anchor.set('href', img.get('src')) anchor.set('target', '_blank') anchor.append(img) return anchor
def handleMatch(self, m): node = ImagePattern.handleMatch(self, m) node = handle_image_element(node, self.__base_dir) return node