Exemple #1
0
	def testImageSize(self):
		size = {'width': 25, 'height': 25}
		
		self.assertEqual(size, zen.get_image_size(zen_file.read('./assets/sample.png')))
		self.assertEqual({'width': 125, 'height': 125}, zen.get_image_size(zen_file.read('./assets/sample2.png')))
		self.assertEqual(size, zen.get_image_size(zen_file.read('./assets/sample.gif')))
		self.assertEqual({'width': 300, 'height': 300}, zen.get_image_size(zen_file.read('./assets/sample2.gif')))
		self.assertEqual(size, zen.get_image_size(zen_file.read('./assets/sample.jpg')))
Exemple #2
0
def update_image_size(editor):
    """
	Updates <img> tag's width and height attributes
	@type editor: ZenEditor
	@since: 0.65
	"""
    editor_file = editor.get_file_path()
    caret_pos = editor.get_caret_pos()

    if editor_file is None:
        raise ZenError("You should save your file before using this action")

    image = _find_image(editor)
    if image:
        # search for image path
        m = re.search(r'src=(["\'])(.+?)\1', image['tag'], re.IGNORECASE)
        if m:
            src = zen_file.locate_file(editor.get_file_path(), m.group(2))
            if not src:
                raise ZenError("Can't locate file %s" % m.group(2))

            stream = zen_file.read(src)
            if not stream:
                raise ZenError("Can't read file %s" % src)

            size = zen_coding.get_image_size(zen_file.read(src))
            if size:
                new_tag = _replace_or_append(image['tag'], 'width',
                                             size['width'])
                new_tag = _replace_or_append(new_tag, 'height', size['height'])

                # try to preserve caret position
                if caret_pos < image['start'] + len(new_tag):
                    relative_pos = caret_pos - image['start']
                    new_tag = new_tag[:
                                      relative_pos] + zen_coding.get_caret_placeholder(
                                      ) + new_tag[relative_pos:]

                editor.replace_content(new_tag, image['start'], image['end'])
                return True

    return False
Exemple #3
0
def update_image_size(editor):
	"""
	Updates <img> tag's width and height attributes
	@type editor: ZenEditor
	@since: 0.65
	"""
	editor_file = editor.get_file_path()
	caret_pos = editor.get_caret_pos()
		
	if editor_file is None:
		raise ZenError("You should save your file before using this action")
	
	image = _find_image(editor)
	if image:
		# search for image path
		m = re.search(r'src=(["\'])(.+?)\1', image['tag'], re.IGNORECASE)
		if m:
			src = zen_file.locate_file(editor.get_file_path(), m.group(2))
			if not src:
				raise ZenError("Can't locate file %s" % m.group(2))
			
			stream = zen_file.read(src)
			if not stream:
				raise ZenError("Can't read file %s" % src)
			
			size = zen_coding.get_image_size(zen_file.read(src))
			if size:
				new_tag = _replace_or_append(image['tag'], 'width', size['width'])
				new_tag = _replace_or_append(new_tag, 'height', size['height'])
				
				# try to preserve caret position
				if caret_pos < image['start'] + len(new_tag):
					relative_pos = caret_pos - image['start']
					new_tag = new_tag[:relative_pos] + zen_coding.get_caret_placeholder() + new_tag[relative_pos:]
				
				editor.replace_content(new_tag, image['start'], image['end'])
				return True
				
	return False