1
###############################################################################
3
# Purpose: Simple input assistant for html and xml. #
4
# Author: Cody Precord #
5
# Copyright: (c) 2009 Cody Precord <staff@editra.org> #
6
# License: wxWindows License #
7
###############################################################################
10
Simple autocompletion support for HTML and XML documents.
14
__author__ = "Cody Precord <cprecord@editra.org>"
15
__cvsid__ = "$Id: htmlcomp.py 70229 2012-01-01 01:27:10Z CJP $"
16
__revision__ = "$Revision: 70229 $"
18
#--------------------------------------------------------------------------#
27
#--------------------------------------------------------------------------#
29
TAGS = ['!--', 'a', 'abbr', 'accept', 'accesskey', 'acronym', 'action',
30
'address', 'align', 'alink', 'alt', 'applet', 'archive', 'area',
31
'article', 'aside', 'audio', 'axis', 'b', 'background', 'base',
32
'basefont', 'bdo', 'bgcolor', 'big', 'blockquote', 'body', 'border',
33
'bordercolor', 'br', 'button', 'canvas', 'caption', 'cellpadding',
34
'cellspacing', 'center', 'char', 'charoff', 'charset', 'checked',
35
'cite', 'cite', 'class', 'classid', 'clear', 'code', 'codebase',
36
'codetype', 'col', 'colgroup', 'color', 'cols', 'colspan', 'command',
37
'compact', 'content', 'coords', 'data', 'datetime', 'datalist', 'dd',
38
'declare', 'defer', 'del', 'details', 'dfn', 'dialog', 'dir', 'dir',
39
'disabled', 'div', 'dl', 'dt', 'dtml-call', 'dtml-comment', 'dtml-if',
40
'dtml-in', 'dtml-let', 'dtml-raise', 'dtml-tree', 'dtml-try',
41
'dtml-unless', 'dtml-var', 'dtml-with', 'em', 'embed', 'enctype',
42
'face', 'fieldset', 'figcaption', 'figure', 'font', 'for', 'form',
43
'footer', 'frame', 'gutter', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head',
44
'header', 'headers', 'height', 'hgroup', 'hr', 'href', 'hreflang',
45
'hspace', 'html', 'http-equiv', 'i', 'id', 'iframe', 'img', 'input',
46
'ins', 'isindex', 'ismap', 'kbd', 'keygen', 'label', 'lang', 'language',
47
'legend', 'li', 'link', 'link', 'longdesc', 'lowsrc', 'map',
48
'marginheight', 'marginwidth', 'mark', 'maxlength', 'menu', 'meta',
49
'meter', 'method', 'multiple', 'name', 'nav', 'nohref', 'noscript',
50
'nowrap', 'object', 'ol', 'optgroup', 'option', 'output', 'p', 'param',
51
'pre', 'profile', 'progress', 'prompt', 'q', 'readonly', 'rel', 'rev',
52
'rows', 'rowspan', 'rp', 'rt', 'ruby', 'rules', 's', 'samp', 'scheme',
53
'scope', 'script', 'scrolling', 'section', 'select', 'selected',
54
'shape', 'size', 'small', 'source', 'span', 'src', 'standby', 'start',
55
'strike', 'strong', 'style', 'sub', 'summary', 'sup', 'tabindex',
56
'table', 'target', 'tbody', 'td', 'text', 'textarea', 'tfoot', 'th',
57
'thead', 'time', 'title', 'tr', 'tt', 'type', 'u', 'ul', 'url',
58
'usemap', 'valign', 'value', 'valuetype', 'var', 'version', 'video',
59
'vlink', 'vspace', 'width', 'wrap', 'xmp']
61
# Tags that usually have a new line inbetween them
62
NLINE_TAGS = ('body', 'head', 'html', 'ol', 'style', 'table', 'tbody', 'ul')
64
TAG_RE = re.compile("\<\s*([a-zA-Z][a-zA-Z0-9]*)")
66
PHP_AREA = [wx.stc.STC_HPHP_COMMENT, wx.stc.STC_HPHP_COMMENTLINE,
67
wx.stc.STC_HPHP_COMPLEX_VARIABLE, wx.stc.STC_HPHP_DEFAULT,
68
wx.stc.STC_HPHP_HSTRING, wx.stc.STC_HPHP_HSTRING_VARIABLE,
69
wx.stc.STC_HPHP_NUMBER, wx.stc.STC_HPHP_OPERATOR,
70
wx.stc.STC_HPHP_SIMPLESTRING,
71
wx.stc.STC_HPHP_VARIABLE, wx.stc.STC_HPHP_WORD]
73
HTML_AREA = [wx.stc.STC_H_ASP, wx.stc.STC_H_ASPAT, wx.stc.STC_H_ATTRIBUTE,
74
wx.stc.STC_H_ATTRIBUTEUNKNOWN, wx.stc.STC_H_CDATA,
75
wx.stc.STC_H_COMMENT, wx.stc.STC_H_DEFAULT,
76
wx.stc.STC_H_DOUBLESTRING, wx.stc.STC_H_ENTITY,
77
wx.stc.STC_H_NUMBER, wx.stc.STC_H_OTHER, wx.stc.STC_H_QUESTION,
78
wx.stc.STC_H_SCRIPT, wx.stc.STC_H_SGML_1ST_PARAM,
79
wx.stc.STC_H_SGML_1ST_PARAM_COMMENT,
80
wx.stc.STC_H_SGML_BLOCK_DEFAULT, wx.stc.STC_H_SGML_COMMAND,
81
wx.stc.STC_H_SGML_COMMENT, wx.stc.STC_H_SGML_DEFAULT,
82
wx.stc.STC_H_SGML_DOUBLESTRING, wx.stc.STC_H_SGML_ENTITY,
83
wx.stc.STC_H_SGML_ERROR, wx.stc.STC_H_SGML_SIMPLESTRING,
84
wx.stc.STC_H_SGML_SPECIAL, wx.stc.STC_H_SINGLESTRING,
85
wx.stc.STC_H_TAG, wx.stc.STC_H_TAGEND,
86
wx.stc.STC_H_TAGUNKNOWN, wx.stc.STC_H_VALUE,
87
wx.stc.STC_H_XCCOMMENT, wx.stc.STC_H_XMLEND,
88
wx.stc.STC_H_XMLSTART]
90
#--------------------------------------------------------------------------#
92
class Completer(completer.BaseCompleter):
93
"""HTML/XML Code completion provider"""
94
def __init__(self, stc_buffer):
95
super(Completer, self).__init__(stc_buffer)
98
self.SetAutoCompKeys([ord('>'), ord('<')])
99
self.SetAutoCompStops(' ')
100
self.SetAutoCompFillups('')
102
def GetAutoCompList(self, command):
103
"""Returns the list of possible completions for a
105
@param command: command lookup is done on
108
if command in [None, u'']:
111
buff = self.GetBuffer()
112
cpos = buff.GetCurrentPos()
114
# Check if we are in a php region or not
115
if buff.GetStyleAt(cpos) not in HTML_AREA:
118
# Get current context
119
cline = buff.GetCurrentLine()
120
ccol = buff.GetColumn(cpos)
121
tmp = buff.GetLine(cline).rstrip()
123
tmp = tmp[:ccol].rstrip()
125
# Check if we are completing an open tag (i.e < was typed)
126
if tmp.endswith('<'):
127
if buff.GetLexer() == wx.stc.STC_LEX_XML:
128
taglst = _FindXmlTags(buff.GetText())
131
return completer.CreateSymbols(taglst, completer.TYPE_ELEMENT)
133
# Check for a self closing tag (i.e />)
134
endchk = tmp.strip().replace(u" ", u"").replace(u"\t", u"")
135
if endchk.endswith(u"/>"):
138
# Try to autocomplete a closing tag (if necessary)
139
tmp = tmp.rstrip('>').rstrip()
140
if len(tmp) and (tmp[-1] in '"\' \t' or tmp[-1].isalpha()):
141
# Walk backwards from the current line
142
for line in range(cline, -1, -1):
143
txt = buff.GetLine(line)
145
txt = txt[:buff.GetColumn(cpos)]
149
parts = txt[idx:].lstrip('<').strip().split()
151
tag = parts[0].rstrip('>')
153
tag not in ('img', 'br', '?php', '?xml', '?') and \
154
not tag[0] in ('!', '/'):
155
rtag = u"</" + tag + u">"
157
if not parts[-1].endswith('>'):
159
return [ completer.Symbol(rtag, completer.TYPE_ELEMENT) ]
164
def OnCompletionInserted(self, pos, text):
165
"""Handle adjusting caret position after some insertions.
166
@param pos: position caret was at before insertion
167
@param text: text that was inserted
170
buff = self.GetBuffer()
171
if text.strip().startswith(u"</"):
172
buff.SetCurrentPos(pos) # move caret back between the tags
173
# HACK: SetCurrentPos causes text to be selected
174
buff.SetSelection(pos, pos)
176
#--------------------------------------------------------------------------#
178
def _FindXmlTags(text):
179
"""Dynamically generate a list of possible xml tags based on tags found in
185
matches = TAG_RE.findall(text)
187
matches.append(u'!--')
188
matches = list(set(matches))