Benutzer:XanonymusX/CharttableConverter.js

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen

Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.

  • Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
  • Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
  • Internet Explorer/Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
  • Opera: Strg+F5
function wikitextbox() {
    var input = document.getElementById("wikitext-in").value;

    input += "=<" // simuliert letzten Parameter, falls nicht vorhanden
    var albums = input.match(/\|\s?Alben\s?\=[\s\S]+?\=(\s|<)/)
    var singles = input.match(/\|\s?Singles\s?\=[\s\S]+?\=(\s|<)/)
    var eps = input.match(/\|\s?EPs\s?\=[\s\S]+?\=(\s|<)/)
    var dvds = input.match(/\|\s?DVDs\s?\=[\s\S]+?\=(\s|<)/)

    var image = input.match(/\|\s?Bild\s?\=.+/)
    var imagecaption = input.match(/\|\s?Bildbeschreibung\s?\=.+/)
    var filelink;

    if (image) {
        filelink = "[[Datei:" + image[0].replace(/\|\s?Bild\s?\=\s?/, "") + "|" + "mini"
        if (imagecaption) {
            filelink += "|" + imagecaption[0].replace(/\|\s?Bildbeschreibung\s?\=\s?/, "")
        }
        filelink += "]]"
    }

    // ALBEN
    if (albums) {
        albums = albums[0].replace(/Jahr\=/g, "JAHR:").replace(/\n?\|.+\=\n?/g, "")
        var albumlines = albums.match(/\{\{Album[\s\S]+?\}\}\s*\n\s*\}\}/g)

        var albumsources = input.match(/\|\s?Quellen Alben\s?\=\s?\n?.+?\n/)
        if (albumsources) {
            albumsources = albumsources[0].replace(/\|\s?Quellen Alben\s?\=\s?\n?/, "").replace(/\n$/, "")
        } else {
            albumsources = ""
        }
        var albumcountries = albums.match(/\|[A-Z\-]+?\|/g)
        var uniquealbumcountries = [...new Set(albumcountries)]
        albumcountries = []
        var i = 0;
        var albumcountrydisplay = "|"
        for (i in uniquealbumcountries) {
            albumcountries[i] = uniquealbumcountries[i].replace(/\|/g, "")
            albumcountrydisplay += albumcountries[i] + "|"
        }
        var albumyears = [];
        i = 0;
        for (i in albumlines) {
            var lineyears = albumlines[i].replace(/\|JAHR:\d\d\d\d/g, "").match(/\d\d\d\d\|/g)
            var j = 0;
            for (j in lineyears) {
                lineyears[j] = lineyears[j].replace(/\|/, "")
            }
            albumyears[i] = Math.min(...lineyears)
        }

        
        var albumtemplate = "{{Charttabelle\n" + albumcountrydisplay + " Quellen = " + albumsources + "\n| Art = Alben" + "\n| INHALT =\n"

        i = 0;
        var k = 1;
        for (i in albumyears) {
            // Titel
            var albumtitles = albumlines[i].match(/\{\{Album\s+?\|\s?.+/)
            var albumtitle = albumtitles[0].replace(/\{\{Album\s+?\|\s?/, "")
            var albumtitleadd = albumtitle.match(/<small\>\(.+?\)<\/small\>/)
            if (albumtitleadd) {
                albumtitle = albumtitle.replace(albumtitleadd, "").trim()
            }

            var n = k+1
            if (albumyears[i] == albumyears[i-1]) {
                albumtemplate += "\n| Titel" + n + " = "
                k = k + 1;
            } else {
                if (i>0) {
                    albumtemplate += "\n}}\n"
                    k = 1
                }
                n = ""
                albumtemplate += "{{Charteintrag\n" + albumcountrydisplay + " Jahr = " + albumyears[i] + "\n| Titel = "
            }                

            albumtemplate += albumtitle

            // Chartdaten
            var albumcharts = albumlines[i].match(/\{\{Charts.+/g)
            var m = 0;
            for (m in albumcharts) {
                var rawline = albumcharts[m]
                var country = rawline.match(/\|[A-Z\-]+?\|/)
                rawline = rawline.replace(/\{\{Charts/, "").replace(country, "")
                country = country[0].replace(/\|/g, "")
                var no1year = 0
                if (rawline.match(/\|JAHR\:/)) {
                  no1year = rawline.match(/\|JAHR\:\d\d\d\d/)
                  rawline = rawline.replace(no1year, "")
                  no1year = no1year[0].replace(/\D/g, "")
                }
                var addtemplates = 0
                if (rawline.match(/\{\{/)) {
                  addtemplates = rawline.match(/\{\{.+?\}\}/)
                  var maskaddtemplates = addtemplates[0].replace(/\|/g, "$!$!").replace(/=/g, "$::$")
                  rawline = rawline.replace(addtemplates, maskaddtemplates)
                }
                var position = rawline.match(/^.*?\|/)
                rawline = rawline.replace(position, "")
                position = position[0].replace(/\|/g, "")
                var entrydate = rawline.match(/^.*?\|/)
                rawline = rawline.replace(entrydate, "")
                entrydate = entrydate[0].replace(/\|/g, "") // wohin damit?
                var weeks = rawline.replace(/\}\}/, "")
                var ref = 0;
                var months = 0;
                if (weeks.match(/\|/)) {
                    ref = weeks.match(/\|.*/)
                    weeks = weeks.replace(ref, "")
                    ref = ref[0].replace(/\|/g, "")
                }
                if (weeks.match(/Mt/)) {
                    months = weeks.match(/\d*/)
                    months = months[0]
                }
                weeks = weeks.replace(/\s/g, "")
                var cert = 0;
                if (position.match(/(S|G|P|D)/)) {
                    cert = position.match(/\d?\d?(S|G|P|D)/)
                    cert = cert[0]
                    position = position.replace(cert, "").replace(/\s/, "")
                }
                if (position.match(/\$!\$!/)) {
                  position = position.replace(/\$!\$!/g, "|")
                }
                if (position.match(/\$::\$/)) {
                  position = position.replace(/\$::\$/g, "=")
                }

                albumtemplate += "\n| POS_" + country + n + " = " + position
                if (ref !== 0) {
                    albumtemplate += ref
                }
                if (months !== 0) {
                    albumtemplate += " | MT_" + country + n + " = " + months
                } else {
                    albumtemplate += " | WO_" + country + n + " = " + weeks
                }
                if (no1year !== 0) {
                  albumtemplate += " | Jahr_" + country + n + " = " + no1year
                }
                if (cert !== 0) {
                    albumtemplate += " | A_" + country + n + " = " + cert
                }
                months = 0
                ref = 0
                cert = 0
                no1year = 0
            }

            // Anmerkungen
            albumtemplate += "\n| Anmerkung" + n + " = "
            if (albumtitleadd) {
                albumtemplate += albumtitleadd[0].replace(/<small\>\(/, "").replace(/\)<\/small\>/, "")
            }
        }

        albumtemplate += "\n}}\n}}"
    }

    // SINGLES
    if (singles) {
        singles = singles[0].replace(/Jahr\=/g, "JAHR:").replace(/\n?\|.+\=\n?/g, "")
        var singlelines = singles.match(/\{\{Single[\s\S]+?\}\}\s*\n\s*\}\}/g)

        var singlesources = input.match(/\|\s?Quellen Singles\s?\=\s?\n?.+?\n/)
        if (singlesources) {
            singlesources = singlesources[0].replace(/\|\s?Quellen Singles\s?\=\s?\n?/, "").replace(/\n$/, "")
        } else {
            singlesources = ""
        }
        var singlecountries = singles.match(/\|[A-Z\-]+?\|/g)
        var uniquesinglecountries = [...new Set(singlecountries)]
        singlecountries = []
        var i = 0;
        var singlecountrydisplay = "|"
        for (i in uniquesinglecountries) {
            singlecountries[i] = uniquesinglecountries[i].replace(/\|/g, "")
            singlecountrydisplay += singlecountries[i] + "|"
        }
        var singleyears = [];
        i = 0;
        for (i in singlelines) {
            var lineyears = singlelines[i].replace(/\|JAHR:\d\d\d\d/g, "").match(/\d\d\d\d\|/g)
            var j = 0;
            for (j in lineyears) {
                lineyears[j] = lineyears[j].replace(/\|/, "")
            }
            singleyears[i] = Math.min(...lineyears)
        }

        
        var singletemplate = "{{Charttabelle\n" + singlecountrydisplay + " Quellen = " + singlesources + "\n| Art = Singles" + "\n| INHALT =\n"

        i = 0;
        var k = 1;
        for (i in singleyears) {
            // Titel
            var singletitles = singlelines[i].match(/\{\{Single\s+?\|\s?.+/)
            var singletitle = singletitles[0].replace(/\{\{Single\s+?\|\s?/, "")
            var singletitleadd = singletitle.match(/<small\>\(.+?\)<\/small\>/)
            if (singletitleadd) {
                singletitle = singletitle.replace(singletitleadd, "").trim()
            }

            var n = k+1
            if (singleyears[i] == singleyears[i-1]) {
                singletemplate += "\n| Titel" + n + " = "
                k = k + 1;
            } else {
                if (i>0) {
                    singletemplate += "\n}}\n"
                    k = 1
                }
                n = ""
                singletemplate += "{{Charteintrag\n" + singlecountrydisplay + " Jahr = " + singleyears[i] + "\n| Titel = "
            }                

            singletemplate += singletitle += "\n| Album" + n + " = "

            // Chartdaten
            var singlecharts = singlelines[i].match(/\{\{Charts.+/g)
            var m = 0;
            for (m in singlecharts) {
                var rawline =  singlecharts[m].replace(/=/g, "$::$")
                var country = rawline.match(/\|[A-Z\-]+?\|/)
                rawline = rawline.replace(/\{\{Charts/, "").replace(country, "")
                country = country[0].replace(/\|/g, "")
                var no1year = 0
                if (rawline.match(/\|JAHR\:/)) {
                  no1year = rawline.match(/\|JAHR\:\d\d\d\d/)
                  rawline = rawline.replace(no1year, "")
                  no1year = no1year[0].replace(/\D/g, "")
                }
                var addtemplates = 0
                if (rawline.match(/\{\{/)) {
                  addtemplates = rawline.match(/\{\{.+?\}\}/)
                  var maskaddtemplates = addtemplates[0].replace(/\|/g, "$!$!")
                  rawline = rawline.replace(addtemplates, maskaddtemplates)
                }
                var position = rawline.match(/^.*?\|/)
                rawline = rawline.replace(position, "")
                position = position[0].replace(/\|/g, "")
                var entrydate = rawline.match(/^.*?\|/)
                rawline = rawline.replace(entrydate, "")
                entrydate = entrydate[0].replace(/\|/g, "") // wohin damit?
                var weeks = rawline.replace(/\}\}/, "")
                var ref = 0;
                var months = 0;
                if (weeks.match(/\|/)) {
                    ref = weeks.match(/\|.*/)
                    weeks = weeks.replace(ref, "")
                    ref = ref[0].replace(/\|/g, "")
                }
                if (weeks.match(/Mt/)) {
                    months = weeks.match(/\d*/)
                    months = months[0]
                }
                weeks = weeks.replace(/\s/g, "")
                var cert = 0;
                if (position.match(/(S|G|P|D)/)) {
                    cert = position.match(/\d?\d?(S|G|P|D)/)
                    cert = cert[0]
                    position = position.replace(cert, "").replace(/\s/, "")
                }
                if (position.match(/\$!\$!/)) {
                  position = position.replace(/\$!\$!/g, "|")
                }
                if (position.match(/\$::\$/)) {
                  position = position.replace(/\$::\$/g, "=")
                }

                singletemplate += "\n| POS_" + country + n + " = " + position
                if (ref !== 0) {
                    singletemplate += ref
                }
                if (months !== 0) {
                    singletemplate += " | MT_" + country + n + " = " + months
                } else {
                    singletemplate += " | WO_" + country + n + " = " + weeks
                }
                if (no1year !== 0) {
                    singletemplate += " | Jahr_" + country + n + " = " + no1year
                }
                if (cert !== 0) {
                    singletemplate += " | A_" + country + n + " = " + cert
                }
                months = 0
                ref = 0
                cert = 0
                no1year = 0
            }

            // Anmerkungen
            singletemplate += "\n| Anmerkung" + n + " = "
            if (singletitleadd) {
                singletemplate += singletitleadd[0].replace(/<small\>\(/, "").replace(/\)<\/small\>/, "")
            }
        }

        singletemplate += "\n}}\n}}"
    }


    // EPS
    if (eps) {
        eps = eps[0].replace(/Jahr\=/g, "JAHR:").replace(/\n?\|.+\=\n?/g, "")
        var eplines = eps.match(/\{\{EP[\s\S]+?\}\}\s*\n\s*\}\}/g)

        var epsources = input.match(/\|\s?Quellen EPs\s?\=\s?\n?.+?\n/)
        if (epsources) {
            epsources = epsources[0].replace(/\|\s?Quellen EPs\s?\=\s?\n?/, "").replace(/\n$/, "")
        } else {
            epsources = ""
        }
        var epcountries = eps.match(/\|[A-Z\-]+?\|/g)
        var uniqueepcountries = [...new Set(epcountries)]
        epcountries = []
        var i = 0;
        var epcountrydisplay = "|"
        for (i in uniqueepcountries) {
            epcountries[i] = uniqueepcountries[i].replace(/\|/g, "")
            epcountrydisplay += epcountries[i] + "|"
        }
        var epyears = [];
        i = 0;
        for (i in eplines) {
            var lineyears = eplines[i].replace(/\|JAHR:\d\d\d\d/g, "").match(/\d\d\d\d\|/g)
            var j = 0;
            for (j in lineyears) {
                lineyears[j] = lineyears[j].replace(/\|/, "")
            }
            epyears[i] = Math.min(...lineyears)
        }

        
        var eptemplate = "{{Charttabelle\n" + epcountrydisplay + " Quellen = " + epsources + "\n| Art = Alben" + "\n| INHALT =\n"

        i = 0;
        var k = 1;
        for (i in epyears) {
            // Titel
            var eptitles = eplines[i].match(/\{\{EP\s+?\|\s?.+/)
            var eptitle = eptitles[0].replace(/\{\{EP\s+?\|\s?/, "")
            var eptitleadd = eptitle.match(/<small\>\(.+?\)<\/small\>/)
            if (eptitleadd) {
                eptitle = eptitle.replace(eptitleadd, "").trim()
            }

            var n = k+1
            if (epyears[i] == epyears[i-1]) {
                eptemplate += "\n| Titel" + n + " = "
                k = k + 1;
            } else {
                if (i>0) {
                    eptemplate += "\n}}\n"
                    k = 1
                }
                n = ""
                eptemplate += "{{Charteintrag\n" + epcountrydisplay + " Jahr = " + epyears[i] + "\n| Titel = "
            }                

            eptemplate += eptitle

            // Chartdaten
            var epcharts = eplines[i].match(/\{\{Charts.+/g)
            var m = 0;
            for (m in epcharts) {
                var rawline =  epcharts[m]
                var country = rawline.match(/\|[A-Z\-]+?\|/)
                rawline = rawline.replace(/\{\{Charts/, "").replace(country, "")
                country = country[0].replace(/\|/g, "")
                var no1year = 0
                if (rawline.match(/\|JAHR\:/)) {
                  no1year = rawline.match(/\|JAHR\:\d\d\d\d/)
                  rawline = rawline.replace(no1year, "")
                  no1year = no1year[0].replace(/\D/g, "")
                }
                var addtemplates = 0
                if (rawline.match(/\{\{/)) {
                  addtemplates = rawline.match(/\{\{.+?\}\}/)
                  var maskaddtemplates = addtemplates[0].replace(/\|/g, "$!$!").replace(/=/g, "$::$")
                  rawline = rawline.replace(addtemplates, maskaddtemplates)
                }
                var position = rawline.match(/^.*?\|/)
                rawline = rawline.replace(position, "")
                position = position[0].replace(/\|/g, "")
                var entrydate = rawline.match(/^.*?\|/)
                rawline = rawline.replace(entrydate, "")
                entrydate = entrydate[0].replace(/\|/g, "") // wohin damit?
                var weeks = rawline.replace(/\}\}/, "")
                var ref = 0;
                var months = 0;
                if (weeks.match(/\|/)) {
                    ref = weeks.match(/\|.*/)
                    weeks = weeks.replace(ref, "")
                    ref = ref[0].replace(/\|/g, "")
                }
                if (weeks.match(/Mt/)) {
                    months = weeks.match(/\d*/)
                    months = months[0]
                }
                weeks = weeks.replace(/\s/g, "")
                var cert = 0;
                if (position.match(/(S|G|P|D)/)) {
                    cert = position.match(/\d?\d?(S|G|P|D)/)
                    cert = cert[0]
                    position = position.replace(cert, "").replace(/\s/, "")
                }
                if (position.match(/\$!\$!/)) {
                  position = position.replace(/\$!\$!/g, "|")
                }
                if (position.match(/\$::\$/)) {
                  position = position.replace(/\$::\$/g, "=")
                }

                eptemplate += "\n| POS_" + country + n + " = " + position
                if (ref !== 0) {
                    eptemplate += ref
                }
                if (months !== 0) {
                    eptemplate += " | MT_" + country + n + " = " + months
                } else {
                    eptemplate += " | WO_" + country + n + " = " + weeks
                }
                if (no1year !== 0) {
                    eptemplate += " | Jahr_" + country + n + " = " + no1year
                }
                if (cert !== 0) {
                    eptemplate += " | A_" + country + n + " = " + cert
                }
                months = 0
                ref = 0
                cert = 0
                no1year = 0
            }

            // Anmerkungen
            eptemplate += "\n| Anmerkung" + n + " = "
            if (eptitleadd) {
                eptemplate += eptitleadd[0].replace(/<small\>\(/, "").replace(/\)<\/small\>/, "")
            }
        }

        eptemplate += "\n}}\n}}"
    }


    // DVDS
    if (dvds) {
        dvds = dvds[0].replace(/Jahr\=/g, "JAHR:").replace(/\n?\|.+\=\n?/g, "")
        var dvdlines = dvds.match(/\{\{DVD[\s\S]+?\}\}\s*\n\s*\}\}/g)

        var dvdsources = input.match(/\|\s?Quellen DVDs\s?\=\s?\n?.+?\n/)
        if (dvdsources) {
            dvdsources = dvdsources[0].replace(/\|\s?Quellen DVDs\s?\=\s?\n?/, "").replace(/\n$/, "")
        } else {
            dvdsources = ""
        }
        var dvdcountries = dvds.match(/\|[A-Z\-]+?\|/g)
        var uniquedvdcountries = [...new Set(dvdcountries)]
        dvdcountries = []
        var i = 0;
        var dvdcountrydisplay = "|"
        for (i in uniquedvdcountries) {
            dvdcountries[i] = uniquedvdcountries[i].replace(/\|/g, "")
            dvdcountrydisplay += dvdcountries[i] + "|"
        }
        var dvdyears = [];
        i = 0;
        for (i in dvdlines) {
            var lineyears = dvdlines[i].replace(/\|JAHR:\d\d\d\d/g, "").match(/\d\d\d\d\|/g)
            var j = 0;
            for (j in lineyears) {
                lineyears[j] = lineyears[j].replace(/\|/, "")
            }
            dvdyears[i] = Math.min(...lineyears)
        }

        
        var dvdtemplate = "{{Charttabelle\n" + dvdcountrydisplay + " Quellen = " + dvdsources + "\n| Art = Alben" + "\n| INHALT =\n"

        i = 0;
        var k = 1;
        for (i in dvdyears) {
            // Titel
            var dvdtitles = dvdlines[i].match(/\{\{DVD\s+?\|\s?.+/)
            var dvdtitle = dvdtitles[0].replace(/\{\{DVD\s+?\|\s?/, "")
            var dvdtitleadd = dvdtitle.match(/<small\>\(.+?\)<\/small\>/)
            if (dvdtitleadd) {
                dvdtitle = dvdtitle.replace(dvdtitleadd, "").trim()
            }

            var n = k+1
            if (dvdyears[i] == dvdyears[i-1]) {
                dvdtemplate += "\n| Titel" + n + " = "
                k = k + 1;
            } else {
                if (i>0) {
                    dvdtemplate += "\n}}\n"
                    k = 1
                }
                n = ""
                dvdtemplate += "{{Charteintrag\n" + dvdcountrydisplay + " Jahr = " + dvdyears[i] + "\n| Titel = "
            }                

            dvdtemplate += dvdtitle

            // Chartdaten
            var dvdcharts = dvdlines[i].match(/\{\{Charts.+/g)
            var m = 0;
            for (m in dvdcharts) {
                var rawline =  dvdcharts[m]
                var country = rawline.match(/\|[A-Z\-]+?\|/)
                rawline = rawline.replace(/\{\{Charts/, "").replace(country, "")
                country = country[0].replace(/\|/g, "")
                var no1year = 0
                if (rawline.match(/\|JAHR\:/)) {
                  no1year = rawline.match(/\|JAHR\:\d\d\d\d/)
                  rawline = rawline.replace(no1year, "")
                  no1year = no1year[0].replace(/\D/g, "")
                }
                var addtemplates = 0
                if (rawline.match(/\{\{/)) {
                  addtemplates = rawline.match(/\{\{.+?\}\}/)
                  var maskaddtemplates = addtemplates[0].replace(/\|/g, "$!$!").replace(/=/g, "$::$")
                  rawline = rawline.replace(addtemplates, maskaddtemplates)
                }
                var position = rawline.match(/^.*?\|/)
                rawline = rawline.replace(position, "")
                position = position[0].replace(/\|/g, "")
                var entrydate = rawline.match(/^.*?\|/)
                rawline = rawline.replace(entrydate, "")
                entrydate = entrydate[0].replace(/\|/g, "") // wohin damit?
                var weeks = rawline.replace(/\}\}/, "")
                var ref = 0;
                var months = 0;
                if (weeks.match(/\|/)) {
                    ref = weeks.match(/\|.*/)
                    weeks = weeks.replace(ref, "")
                    ref = ref[0].replace(/\|/g, "")
                }
                if (weeks.match(/Mt/)) {
                    months = weeks.match(/\d*/)
                    months = months[0]
                }
                weeks = weeks.replace(/\s/g, "")
                var cert = 0;
                if (position.match(/(S|G|P|D)/)) {
                    cert = position.match(/\d?\d?(S|G|P|D)/)
                    cert = cert[0]
                    position = position.replace(cert, "").replace(/\s/, "")
                }
                if (position.match(/\$!\$!/)) {
                  position = position.replace(/\$!\$!/g, "|")
                }
                if (position.match(/\$::\$/)) {
                  position = position.replace(/\$::\$/g, "=")
                }

                dvdtemplate += "\n| POS_" + country + n + " = " + position
                if (ref !== 0) {
                    dvdtemplate += ref
                }
                if (months !== 0) {
                    dvdtemplate += " | MT_" + country + n + " = " + months
                } else {
                    dvdtemplate += " | WO_" + country + n + " = " + weeks
                }
                if (no1year !== 0) {
                    dvdtemplate += " | Jahr_" + country + n + " = " + no1year
                }
                if (cert !== 0) {
                    dvdtemplate += " | A_" + country + n + " = " + cert
                }
                months = 0
                ref = 0
                cert = 0
                no1year = 0
            }

            // Anmerkungen
            dvdtemplate += "\n| Anmerkung" + n + " = "
            if (dvdtitleadd) {
                dvdtemplate += dvdtitleadd[0].replace(/<small\>\(/, "").replace(/\)<\/small\>/, "")
            }
        }

        dvdtemplate += "\n}}\n}}"
    }

    
    // EXPORT
    var templates = "== Diskografie =="
    if (filelink) {
        templates += "\n" + filelink
    }
    if (albumtemplate) {
        templates += "\n=== Alben ===\n" + albumtemplate + "\n"
    }
    if (eptemplate) {
        templates += "\n=== EPs ===\n" + eptemplate + "\n"
    }
    if (singletemplate) {
        templates += "\n=== Singles ===\n" + singletemplate + "\n"
    }
    if (dvdtemplate) {
        templates += "\n=== Videoalben ===\n" + dvdtemplate + "\n"
    }

    $('#wikitext-out').val(templates.trim());
}

function wikitext() {
    var input = document.getElementById("wikitext-in").value;
    var tablestart = input.search(/{\|/)
    var tableend = input.search(/|\}/)
    var tablesections = input.split(/\|\-/)
    var countrylines = tablesections[2].match(/\!.+\|([\s\w\-\/\&]+)\]\]/gm) //unklar
  
    var sources = ""
    if (tablesections[1].match(/<ref.+\>/gm)) { // unklar (Zeilenumbrüche)
      sources = tablesections[1].match(/<ref.+\>/gm)
    }
  
    var rformat;
    var titleaddition;
    if (tablesections[1].match(/Titel.+Album/)) {
        rformat = "Singles"
    } else if (tablesections[1].match(/Titel.+<small/)) {
        titleaddition = tablesections[1].match(/Titel.+?<small\>(.+)<\/small\>/)
        titleaddition[1] = titleaddition[1].replace(/\'\'/g, "")
        rformat = "Alben"
    } else {
        rformat = "Alben"
    };
  
    var extra
    if (tablesections[1].match(/Anmerkungen\s?\n\!/m)) {
      var extraline = tablesections[1].match(/Anmerkungen\s?\n\!.+\|\s?(.+)/m)
      extra = extraline[1]
    }
  
    var countries = [];
    var countrylink;
    var countrydisplay = "";
    var i = 0;
    for (i in countrylines) {
      countrylink = countrylines[i].match(/\|([\s\w\-\/\&]+)\]\]/); //unklar
      countries[i] = countrylink[1];
      countrydisplay += "|" + countries[i];
    }
  
    var years = [];
    var yearlines = [];
    var titles = [];
    var positionlines = [];
    var comments = [];
    var extras = [];
    var extraspan = [];
    var j = 3;
    for (j in tablesections) {
      var k = j - 3;
      yearlines[k] = ["", "1"];
      if (tablesections[j].match(/\|\s?(\d\d\d\d)/)) {
        years[k] = tablesections[j].match(/\d\d\d\d/)
        if (tablesections[j].match(/\|.*rowspan.+\|\s?\d\d\d\d/)) {
          yearlines[k] = tablesections[j].match(/rowspan\=\"?(\d\d?)\"?.+?\|/)
        }
      }
      
      if (tablesections[j].match(/\|\s?\d\d\d\d\s\|\s?.+/m)) {
        titles[k] = tablesections[j].match(/\|\s?\d\d\d\d\s\|\s?(.+)/m)
      } else {
        titles[k] = tablesections[j].match(/^.*?\n\|\s?(.+)/m)
      }
  
      positionlines[k] = tablesections[j].match(/\|.+center.+\|.+/gm);
    //  positionlines[k] = tablesections[j].match(/\|\s?<\!\-\-.+?\|.+/gm);
      if (tablesections[j].match(/\|\s?<small\>/)) {                             // unklar
        comments[k] = tablesections[j].match(/\|\s?<small\>(.+)<\/small\>/)
        if (tablesections[j].match(/\|\s?<small\>.+<\/small\>\n\|[^\}]\s?.+/)) {
          extras[k] = tablesections[j].match(/\|\s?<small\>.+<\/small\>\n\|\s?(.+)/)
        }
      } else {
        comments[k] = ""
      }
      if (extras[k]) {
        if (extras[k][1].match(/rowspan/)) {
          extraspan[k] = extras[k][1].match(/rowspan\=\"?(\d+?)\"?\s?\|/)
          extras[k][1] = extras[k][1].replace(/rowspan\=\"?\d+?\"?\s?\|/, "")
        }
      }
    }
  
  
    var template = "{{Charttabelle\n" + countrydisplay + "\n| Quellen = " + sources + "\n| Art = " + rformat
    if (titleaddition) {
      template += "\n| TitelErg = " + titleaddition[1]
    }
    if (extra) {
      template += "\n| Extra = " + extra
    }
    template += "\n| INHALT =\n"
  
    var hidecountry = [];
    for (var l = 0; l < years.length; l++) {
      template += "{{Charteintrag" + "\n" + countrydisplay + "| Jahr = " + years[l]
      for (var p = 1; p <= yearlines[l][1]; p++) {
        var q = l+p-1
        template += "\n| Titel"
        if (p > 1) {
          template += p
        }
        template += " = "
        if (titles[q][1].match(/<br.+?small\>/)) {
          var addtitle = titles[q][1].match(/(.+)<br.+?small\>(.+?)<\/small\>/)
          var title = addtitle[1]
          title = title.replace(/\'\'/g, "")
          var add = addtitle[2]
          add = add.replace(/\'\'/g, "")
          template += title
          if (rformat == "Singles") {
            template += "\n| Album"
          } else {
            template += "\n| TitelErg"
          }
          if (p > 1) {
            template += p
          }
          template += " = " + add + "\n"
        } else {
          template += titles[q][1] + "\n"
        }
        
        var m = 0;
        var position = [];
        var positiondisplay = [];
        var posrowspan = [];
        var week = "";
        var month;
        var certification = [];
        var diffyear = [];
        var diffyearbrackets = [];
  
        for (m in countries) {
  //        if (hidecountry[m]) {
  //          position = ["", "n"]
  //        } else {
            position = positionlines[q][m].match(/\|.+center.+?\|\s?(.+)/);
  //        }
          positiondisplay = position[1]
          if (position[0].match(/rowspan/)) {
            var posrowspanexpr = position[0].match(/rowspan\=\"?(\d+?)\"?/)   // unklar
            posrowspan[m] = posrowspanexpr[1]
  //          hidecountry[m] = "hidden"
          }
          var weekline = positionlines[q][m].match(/<small\>\((.+?)\sWo\.\)/);
          var monthline = positionlines[q][m].match(/<small\>\((.+?)\sMt\.\)/);
          if (weekline) {
            week = weekline[1]
            positiondisplay = positiondisplay.replace(/<small\>\((.+?)\sWo\.\)<\/small\>/, "")
          } else if (monthline) {
            month = monthline[1]
            week = month
            positiondisplay = positiondisplay.replace(/<small\>\((.+?)\sMt\.\)<\/small\>/, "")
            };
          positiondisplay = positiondisplay.replace(/<br\s?\/?\>/g, "")
          positiondisplay = positiondisplay.replace(/<\!\-\-.+?\-\-\>/g, "") // Kommentare unterdrücken
          
          var poslink = positiondisplay.match(/\[\[.+\]\]/)
          if (poslink) {
            if (positiondisplay.match(/Nummer\-eins/)) {
              if (positiondisplay.match(/\(\d{4}\)/)) {
                diffyearbrackets[m] = positiondisplay.match(/\((\d{4})\)/)
                if (diffyearbrackets[m][1] != years[l]) {
                  diffyear[m] = diffyearbrackets[m][1]
                }
              }
              positiondisplay = positiondisplay.replace(/\'{0,3}?\[\[.+?\]\]\'{0,3}?/, "1")
            }        
            if (positiondisplay.match(/(Datei|File|Bild|Image)/)) {
              certification[m] = positiondisplay.match(/\[\[.+\]\]/)
              positiondisplay = positiondisplay.replace(/\[\[.+\]\]/, "")
            }
          }
          positiondisplay = positiondisplay.replace(/\'\'\'/g, "")
  
          if (positiondisplay != "—") {
            template += "| POS_" + countries[m]
            if (p > 1) {
              template += p
            }
            template += " = " + positiondisplay
            if (positiondisplay !== "n") {
              if (monthline) {
                template += " | MT_"
              } else {
                template += " | WO_"
              }
              template += countries[m]
              if (p > 1) {
                template += p
              }
              template += " = "
              if (weekline || monthline) {
                if (week == "…") {
                  template += ""
                } else {
                  template += week
                }
              } else {
                template += "n"
              }
              if (diffyear[m]) {
                template += " | Jahr_" + countries[m]
                if (p > 1) {
                  template += p
                }
                template += " = " + diffyear[m]
              }
              if (certification[m]) {
                template += " | A_" + countries[m]
                if (p > 1) {
                  template += p
                }
                var shortcut = certshortcut(certification[m])
                template += " = " + shortcut
              }
              if (posrowspan[m]) {
                template += " | Zeilen_" + countries[m]
                if (p > 1) {
                  template += p
                }
                template += " = " + posrowspan[m]
              }
            }
            template += "\n"
          }
       };
        template += "| Anmerkung"
        if (p > 1) {
          template += p
        }
        template += " = "
        if (comments[q][1]) {
          template += comments[q][1]
        } else {
          template += ""
        }
        if (extras[q]) {
          template += "\n| Extra"
          if (p > 1) {
            template += p
          }
          template += " = " + extras[q][1]
          if (extraspan[q]) {
            template += " | Zeilen_Extra"
            if (p > 1) {
              template += p
            }
            template += " = " + extraspan[q][1]
          }
        }
     };
     template += "\n}}\n"
    }
    template += "}}"
  
    template = template.replace(/\s\n+/g, "\n")
    template = template.replace(/\s\s+/g, " ") // Leerräume reduzieren
    //template = template.replace(/<\/?small\>/g, "") // small-Attribute immer unterdrücken
    template = template.replace(/\sPOS\_.+?\=\s\—\s\|\sWO\_.+?\n?\|/g, "") // leere Chartzellen unterdrücken
    template = template.replace(/\{\{Charteintrag\n\|.+Jahr\s\=\sundefined[\s\S]+?\}\}\n/gm, "") // undefinierte Jahre unterdrücken
  
    if (!input.match(/^\s*\{\|/) || !input.match(/\|\}\s*$/)) {
      template = "Keine Tabelle!"
    }
  
    $('#wikitext-out').val(template);
  }


function wikitextxmas() {
  var input = document.getElementById("wikitext-in").value;
  var tablestart = input.search(/{\|/)
  var tableend = input.search(/|\}/)
  var tablesections = input.split(/\|\-/)
  var countrylines = tablesections[2].match(/\!.+\|([\s\w\-\/\&]+)\]\]/gm) //unklar

  var sources = ""
  if (tablesections[1].match(/<ref.+\>/gm)) { // unklar (Zeilenumbrüche)
    sources = tablesections[1].match(/<ref.+\>/gm)
  }

  var countries = [];
  var countrylink;
  var countrydisplay = "";
  var i = 0;
  for (i in countrylines) {
    countrylink = countrylines[i].match(/\|([\s\w\-\/\&]+)\]\]/); //unklar
    countries[i] = countrylink[1];
    countrydisplay += "|" + countries[i];
  }

  var years = [];
  var yearlines = [];
  var positionlines = [];
  var j = 3;
  for (j in tablesections) {
    var k = j - 3;
    yearlines[k] = ["", "1"];
    if (tablesections[j].match(/\|\s?\d\d\d\d/)) {
      years[k] = tablesections[j].match(/\|\s?(\d\d\d\d.+?)\s/)[1]
    } else if (tablesections[j].match(/\|\s?\'\'Insgesamt\'\'/)) {
      years[k] = "Insgesamt"
    } else {
      years[k] = "TRENNER"
    }
    if (years[k] == "TRENNER") {
      positionlines[k] = "TRENNER"
    } else {
      positionlines[k] = tablesections[j].match(/\|\s?<\!\-\-.+\-\-\>.+/gm);
    }    
  }


  var template = "{{Weihnachtscharttabelle\n" + countrydisplay + "\n| Quellen = " + sources + "\n| Art = " + "\n| INHALT =\n"

  var hidecountry = [];
  for (var l = 0; l < years.length; l++) {
    if (years[l] == "TRENNER") {
      template += "{{Weihnachtscharteintrag/Trenner}}\n"
    } else {
      template += "{{Weihnachtscharteintrag" + "\n" + countrydisplay + "\n| Periode = " + years[l] + "\n"
      for (var p = 1; p <= yearlines[l][1]; p++) {
        var q = l+p-1
        
        var m = 0;
        var position = [];
        var positiondisplay = [];
        var posrowspan = [];
        var week = "";
        var month;
        var certification = [];
        var diffyear = [];
        var diffyearbrackets = [];

        for (m in countries) {
          position = positionlines[q][m].match(/\|\s?<\!\-\-.+\-\-\>(.+)/);
          positiondisplay = position[1].replace(/\|/, "")
                  
          var weekline = positionlines[q][m].match(/<small\>\((.+?)\sWo\.\)/);
          var monthline = positionlines[q][m].match(/<small\>\((.+?)\sMt\.\)/);
          if (weekline) {
            week = weekline[1]
            positiondisplay = positiondisplay.replace(/<small\>\((.+?)\sWo\.\)<\/small\>/, "")
          } else if (monthline) {
            month = monthline[1]
            week = month
            positiondisplay = positiondisplay.replace(/<small\>\((.+?)\sMt\.\)<\/small\>/, "")
            };
          positiondisplay = positiondisplay.replace(/<br\s?\/?\>/g, "")
          positiondisplay = positiondisplay.replace(/<\!\-\-.+?\-\-\>/g, "") // Kommentare unterdrücken
          
          var poslink = positiondisplay.match(/\[\[.+\]\]/)
          if (poslink) {
            if (positiondisplay.match(/Nummer\-eins/)) {
              if (positiondisplay.match(/\(\d{4}\)/)) {
                diffyearbrackets[m] = positiondisplay.match(/\((\d{4})\)/)
                if (diffyearbrackets[m][1] != years[l].match(/\d{4}/)) {
                  diffyear[m] = diffyearbrackets[m][1]
                }
              }
              positiondisplay = positiondisplay.replace(/\'{0,3}?\[\[.+?\]\]\'{0,3}?/, "1")
            }        
            if (positiondisplay.match(/(Datei|File|Bild|Image)/)) {
              certification[m] = positiondisplay.match(/\[\[.+\]\]/)
              positiondisplay = positiondisplay.replace(/\[\[.+\]\]/, "")
            }
          }
          positiondisplay = positiondisplay.replace(/\'\'\'/g, "")

          if (!positiondisplay.match(/\—$/)) {
            template += "| POS_" + countries[m]
            if (p > 1) {
              template += p
            }
            template += " = " + positiondisplay
            if (positiondisplay !== "n") {
              if (monthline) {
                template += " | MT_"
              } else {
                template += " | WO_"
              }
              template += countries[m]
              if (p > 1) {
                template += p
              }
              template += " = "
              if (weekline || monthline) {
                if (week == "…") {
                  template += ""
                } else {
                  template += week
                }
              } else {
                template += "n"
              }
              if (diffyear[m]) {
                template += " | Jahr_" + countries[m]
                if (p > 1) {
                  template += p
                }
                template += " = " + diffyear[m]
              }
              if (certification[m]) {
                template += " | A_" + countries[m]
                if (p > 1) {
                  template += p
                }
                var shortcut = certshortcut(certification[m])
                template += " = " + shortcut
              }
              if (posrowspan[m]) {
                template += " | Zeilen_" + countries[m]
                if (p > 1) {
                  template += p
                }
                template += " = " + posrowspan[m]
              }
            }
            template += "\n"
          }
      };
    };
    template += "\n}}\n"
  }
  }
  template += "}}"

  template = template.replace(/\s\n+/g, "\n")
  template = template.replace(/\s\s+/g, " ") // Leerräume reduzieren
  //template = template.replace(/<\/?small\>/g, "") // small-Attribute immer unterdrücken
  template = template.replace(/\sPOS\_.+?\=\s\—\s\|\sWO\_.+?\n?\|/g, "") // leere Chartzellen unterdrücken
  template = template.replace(/\{\{Charteintrag\n\|.+Jahr\s\=\sundefined[\s\S]+?\}\}\n/gm, "") // undefinierte Jahre unterdrücken

  if (!input.match(/^\s*\{\|/) || !input.match(/\|\}\s*$/)) {
    template = "Keine Tabelle!"
  }

  $('#wikitext-out').val(template);
  
}

  
  function certshortcut(certimage) {
    var certtypes = ["S", "G", "P", "D"]
    var numbersG = ["", "", "Zwei", "Drei", "Vier", "Fünf", "Sechs", "Sieben", "Acht", "Neun", "Zehn", "Elf", "Zwölf"]
    certimage = certimage + ""
    var certimagef = certimage.replace(/px/g, "")
    var certimages = []
    if (certimagef.match(/\]\]\s?\[\[/)) {
      certimages = certimagef.split(/\]\]\s?\[\[/)
    } else {
      certimages[0] = certimagef
    }
    var shortcut = ""
  
    for (a = 0; a < certimages.length; a++) {
      var certtype = ""
      var certnumber = ""
      if (certimages[a].match(/\d+?\-fach/)) {
        var number = certimages[a].match(/(\d+?)\-fach/)
        certnumber = number[1]
      } else if (certimages[a].match(/(Zwei|Drei|Vier|Fünf|Sechs|Sieben|Acht|Neun|Zehn|Elf|Zwölf)/i)) {
          for (var n = 3; ; n++) {
            if (certimages[a].match(numbersG[n])) {
              certnumber = n
              break
            }
          }
      } else if (certimages[a].match(/Doppel/)) {
        certnumber = 2
      }
      if (certimages[a].match(/(silber|silver)/i)) {
        certtype = "S"
      } else if (certimages[a].match(/gold/i)) {
        certtype = "G"
      } else if (certimages[a].match(/platin/i)) {
        certtype = "P"
      } else if (certimages[a].match(/(diamant|diamond)/i)) {
        certtype = "D"
      }
      shortcut += certnumber + certtype
      if (certimages.length > 1 && a < (certimages.length - 1)) {
        shortcut += "+"
      }
    }
    
    return shortcut
  }
  
function convert() {
    if (document.getElementById("wikitext-in").value.match("Infobox Chartplatzierungen")) {
        try {
            wikitextbox()
        } catch (error) {
            $('#wikitext-out').val(error);
        }
    } else if (document.getElementById("wikitext-in").value.match("Anmerkungen")) {
        try {
            wikitext()
        } catch (error) {
            $('#wikitext-out').val(error);
        }
    } else {
    	try {
            wikitextxmas()
        } catch (error) {
            $('#wikitext-out').val(error);
        }
    }
}
  
function examplebox() {
    var ex = "{{Infobox Chartplatzierungen\n| Bild = Bild.jpg\n| Bildbeschreibung = Max Mustermann live\n| Singles =\n  {{Single\n    |Schubi dubi du <small>(feat. Erika Musterfrau)</small>\n    "
    ex += "|{{Charts|DE|5 G|20.05.2011|23}}\n    |{{Charts|AT|10 |20.05.2011|4}}\n    |{{Charts|CH|1 P|Jahr=2012|22.05.2011|27}}\n  }}\n"
    ex += "| Alben =\n  {{Album\n    |The Album\n    |{{Charts|DE|30|01.06.2010|5}}\n    |{{Charts|UK|88|20.06.2010|1}}\n    |{{Charts|US|40|15.06.2010|4|<ref>Beleg</ref>}}\n  }}\n"
    ex += '| Quellen Alben =\n<ref name="albumcharts">Chartsurfer</ref>\n}}\n| Quellen Singles =\n<ref name="singlecharts">Chartsurfer 2</ref>\n}}'
  
    $('#wikitext-in').val(ex);
}

function example() {
    var ex = '{| class="wikitable"\n|- class="hintergrundfarbe8"\n! rowspan="2"| Jahr\n! width="210" rowspan="2"| Titel\n! colspan="3"| Chartplatzierungen<ref>Quellen</ref>\n! width="215" rowspan="2"| Anmerkungen'
    ex += '\n|- class="hintergrundfarbe8"\n! width="50"| {{DEU|#}} [[Deutsche Musikcharts|DE]]\n! width="50"| {{AUT|#}} [[Ö3 Austria Top 40|AT]]\n! width="50"| {{CHE|#}} [[Schweizer Hitparade|CH]]'
    ex += '\n|- bgcolor="#E1E9F3"\n| bgcolor="#f0f0ff"| 2000\n| Tra la la\n| align="center" <!--DE-->| —\n| align="center" <!--AT-->| 37 <br/><small>(13 Wo.)</small>\n| align="center" <!--CH-->| —\n| <small>Erstveröffentlichung: 25. September 2000</small>'
    ex += '\n|- bgcolor="#eeeeee"\n| bgcolor="#f0f0ff"| 2002\n| Schubi dubi du\n| align="center" <!--DE-->| 11 [[Datei:Gold record icon.svg|15px|Gold]] <br/><small>(35 Wo.)</small>\n| align="center" <!--AT-->| —\n| align="center" <!--CH-->| —\n| <small>Erstveröffentlichung: 6. Mai 2002 <br/>Verkäufe: + 150.000</small>\n|}'
  
    $('#wikitext-in').val(ex);
  }

function collapse() {
	var coll = document.getElementsByClassName("collapsible");
	var y;

	for (y = 0; y < coll.length; y++) {
		coll[y].classList.toggle("active");
		var content = coll[y].nextElementSibling;
		if (content.style.display === "block") {
			content.style.display = "none";
		} else {
			content.style.display = "block";
		}
	}
}