示例#1
0
def insert_formatted_newline(editor, mode='html'):
    """
	Inserts newline character with proper indentation
	@param editor: Editor instance
	@type editor: ZenEditor
	@param mode: Syntax mode (only 'html' is implemented)
	@type mode: str
	"""
    caret_pos = editor.get_caret_pos()
    nl = zen_coding.get_newline()
    pad = zen_coding.get_variable('indentation')

    if mode == 'html':
        # let's see if we're breaking newly created tag
        pair = html_matcher.get_tags(editor.get_content(),
                                     editor.get_caret_pos(),
                                     editor.get_profile_name())

        if pair[0] and pair[1] and pair[0]['type'] == 'tag' and pair[0][
                'end'] == caret_pos and pair[1]['start'] == caret_pos:
            editor.replace_content(
                nl + pad + zen_coding.get_caret_placeholder() + nl, caret_pos)
        else:
            editor.replace_content(nl, caret_pos)
    else:
        editor.replace_content(nl, caret_pos)

    return True
示例#2
0
def split_join_tag(editor, profile_name=None):
    """
	Splits or joins tag, e.g. transforms it into a short notation and vice versa:
	<div></div> → <div /> : join
	<div /> → <div></div> : split
	@param editor: Editor instance
	@type editor: ZenEditor
	@param profile_name: Profile name
	@type profile_name: str
	"""
    caret_pos = editor.get_caret_pos()
    profile = zen_coding.get_profile(profile_name or editor.get_profile_name())
    caret = zen_coding.get_caret_placeholder()

    # find tag at current position
    pair = html_matcher.get_tags(editor.get_content(), caret_pos, profile_name
                                 or editor.get_profile_name())
    if pair and pair[0]:
        new_content = pair[0].full_tag

        if pair[1]:  # join tag
            closing_slash = ''
            if profile['self_closing_tag'] is True:
                closing_slash = '/'
            elif profile['self_closing_tag'] == 'xhtml':
                closing_slash = ' /'

            new_content = re.sub(r'\s*>$', closing_slash + '>', new_content)

            # add caret placeholder
            if len(new_content) + pair[0].start < caret_pos:
                new_content += caret
            else:
                d = caret_pos - pair[0].start
                new_content = new_content[0:d] + caret + new_content[d:]

            editor.replace_content(new_content, pair[0].start, pair[1].end)
        else:  # split tag
            nl = zen_coding.get_newline()
            pad = zen_coding.get_variable('indentation')

            # define tag content depending on profile
            tag_content = profile[
                'tag_nl'] is True and nl + pad + caret + nl or caret

            new_content = '%s%s</%s>' % (re.sub(
                r'\s*\/>$', '>', new_content), tag_content, pair[0].name)
            editor.replace_content(new_content, pair[0].start, pair[0].end)

        return True
    else:
        return False
示例#3
0
def split_join_tag(editor, profile_name=None):
	"""
	Splits or joins tag, e.g. transforms it into a short notation and vice versa:
	<div></div> → <div /> : join
	<div /> → <div></div> : split
	@param editor: Editor instance
	@type editor: ZenEditor
	@param profile_name: Profile name
	@type profile_name: str
	"""
	caret_pos = editor.get_caret_pos()
	profile = zen_coding.get_profile(profile_name or editor.get_profile_name())
	caret = zen_coding.get_caret_placeholder()

	# find tag at current position
	pair = html_matcher.get_tags(editor.get_content(), caret_pos, profile_name or editor.get_profile_name())
	if pair and pair[0]:
		new_content = pair[0].full_tag
		
		if pair[1]: # join tag
			closing_slash = ''
			if profile['self_closing_tag'] is True:
				closing_slash = '/'
			elif profile['self_closing_tag'] == 'xhtml':
				closing_slash = ' /'
				
			new_content = re.sub(r'\s*>$', closing_slash + '>', new_content)
			
			# add caret placeholder
			if len(new_content) + pair[0].start < caret_pos:
				new_content += caret
			else:
				d = caret_pos - pair[0].start
				new_content = new_content[0:d] + caret + new_content[d:]
			
			editor.replace_content(new_content, pair[0].start, pair[1].end)
		else: # split tag
			nl = zen_coding.get_newline()
			pad = zen_coding.get_variable('indentation')
			
			# define tag content depending on profile
			tag_content = profile['tag_nl'] is True and nl + pad + caret + nl or caret
			
			new_content = '%s%s</%s>' % (re.sub(r'\s*\/>$', '>', new_content), tag_content, pair[0].name)
			editor.replace_content(new_content, pair[0].start, pair[0].end)
		
		return True
	else:
		return False
示例#4
0
def expand_abbreviation_with_tab(editor, syntax, profile_name='xhtml'):
	"""
	A special version of <code>expandAbbreviation</code> function: if it can't
	find abbreviation, it will place Tab character at caret position
	@param editor: Editor instance
	@type editor: ZenEditor
	@param syntax: Syntax type (html, css, etc.)
	@type syntax: str
	@param profile_name: Output profile name (html, xml, xhtml)
	@type profile_name: str
	"""
	if not expand_abbreviation(editor, syntax, profile_name):
		editor.replace_content(zen_coding.get_variable('indentation'), editor.get_caret_pos())
	
	return True 
示例#5
0
def expand_abbreviation_with_tab(editor, syntax, profile_name='xhtml'):
	"""
	A special version of <code>expandAbbreviation</code> function: if it can't
	find abbreviation, it will place Tab character at caret position
	@param editor: Editor instance
	@type editor: ZenEditor
	@param syntax: Syntax type (html, css, etc.)
	@type syntax: str
	@param profile_name: Output profile name (html, xml, xhtml)
	@type profile_name: str
	"""
	if not expand_abbreviation(editor, syntax, profile_name):
		editor.replace_content(zen_coding.get_variable('indentation'), editor.get_caret_pos())

	return True
示例#6
0
    def get_profile_name(self):
        """
		Returns current output profile name (@see zen_coding#setup_profile)
		@return {String}
		"""
        forced_profile = zen.get_variable("profile")
        if forced_profile:
            return forced_profile

        close_string = tea.get_tag_closestring(self._context)
        if close_string == "/":
            return "xml"
        elif close_string != " /":
            return "html"
        else:
            return "xhtml"
示例#7
0
	def get_profile_name(self):
		"""
		Returns current output profile name (@see zen_coding#setup_profile)
		@return {String}
		"""
		forced_profile = zen.get_variable('profile')
		if forced_profile:
			return forced_profile
		
		close_string = tea.get_tag_closestring(self._context)
		if close_string == '/':
			return 'xml'
		elif close_string != ' /':
			return 'html'
		else:
			return 'xhtml'
示例#8
0
def insert_formatted_newline(editor, mode='html'):
	"""
	Inserts newline character with proper indentation
	@param editor: Editor instance
	@type editor: ZenEditor
	@param mode: Syntax mode (only 'html' is implemented)
	@type mode: str
	"""
	caret_pos = editor.get_caret_pos()
	nl = zen_coding.get_newline()
	pad = zen_coding.get_variable('indentation')
		
	if mode == 'html':
		# let's see if we're breaking newly created tag
		pair = html_matcher.get_tags(editor.get_content(), editor.get_caret_pos(), editor.get_profile_name())
		
		if pair[0] and pair[1] and pair[0]['type'] == 'tag' and pair[0]['end'] == caret_pos and pair[1]['start'] == caret_pos:
			editor.replace_content(nl + pad + zen_coding.get_caret_placeholder() + nl, caret_pos)
		else:
			editor.replace_content(nl, caret_pos)
	else:
		editor.replace_content(nl, caret_pos)
		
	return True