diff --git a/mode/markdown.py b/mode/markdown.py index 3c2548a..981a4b0 100644 --- a/mode/markdown.py +++ b/mode/markdown.py @@ -1,61 +1,60 @@ -from lex import Grammar, PatternRule, RegionRule, OverrideRegionRule +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 -def make_string_rules(forbid): - return [ - PatternRule('escaped', r'\\.'), - PatternRule('data', r'[^\\' + forbid + ']+'), - ] +class MarkdownGrammar(Grammar): pass -class LineGrammar(Grammar): rules = [PatternRule('data', r'^.*\n$')] -class MarkdownString1(Grammar): rules = make_string_rules('*') -class MarkdownString2(Grammar): rules = make_string_rules('`') -class MarkdownString3(Grammar): rules = make_string_rules('|') +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'^###### .+'), -class MarkdownGrammar(Grammar): - rules = [ - #PatternRule(r'escape', r'\\.'), + PatternRule('md.bullet', r'^ *[\*\-\*] '), - #PatternRule(r'bullet', r'^ *[-*+](?=$| )'), - #PatternRule(r'enumeration', r'^ *(?:[0-9]+|#)\.(?=$| )'), + PatternMatchRule('', r'(\[.+?\])(\(.+?\))', 'md.linkname', 'md.linkurl'), - #RegionRule(r'strong_emphasis', r'\*\*', MarkdownString1, r'\*\*'), - #RegionRule(r'emphasis', r'\*', MarkdownString1, r'\*'), - #RegionRule(r'inline_literal', r'``', MarkdownString2, r'``'), - #RegionRule(r'interpreted', r'`', MarkdownString2, r'`_?'), + PatternRule('md.block', r'^>\s.*$'), + PatternRule('md.code', r'^(?: |\t).*$'), - #PatternRule(r'anonymous', r'[a-zA-Z]+__'), - #PatternRule(r'reference', r'[a-zA-Z]+_(?![a-zA-Z0-9_])'), - #RegionRule(r'inline_internal', r'_`', MarkdownString2, r'`'), + PatternRule('md.inlinelink', r'http://[^`*\t(){}\[\] ]+'), - #RegionRule(r'substitution', r'\|(?! )', MarkdownString3, r'\|'), - #PatternRule('substitution', r'r\|[^ ]+\|'), - #RegionRule(r'substitution', r'\|(?! )', MarkdownString3, r'\|'), - #PatternRule(r'footnote', r'\[[0-9]+\]_'), - #PatternRule(r'citation', r'\[.+?\]_'), - #PatternRule(r'md_url', r'http://[^ ]+'), + 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'^```'), - #PatternRule(r'title', r'^={3,}\n$'), - #PatternRule(r'subtitle', r'^-{3,}\n$'), + RegionRule('md.code', r'^```.+$', Grammar, r'^```'), - #PatternRule(r'option', r'^(?:--|-|/)[a-zA-Z]+(?:[ =][-a-zA-Z_]+)?'), - #RegionRule(r'table', r'\+-+(\+-+)*\+\n$', LineGrammar, r'^\n$'), + RegionRule('md.bold', r'\*\*', MarkdownGrammar, r'\*\*'), + RegionRule('md.tt', r'``', MarkdownGrammar, r'``'), - #PatternRule(r'field', r'^:.+?:'), + RegionRule('md.bold', r'\*', MarkdownGrammar, r'\*'), + RegionRule('md.tt', r'`', MarkdownGrammar, 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$', LineGrammar, r'^(?=[^\n ])'), + PatternRule('md.word', r'[a-zA-Z\'"\-]+'), + + PatternRule('spaces', ' +'), + PatternRule('eol', ' +'), + PatternRule('null', r'.'), +] - PatternRule('md.word', r'[a-zA-Z]+'), - PatternRule('spaces', ' +'), - PatternRule('eol', ' +'), - PatternRule('null', r'.'), - ] class MdWrapParagraph(WrapParagraph): limit = 75 @@ -63,60 +62,74 @@ 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 = { - #'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'), - #'md_url': ('blue', 'default', 'bold'), - # - #'table.start': ('cyan', 'default'), - #'table.data': ('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.data': ('red', 'default'), - #'emphasis.null': ('red', 'default'), - #'emphasis.end': ('red', 'default'), - #'strong_emphasis.start': ('red', 'default', 'bold'), - #'strong_emphasis.data': ('red', 'default', 'bold'), - #'strong_emphasis.null': ('red', 'default', 'bold'), - #'strong_emphasis.end': ('red', 'default', 'bold'), - #'interpreted.start': ('magenta', 'default'), - #'interpreted.data': ('magenta', 'default'), - #'interpreted.null': ('magenta', 'default'), - #'interpreted.end': ('magenta', 'default'), - #'inline_literal.start': ('magenta', 'default', 'bold'), - #'inline_literal.data': ('magenta', 'default', 'bold'), - #'inline_literal.null': ('magenta', 'default', 'bold'), - #'inline_literal.end': ('magenta', 'default', 'bold'), - #'inline_internal.start': ('yellow', 'default'), - #'inline_internal.data': ('yellow', 'default'), - #'inline_internal.null': ('yellow', 'default'), - #'inline_internal.end': ('magenta', 'default'), - #'substitution.start': ('cyan', 'default', 'bold'), - #'substitution.data': ('cyan', 'default', 'bold'), - #'substitution.null': ('cyan', 'default', 'bold'), - #'substitution.end': ('cyan', 'default', 'bold'), - # - #'code.start': ('yellow', 'default', 'bold'), - #'literal_block.start': ('yellow', 'default'), - #'literal_block.data': ('yellow', 'default'), - #'literal_block.null': ('yellow', 'default'), + '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 = {