MediaWiki:Gadget-collectionparser.js

// Adapted from User:Pediapress/collection-parser.js

function putOutList(list) { var pagename, bookname = list[0]; var out = '== ' + bookname.replace(/_/g, " ") +' ==\n:' + bookname.replace(/_/g, " ") + '\n';

for (var i = 1; i < list.length; i++) { if ( list[i] != null && list[i].match(/^http:/) ) { pagename = getArticleId(list[i], bookname); out += ":" + pagename.replace(/_/g, " ") + "\n"; } else { // kick out empty headlines and those without links if ( (i < list.length-1 && list[i+1].match(/^http:/)) && list[i] != " " ) { out += ";" + list[i] + "\n"; }		}	}

out += "\n";

// print the collectionmarkup into a textarea var collectionName = prompt("Name your collection:", ""); if (collectionName) { var w = window.open(wgServer + "/w/index.php?title=User:" + wgUserName + "/Collections/" + collectionName + "&action=edit"); $j(w).load( function { refreshTextArea(w, out); } ); } } function refreshTextArea(w, out) { var txt = w.document.getElementById('wpTextbox1'); txt.value = out; } function getArticleId(link, bookname) { var linkList = link.split("/"); var isId = 0; var output = "";

for (var i = 0; i < linkList.length; i++) { if (isId == 0 && linkList[i] == bookname) { isId++; } else if (isId == 1) { output += linkList[i]; isId++; } else if (isId > 1) { output += "/" + linkList[i]; }	}	return output; } // look for headlines and links function parseContent { var bookLinks = initBooklinks; var contDiv = document.getElementById('bodyContent') || document.getElementById('article') || document.getElementById('content'); var bodyElements = contDiv.getElementsByTagName("*"); var element;

for (var i = 0; i < bodyElements.length; i++) { element = bodyElements[i]; if (element.nodeName.match(/^H\d/)) { bookLinks[bookLinks.length] = getHeadingText(element); } else if (element.nodeName == "A" && isBookInternalLink(element)) { bookLinks[bookLinks.length] = element.href; }	}	return bookLinks; } //extracts the text from a headline function getHeadingText(heading) { if (heading.id == "siteSub") { return " "; } else { var elements = heading.childNodes; for (var i = 0; i < elements.length; i++) { if (elements[i].nodeName == "SPAN" && elements[i].getAttribute("class") == "mw-headline") { return elements[i].innerHTML; }		}	}	return "chapter"; } function initBooklinks { var bookName = wgPageName.split("/"); if (bookName.length > 1) { return [bookName[0], "http://" + wgPageName]; } else { return [wgPageName]; } }

//checks links for targets inside the wikibook function isBookInternalLink(link) { var articleBaseURL = wgServer + wgArticlePath; var bookName = wgPageName; var articleURL = articleBaseURL.substring(0, articleBaseURL.length-2) + bookName.split("/", 1)[0];

if (link.href.substring(0,articleURL.length) != articleURL || link.href.match(/#/) || link.href.match(/Print_version/i)) { return false; } else { return true; } } // added link has been clicked function getCollection { var bookLinks = parseContent; putOutList(bookLinks); } // add an link at thr right upper corner function addPediapressLinks { if (wgNamespaceNumber == 0 || wgNamespaceNumber == 102 || wgNamespaceNumber == 110 ||wgNamespaceNumber == 500) { addPortletLink ('p-personal', 'javascript:getCollection', 'get collection'); } } addOnloadHook(addPediapressLinks);