// create toggle for wiki feature document.writeln('
wiki?

'); var textarea=get_textarea(); // obsolet var allowed_nodes = {479:'perlquestion',480:'perlmeditation',1040:'monkdiscuss', 1597: 'obfuscated',1044:'CUFP'}; if ( textarea ) { // activate wikifeature document.forms.wiki.elements.on.checked=true; // window.alert('wiki enabled!!!'); } var text; /* expand wiki code to monk code with capital letters */ function wiki2monks () { if ( textarea && document.forms.wiki.elements.on.checked) { text=textarea.value; // constants regex var singleword = '\\S+' ; var singleline = '\\S|\\S.*?\\S' ; //******* protect special areas text = init_exclusions(text); text = exclude_all(text); //******* Replace Inline Markup //*** autolinks to modules, pragmas, cpan text=text.replace(/(\s)(\w+::\w[\w:]*)(\s)/g,link_module); //*** normalize newlines text=text.replace(/\r\n/g,'\n'); // IE sucks! //*** 'inline code' -> '...' around_words('inline code', 'C', "'", singleline); text = exclude_all(text); // protect new code areas //*** ?documentation? -> [DOC://...] text=text.replace(/(\s)\?(\S+)\?(\s)/g,'$1[DOC://$2|$2]$3'); text = exclude_all(text); // protect new doc areas //*** emphasizer * / _ -> around_words('bold', 'B', '*', singleword); around_words('italic', 'I', '/', singleword); around_words('underline', 'U', '_', singleword); //****** complete lines (markup at start of line) //*** > quotes -> > ... text=text.replace(/(^|\n\n)(> )([^]*?)(?=\n\n|$)/g,'$1> $3'); // quote //*** \n=== headlines ->
...
text = heading(text); //*** - list items ->
  • ...
  • // paragraphs starting with (-)+ = (un)orderd text=text.replace(/(\n)(\n-\s+[^]*?)(\n\n)/g,'$1$2\n<\/UL\>\n'); // bullets unordered text=text.replace(/(\n)(\n\+\s+[^]*?)(\n\n)/g,'$1$2\n<\/OL\>\n'); // bullets ordered text=text.replace(/^(\-|\+)(\s+.*?)$/gm,'$2'); // bullets (simple) //*** paragraph text=text.replace(/(\n\n+)/g,'$1'); // paragraph //****** reinclude protected areas text = include_all(text); textarea.value=text; } } /* replace words surrounded by wikitags with pm markup eg " *words* " -> " words " */ function around_words ( doc, monk_tag, wiki_tag, inner_re) { var empty = '(^|\\s|$)'; var marker = '(\\' + wiki_tag + ')'; var inner_grp = '(' + inner_re + ')'; var regex = new RegExp( empty + marker + inner_grp + marker + empty, 'g'); var pre_tag = '<' + monk_tag + '>'; var post_tag = ''; var substitute = '$1' + pre_tag + '$3' + post_tag + '$5'; // console.log(doc,regex,substitute); text=text.replace( regex, substitute); } function around_paragraph ( doc, monk_tag, wiki_tag, inner_re) { //stub } /* transform headlines (both ways) eg "^=== text" <->
    text
    */ function heading (text,revert) { var maps =[ ['===', 'H5'], ['==' , 'H4'], ['=' , 'H3'], ]; if (revert) { for (var i=maps.length-1; i>=0 ;i--) { var map=maps[i]; var tag0 = new RegExp( '<'+ map[1] + '>','g'); text=text.replace(tag0,map[0]); var tag1 = new RegExp( '','g'); text=text.replace(tag1,''); } } else { for (var i=0; i$2'; // console.log(from + ' -> ' + to); text=text.replace(from,to); } } return text; } /* revert PM markup back to wiki markup */ function monks2wiki () { if ( textarea && document.forms.wiki.elements.on.checked) { var text=textarea.value; // protect areas like code sections text = exclude_all(text); // delete

    text=text.replace(/

    /g,''); // no quote text=text.replace(/(^|\n\n)(> )([^]*?)<\/I>(?=\n\n|$)/g,'$1> $3'); // revert around word markup text=text.replace(/<\/?I>/g,'/'); text=text.replace(/<\/?B>/g,'*'); text=text.replace(/<\/?U>/g,'_'); // revert listitems text=text.replace(/

  • /g,'-'); text=text.replace(/<\/?(U|O)L>/g,''); // revert

    ..

    text = heading(text,1); // reinclude protected areas text = include_all(text); // revert ' (was protected) text=text.replace(/<\/?C>/g,"'"); textarea.value=text; } } function link_module(match, pre, name, post, offset, string) { var core_modules = { 'B::Deparse' : 1, }; var protokoll= core_modules[name] ? 'doc' : 'metamod'; return pre+'['+protokoll.toUpperCase()+'://'+name+']'+post; } (function (){ var DBG=false; var counter=0; // var matches = []; var exclusions = []; var delim="\0"; var delim0=delim+"<<"; var delim1=">>"+delim; init_exclusions =function (text) { exclusions = []; counter=0; text = exclude(text,new RegExp(delim+"+","g"),"delim"); return text; } exclude_all = function (text){ text = exclude(text,/<(c|code)>[^]*?<\/\1>/g,"code"); text = exclude(text,/\[(\w+):\/\/.*?\]/g,"link"); text = exclude(text,/[^]*?<\/nowiki>/g,"nowiki"); text = exclude(text,/<\w+\s+.+?>/g,"innertag"); text = exclude(text,new RegExp("<"+"!--[^]*?--"+">","g"),"comment"); if (DBG) console.log(exclusions); return text; } function exclude(text,pattern,name){ var text_out=text; var match; while (match = pattern.exec(text)) { // if (DBG) console.log(match); var substring = match[0]; var placeholder = name.toUpperCase(); if (match[1]) placeholder += "_"+match[1]; placeholder = delim0 + placeholder + "_" +counter + delim1; text_out = text_out.replace( substring, placeholder ); // matches[counter] = substring; exclusions.push( [placeholder,substring] ); counter++; } return text_out; } include_all = function (text){ var exclusion; while ( exclusion = exclusions.pop() ){ var placeholder = exclusion[0]; var substring = exclusion[1]; substring = substring.replace(/\$/g,'$$$$'); // escape $ to avoid interpolation text = text.replace( placeholder, substring ); } return text; } })(); /* textareas meant for posting discussions have names ending on '_doctext' */ function get_textarea() { var T=document.getElementsByTagName("textarea"); for (var i in T) { var n=T[i].name ; if (n && n.match(/_doctext$/)) return T[i]; } } if (textarea) { // after pageload revert upercase PM markup to original wikicode window.onload= monks2wiki; // on submit of form wikisyntax to PM markup textarea.form.onsubmit = wiki2monks; } // ********** test code ********** /* test heading() */ function t_heading () { var t='= eins\n== zwei\n=== drei'; //console.log(t); var a=heading(t); //console.log(a); var b=heading(a,1); // console.log(b); var a=heading(b); var b=heading(a,1); if (t!=b) { console.log('heading test FAILED: \n'+t+'\n->\n'+a+'\n->\n'+b); } else { console.log("heading test OK"); } } /* END OF WIKIFEATURE */