import color, method, mode from lex import Grammar, PatternRule, RegionRule, PatternGroupRule, OverrideRegionRule from mode.text import TextInsertSpace from mode.python import PythonGrammar class RSTString(Grammar): rules = [ PatternRule('escaped', r'\\.'), ] class RSTGrammar(Grammar): rules = [ PatternRule(r'escape', r'\\.'), RegionRule(r'strong_emphasis', r'\*\*', RSTString, r'\*\*'), RegionRule(r'emphasis', r'\*', RSTString, r'\*'), RegionRule(r'inline_literal', r'``', RSTString, r'``'), RegionRule(r'interpreted', r'`', RSTString, r'`_?'), PatternRule(r'anonymous', r'[a-zA-Z]+__'), PatternRule(r'reference', r'[a-zA-Z]+_'), RegionRule(r'inline_internal', r'_`', RSTString, r'`'), RegionRule(r'substitution', r'\|(?! )', RSTString, r'\|'), PatternRule(r'footnote', r'\[[0-9]+\]_'), PatternRule(r'citation', r'\[.+?\]_'), PatternRule(r'rst_url', r'http://[^ ]+'), PatternRule(r'title', r'^={3,}\n$'), PatternRule(r'subtitle', r'^-{3,}\n$'), PatternRule(r'option', r'^(?:--|-|/)[a-zA-Z]+(?:[ =][-a-zA-Z_]+)?'), RegionRule(r'table', r'\+-+(\+-+)*\+\n$', Grammar, r'^\n$'), PatternRule(r'bullet', r'^ *[-*+]'), PatternRule(r'enumeration', r'^ *(?:[0-9]+|#)\.'), PatternRule(r'field', r'^:.+?:'), PatternRule(r'lineblock', r'^\|'), RegionRule(r'blocktest', r'>>>', PythonGrammar, '^\n'), OverrideRegionRule(r'code', r'^\.\. code-block:: +(?P.+)\n$', None, r'^[^\n ]'), RegionRule(r'literal_block', r'::\n$', Grammar, r'^[^\n ]'), ] class RstWrapParagraph(method.WrapParagraph): limit = 75 class RstInsertSpace(TextInsertSpace): limit = 75 wrapper = RstWrapParagraph class RST(mode.Fundamental): modename = 'RST' extensions = ['.rst'] grammar = RSTGrammar colors = { 'title': ('blue', 'default', 'bold'), 'subtitle': ('blue', 'default'), 'field': ('green', 'default', 'bold'), 'option': ('green', 'default', 'bold'), 'anonymous': ('blue', 'default', 'bold'), 'reference': ('blue', 'default', 'bold'), 'footnote': ('blue', 'default', 'bold'), 'citation': ('blue', 'default', 'bold'), 'rst_url': ('blue', 'default', 'bold'), 'table.start': ('cyan', 'default'), 'table.null': ('cyan', 'default'), 'bullet': ('magenta', 'default', 'bold'), 'enumeration': ('magenta', 'default', 'bold'), 'lineblock': ('cyan', 'default', 'bold'), 'blocktest.start': ('cyan', 'default', 'bold'), 'emphasis.start': ('red', 'default'), 'emphasis.null': ('red', 'default'), 'emphasis.end': ('red', 'default'), 'strong_emphasis.start': ('red', 'default', 'bold'), 'strong_emphasis.null': ('red', 'default', 'bold'), 'strong_emphasis.end': ('red', 'default', 'bold'), 'interpreted.start': ('magenta', 'default'), 'interpreted.null': ('magenta', 'default'), 'interpreted.end': ('magenta', 'default'), 'inline_literal.start': ('magenta', 'default', 'bold'), 'inline_literal.null': ('magenta', 'default', 'bold'), 'inline_literal.end': ('magenta', 'default', 'bold'), 'inline_internal.start': ('yellow', 'default'), 'inline_internal.null': ('yellow', 'default'), 'inline_internal.end': ('magenta', 'default'), 'substitution.start': ('cyan', 'default', 'bold'), 'substitution.null': ('cyan', 'default', 'bold'), 'substitution.end': ('cyan', 'default', 'bold'), 'code.start': ('yellow', 'default', 'bold'), 'literal_block.start': ('yellow', 'default', 'bold'), 'literal_block.null': ('green', 'default', 'bold'), } actions = [RstInsertSpace, RstWrapParagraph] config = { 'rst.margin': 75, } 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