pmacs3/mode/rst.py

108 lines
4.1 KiB
Python
Raw Normal View History

import color, method, mode
2008-10-15 22:49:28 -04:00
from lex import Grammar, PatternRule, RegionRule, PatternGroupRule, OverrideRegionRule
from mode.text import TextInsertSpace
2008-10-15 22:49:28 -04:00
from mode.python import PythonGrammar
2007-07-23 15:52:53 -04:00
class RSTString(Grammar):
rules = [
PatternRule('escaped', r'\\.'),
]
class RSTGrammar(Grammar):
rules = [
2008-10-15 22:49:28 -04:00
PatternRule(r'escape', r'\\.'),
2007-07-23 15:52:53 -04:00
RegionRule(r'strong_emphasis', r'\*\*', RSTString, r'\*\*'),
2008-10-15 22:49:28 -04:00
RegionRule(r'emphasis', r'\*', RSTString, r'\*'),
2007-07-23 15:52:53 -04:00
RegionRule(r'inline_literal', r'``', RSTString, r'``'),
2008-10-15 22:49:28 -04:00
RegionRule(r'interpreted', r'`', RSTString, r'`_?'),
2007-07-23 15:52:53 -04:00
PatternRule(r'anonymous', r'[a-zA-Z]+__'),
PatternRule(r'reference', r'[a-zA-Z]+_'),
2008-10-15 22:49:28 -04:00
RegionRule(r'inline_internal', r'_`', RSTString, r'`'),
RegionRule(r'substitution', r'\|(?! )', RSTString, r'\|'),
2007-07-23 15:52:53 -04:00
PatternRule(r'footnote', r'\[[0-9]+\]_'),
PatternRule(r'citation', r'\[.+?\]_'),
PatternRule(r'rst_url', r'http://[^ ]+'),
2007-07-23 15:52:53 -04:00
2008-10-15 22:49:28 -04:00
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$'),
2007-07-23 15:52:53 -04:00
PatternRule(r'bullet', r'^ *[-*+]'),
PatternRule(r'enumeration', r'^ *(?:[0-9]+|#)\.'),
2008-10-15 22:49:28 -04:00
PatternRule(r'field', r'^:.+?:'),
PatternRule(r'lineblock', r'^\|'),
RegionRule(r'blocktest', r'>>>', PythonGrammar, '^\n'),
OverrideRegionRule(r'code', r'^\.\. code-block:: +(?P<grammar>.+)\n$', None, r'^[^\n ]'),
RegionRule(r'literal_block', r'::\n$', Grammar, r'^[^\n ]'),
]
2008-04-18 23:32:08 -04:00
class RstInsertSpace(TextInsertSpace):
limit = 75
class RstWrapParagraph(method.WrapParagraph):
limit = 75
2007-10-21 20:55:29 -04:00
class RST(mode.Fundamental):
2007-10-18 17:07:35 -04:00
modename = 'RST'
extensions = ['.rst']
grammar = RSTGrammar
colors = {
2008-10-15 22:49:28 -04:00
'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'),
2007-07-23 15:52:53 -04:00
2008-10-15 22:49:28 -04:00
'table.start': ('cyan', 'default'),
'table.null': ('cyan', 'default'),
'bullet': ('magenta', 'default', 'bold'),
'enumeration': ('magenta', 'default', 'bold'),
2008-10-15 22:49:28 -04:00
'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'),
}
2008-04-18 23:32:08 -04:00
actions = [RstInsertSpace, RstWrapParagraph]
def __init__(self, w):
mode.Fundamental.__init__(self, w)
2008-04-18 23:32:08 -04:00
self.add_bindings('rst-insert-space', ('SPACE',))
self.add_bindings('rst-wrap-paragraph', ('M-q',))
2007-10-19 02:41:33 -04:00
install = RST.install