summaryrefslogtreecommitdiff
path: root/resources/src/mediawiki/mediawiki.jqueryMsg.peg
blob: 716c326190f5fbdefd16f2946352bf52b5a3d550 (plain)
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
/* PEG grammar for a subset of wikitext, useful in the MediaWiki frontend */

start
  = e:expression* { return e.length > 1 ? [ "CONCAT" ].concat(e) : e[0]; }

expression
  = template
  / link
  / extlink
  / replacement
  / literal

paramExpression
  = template
  / link
  / extlink
  / replacement
  / literalWithoutBar

template
  = "{{" t:templateContents "}}" { return t; }

templateContents
  = twr:templateWithReplacement p:templateParam* { return twr.concat(p) }
  / twr:templateWithOutReplacement p:templateParam* { return twr.concat(p) }
  / twr:templateWithOutFirstParameter p:templateParam* { return twr.concat(p) }
  / t:templateName p:templateParam* { return p.length ? [ t, p ] : [ t ] }

templateWithReplacement
  = t:templateName ":" r:replacement { return [ t, r ] }

templateWithOutReplacement
  = t:templateName ":" p:paramExpression { return [ t, p ] }

templateWithOutFirstParameter
  = t:templateName ":" { return [ t, "" ] }

templateParam
  = "|" e:paramExpression* { return e.length > 1 ? [ "CONCAT" ].concat(e) : e[0]; }

templateName
  = tn:[A-Za-z_]+ { return tn.join('').toUpperCase() }

/* TODO: Update to reflect separate piped and unpiped handling */
link
  = "[[" w:expression "]]" { return [ 'WLINK', w ]; }

extlink
  = "[" url:url whitespace text:expression "]" { return [ 'LINK', url, text ] }

url
  = url:[^ ]+ { return url.join(''); }

whitespace
  = [ ]+

replacement
  = '$' digits:digits { return [ 'REPLACE', parseInt( digits, 10 ) - 1 ] }

digits
  = [0-9]+

literal
  = lit:escapedOrRegularLiteral+ { return lit.join(''); }

literalWithoutBar
  = lit:escapedOrLiteralWithoutBar+ { return lit.join(''); }

escapedOrRegularLiteral
  = escapedLiteral
  / regularLiteral

escapedOrLiteralWithoutBar
  = escapedLiteral
  / regularLiteralWithoutBar

escapedLiteral
  = "\\" escaped:. { return escaped; }

regularLiteral
  = [^{}\[\]$\\]

regularLiteralWithoutBar
  = [^{}\[\]$\\|]