1 package r
2
3 import (
4 "strings"
5
6 . "github.com/alecthomas/chroma"
7 "github.com/alecthomas/chroma/lexers/internal"
8 )
9
10
11 var Restructuredtext = internal.Register(MustNewLazyLexer(
12 &Config{
13 Name: "reStructuredText",
14 Aliases: []string{"rst", "rest", "restructuredtext"},
15 Filenames: []string{"*.rst", "*.rest"},
16 MimeTypes: []string{"text/x-rst", "text/prs.fallenstein.rst"},
17 },
18 restructuredtextRules,
19 ))
20
21 func restructuredtextRules() Rules {
22 return Rules{
23 "root": {
24 {"^(=+|-+|`+|:+|\\.+|\\'+|\"+|~+|\\^+|_+|\\*+|\\++|#+)([ \\t]*\\n)(.+)(\\n)(\\1)(\\n)", ByGroups(GenericHeading, Text, GenericHeading, Text, GenericHeading, Text), nil},
25 {"^(\\S.*)(\\n)(={3,}|-{3,}|`{3,}|:{3,}|\\.{3,}|\\'{3,}|\"{3,}|~{3,}|\\^{3,}|_{3,}|\\*{3,}|\\+{3,}|#{3,})(\\n)", ByGroups(GenericHeading, Text, GenericHeading, Text), nil},
26 {`^(\s*)([-*+])( .+\n(?:\1 .+\n)*)`, ByGroups(Text, LiteralNumber, UsingSelf("inline")), nil},
27 {`^(\s*)([0-9#ivxlcmIVXLCM]+\.)( .+\n(?:\1 .+\n)*)`, ByGroups(Text, LiteralNumber, UsingSelf("inline")), nil},
28 {`^(\s*)(\(?[0-9#ivxlcmIVXLCM]+\))( .+\n(?:\1 .+\n)*)`, ByGroups(Text, LiteralNumber, UsingSelf("inline")), nil},
29 {`^(\s*)([A-Z]+\.)( .+\n(?:\1 .+\n)+)`, ByGroups(Text, LiteralNumber, UsingSelf("inline")), nil},
30 {`^(\s*)(\(?[A-Za-z]+\))( .+\n(?:\1 .+\n)+)`, ByGroups(Text, LiteralNumber, UsingSelf("inline")), nil},
31 {`^(\s*)(\|)( .+\n(?:\| .+\n)*)`, ByGroups(Text, Operator, UsingSelf("inline")), nil},
32 {`^( *\.\.)(\s*)((?:source)?code(?:-block)?)(::)([ \t]*)([^\n]+)(\n[ \t]*\n)([ \t]+)(.*)(\n)((?:(?:\8.*|)\n)+)`, EmitterFunc(rstCodeBlock), nil},
33 {`^( *\.\.)(\s*)([\w:-]+?)(::)(?:([ \t]*)(.*))`, ByGroups(Punctuation, Text, OperatorWord, Punctuation, Text, UsingSelf("inline")), nil},
34 {`^( *\.\.)(\s*)(_(?:[^:\\]|\\.)+:)(.*?)$`, ByGroups(Punctuation, Text, NameTag, UsingSelf("inline")), nil},
35 {`^( *\.\.)(\s*)(\[.+\])(.*?)$`, ByGroups(Punctuation, Text, NameTag, UsingSelf("inline")), nil},
36 {`^( *\.\.)(\s*)(\|.+\|)(\s*)([\w:-]+?)(::)(?:([ \t]*)(.*))`, ByGroups(Punctuation, Text, NameTag, Text, OperatorWord, Punctuation, Text, UsingSelf("inline")), nil},
37 {`^ *\.\..*(\n( +.*\n|\n)+)?`, CommentPreproc, nil},
38 {`^( *)(:[a-zA-Z-]+:)(\s*)$`, ByGroups(Text, NameClass, Text), nil},
39 {`^( *)(:.*?:)([ \t]+)(.*?)$`, ByGroups(Text, NameClass, Text, NameFunction), nil},
40 {`^(\S.*(?<!::)\n)((?:(?: +.*)\n)+)`, ByGroups(UsingSelf("inline"), UsingSelf("inline")), nil},
41 {`(::)(\n[ \t]*\n)([ \t]+)(.*)(\n)((?:(?:\3.*|)\n)+)`, ByGroups(LiteralStringEscape, Text, LiteralString, LiteralString, Text, LiteralString), nil},
42 Include("inline"),
43 },
44 "inline": {
45 {`\\.`, Text, nil},
46 {"``", LiteralString, Push("literal")},
47 {"(`.+?)(<.+?>)(`__?)", ByGroups(LiteralString, LiteralStringInterpol, LiteralString), nil},
48 {"`.+?`__?", LiteralString, nil},
49 {"(`.+?`)(:[a-zA-Z0-9:-]+?:)?", ByGroups(NameVariable, NameAttribute), nil},
50 {"(:[a-zA-Z0-9:-]+?:)(`.+?`)", ByGroups(NameAttribute, NameVariable), nil},
51 {`\*\*.+?\*\*`, GenericStrong, nil},
52 {`\*.+?\*`, GenericEmph, nil},
53 {`\[.*?\]_`, LiteralString, nil},
54 {`<.+?>`, NameTag, nil},
55 {"[^\\\\\\n\\[*`:]+", Text, nil},
56 {`.`, Text, nil},
57 },
58 "literal": {
59 {"[^`]+", LiteralString, nil},
60 {"``((?=$)|(?=[-/:.,; \\n\\x00\\\u2010\\\u2011\\\u2012\\\u2013\\\u2014\\\u00a0\\'\\\"\\)\\]\\}\\>\\\u2019\\\u201d\\\u00bb\\!\\?]))", LiteralString, Pop(1)},
61 {"`", LiteralString, nil},
62 },
63 }
64 }
65
66 func rstCodeBlock(groups []string, state *LexerState) Iterator {
67 iterators := []Iterator{}
68 tokens := []Token{
69 {Punctuation, groups[1]},
70 {Text, groups[2]},
71 {OperatorWord, groups[3]},
72 {Punctuation, groups[4]},
73 {Text, groups[5]},
74 {Keyword, groups[6]},
75 {Text, groups[7]},
76 }
77 code := strings.Join(groups[8:], "")
78 lexer := internal.Get(groups[6])
79 if lexer == nil {
80 tokens = append(tokens, Token{String, code})
81 iterators = append(iterators, Literator(tokens...))
82 } else {
83 sub, err := lexer.Tokenise(nil, code)
84 if err != nil {
85 panic(err)
86 }
87 iterators = append(iterators, Literator(tokens...), sub)
88 }
89 return Concaterator(iterators...)
90 }
91
View as plain text