vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
mediawiki.lua
(1747B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- MediaWiki LPeg lexer.
3 -- Contributed by Alexander Misel.
4
5 local lexer = require('lexer')
6 local token, word_match = lexer.token, lexer.word_match
7 local P, S, B = lpeg.P, lpeg.S, lpeg.B
8
9 local lex = lexer.new('mediawiki')
10
11 -- Comments.
12 lex:add_rule('comment', token(lexer.COMMENT, lexer.range('<!--', '-->')))
13
14 -- HTML-like tags
15 local tag_start = token(lexer.TAG, '<' * P('/')^-1 * lexer.alnum^1 * lexer.space^0)
16 local dq_str = '"' * ((lexer.any - S('>"\\')) + ('\\' * lexer.any))^0 * '"'
17 local tag_attr = token(lexer.ATTRIBUTE, lexer.alpha^1 * lexer.space^0 *
18 ('=' * lexer.space^0 * (dq_str + (lexer.any - lexer.space - '>')^0)^-1)^0 * lexer.space^0)
19 local tag_end = token(lexer.TAG, P('/')^-1 * '>')
20 lex:add_rule('tag', tag_start * tag_attr^0 * tag_end)
21
22 -- Link
23 lex:add_rule('link', token(lexer.STRING, S('[]')))
24 lex:add_rule('internal_link', B('[[') * token(lexer.LINK, (lexer.any - '|' - ']]')^1))
25
26 -- Templates and parser functions.
27 lex:add_rule('template', token(lexer.OPERATOR, S('{}')))
28 lex:add_rule('parser_func',
29 B('{{') * token(lexer.FUNCTION, '#' * lexer.alpha^1 + lexer.upper^1 * ':'))
30 lex:add_rule('template_name', B('{{') * token(lexer.LINK, (lexer.any - S('{}|'))^1))
31
32 -- Operators.
33 lex:add_rule('operator', token(lexer.OPERATOR, S('-=|#~!')))
34
35 -- Behavior switches
36 local start_pat = P(function(_, pos) return pos == 1 end)
37 lex:add_rule('behavior_switch', (B(lexer.space) + start_pat) * token('behavior_switch', word_match(
38 '__TOC__ __FORCETOC__ __NOTOC__ __NOEDITSECTION__ __NOCC__ __NOINDEX__')) * #lexer.space)
39 lex:add_style('behavior_switch', lexer.styles.keyword)
40
41 lexer.property['scintillua.comment'] = '<!--|-->'
42 lexer.property['scintillua.angle.braces'] = '1'
43
44 return lex