2008-05-18 22:04:39 -04:00
|
|
|
import os, urllib
|
2008-05-18 22:09:44 -04:00
|
|
|
import color, default, method, mode
|
2007-10-21 20:52:48 -04:00
|
|
|
from lex import Grammar, PatternRule, RegionRule
|
2007-07-21 11:40:53 -04:00
|
|
|
from mode.xml import TagGrammar
|
2008-04-02 19:50:18 -04:00
|
|
|
from mode.javascript import JavascriptGrammar
|
|
|
|
from mode.css import CSSGrammar
|
2007-07-21 11:40:53 -04:00
|
|
|
|
|
|
|
class HTMLGrammar(Grammar):
|
|
|
|
rules = [
|
|
|
|
# TODO: how does cdata work again?
|
|
|
|
RegionRule(r'comment', r'<!--', Grammar, r'-->'),
|
2008-04-02 19:50:18 -04:00
|
|
|
# BUG: not all scripts are javascript and not all styles are CSS
|
|
|
|
# but, dynamically choosing a grammar based on the 'type' attribute
|
|
|
|
# (which may be on a different line) is impractical.
|
2007-07-21 11:40:53 -04:00
|
|
|
RegionRule(r'script', r'<(?=script[^a-zA-Z0-9_])', TagGrammar, r'>', JavascriptGrammar, r'</(?=script>)', TagGrammar, r'>'),
|
2008-04-02 19:50:18 -04:00
|
|
|
RegionRule(r'style', r'<(?=style[^a-zA-Z0-9_])', TagGrammar, r'>', CSSGrammar, r'</(?=style>)', TagGrammar, r'>'),
|
2007-07-21 11:40:53 -04:00
|
|
|
RegionRule(r'tag', r'</?', TagGrammar, r'/?>'),
|
2008-05-06 02:03:47 -04:00
|
|
|
PatternRule(r'entity', r'&.*?;'),
|
2007-07-21 11:40:53 -04:00
|
|
|
]
|
|
|
|
|
2008-04-16 10:44:24 -04:00
|
|
|
class HtmlViewPage(method.Method):
|
2008-04-18 23:32:08 -04:00
|
|
|
'''View the HTML data in a browser (curses or external)'''
|
2008-04-16 10:44:24 -04:00
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
viewcmd = w.application.config.get('html.viewcmd')
|
|
|
|
viewbg = viewcmd.endswith('&')
|
|
|
|
if viewbg:
|
|
|
|
viewcmd = viewcmd[:-1]
|
|
|
|
argv = (viewcmd, w.buffer.path)
|
|
|
|
if viewbg:
|
|
|
|
if os.fork() == 0:
|
|
|
|
try:
|
|
|
|
os.execvp(viewcmd, argv)
|
|
|
|
except OSError:
|
|
|
|
os._exit(1)
|
|
|
|
else:
|
|
|
|
w.application.run_external(*argv)
|
|
|
|
|
2008-05-18 22:04:39 -04:00
|
|
|
class HtmlValidatePage(method.Method):
|
|
|
|
'''View the HTML data in a browser (curses or external)'''
|
|
|
|
args = [method.arg('url', dv=default.build_mode_var('url'), ld=True,
|
|
|
|
h='')]
|
|
|
|
base = 'http://validator.w3.org/check?'
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
w.mode.url = vargs['url']
|
|
|
|
viewcmd = w.application.config.get('html.viewcmd')
|
|
|
|
viewbg = viewcmd.endswith('&')
|
|
|
|
if viewbg:
|
|
|
|
viewcmd = viewcmd[:-1]
|
|
|
|
urlarg = urllib.urlencode({'uri': w.mode.url})
|
|
|
|
argv = (viewcmd, self.base + urlarg)
|
|
|
|
if viewbg:
|
|
|
|
if os.fork() == 0:
|
|
|
|
try:
|
|
|
|
os.execvp(viewcmd, argv)
|
|
|
|
except OSError:
|
|
|
|
os._exit(1)
|
|
|
|
else:
|
|
|
|
w.application.run_external(*argv)
|
|
|
|
|
2008-04-04 19:16:21 -04:00
|
|
|
class HtmlCheckSpelling(method.Method):
|
|
|
|
"""Check the spelling of the document via ispell -t"""
|
|
|
|
def _execute(self, w, **vargs):
|
|
|
|
# -x no backup file
|
|
|
|
# -M show context menu
|
|
|
|
# -H treat input document as HTML
|
|
|
|
w.application.run_external('ispell', '-x', '-M', '-H', w.buffer.path)
|
|
|
|
if w.buffer.changed_on_disk():
|
|
|
|
w.buffer.reload()
|
|
|
|
|
2008-04-18 23:32:08 -04:00
|
|
|
class HTML(mode.Fundamental):
|
|
|
|
modename = 'HTML'
|
|
|
|
extensions = ['.html', '.htm', '.shtml', '.shtm', '.xhtml']
|
|
|
|
grammar = HTMLGrammar
|
2008-05-06 02:03:47 -04:00
|
|
|
colors = {
|
|
|
|
'entity': ('magenta', 'default', 'bold'),
|
|
|
|
}
|
2008-04-18 23:32:08 -04:00
|
|
|
config = {
|
|
|
|
'html.viewcmd': 'firefox&',
|
2008-05-18 22:09:44 -04:00
|
|
|
#'html.viewcmd': 'links',
|
2008-04-18 23:32:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_colorbase = {
|
2008-05-03 13:31:30 -04:00
|
|
|
'start': ('default', 'default', 'bold'),
|
|
|
|
'namespace': ('magenta', 'default', 'bold'),
|
|
|
|
'name': ('blue', 'default', 'bold'),
|
|
|
|
'attrname': ('cyan', 'default', 'bold'),
|
|
|
|
'string.start': ('green', 'default', 'bold'),
|
|
|
|
'string.null': ('green', 'default', 'bold'),
|
|
|
|
'string.end': ('green', 'default', 'bold'),
|
|
|
|
'end': ('default', 'default', 'bold'),
|
2008-04-18 23:32:08 -04:00
|
|
|
}
|
|
|
|
for _name in _colorbase:
|
|
|
|
colors['script.%s' % _name] = _colorbase[_name]
|
|
|
|
colors['style.%s' % _name] = _colorbase[_name]
|
|
|
|
colors['tag.%s' % _name] = _colorbase[_name]
|
2008-05-18 22:09:44 -04:00
|
|
|
actions = [HtmlViewPage, HtmlValidatePage, HtmlCheckSpelling]
|
2008-04-18 23:32:08 -04:00
|
|
|
|
|
|
|
def __init__(self, w):
|
|
|
|
mode.Fundamental.__init__(self, w)
|
|
|
|
self.add_bindings('close-paren', (')',))
|
|
|
|
self.add_bindings('close-brace', ('}',))
|
|
|
|
self.add_bindings('close-bracket', (']',))
|
2008-05-06 01:23:58 -04:00
|
|
|
self.add_bindings('xml-create-tag', ('M-t',))
|
2008-05-18 22:04:39 -04:00
|
|
|
self.url = None
|
2008-04-18 23:32:08 -04:00
|
|
|
|
2007-10-19 02:41:33 -04:00
|
|
|
install = HTML.install
|