-
Notifications
You must be signed in to change notification settings - Fork 728
/
markdown.rb
182 lines (141 loc) · 4.42 KB
/
markdown.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# -*- coding: utf-8 -*- #
# frozen_string_literal: true
module Rouge
module Lexers
class Markdown < RegexLexer
title "Markdown"
desc "Markdown, a light-weight markup language for authors"
tag 'markdown'
aliases 'md', 'mkd'
filenames '*.markdown', '*.md', '*.mkd'
mimetypes 'text/x-markdown'
def html
@html ||= HTML.new(options)
end
start { html.reset! }
edot = /\\.|[^\\\n]/
state :root do
# YAML frontmatter
rule(/\A(---\s*\n.*?\n?)^(---\s*$\n?)/m) { delegate YAML }
rule %r/\\./, Str::Escape
rule %r/^[\S ]+\n(?:---*)\n/, Generic::Heading
rule %r/^[\S ]+\n(?:===*)\n/, Generic::Subheading
rule %r/^#(?=[^#]).*?$/, Generic::Heading
rule %r/^##*.*?$/, Generic::Subheading
rule %r/^([ \t]*)(`{3,}|~{3,})([^\n]*\n)((.*?)(\n\1)(\2))?/m do |m|
name = m[3].strip
sublexer =
begin
Lexer.find_fancy(name.empty? ? "guess" : name, m[5], @options)
rescue Guesser::Ambiguous => e
e.alternatives.first.new(@options)
end
sublexer ||= PlainText.new(@options.merge(:token => Str::Backtick))
sublexer.reset!
token Text, m[1]
token Punctuation, m[2]
token Name::Label, m[3]
if m[5]
delegate sublexer, m[5]
end
token Text, m[6]
if m[7]
token Punctuation, m[7]
else
push do
rule %r/^([ \t]*)(#{m[2]})/ do |mb|
pop!
token Text, mb[1]
token Punctuation, mb[2]
end
rule %r/^.*\n/ do |mb|
delegate sublexer, mb[1]
end
end
end
end
rule %r/\n\n(( |\t).*?\n|\n)+/, Str::Backtick
rule %r/(`+)(?:#{edot}|\n)+?\1/, Str::Backtick
# various uses of * are in order of precedence
# line breaks
rule %r/^(\s*[*]){3,}\s*$/, Punctuation
rule %r/^(\s*[-]){3,}\s*$/, Punctuation
# bulleted lists
rule %r/^\s*[*+-](?=\s)/, Punctuation
# numbered lists
rule %r/^\s*\d+\./, Punctuation
# blockquotes
rule %r/^\s*>.*?$/, Generic::Traceback
# link references
# [foo]: bar "baz"
rule %r(^
(\s*) # leading whitespace
(\[) (#{edot}+?) (\]) # the reference
(\s*) (:) # colon
)x do
groups Text, Punctuation, Str::Symbol, Punctuation, Text, Punctuation
push :title
push :url
end
# links and images
rule %r/(!?\[)(#{edot}*?)(\])(?=[\[(])/ do
groups Punctuation, Name::Variable, Punctuation
push :link
end
rule %r/[*][*]#{edot}*?[*][*]/, Generic::Strong
rule %r/__#{edot}*?__/, Generic::Strong
rule %r/[*]#{edot}*?[*]/, Generic::Emph
rule %r/_#{edot}*?_/, Generic::Emph
# Automatic links
rule %r/<.*?@.+[.].+>/, Name::Variable
rule %r[<(https?|mailto|ftp)://#{edot}*?>], Name::Variable
rule %r/[^\\`\[*\n&<]+/, Text
# inline html
rule(/&\S*;/) { delegate html }
rule(/<#{edot}*?>/) { delegate html }
rule %r/[&<]/, Text
# An opening square bracket that is not a link
rule %r/\[/, Text
rule %r/\n/, Text
end
state :link do
rule %r/(\[)(#{edot}*?)(\])/ do
groups Punctuation, Str::Symbol, Punctuation
pop!
end
rule %r/[(]/ do
token Punctuation
push :inline_title
push :inline_url
end
rule %r/[ \t]+/, Text
rule(//) { pop! }
end
state :url do
rule %r/[ \t]+/, Text
# the url
rule %r/(<)(#{edot}*?)(>)/ do
groups Name::Tag, Str::Other, Name::Tag
pop!
end
rule %r/\S+/, Str::Other, :pop!
end
state :title do
rule %r/"#{edot}*?"/, Name::Namespace
rule %r/'#{edot}*?'/, Name::Namespace
rule %r/[(]#{edot}*?[)]/, Name::Namespace
rule %r/\s*(?=["'()])/, Text
rule(//) { pop! }
end
state :inline_title do
rule %r/[)]/, Punctuation, :pop!
mixin :title
end
state :inline_url do
rule %r/[^<\s)]+/, Str::Other, :pop!
rule %r/\s+/m, Text
mixin :url
end
end
end
end