pmacs3/mode/markdown.py

168 lines
4.8 KiB
Python

from lex import Grammar, PatternRule, RegionRule, OverrideRegionRule, PatternMatchRule
from mode import Fundamental
from method import Method, WrapParagraph
from mode.text import TextInsertSpace
from mode.c import CGrammar
from mode.html import HTMLGrammar
from mode.java import JavaGrammar
from mode.javascript import JavascriptGrammar
from mode.python import PythonGrammar
from mode.scala import ScalaGrammar
from mode.sql import SqlGrammar
from mode.xml import XMLGrammar
class LineGrammar(Grammar): pass
LineGrammar.rules = [
PatternRule('line', r'.+'),
PatternRule('eol', r'\n'),
]
class WordGrammar(Grammar): pass
WordGrammar.rules = [
PatternRule('word', r'[A-Za-z0-9_]+'),
]
class MarkdownGrammar(Grammar): pass
MarkdownGrammar.rules = [
PatternRule('md.head1', r'^#.+'),
PatternRule('md.head2', r'^##.+'),
PatternRule('md.head3', r'^###.+'),
PatternRule('md.head4', r'^####.+'),
PatternRule('md.head5', r'^#####.+'),
PatternRule('md.head6', r'^######.+'),
RegionRule('md.tag', r'^<(?P<tag>[a-zA-Z_][a-zA-Z0-9_]*)>$', HTMLGrammar, r'^</%(tag)s>$'),
PatternRule('md.bullet', r'^\s*[\*\-\*] '),
PatternMatchRule('', r'(\[.+?\])(\(.+?\))', 'md.linkname', 'md.linkurl'),
PatternRule('md.block3', r'^(?:>\s*>\s*>\s*)*>\s*>\s*>(?:[^>].*)?\n$'),
PatternRule('md.block2', r'^(?:>\s*>\s*>\s*)*>\s*>(?:[^>].*)?\n$'),
PatternRule('md.block1', r'^(?:>\s*>\s*>\s*)*>(?:[^>].*)?\n$'),
PatternRule('md.code', r'^(?: |\t).*$'),
PatternRule('md.inlinelink', r'http://[^`*\t(){}\[\] ]+'),
RegionRule('md.lang', r'^```c$', CGrammar, r'^```'),
RegionRule('md.lang', r'^```html$', HTMLGrammar, r'^```'),
RegionRule('md.lang', r'^```(?:javascript|js)$', JavascriptGrammar, r'^```'),
RegionRule('md.lang', r'^```java$', JavaGrammar, r'^```'),
RegionRule('md.lang', r'^```python$', PythonGrammar, r'^```'),
RegionRule('md.lang', r'^```scala$', ScalaGrammar, r'^```'),
RegionRule('md.lang', r'^```sql$', SqlGrammar, r'^```'),
RegionRule('md.lang', r'^```xml$', XMLGrammar, r'^```'),
RegionRule('md.code', r'^```.+$', LineGrammar, r'^```'),
RegionRule('md.bold', r'\*\*', MarkdownGrammar, r'\*\*'),
RegionRule('md.tt', r'``', MarkdownGrammar, r'``'),
RegionRule('md.bold', r'\*', MarkdownGrammar, r'\*'),
RegionRule('md.tt', r'`', WordGrammar, r'`'),
PatternRule('md.escaped', r'\\.'),
PatternRule('md.word', r'[a-zA-Z\'"\-]+'),
PatternRule('spaces', '\s+'),
PatternRule('eol', r'\n'),
#PatternRule('null', r'.'),
]
class MdWrapParagraph(WrapParagraph):
limit = 75
class MdInsertSpace(TextInsertSpace):
limit = 75
wrapper = MdWrapParagraph
# white is for delimiters, operators, numbers
default = ('default', 'default')
# magenta is for keywords/builtins
lo_magenta = ('magenta202', 'default')
hi_magenta = ('magenta414', 'default')
# red is for comments
lo_red = ('red300', 'default')
hi_red = ('red511', 'default')
# orange is for macro definitions, headers and constants
hi_orange = ('yellow531', 'default')
lo_orange = ('yellow520', 'default')
# yellow is for parts of macros
hi_yellow = ('yellow551', 'default')
lo_yellow = ('yellow330', 'default')
# green is for strings and characters
lo_green = ('green030', 'default')
hi_green = ('green050', 'default')
# cyan is for types
lo_cyan = ('cyan033', 'default')
hi_cyan = ('cyan155', 'default')
# blue is definitions, functions and some macros
lo_blue = ('blue113', 'default')
hi_blue = ('blue225', 'default')
class Markdown(Fundamental):
name = 'Markdown'
extensions = ['.md', ".markdown"]
grammar = MarkdownGrammar
colors = {
'md.head1': hi_blue,
'md.head2': hi_blue,
'md.head3': hi_blue,
'md.head4': hi_blue,
'md.head5': hi_blue,
'md.head6': hi_blue,
'md.tag.start': hi_magenta,
'md.tag.end': hi_magenta,
'md.linkname': hi_green,
'md.linkurl': hi_cyan,
'md.block1': hi_yellow,
'md.block2': hi_orange,
'md.block3': hi_red,
'md.code': lo_green,
'md.code.start': lo_green,
'md.code.null': lo_green,
'md.code.end': lo_green,
'md.lang': lo_green,
'md.lang.start': lo_green,
'md.lang.end': lo_green,
'md.bullet': lo_magenta,
'md.inlinelink': hi_red,
'md.bold.start': hi_orange,
'md.bold.null': hi_orange,
'md.bold.md.word': hi_orange,
'md.bold.end': hi_orange,
'md.tt.start': lo_orange,
'md.tt.word': lo_orange,
'md.tt.null': lo_orange,
'md.tt.md.word': lo_orange,
'md.tt.end': lo_orange,
}
actions = [MdInsertSpace, MdWrapParagraph]
config = {
'md.margin': 75,
}
_bindings = {
'md-insert-space': ('SPACE',),
'md-wrap-paragraph': ('M-q',),
}
install = Markdown.install