pmacs3/mode/rst.py

134 lines
5.2 KiB
Python
Raw Normal View History

from lex import Grammar, PatternRule, RegionRule, OverrideRegionRule
2009-03-29 20:50:27 -04:00
from mode import Fundamental
from method import Method, WrapParagraph
from mode.text import TextInsertSpace
2008-10-15 22:49:28 -04:00
from mode.python import PythonGrammar
2009-03-29 20:50:27 -04:00
def make_string_rules(forbid):
return [
2007-07-23 15:52:53 -04:00
PatternRule('escaped', r'\\.'),
2009-03-29 20:50:27 -04:00
PatternRule('data', r'[^\\' + forbid + ']+'),
2007-07-23 15:52:53 -04:00
]
2009-03-29 20:50:27 -04:00
class LineGrammar(Grammar): rules = [PatternRule('data', r'^.*\n$')]
class RSTString1(Grammar): rules = make_string_rules('*')
class RSTString2(Grammar): rules = make_string_rules('`')
class RSTString3(Grammar): rules = make_string_rules('|')
class RSTGrammar(Grammar):
rules = [
2008-10-15 22:49:28 -04:00
PatternRule(r'escape', r'\\.'),
2007-07-23 15:52:53 -04:00
2008-11-12 10:17:39 -05:00
PatternRule(r'bullet', r'^ *[-*+](?=$| )'),
PatternRule(r'enumeration', r'^ *(?:[0-9]+|#)\.(?=$| )'),
2008-11-12 10:16:26 -05:00
2009-03-29 20:50:27 -04:00
RegionRule(r'strong_emphasis', r'\*\*', RSTString1, r'\*\*'),
RegionRule(r'emphasis', r'\*', RSTString1, r'\*'),
#RegionRule(r'inline_literal', r'``', RSTString2, r'``'),
#RegionRule(r'interpreted', r'`', RSTString2, r'`_?'),
2007-07-23 15:52:53 -04:00
PatternRule(r'anonymous', r'[a-zA-Z]+__'),
PatternRule(r'reference', r'[a-zA-Z]+_(?![a-zA-Z0-9_])'),
2009-03-29 20:50:27 -04:00
RegionRule(r'inline_internal', r'_`', RSTString2, r'`'),
2008-10-15 22:49:28 -04:00
2010-09-30 18:18:57 -04:00
#RegionRule(r'substitution', r'\|(?! )', RSTString3, r'\|'),
PatternRule('substitution', r'r\|[^ ]+\|'),
#RegionRule(r'substitution', r'\|(?! )', RSTString3, 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_]+)?'),
2009-03-29 20:50:27 -04:00
RegionRule(r'table', r'\+-+(\+-+)*\+\n$', LineGrammar, r'^\n$'),
2008-10-15 22:49:28 -04:00
PatternRule(r'field', r'^:.+?:'),
PatternRule(r'lineblock', r'^\|'),
RegionRule(r'blocktest', r'>>>', PythonGrammar, '^\n'),
2009-03-29 20:50:27 -04:00
OverrideRegionRule(r'code', r'^\.\. code-block:: +(?P<grammar>.+)\n$', None, r'^(?=[^\n ])'),
RegionRule(r'literal_block', r'::\n$', LineGrammar, r'^(?=[^\n ])'),
PatternRule('rst_word', r'[a-zA-Z]+'),
PatternRule('spaces', ' +'),
PatternRule('eol', ' +'),
PatternRule('rst_null', r'.'),
]
2009-03-29 20:50:27 -04:00
class RstWrapParagraph(WrapParagraph):
2008-04-18 23:32:08 -04:00
limit = 75
class RstInsertSpace(TextInsertSpace):
limit = 75
wrapper = RstWrapParagraph
2008-04-18 23:32:08 -04:00
2009-03-14 19:53:44 -04:00
class RstBuild(Method):
pass
2009-03-29 20:50:27 -04:00
class RST(Fundamental):
name = 'RST'
2007-10-18 17:07:35 -04:00
extensions = ['.rst']
grammar = RSTGrammar
colors = {
2008-11-12 11:18:05 -05: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'),
2009-03-29 20:50:27 -04:00
'table.data': ('cyan', 'default'),
2008-10-15 22:49:28 -04:00
'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'),
2009-03-29 20:50:27 -04:00
'emphasis.data': ('red', 'default'),
2008-10-15 22:49:28 -04:00
'emphasis.null': ('red', 'default'),
'emphasis.end': ('red', 'default'),
'strong_emphasis.start': ('red', 'default', 'bold'),
2009-03-29 20:50:27 -04:00
'strong_emphasis.data': ('red', 'default', 'bold'),
2008-10-15 22:49:28 -04:00
'strong_emphasis.null': ('red', 'default', 'bold'),
'strong_emphasis.end': ('red', 'default', 'bold'),
'interpreted.start': ('magenta', 'default'),
2009-03-29 20:50:27 -04:00
'interpreted.data': ('magenta', 'default'),
2008-10-15 22:49:28 -04:00
'interpreted.null': ('magenta', 'default'),
'interpreted.end': ('magenta', 'default'),
'inline_literal.start': ('magenta', 'default', 'bold'),
2009-03-29 20:50:27 -04:00
'inline_literal.data': ('magenta', 'default', 'bold'),
2008-10-15 22:49:28 -04:00
'inline_literal.null': ('magenta', 'default', 'bold'),
'inline_literal.end': ('magenta', 'default', 'bold'),
'inline_internal.start': ('yellow', 'default'),
2009-03-29 20:50:27 -04:00
'inline_internal.data': ('yellow', 'default'),
2008-10-15 22:49:28 -04:00
'inline_internal.null': ('yellow', 'default'),
'inline_internal.end': ('magenta', 'default'),
'substitution.start': ('cyan', 'default', 'bold'),
2009-03-29 20:50:27 -04:00
'substitution.data': ('cyan', 'default', 'bold'),
2008-10-15 22:49:28 -04:00
'substitution.null': ('cyan', 'default', 'bold'),
'substitution.end': ('cyan', 'default', 'bold'),
'code.start': ('yellow', 'default', 'bold'),
2009-03-29 20:50:27 -04:00
'literal_block.start': ('yellow', 'default'),
'literal_block.data': ('yellow', 'default'),
'literal_block.null': ('yellow', 'default'),
}
2008-04-18 23:32:08 -04:00
actions = [RstInsertSpace, RstWrapParagraph]
config = {
'rst.margin': 75,
}
2009-02-15 12:06:35 -05:00
_bindings = {
'rst-insert-space': ('SPACE',),
'rst-wrap-paragraph': ('M-q',),
}
2007-10-19 02:41:33 -04:00
install = RST.install