Zum Inhalt springen

Benutzer:XanonymusX/CharttableConverter.js

aus Wikipedia, der freien Enzyklopädie
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 4. Februar 2019 um 20:37 Uhr durch XanonymusX (Diskussion | Beiträge) (versuch). Sie kann sich erheblich von der aktuellen Version unterscheiden.

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
  • Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
/*function toolbarlink() {
	mw.util.addPortletLink( "p-tb", "https://tools.wmflabs.org/charttableconverter", "Charttabellen-Konverter", "charttableconverter", 
	"Ruft das Tool zum Konvertieren alter Charttabellen in die Vorlage auf", null, null );
}

function opentool() {
	var selectedtext = Window.getSelection();
	var input
	if (selectedtext) {
		input = selectedtext
	} else {
		input = ""
	}
	var link = "https://tools.wmflabs.org/charttableconverter"
	
	window.open("link")
}

function showlink() {
    $( toolbarlink );
}*/


// main code of tool -----------------------------------------------------------------------------------

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 + "|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 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() {
  try {
    wikitext()
  } catch (error) {
    $('#wikitext-out').val(error);
  }
}

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);
}