Exemple #1
0
def encode_to_base64(editor, img_path, pos):
    """
	Encodes image to base64
	@requires: zen_file
	
	@type editor: ZenEditor
	@param img_path: Path to image
	@type img_path: str
	@param pos: Caret position where image is located in the editor
	@type pos: int
	@return: bool
	"""
    editor_file = editor.get_file_path()
    default_mime_type = 'application/octet-stream'

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

    # locate real image path
    real_img_path = zen_file.locate_file(editor_file, img_path)
    if real_img_path is None:
        raise ZenError("Can't find %s file" % img_path)

    b64 = base64.b64encode(zen_file.read(real_img_path))
    if not b64:
        raise ZenError("Can't encode file content to base64")

    b64 = 'data:' + (mime_types[zen_file.get_ext(real_img_path)]
                     or default_mime_type) + ';base64,' + b64

    editor.replace_content('$0' + b64, pos, pos + len(img_path))
    return True
Exemple #2
0
def encode_to_base64(editor, img_path, pos):
	"""
	Encodes image to base64
	@requires: zen_file
	
	@type editor: ZenEditor
	@param img_path: Path to image
	@type img_path: str
	@param pos: Caret position where image is located in the editor
	@type pos: int
	@return: bool
	"""
	editor_file = editor.get_file_path()
	default_mime_type = 'application/octet-stream'
		
	if editor_file is None:
		raise ZenError("You should save your file before using this action")
	
	
	# locate real image path
	real_img_path = zen_file.locate_file(editor_file, img_path)
	if real_img_path is None:
		raise ZenError("Can't find %s file" % img_path)
	
	b64 = base64.b64encode(zen_file.read(real_img_path))
	if not b64:
		raise ZenError("Can't encode file content to base64")
	
	
	b64 = 'data:' + (mime_types[zen_file.get_ext(real_img_path)] or default_mime_type) + ';base64,' + b64
	
	editor.replace_content('$0' + b64, pos, pos + len(img_path))
	return True
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
Exemple #4
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