2009-03-30 01:10:25 -04:00
|
|
|
import os
|
2020-08-31 20:58:27 -04:00
|
|
|
import urllib.request, urllib.parse, urllib.error
|
2009-03-30 01:10:25 -04:00
|
|
|
import default
|
|
|
|
import method
|
|
|
|
from mode import Fundamental
|
2007-10-21 20:52:48 -04:00
|
|
|
from lex import Grammar, PatternRule, RegionRule
|
2009-04-08 00:21:29 -04:00
|
|
|
from mode.xml import TagGrammar, CommentGrammar, MetadataGrammar, CDataGrammar
|
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
|
|
|
|
2009-04-08 00:21:29 -04:00
|
|
|
class DoctypeGrammar(Grammar):
|
|
|
|
rules = [PatternRule('data', '[^>]+')]
|
2008-10-07 23:39:22 -04:00
|
|
|
|
2007-07-21 11:40:53 -04:00
|
|
|
class HTMLGrammar(Grammar):
|
|
|
|
rules = [
|
2009-05-06 14:39:26 -04:00
|
|
|
PatternRule(r'escaped', r'&[^;]+;'),
|
2009-04-08 00:21:29 -04:00
|
|
|
PatternRule(r'html.data', r'[^&<]+'),
|
2008-10-07 23:39:22 -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.
|
2009-04-08 00:21:29 -04:00
|
|
|
RegionRule('html.tag', '<(?=script[^a-zA-Z0-9_])', TagGrammar, '>',
|
|
|
|
JavascriptGrammar, '</(?=script>)', TagGrammar, '>'),
|
|
|
|
RegionRule('html.tag', '<(?=style[^a-zA-Z0-9_])', TagGrammar, '>',
|
|
|
|
CSSGrammar, '</(?=style>)', TagGrammar, '>'),
|
2009-05-06 14:39:26 -04:00
|
|
|
RegionRule('comment', r'<!--', CommentGrammar, r'-->'),
|
|
|
|
RegionRule('html.tag', r'<(?![\?!])', TagGrammar, r'/?>'),
|
|
|
|
RegionRule('html.metadata', r'<\?', MetadataGrammar, r'\?>'),
|
|
|
|
RegionRule('html.doctype', r'<!', DoctypeGrammar, r'>'),
|
|
|
|
PatternRule('html.entity', r'&[a-z]+;'),
|
|
|
|
RegionRule('html.cdata', r'<!\[CDATA\[', CDataGrammar, 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)'''
|
2008-10-07 23:39:22 -04:00
|
|
|
args = [method.arg('url', dv=default.build_mode_var('url'), ld=True, h='')]
|
2008-05-18 22:04:39 -04:00
|
|
|
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]
|
2020-08-31 20:58:27 -04:00
|
|
|
urlarg = urllib.parse.urlencode({'uri': w.mode.url})
|
2008-05-18 22:04:39 -04:00
|
|
|
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
|
2008-11-12 10:26:56 -05:00
|
|
|
if w.buffer.changed():
|
|
|
|
w.set_error("There are unsaved changes; please save first.")
|
|
|
|
return
|
2008-04-04 19:16:21 -04:00
|
|
|
w.application.run_external('ispell', '-x', '-M', '-H', w.buffer.path)
|
|
|
|
if w.buffer.changed_on_disk():
|
|
|
|
w.buffer.reload()
|
|
|
|
|
2009-03-30 01:10:25 -04:00
|
|
|
class HTML(Fundamental):
|
2009-03-17 15:24:10 -04:00
|
|
|
name = 'HTML'
|
2009-04-08 00:21:29 -04:00
|
|
|
extensions = ['.html', '.htm', '.shtml', '.shtm', '.xhtml', '.xhtm']
|
2008-04-18 23:32:08 -04:00
|
|
|
grammar = HTMLGrammar
|
2008-05-06 02:03:47 -04:00
|
|
|
colors = {
|
2009-04-08 00:21:29 -04:00
|
|
|
'html.entity': ('magenta', 'default', 'bold'),
|
|
|
|
'html.metadata.start': ('magenta', 'default', 'bold'),
|
|
|
|
'html.metadata.data': ('magenta', 'default', 'bold'),
|
|
|
|
'html.metadata.end': ('magenta', 'default', 'bold'),
|
|
|
|
'html.doctype.start': ('magenta', 'default', 'bold'),
|
|
|
|
'html.doctype.data': ('magenta', 'default', 'bold'),
|
|
|
|
'html.doctype.end': ('magenta', 'default', 'bold'),
|
|
|
|
'html.tag.start': ('default', 'default', 'bold'),
|
|
|
|
'html.tag.namespace': ('magenta', 'default', 'bold'),
|
|
|
|
'html.tag.name': ('blue', 'default', 'bold'),
|
|
|
|
'html.tag.attrname': ('cyan', 'default', 'bold'),
|
|
|
|
'html.tag.end': ('default', 'default', 'bold'),
|
2008-05-06 02:03:47 -04:00
|
|
|
}
|
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
|
|
|
}
|
2008-05-18 22:09:44 -04:00
|
|
|
actions = [HtmlViewPage, HtmlValidatePage, HtmlCheckSpelling]
|
2009-02-15 12:06:35 -05:00
|
|
|
_bindings = {
|
2009-04-08 00:21:29 -04:00
|
|
|
'close-paren': (')',),
|
|
|
|
'close-brace': ('}',),
|
|
|
|
'close-bracket': (']',),
|
2009-02-15 12:06:35 -05:00
|
|
|
'xml-create-tag': ('M-t',),
|
|
|
|
}
|
|
|
|
url = None
|
2008-04-18 23:32:08 -04:00
|
|
|
|
2007-10-19 02:41:33 -04:00
|
|
|
install = HTML.install
|