pmacs3/mode/xml.py

76 lines
2.8 KiB
Python

import color, method, mode
from lex import Grammar, PatternRule, RegionRule
class StringGrammar1(Grammar):
rules = [PatternRule(r'data', r'[^"]+')]
class StringGrammar2(Grammar):
rules = [PatternRule(r'data', r"[^']+")]
class CommentGrammar(Grammar):
rules = [PatternRule(r'data', r'(?:[^-]|-(?!-)|--(?!>))+')]
class MetadataGrammar(Grammar):
rules = [PatternRule(r'data', r'(?:[^?]|\?(?!>))+')]
class TagGrammar(Grammar):
rules = [
PatternRule(r'attrname', r'[a-zA-Z_][a-zA-Z0-9_]+(?==)'),
PatternRule(r'namespace', r'[a-zA-Z_]+(?=:)'),
PatternRule(r'name', r'[a-zA-Z_][a-zA-Z0-9_]+'),
PatternRule(r'delimiter', r'[/=]'),
RegionRule(r'string', r'"', StringGrammar1, r'"'),
RegionRule(r'string', r"'", StringGrammar2, r"'"),
PatternRule(r'spaces', r' +'),
PatternRule(r'eol', r'\n'),
]
class XMLGrammar(Grammar):
rules = [
# TODO: how does cdata work again?
PatternRule(r'data', r'[^<& \n]+'),
PatternRule(r'spaces', r' +'),
RegionRule(r'comment', r'<!--', CommentGrammar, r'-->'),
RegionRule(r'xml_tag', r'<', TagGrammar, r'/?>'),
PatternRule(r'xml_entity', r'&[a-z]+;'),
PatternRule(r'eol', r'\n'),
RegionRule(r'metadata', r'<\?', MetadataGrammar, r'\?>'),
]
class XmlValidate(method.shell.Exec):
'''Valid the buffer's contents as valid XML.'''
show_success = True
args = []
def _execute(self, w, **vargs):
self._doit(w, w.buffer.path, 'xmlwf %(path)r', cmdname='xml-validate')
class XmlCreateTag(method.Method):
''''''
args = [method.Argument('tagname', prompt="Tag Name: ", help="Create an open/close tag pair")]
def _execute(self, w, **vargs):
t = vargs['tagname']
w.insert_string_at_cursor("<%s>" % t)
p = w.logical_cursor()
w.insert_string_at_cursor("</%s>" % t)
w.goto(p)
class XML(mode.Fundamental):
modename = 'XML'
extensions = ['.xml', '.xml.in', '.xsl', '.xsd']
grammar = XMLGrammar
colors = {
'xml_tag.start': ('default', 'default', 'bold'),
'xml_tag.namespace': ('magenta', 'default', 'bold'),
'xml_tag.name': ('blue', 'default', 'bold'),
'xml_tag.attrname': ('cyan', 'default', 'bold'),
'xml_tag.string.start': ('green', 'default', 'bold'),
'xml_tag.string.null': ('green', 'default', 'bold'),
'xml_tag.string.end': ('green', 'default', 'bold'),
'xml_tag.end': ('default', 'default', 'bold'),
'xml_entity': ('magenta', 'default', 'bold'),
}
actions = [XmlValidate, XmlCreateTag]
def __init__(self, w):
mode.Fundamental.__init__(self, w)
self.add_bindings('xml-create-tag', ('M-t',))
install = XML.install