<commit_before>package tools.haxelib;

import neko.zip.File;

typedef UserInfos = {
	var name : String;
	var fullname : String;
	var email : String;
	var libraries : Array<String>;
}

typedef VersionInfos = {
	var date : String;
	var name : String;
	var comments : String;
}

typedef LibraryInfos = {
	var name : String;
	var desc : String;
	var url : String;
	var owner : String;
	var curversion : String;
	var versions : Array<VersionInfos>;
}

typedef XmlInfos = {
	var lib : String;
	var url : String;
	var user : String;
	var desc : String;
	var version : String;
	var versionDesc : String;
}

class Datas {


	static var XML = "haxelib.xml";

	public static var REPOSITORY = "files";
	public static var alphanum = ~/^[A-Za-z0-9_.-]+$/;


	static function requiredAttribute( x : Xml, name ) {
		var v = x.get(name);
		if( v == null )
			throw "Missing required attribute '"+name+"' in node "+x.nodeName;
		return v;
	}

	static function requiredNode( x : Xml, name ) {
		var v = x.elementsNamed(name).next();
		if( v == null )
			throw "Missing required node '"+name+"' in node "+x.nodeName;
		return v;
	}

	static function requiredText( x : Xml ) {
		var v = x.firstChild();
		if( v == null || (v.nodeType != Xml.PCData && v.nodeType != Xml.CData) )
			throw "Missing required text in node "+x.nodeName;
		return v.nodeValue;
	}

	public static function fileName( lib : String, ver : String ) {
		return lib.split(".").join("-")+"-"+ver.split(".").join("-")+".zip";
	}

	public static function readInfos( zip : List<ZipEntry> ) : XmlInfos {
		var xmldata = null;
		for( f in zip )
			if( StringTools.endsWith(f.fileName,XML) ) {
				xmldata = neko.zip.File.unzip(f);
				break;
			}
		if( xmldata == null )
			throw XML+" not found in package";
		var x = Xml.parse(xmldata).firstElement();
		var lib = requiredAttribute(x,"name");
		if( lib.length < 3 || !alphanum.match(lib) )
			throw "Library name must contain at least 3 characters and only AZaz09_.- characters";
		var url = requiredAttribute(x,"url");
		var user = requiredAttribute(requiredNode(x,"user"),"name");
		if( user.length < 3 || !alphanum.match(user) )
			throw "User name must contain at least 3 characters and only AZaz09_.- characters";
		var desc = requiredText(requiredNode(x,"description"));<commit_msg>public XML, added safe.<commit_after>package tools.haxelib;

import neko.zip.File;

typedef UserInfos = {
	var name : String;
	var fullname : String;
	var email : String;
	var libraries : Array<String>;
}

typedef VersionInfos = {
	var date : String;
	var name : String;
	var comments : String;
}

typedef LibraryInfos = {
	var name : String;
	var desc : String;
	var url : String;
	var owner : String;
	var curversion : String;
	var versions : Array<VersionInfos>;
}

typedef XmlInfos = {
	var lib : String;
	var url : String;
	var user : String;
	var desc : String;
	var version : String;
	var versionDesc : String;
}

class Datas {


	public static var XML = "haxelib.xml";
	public static var REPOSITORY = "files";
	public static var alphanum = ~/^[A-Za-z0-9_.-]+$/;


	static function requiredAttribute( x : Xml, name ) {
		var v = x.get(name);
		if( v == null )
			throw "Missing required attribute '"+name+"' in node "+x.nodeName;
		return v;
	}

	static function requiredNode( x : Xml, name ) {
		var v = x.elementsNamed(name).next();
		if( v == null )
			throw "Missing required node '"+name+"' in node "+x.nodeName;
		return v;
	}

	static function requiredText( x : Xml ) {
		var v = x.firstChild();
		if( v == null || (v.nodeType != Xml.PCData && v.nodeType != Xml.CData) )
			throw "Missing required text in node "+x.nodeName;
		return v.nodeValue;
	}

	public static function safe( name : String ) {
		return name.split(".").join("-");
	}

	public static function fileName( lib : String, ver : String ) {
		return safe(lib)+"-"+safe(ver)+".zip";
	}

	public static function readInfos( zip : List<ZipEntry> ) : XmlInfos {
		var xmldata = null;
		for( f in zip )
			if( StringTools.endsWith(f.fileName,XML) ) {
				xmldata = neko.zip.File.unzip(f);
				break;
			}
		if( xmldata == null )
			throw XML+" not found in package";
		var x = Xml.parse(xmldata).firstElement();
		var lib = requiredAttribute(x,"name");
		if( lib.length < 3 || !alphanum.match(lib) )
			throw "Library name must contain at least 3 characters and only AZaz09_.- characters";
		var url = requiredAttribute(x,"url");
		var user = requiredAttribute(requiredNode(x,"user"),"name");
		if( user.length < 3 || !alphanum.match(user) )
			throw "User name must contain at least 3 characters and only AZaz09_.- characters";
		var desc = requiredText(requiredNode(x,"description"));
{'fraction_non_alphanumeric': 0.1418103448275862, 'fraction_numerical': 0.004310344827586207, 'mean_word_length': 3.6225099601593627, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4197437', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1574, 'n_words': 491}