pmacs3/mode/markdown.py

144 lines
4.1 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 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'^###### .+'),
PatternRule('md.bullet', r'^ *[\*\-\*] '),
PatternMatchRule('', r'(\[.+?\])(\(.+?\))', 'md.linkname', 'md.linkurl'),
PatternRule('md.block', r'^>\s.*$'),
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'^```.+$', Grammar, r'^```'),
RegionRule('md.bold', r'\*\*', MarkdownGrammar, r'\*\*'),
RegionRule('md.tt', r'``', MarkdownGrammar, r'``'),
RegionRule('md.bold', r'\*', MarkdownGrammar, r'\*'),
RegionRule('md.tt', r'`', MarkdownGrammar, r'`'),
PatternRule('md.word', r'[a-zA-Z\'"\-]+'),
PatternRule('spaces', ' +'),
PatternRule('eol', ' +'),
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.linkname': hi_green,
'md.linkurl': hi_cyan,
'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.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