2011-11-02 21:09:19 +00:00
|
|
|
/* Produces output more or less compatible with FakeParser; plug it into FP's output and see */
|
|
|
|
|
|
|
|
{
|
|
|
|
var dp = function ( msg ) {
|
|
|
|
if ( false ) {
|
|
|
|
console.log(msg);
|
|
|
|
}
|
|
|
|
}
|
2011-11-07 14:39:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Flags for specific parse environments (inside tables, links etc). Flags
|
|
|
|
* trigger syntactic stops in the inline_breaks production, which
|
|
|
|
* terminates inline and attribute matches. Flags merely reduce the number
|
|
|
|
* of productions needed: The grammar is still context-free as the
|
|
|
|
* productions can just be unrolled for all combinations of environments
|
|
|
|
* at the cost of a much larger grammar.
|
|
|
|
*/
|
|
|
|
var syntaxFlags = {};
|
|
|
|
var setFlag = function(flag) {
|
|
|
|
if (syntaxFlags[flag] !== undefined) {
|
|
|
|
syntaxFlags[flag]++;
|
|
|
|
} else {
|
|
|
|
syntaxFlags[flag] = 1;
|
2011-11-02 21:09:19 +00:00
|
|
|
}
|
2011-11-07 14:39:12 +00:00
|
|
|
return true;
|
2011-11-02 21:09:19 +00:00
|
|
|
}
|
2011-11-07 14:39:12 +00:00
|
|
|
var clearFlag = function(flag) {
|
|
|
|
syntaxFlags[flag]--;
|
|
|
|
}
|
|
|
|
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Temporary debugging help. Is there anything similar in JS or a library? */
|
|
|
|
var print_r = function (arr, level) {
|
|
|
|
|
|
|
|
var dumped_text = "";
|
|
|
|
if (!level) level = 0;
|
|
|
|
|
|
|
|
//The padding given at the beginning of the line.
|
|
|
|
var level_padding = "";
|
|
|
|
var bracket_level_padding = "";
|
|
|
|
|
2011-11-04 11:08:11 +00:00
|
|
|
for (var j = 0; j < level + 1; j++) level_padding += " ";
|
|
|
|
for (var b = 0; b < level; b++) bracket_level_padding += " ";
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
if (typeof(arr) == 'object') { //Array/Hashes/Objects
|
|
|
|
dumped_text += "Array\n";
|
|
|
|
dumped_text += bracket_level_padding + "(\n";
|
|
|
|
for (var item in arr) {
|
|
|
|
|
|
|
|
var value = arr[item];
|
|
|
|
|
|
|
|
if (typeof(value) == 'object') { //If it is an array,
|
|
|
|
dumped_text += level_padding + "[" + item + "] => ";
|
|
|
|
dumped_text += print_r(value, level + 2);
|
|
|
|
} else {
|
|
|
|
dumped_text += level_padding + "[" + item + "] => '" + value + "'\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
dumped_text += bracket_level_padding + ")\n\n";
|
2011-11-04 11:08:11 +00:00
|
|
|
} else { //Strings/Chars/Numbers etc.
|
|
|
|
dumped_text = "=>" + arr + "<=(" + typeof(arr) + ")";
|
2011-11-02 21:09:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dumped_text;
|
|
|
|
|
|
|
|
}
|
2011-11-04 10:02:59 +00:00
|
|
|
|
|
|
|
// Convert list prefixes to a list of WikiDom list styles
|
|
|
|
var bulletsToTypes = function (bullets) {
|
|
|
|
var bTypes = [];
|
|
|
|
var blen = bullets.length;
|
|
|
|
for (var i = 0; i < bullets.length; i++) {
|
|
|
|
switch (bullets[i]) {
|
|
|
|
case '*':
|
|
|
|
bTypes.push('bullet'); break;
|
|
|
|
case '#':
|
|
|
|
bTypes.push('number'); break;
|
|
|
|
case ';':
|
2011-11-07 20:36:34 +00:00
|
|
|
bTypes.push('term'); break;
|
2011-11-04 10:02:59 +00:00
|
|
|
case ':':
|
2011-11-07 20:36:34 +00:00
|
|
|
bTypes.push('description'); break;
|
2011-11-04 10:02:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return bTypes;
|
2011-11-08 16:08:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var extractInline = function ( node ) {
|
|
|
|
return { text: extractText(node)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
var extractText = function ( node ) {
|
|
|
|
dp("extract: " + print_r(node));
|
|
|
|
if (typeof node === 'string') {
|
|
|
|
return node;
|
|
|
|
} else if ($.isArray(node)) {
|
|
|
|
var texts = [];
|
|
|
|
for (var i = 0, length = node.length; i < length; i++) {
|
|
|
|
texts.push(extractText(node[i]));
|
|
|
|
}
|
|
|
|
return texts.join('');
|
|
|
|
} else if ( 'text' in node ) {
|
|
|
|
return extractText(node.text);
|
|
|
|
} else if ( 'content' in node ) {
|
|
|
|
return extractText(node.content);
|
|
|
|
} else if ( 'children' in node ) {
|
|
|
|
var texts = [];
|
|
|
|
for (var i = 0, length = node.children.length; i < length; i++) {
|
|
|
|
texts.push(extractText(node.children[i]));
|
|
|
|
}
|
|
|
|
return texts.join('');
|
|
|
|
} else {
|
|
|
|
console.log("extract failed!" + print_r(node));
|
|
|
|
throw ("extract failed: " + print_r(node));
|
|
|
|
}
|
|
|
|
};
|
2011-11-02 21:09:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
start
|
2011-11-07 23:09:30 +00:00
|
|
|
= e:block* newline* {
|
2011-11-07 21:05:37 +00:00
|
|
|
var es = [];
|
|
|
|
// flatten sub-arrays, as a list block can contain multiple lists
|
|
|
|
for(var i = 0, length = e.length; i < length; i++) {
|
|
|
|
var ei = e[i];
|
|
|
|
if ($.isArray(ei))
|
|
|
|
es = es.concat(ei);
|
|
|
|
else
|
|
|
|
es.push(ei);
|
|
|
|
};
|
|
|
|
return {
|
|
|
|
type: 'page',
|
|
|
|
children: es
|
|
|
|
}
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
anyblock = block / inline
|
|
|
|
anyblockline = block / inlineline
|
|
|
|
|
|
|
|
|
|
|
|
// All chars that cannot start syntactic structures
|
|
|
|
text = t:[A-Za-z0-9,._ -]+ { return t.join('') }
|
|
|
|
|
|
|
|
space
|
2011-11-07 14:39:12 +00:00
|
|
|
= s:[ \t]+ { return s.join(''); }
|
|
|
|
|
|
|
|
|
|
|
|
// Start of line
|
2011-11-08 16:08:05 +00:00
|
|
|
sol = (newline / & { return pos === 0; } { return true; })
|
|
|
|
cn:(comment n:newline? { return n })? {
|
|
|
|
return cn;
|
|
|
|
}
|
|
|
|
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
newline
|
2011-11-07 14:39:12 +00:00
|
|
|
= '\n' / '\r\n'
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
block
|
2011-11-07 23:09:30 +00:00
|
|
|
= (sol space* &newline)? block_lines
|
2011-11-07 14:39:12 +00:00
|
|
|
/ para
|
|
|
|
/ comment
|
2011-11-07 23:09:30 +00:00
|
|
|
/ sol
|
2011-11-07 14:39:12 +00:00
|
|
|
|
|
|
|
block_lines
|
|
|
|
= h
|
2011-11-02 21:09:19 +00:00
|
|
|
/ table
|
|
|
|
/ lists
|
2011-11-07 14:39:12 +00:00
|
|
|
/ pre_indent
|
|
|
|
|
2011-11-02 21:09:19 +00:00
|
|
|
|
2011-11-07 14:39:12 +00:00
|
|
|
/* Headings */
|
2011-11-02 21:09:19 +00:00
|
|
|
h = h1 / h2 / h3 / h4 / h5 / h6
|
|
|
|
|
2011-11-07 23:09:30 +00:00
|
|
|
h1 = sol '='
|
|
|
|
(
|
|
|
|
& { setFlag('h'); return setFlag('h1') }
|
|
|
|
c:inlineline '=' &newline {
|
|
|
|
clearFlag('h');
|
|
|
|
clearFlag('h1');
|
2011-11-07 21:05:37 +00:00
|
|
|
return {
|
|
|
|
type: 'heading',
|
|
|
|
attributes: {level: 1},
|
2011-11-08 16:08:05 +00:00
|
|
|
content: extractInline(c)
|
2011-11-07 21:05:37 +00:00
|
|
|
}
|
2011-11-07 23:09:30 +00:00
|
|
|
}
|
|
|
|
/ { clearFlag('h'); clearFlag('h1'); return null }
|
|
|
|
)
|
|
|
|
|
|
|
|
h2 = sol '=='
|
|
|
|
(
|
|
|
|
& { setFlag('h'); return setFlag('h2') }
|
|
|
|
c:inlineline '==' &newline {
|
|
|
|
clearFlag('h');
|
|
|
|
clearFlag('h2');
|
2011-11-07 21:05:37 +00:00
|
|
|
return {
|
|
|
|
type: 'heading',
|
|
|
|
attributes: {level: 2},
|
2011-11-08 16:08:05 +00:00
|
|
|
content: extractInline(c)
|
2011-11-07 21:05:37 +00:00
|
|
|
}
|
2011-11-07 23:09:30 +00:00
|
|
|
}
|
|
|
|
/ { clearFlag('h'); clearFlag('h2'); return null }
|
|
|
|
)
|
|
|
|
|
|
|
|
h3 = sol '==='
|
|
|
|
(
|
|
|
|
& { setFlag('h'); return setFlag('h3') }
|
|
|
|
c:inlineline '===' &newline {
|
|
|
|
clearFlag('h');
|
|
|
|
clearFlag('h3');
|
2011-11-07 21:05:37 +00:00
|
|
|
return {
|
|
|
|
type: 'heading',
|
|
|
|
attributes: {level: 3},
|
2011-11-08 16:08:05 +00:00
|
|
|
content: extractInline(c)
|
2011-11-07 21:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-07 23:09:30 +00:00
|
|
|
/ { clearFlag('h'); clearFlag('h3'); return null }
|
|
|
|
)
|
|
|
|
|
|
|
|
h4 = sol '===='
|
|
|
|
(
|
|
|
|
& { setFlag('h'); return setFlag('h4') }
|
|
|
|
c:inlineline '====' &newline {
|
|
|
|
clearFlag('h');
|
|
|
|
clearFlag('h4');
|
2011-11-07 21:05:37 +00:00
|
|
|
return {
|
|
|
|
type: 'heading',
|
|
|
|
attributes: {level: 4},
|
2011-11-08 16:08:05 +00:00
|
|
|
content: extractInline(c)
|
2011-11-07 21:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-07 23:09:30 +00:00
|
|
|
/ { clearFlag('h'); clearFlag('h4'); return null }
|
|
|
|
)
|
|
|
|
|
|
|
|
h5 = sol '====='
|
|
|
|
(& { setFlag('h'); return setFlag('h5') }
|
|
|
|
c:inlineline '=====' &newline {
|
|
|
|
clearFlag('h');
|
|
|
|
clearFlag('h5');
|
2011-11-07 21:05:37 +00:00
|
|
|
return {
|
|
|
|
type: 'heading',
|
|
|
|
attributes: {level: 5},
|
2011-11-08 16:08:05 +00:00
|
|
|
content: extractInline(c)
|
2011-11-07 21:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-07 23:09:30 +00:00
|
|
|
/ { clearFlag('h'); clearFlag('h5'); return null }
|
|
|
|
)
|
|
|
|
|
|
|
|
h6 = sol '======'
|
|
|
|
(& { setFlag('h'); return setFlag('h6') }
|
|
|
|
c:inlineline '======' &newline {
|
|
|
|
clearFlag('h');
|
|
|
|
clearFlag('h6');
|
2011-11-07 21:05:37 +00:00
|
|
|
return {
|
|
|
|
type: 'heading',
|
|
|
|
attributes: {level: 6},
|
2011-11-08 16:08:05 +00:00
|
|
|
content: extractInline(c)
|
2011-11-07 21:05:37 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-07 23:09:30 +00:00
|
|
|
/ { clearFlag('h'); clearFlag('h6'); return null }
|
|
|
|
)
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
heading_marker
|
|
|
|
= '=' '='*
|
|
|
|
|
|
|
|
heading_text
|
2011-11-07 23:09:30 +00:00
|
|
|
= h:( !(heading_marker newline) x:inlineline { return x } )* { return h.join(''); }
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
|
2011-11-04 07:45:05 +00:00
|
|
|
// TODO: convert inline content to annotations!
|
2011-11-02 21:09:19 +00:00
|
|
|
para
|
2011-11-07 23:09:30 +00:00
|
|
|
= (sol br)? para_lines
|
|
|
|
|
|
|
|
para_lines
|
2011-11-08 16:08:05 +00:00
|
|
|
= s:sol c:inlineline cs:(!block_lines para_lines)* {
|
2011-11-07 23:09:30 +00:00
|
|
|
return {
|
|
|
|
type: 'paragraph',
|
2011-11-08 16:08:05 +00:00
|
|
|
content: extractInline([s].concat([c]).concat(cs))
|
2011-11-07 23:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
br = space* &newline { return {type: 'br'} }
|
2011-11-07 14:39:12 +00:00
|
|
|
|
|
|
|
pre_indent
|
|
|
|
= l:pre_indent_line+ {
|
|
|
|
return {
|
|
|
|
type: 'pre',
|
2011-11-08 16:08:05 +00:00
|
|
|
content: extractInline(l)
|
2011-11-07 14:39:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pre_indent_line = sol space l:inlineline { return l }
|
|
|
|
|
|
|
|
// Syntax that stops inline expansion
|
|
|
|
inline_breaks
|
|
|
|
= //& { console.log(print_r(syntaxFlags)); return true; }
|
|
|
|
& { return syntaxFlags['table']; }
|
|
|
|
a:(newline [!|] / '||' / '!!' / '|}') { dp("table break" + print_r(a)); return true; }
|
|
|
|
/ & { return syntaxFlags['italic']; } italic_marker { return true; }
|
|
|
|
/ & { return syntaxFlags['bold']; } bold_marker { return true; }
|
|
|
|
/ & { return syntaxFlags['linkdesc']; } link_end { return true; }
|
2011-11-07 23:09:30 +00:00
|
|
|
/ & { return syntaxFlags['h']; }
|
|
|
|
( & { return syntaxFlags['h1'] } '=' newline { return true; }
|
|
|
|
/ & { return syntaxFlags['h2'] } '==' newline { return true; }
|
|
|
|
/ & { return syntaxFlags['h3'] } '===' newline { return true; }
|
|
|
|
/ & { return syntaxFlags['h4'] } '====' newline { return true; }
|
|
|
|
/ & { return syntaxFlags['h5'] } '=====' newline { return true; }
|
|
|
|
/ & { return syntaxFlags['h6'] } '======' newline { return true; }
|
|
|
|
)
|
|
|
|
|
2011-11-07 14:39:12 +00:00
|
|
|
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
inline
|
2011-11-07 14:39:12 +00:00
|
|
|
= c:(text / inline_element / (!inline_breaks ch:. { return ch; }))+ {
|
2011-11-07 21:05:37 +00:00
|
|
|
var out = [];
|
|
|
|
var text = '';
|
|
|
|
for (var i = 0; i < c.length; i++) {
|
|
|
|
if (typeof c[i] == 'string') {
|
|
|
|
text += c[i];
|
|
|
|
} else {
|
|
|
|
if (text.length) {
|
|
|
|
out.push({
|
|
|
|
type: 'text',
|
|
|
|
text: text
|
|
|
|
});
|
|
|
|
text = '';
|
|
|
|
}
|
|
|
|
out.push(c[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (text.length) {
|
2011-11-02 21:09:19 +00:00
|
|
|
out.push({
|
2011-11-07 21:05:37 +00:00
|
|
|
type: 'text',
|
|
|
|
text: text
|
2011-11-02 21:09:19 +00:00
|
|
|
});
|
|
|
|
}
|
2011-11-07 21:05:37 +00:00
|
|
|
return out;
|
2011-11-02 21:09:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inlineline
|
2011-11-07 14:39:12 +00:00
|
|
|
= c:(text / !inline_breaks (inline_element / [^\n]))+ {
|
2011-11-07 21:05:37 +00:00
|
|
|
var out = [];
|
|
|
|
var text = '';
|
2011-11-08 16:08:05 +00:00
|
|
|
//dp("inlineline: " + print_r(c));
|
2011-11-07 21:05:37 +00:00
|
|
|
for (var i = 0; i < c.length; i++) {
|
|
|
|
if (typeof c[i] == 'string') {
|
|
|
|
text += c[i];
|
|
|
|
} else {
|
|
|
|
if (text.length) {
|
|
|
|
out.push({
|
|
|
|
type: 'text',
|
|
|
|
text: text
|
|
|
|
});
|
|
|
|
text = '';
|
|
|
|
}
|
|
|
|
out.push(c[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (text.length) {
|
2011-11-02 21:09:19 +00:00
|
|
|
out.push({
|
2011-11-07 21:05:37 +00:00
|
|
|
text: text,
|
|
|
|
//annotations: []
|
2011-11-02 21:09:19 +00:00
|
|
|
});
|
|
|
|
}
|
2011-11-07 21:05:37 +00:00
|
|
|
return out;
|
2011-11-02 21:09:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-07 14:39:12 +00:00
|
|
|
/* TODO: convert all these to annotations!
|
|
|
|
* -> need (start, end) offsets within block
|
|
|
|
*/
|
2011-11-02 21:09:19 +00:00
|
|
|
inline_element
|
|
|
|
= comment
|
|
|
|
/ xmlish_tag
|
|
|
|
/ extlink
|
|
|
|
/ template
|
|
|
|
/ link
|
|
|
|
/ bold
|
|
|
|
/ italic
|
|
|
|
|
|
|
|
comment
|
2011-11-07 23:09:30 +00:00
|
|
|
= '<!--' c:comment_chars* '-->'
|
|
|
|
(space* newline space* comment)* {
|
|
|
|
return {
|
|
|
|
type: 'comment',
|
|
|
|
text: c.join('')
|
|
|
|
}
|
2011-11-07 21:05:37 +00:00
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
comment_chars
|
|
|
|
= c:[^-] { return c; }
|
2011-11-07 14:39:12 +00:00
|
|
|
/ c:'-' !'->' { return c; }
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
extlink
|
|
|
|
= "[" target:url " " text:extlink_text "]" {
|
2011-11-07 21:05:37 +00:00
|
|
|
return {
|
|
|
|
type: 'extlink',
|
|
|
|
target: target,
|
|
|
|
text: text
|
|
|
|
}
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
// = "[" target:url text:extlink_text "]" { return { type: 'extlink', target: target, text: text } }
|
|
|
|
|
|
|
|
url
|
|
|
|
= proto:"http:" rest:([^ \]]+) { return proto + rest.join(''); }
|
|
|
|
|
|
|
|
extlink_text
|
|
|
|
= c:[^\]]+ { return c.join(''); }
|
|
|
|
|
|
|
|
template
|
|
|
|
= "{{" target:link_target params:("|" p:template_param { return p })* "}}" {
|
2011-11-07 21:05:37 +00:00
|
|
|
var obj = {
|
|
|
|
type: 'template',
|
|
|
|
target: target
|
|
|
|
};
|
|
|
|
if (params && params.length) {
|
|
|
|
obj.params = params;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
template_param
|
|
|
|
= name:template_param_name "=" c:template_param_text {
|
2011-11-07 21:05:37 +00:00
|
|
|
return {
|
|
|
|
name: name,
|
|
|
|
content: c
|
|
|
|
};
|
|
|
|
} / c:template_param_text {
|
|
|
|
return {
|
|
|
|
content: c
|
|
|
|
};
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
2011-11-07 21:05:37 +00:00
|
|
|
tplarg
|
|
|
|
= "{{{" name:link_target params:("|" p:template_param { return p })* "}}}" {
|
|
|
|
var obj = {
|
|
|
|
type: 'tplarg',
|
|
|
|
name: name
|
|
|
|
};
|
|
|
|
if (params && params.length) {
|
|
|
|
obj.params = params;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
template_param_name
|
|
|
|
= h:( !"}}" x:([^=|]) { return x } )* { return h.join(''); }
|
|
|
|
|
|
|
|
template_param_text
|
|
|
|
= template_param_text_chunk*
|
|
|
|
/* = h:( !"}}" x:([^|]) { return x } )* { return h.join(''); }*/
|
|
|
|
|
|
|
|
template_param_text_chunk
|
|
|
|
= comment
|
|
|
|
/ xmlish_tag
|
|
|
|
/ extlink
|
|
|
|
/ template
|
|
|
|
/ link
|
|
|
|
/ bold
|
|
|
|
/ italic
|
|
|
|
/ !"}}" x:([^|]) { return x }
|
|
|
|
|
|
|
|
link
|
|
|
|
= "[[" target:link_target text:("|" link_text)* "]]" {
|
2011-11-07 21:05:37 +00:00
|
|
|
var obj = {
|
|
|
|
type: 'link',
|
|
|
|
target: target
|
|
|
|
};
|
|
|
|
if (text && text.length) {
|
|
|
|
obj.text = text[0][1]; // ehhhh
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
link_target
|
|
|
|
= h:( !"]]" x:([^|]) { return x } )* { return h.join(''); }
|
|
|
|
|
|
|
|
link_text
|
2011-11-07 14:39:12 +00:00
|
|
|
= h:( & { return setFlag('linkdesc'); }
|
2011-11-07 21:05:37 +00:00
|
|
|
x:inlineline { return x }
|
2011-11-07 14:39:12 +00:00
|
|
|
)* {
|
|
|
|
clearFlag('linkdesc')
|
2011-11-07 21:05:37 +00:00
|
|
|
return h.join('');
|
2011-11-07 14:39:12 +00:00
|
|
|
}
|
|
|
|
/ & { clearFlag('linkdesc') } { return null; }
|
|
|
|
|
|
|
|
link_end = "]]"
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
bold
|
2011-11-07 14:39:12 +00:00
|
|
|
= bold_marker
|
|
|
|
& { dp('benter:' + pos); return setFlag('bold'); }
|
|
|
|
c:inlineline
|
|
|
|
bold_marker {
|
|
|
|
clearFlag('bold');
|
|
|
|
return {
|
|
|
|
type: 'b',
|
2011-11-08 16:08:05 +00:00
|
|
|
content: {text: c}
|
2011-11-07 14:39:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/ bold_marker { clearFlag('bold'); return null }
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
bold_marker
|
|
|
|
= "'''"
|
|
|
|
|
|
|
|
|
|
|
|
italic
|
2011-11-07 14:39:12 +00:00
|
|
|
= italic_marker
|
|
|
|
& { dp('ienter:' + pos); return setFlag('italic'); }
|
2011-11-07 21:05:37 +00:00
|
|
|
c:inlineline
|
|
|
|
italic_marker {
|
2011-11-07 14:39:12 +00:00
|
|
|
clearFlag('italic');
|
|
|
|
dp('ileave:' + pos);
|
|
|
|
return {
|
|
|
|
type: 'i',
|
2011-11-08 16:08:05 +00:00
|
|
|
content: {text: c}
|
2011-11-07 14:39:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/ italic_marker { clearFlag('italic'); return null }
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
italic_marker
|
|
|
|
= "''"
|
|
|
|
|
|
|
|
/* Will need to check anything xmlish agains known/allowed HTML tags and
|
|
|
|
* registered extensions, otherwise fail the match. Should ref be treated as a
|
|
|
|
* regular extension? */
|
2011-11-07 21:05:37 +00:00
|
|
|
xmlish_tag = ref / references
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
ref = ref_full / ref_empty
|
|
|
|
|
|
|
|
/* Can we do backreferences to genericize this? */
|
|
|
|
ref_full
|
|
|
|
= start:ref_start ">" content:ref_content* close:ref_end {
|
2011-11-07 21:05:37 +00:00
|
|
|
return {
|
|
|
|
type: 'ext',
|
|
|
|
name: 'ref',
|
|
|
|
params: start.params,
|
|
|
|
ws: start.ws,
|
|
|
|
content: content,
|
|
|
|
close: close
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ref_empty
|
|
|
|
= start:ref_start close:(space* "/>") {
|
2011-11-07 21:05:37 +00:00
|
|
|
return {
|
|
|
|
type: 'ext',
|
|
|
|
name: 'ref',
|
|
|
|
ws: start.ws,
|
|
|
|
params: start.params,
|
|
|
|
close: close
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ref_start
|
|
|
|
= "<ref" params:ext_param* ws:space* {
|
2011-11-07 21:05:37 +00:00
|
|
|
return {
|
|
|
|
params: params,
|
|
|
|
ws: ws
|
|
|
|
};
|
2011-11-02 21:09:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ref_end
|
|
|
|
= all:("</ref" space* ">") {
|
|
|
|
return all.join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
ref_content
|
2011-11-07 14:39:12 +00:00
|
|
|
= !ref_end a:(inline) {
|
2011-11-02 21:09:19 +00:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fixme probably have to programatically add these */
|
|
|
|
references = references_full / references_empty
|
|
|
|
|
|
|
|
references_full
|
|
|
|
= start:references_start ">" content:references_content* close:references_end {
|
2011-11-07 21:05:37 +00:00
|
|
|
return {
|
|
|
|
type: 'ext',
|
|
|
|
name: 'references',
|
|
|
|
params: start.params,
|
|
|
|
ws: start.ws,
|
|
|
|
content: content,
|
|
|
|
close: close
|
|
|
|
}
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
references_empty
|
|
|
|
= start:references_start close:(space* "/>") {
|
2011-11-07 21:05:37 +00:00
|
|
|
return {
|
|
|
|
type: 'ext',
|
|
|
|
name: 'references',
|
|
|
|
ws: start.ws,
|
|
|
|
params: start.params,
|
|
|
|
close: close
|
|
|
|
}
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
references_start
|
|
|
|
= "<references" params:ext_param* ws:space* {
|
2011-11-07 21:05:37 +00:00
|
|
|
return {
|
|
|
|
params: params,
|
|
|
|
ws: ws
|
|
|
|
};
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
references_end
|
|
|
|
= all:("</references" space* ">") {
|
2011-11-07 21:05:37 +00:00
|
|
|
return all.join('');
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
references_content
|
2011-11-07 14:39:12 +00:00
|
|
|
= !references_end a:(inline) {
|
2011-11-07 21:05:37 +00:00
|
|
|
return a;
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
ext_param
|
|
|
|
= space* name:ext_param_name "=" val:ext_param_val {
|
2011-11-07 21:05:37 +00:00
|
|
|
val.name = name;
|
|
|
|
return val;
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
ext_param_name
|
|
|
|
= name:[a-zA-Z0-9-]+ {
|
|
|
|
return name.join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
ext_param_val
|
|
|
|
= t:[0-9A-Za-z]+ { return {text: t.join('') } }
|
|
|
|
/ "'" t:[^'>]+ "'" { return { quote: "'", text: t.join('') } }
|
|
|
|
/ '"' t:[^">]+ '"' { return { quote: '"', text: t.join('') } }
|
|
|
|
|
|
|
|
lists = es:(dtdd / li)+
|
2011-11-07 21:05:37 +00:00
|
|
|
{
|
2011-11-04 11:08:11 +00:00
|
|
|
// Flatten es
|
2011-11-04 10:02:59 +00:00
|
|
|
var esLen = es.length;
|
|
|
|
var flatEs = [];
|
|
|
|
for (var i = 0; i < esLen; i++) {
|
2011-11-04 11:08:11 +00:00
|
|
|
var ei = es[i];
|
|
|
|
if ($.isArray(ei)) {
|
|
|
|
flatEs = flatEs.concat(ei);
|
2011-11-02 21:09:19 +00:00
|
|
|
} else {
|
2011-11-04 11:08:11 +00:00
|
|
|
flatEs.push(ei);
|
2011-11-02 21:09:19 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-04 10:02:59 +00:00
|
|
|
return {
|
|
|
|
type: 'list',
|
2011-11-07 21:05:37 +00:00
|
|
|
children: flatEs
|
2011-11-02 21:09:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-07 14:39:12 +00:00
|
|
|
li = sol
|
|
|
|
bullets:list_char+
|
|
|
|
c:inlineline
|
|
|
|
&newline
|
2011-11-02 21:09:19 +00:00
|
|
|
{
|
|
|
|
return {
|
2011-11-04 10:02:59 +00:00
|
|
|
type: 'listItem',
|
|
|
|
attributes: {
|
|
|
|
styles: bulletsToTypes(bullets)
|
|
|
|
},
|
2011-11-08 16:08:05 +00:00
|
|
|
content: extractInline(c)
|
2011-11-02 21:09:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-11-07 14:39:12 +00:00
|
|
|
dtdd = sol
|
|
|
|
bullets:list_char+
|
2011-11-02 21:09:19 +00:00
|
|
|
c:(inline_element / [^:\n])+
|
|
|
|
":"
|
2011-11-04 11:08:11 +00:00
|
|
|
d:(inline_element / [^\n])+
|
2011-11-07 14:39:12 +00:00
|
|
|
&newline
|
2011-11-02 21:09:19 +00:00
|
|
|
{
|
|
|
|
// reject rule if bullets do not end in semicolon
|
|
|
|
if (bullets[bullets.length - 1] != ';') {
|
|
|
|
return null;
|
|
|
|
} else {
|
2011-11-04 10:02:59 +00:00
|
|
|
return [
|
2011-11-07 21:05:37 +00:00
|
|
|
{
|
|
|
|
type: 'listItem',
|
|
|
|
attributes: {styles: bulletsToTypes(bullets)},
|
2011-11-08 16:08:05 +00:00
|
|
|
content: extractInline(c)
|
2011-11-07 21:05:37 +00:00
|
|
|
}, {
|
|
|
|
type: 'listItem',
|
|
|
|
attributes: {styles: bulletsToTypes(
|
|
|
|
bullets.slice(0, bullets.length - 1) + ':')},
|
2011-11-08 16:08:05 +00:00
|
|
|
content: extractInline(d)
|
2011-11-07 21:05:37 +00:00
|
|
|
}
|
2011-11-04 10:02:59 +00:00
|
|
|
]
|
2011-11-02 21:09:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
list_char = [*#:;]
|
|
|
|
|
|
|
|
|
|
|
|
/* Tables */
|
|
|
|
|
|
|
|
table
|
|
|
|
= tas:table_start c:table_caption? b:table_body? table_end {
|
|
|
|
var res = {type: 'table'}
|
|
|
|
var body = b !== '' ? b : [];
|
|
|
|
if (c !== '') {
|
2011-11-04 12:06:49 +00:00
|
|
|
res.children = [c].concat(body);
|
2011-11-02 21:09:19 +00:00
|
|
|
} else {
|
2011-11-04 12:06:49 +00:00
|
|
|
res.children = body;
|
2011-11-02 21:09:19 +00:00
|
|
|
}
|
|
|
|
if (tas.length > 0) {
|
|
|
|
// FIXME: actually parse and build structure
|
|
|
|
res.attributes = { unparsed: tas }
|
|
|
|
}
|
2011-11-07 14:39:12 +00:00
|
|
|
//dp(print_r(res));
|
2011-11-02 21:09:19 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
table_start
|
2011-11-07 14:39:12 +00:00
|
|
|
= sol
|
|
|
|
"{|"
|
|
|
|
& { setFlag('table'); return true; }
|
|
|
|
ta:table_attribs*
|
|
|
|
space* {
|
|
|
|
//dp("table_start " + print_r(ta) + ", pos:" + pos);
|
2011-11-02 21:09:19 +00:00
|
|
|
return ta;
|
|
|
|
}
|
2011-11-07 14:39:12 +00:00
|
|
|
/ sol "{|" { clearFlag('table'); return null; }
|
2011-11-02 21:09:19 +00:00
|
|
|
|
2011-11-07 14:39:12 +00:00
|
|
|
table_attribs
|
|
|
|
= text / ! inline_breaks !newline .
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
table_caption
|
2011-11-07 14:39:12 +00:00
|
|
|
= newline
|
|
|
|
"|+" c:inline* {
|
2011-11-07 21:05:37 +00:00
|
|
|
return {
|
|
|
|
type: 'tableCaption',
|
|
|
|
content: c[0]
|
|
|
|
}
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
table_body
|
|
|
|
= & { dp("table_body enter"); return true; }
|
|
|
|
firstrow:table_firstrow otherrows:table_row* {
|
|
|
|
/* dp('table first and otherrows: '
|
|
|
|
* + print_r([firstrow].concat(otherrows))); */
|
|
|
|
return [firstrow].concat(otherrows);
|
|
|
|
}
|
|
|
|
/ otherrows:table_row* {
|
|
|
|
//dp('table otherrows: ' + print_r(otherrows));
|
|
|
|
return otherrows;
|
|
|
|
}
|
|
|
|
|
|
|
|
table_firstrow
|
|
|
|
= td:table_data+ {
|
|
|
|
return {
|
|
|
|
type: 'tableRow',
|
2011-11-04 12:06:49 +00:00
|
|
|
children: td
|
2011-11-02 21:09:19 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
table_row
|
2011-11-07 14:39:12 +00:00
|
|
|
= & { dp("table row enter"); return true; }
|
|
|
|
newline
|
|
|
|
"|-" thtd_attribs? space* td:(table_data / table_header)* {
|
2011-11-07 21:05:37 +00:00
|
|
|
return {
|
|
|
|
type: 'tableRow',
|
|
|
|
children: td
|
|
|
|
};
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
table_data
|
|
|
|
= & { dp("table_data enter, pos=" + pos); return true; }
|
2011-11-07 14:39:12 +00:00
|
|
|
("||" / newline "|")
|
|
|
|
! [}+-]
|
|
|
|
a:thtd_attribs?
|
|
|
|
td:(!inline_breaks anyblock)* {
|
2011-11-07 21:05:37 +00:00
|
|
|
dp("table data result: " + print_r(td) + ", attribts: " + print_r(a));
|
|
|
|
return {
|
|
|
|
type: 'tableCell',
|
|
|
|
attributes: { unparsed: a },
|
|
|
|
children: td
|
|
|
|
};
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
2011-11-07 14:39:12 +00:00
|
|
|
table_header
|
|
|
|
= ("!!" / newline "!")
|
|
|
|
a:thtd_attribs?
|
|
|
|
c:inline {
|
|
|
|
return {
|
|
|
|
type: 'tableHeading',
|
2011-11-07 21:05:37 +00:00
|
|
|
attributes: { unparsed: a },
|
|
|
|
children: c
|
2011-11-07 14:39:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
thtd_attribs
|
|
|
|
// In particular, do not match [|\n]
|
|
|
|
= a:(text / ! inline_breaks [="':;/,.-] )+ "|" ! [|}+-] {
|
2011-11-07 21:05:37 +00:00
|
|
|
return a;
|
|
|
|
}
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
|
2011-11-07 14:39:12 +00:00
|
|
|
table_end = newline? "|}" { clearFlag('table'); }
|
2011-11-02 21:09:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Wikidom TODO:
|
|
|
|
* split off text into content nodes
|
|
|
|
* convert inlines into annotations
|
|
|
|
* change contents into children
|
|
|
|
*/
|
2011-11-07 21:05:37 +00:00
|
|
|
|
|
|
|
/* Tabs do not mix well with the hybrid production syntax */
|
|
|
|
/* vim: et:ts=4:sw=4:cindent */
|