Skip to content

Commit

Permalink
Migrated zen syntax test to tea_actions; added syntax-awareness to Wr…
Browse files Browse the repository at this point in the history
…ap With Abbreviation
  • Loading branch information
onecrayon committed Jan 6, 2010
1 parent 115c0b7 commit 4259ecd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
36 changes: 36 additions & 0 deletions src/Support/Library/tea_actions.py
Expand Up @@ -6,6 +6,7 @@
from Foundation import *

import html_replace
from zencoding import html_matcher as html_matcher

# ===============================================================
# Interact with the user and output information
Expand Down Expand Up @@ -407,6 +408,41 @@ def indent_snippet(context, snippet, range):
snippet += current_indent
return snippet

# ===============================================================
# Check document syntax methods
# ===============================================================

def get_zen_doctype(context, default='html'):
'''
Tests the document to see if it is CSS or XSL; for use with zen
coding actions to determine type of snippets to use
'''
doc_type = default
css_exts = ['css', 'less']
xsl_exts = ['xsl', 'xslt']
path = context.path()
if path is not None:
pos = path.rfind('.')
if pos != -1:
pos += 1
ext = path[pos:]
if ext in css_exts:
doc_type = 'css'
elif ext in xsl_exts:
doc_type = 'xsl'
# No luck with the extension; check for inline style tags
if doc_type == 'html':
range = get_range(context)
cursor = range.location + range.length
content = context.string()
start, end = html_matcher.match(content, cursor)
tag = html_matcher.last_match['opening_tag']
if tag is not None:
tag = tag.name
if tag == 'style':
doc_type = 'css'
return doc_type

# ===============================================================
# Insertion methods
# ===============================================================
Expand Down
26 changes: 1 addition & 25 deletions src/Support/Scripts/TEASnippetWithWord.py
Expand Up @@ -5,7 +5,6 @@
import tea_actions as tea
from zencoding import zen_core, settings_loader
from zencoding.zen_settings import zen_settings
from zencoding import html_matcher as html_matcher

def act(controller, bundle, options):
context = tea.get_context(controller)
Expand Down Expand Up @@ -55,30 +54,7 @@ def place_ins_point(text):
zen_core.insertion_point = place_ins_point

# Determine doctype as best we can based on file extension
doc_type = 'html'
css_exts = ['css', 'less']
xsl_exts = ['xsl', 'xslt']
path = context.path()
if path is not None:
pos = path.rfind('.')
if pos != -1:
pos += 1
ext = path[pos:]
if ext in css_exts:
doc_type = 'css'
elif ext in xsl_exts:
doc_type = 'xsl'
# No luck with the extension; check for inline style tags
if doc_type == 'html':
range = tea.get_range(context)
cursor = range.location + range.length
content = context.string()
start, end = html_matcher.match(content, cursor)
tag = html_matcher.last_match['opening_tag']
if tag is not None:
tag = tag.name
if tag == 'style':
doc_type = 'css'
doc_type = tea.get_zen_doctype(context)

# Prepare the snippet
snippet = zen_core.expand_abbreviation(fullword, doc_type, 'xhtml')
Expand Down
4 changes: 2 additions & 2 deletions src/Support/Scripts/TEAWrapWithAbbreviation.py
Expand Up @@ -79,8 +79,8 @@ def is_space(char):
rng = tea.new_range(start, end - start)
text = tea.get_selection(context, rng)

# NEED A WAY TO DETECT DOCUMENT TYPE
doc_type = 'html'
# Fetch the doctype based on file extension
doc_type = tea.get_zen_doctype(context)

text = self.unindent(context, text)

Expand Down

0 comments on commit 4259ecd

Please sign in to comment.