pmacs3/mode/rst.py

94 lines
3.6 KiB
Python

import color, method, mode
from lex import Grammar, PatternRule, RegionRule, PatternGroupRule
from mode.text import TextInsertSpace
class RSTString(Grammar):
rules = [
PatternRule('escaped', r'\\.'),
]
class RSTGrammar(Grammar):
rules = [
PatternRule(r'title', r'^=+\n$'),
PatternRule(r'subtitle', r'^-+\n$'),
# this is kind of a hack, ugh
PatternGroupRule(r'definition', r'term', r'^[^ ]+\n', r'definition', r'^ +'),
PatternRule(r'fieldname', r'^:.+?:'),
PatternRule(r'optionname', r'^(?:--|-|/)[a-zA-Z](?: ?[-a-zA-Z_]+)'),
PatternRule(r'lineblock', r'^\|'),
RegionRule(r'blocktest', r'>>>', Grammar, '^\n'),
PatternRule(r'transition', r'^[-=.,;]{4,}\n'),
#OverridePatternRule(r'comment', r'#@@:(?P<token>[.a-zA-Z0-9_]+):(?P<mode>[.a-zA-Z0-9_]+) *$'),
#OverridePatternRule(r'code', r'^\.\. code-block:: (?P<mode>:.+)\n$'),
RegionRule(r'emphasis', r'\*', RSTString, r'\*'),
RegionRule(r'strong_emphasis', r'\*\*', RSTString, r'\*\*'),
RegionRule(r'interpreted', r'`', RSTString, r'`'),
RegionRule(r'inline_literal', r'``', RSTString, r'``'),
RegionRule(r'substitution', r'\|', RSTString, r'\|'),
PatternRule(r'anonymous', r'[a-zA-Z]+__'),
PatternRule(r'reference', r'[a-zA-Z]+_'),
PatternRule(r'footnote', r'\[[0-9]+\]_'),
PatternRule(r'citation', r'\[.+?\]_'),
PatternRule(r'rst_url', r'http://[^ ]+'),
PatternRule(r'bullet', r'^ *[-*+]'),
PatternRule(r'enumeration', r'^ *(?:[0-9]+|#)\.'),
]
class RstInsertSpace(TextInsertSpace):
limit = 75
class RstWrapParagraph(method.WrapParagraph):
limit = 75
class RST(mode.Fundamental):
modename = 'RST'
extensions = ['.rst']
grammar = RSTGrammar
colors = {
'title': ('blue', 'default', 'bold'),
#'title_over': ('blue', 'default', 'bold'),
#'title_under': ('blue', 'default', 'bold'),
'subtitle': ('cyan', 'default', 'bold'),
#'subtitle_over': ('cyan', 'default', 'bold'),
#'subtitle_under': ('cyan', 'default', 'bold'),
'definition': ('green', 'default', 'bold'),
'fieldname': ('green', 'default', 'bold'),
'optionname': ('green', 'default', 'bold'),
'lineblock': ('cyan', 'default', 'bold'),
'blocktest': ('cyan', 'default', 'bold'),
'transition': ('cyan', 'default', 'bold'),
'emphasis.start': ('red', 'default', 'bold'),
'emphasis.null': ('red', 'default', 'bold'),
'emphasis.end': ('red', 'default', 'bold'),
'strong_emphasis.start': ('red', 'default', 'bold'),
'strong_emphasis.null': ('red', 'default', 'bold'),
'strong_emphasis.end': ('red', 'default', 'bold'),
'interpreted': ('magenta', 'default', 'bold'),
'inline_literal': ('magenta', 'default', 'bold'),
'substitution': ('magenta', 'default', 'bold'),
'anonymous': ('blue', 'default', 'bold'),
'reference': ('blue', 'default', 'bold'),
'footnote': ('blue', 'default', 'bold'),
'citation': ('blue', 'default', 'bold'),
'rst_url': ('blue', 'default', 'bold'),
'bullet': ('magenta', 'default', 'bold'),
'enumeration': ('magenta', 'default', 'bold'),
}
actions = [RstInsertSpace, RstWrapParagraph]
def __init__(self, w):
mode.Fundamental.__init__(self, w)
self.add_bindings('rst-insert-space', ('SPACE',))
self.add_bindings('rst-wrap-paragraph', ('M-q',))
install = RST.install