<commit_before><?php

/**
 * Generates an HTML <time /> element for the designated datestamp.
 * 
 * @param string $format	Date format to display to the reader. Same syntax used by PHP's own "date" function.
 * @param int|string $time	Time to format. Accepts either a UNIX timestamp or a string accepted by strtotime.
 * @return string An HTML representation of the formatted time. 
 */
function html_time($format, $time = NULL){
	$time	=	is_string($time) ? strtotime($time) : ($time ?: time());

	return sprintf('<time datetime="%1$s">%2$s</time>', date('c', $time), date($format, $time));
}



/**
 * Generates an HTML tag with the supplied parameters.
 * 
 * @param string $name - Tag name.
 * @param array $attr - Array of HTML attributes to insert.
 * @param string $content - Content to include between element's open/close tags. Ignored if $name is a self-closing element.
 * @param bool $close - If TRUE (default), the element's closing tag is appended to the returned value.
 * 
 * @return string The compiled markup.
 */
function build_tag($name, $attr = NULL, $content = NULL, $close = TRUE){

	#	Still XHTML-compliant.
	$name	=	strtolower(trim($name));


	#	Start building our HTML tag.
	$output	=	'<' . $name;

	#	Compile HTML attributes
	if($attr) foreach($attr as $key => $value)
		$output	.=	' '.$key.'="'.htmlentities(html_entity_decode($value)).'"';

	#	Ladies and gentlemen, this is a self-closing tag.
	if(in_array($name, explode(' ', 'area base basefont br col command frame hr img input keygen link menuitem meta param source track wbr')))
		return $output.' />';


	#	Close tag, injecting any specified content if need be.
	else return $output . '>'.($content ?: '').($close ? '</'.$name.'>' : '');
}



/**
 * Replaces any variable declarations found in a string with the accompanying values found in the supplied key/value pair.
 * This provides a safer and more controlled alternative to PHP's native eval function.
 * 
 * @example template('<input type="$type" id="$id" />', array(<commit_msg>Add PHP function for parsing basic property lists<commit_after><?php

/**
 * Generates an HTML <time /> element for the designated datestamp.
 * 
 * @param string $format	Date format to display to the reader. Same syntax used by PHP's own "date" function.
 * @param int|string $time	Time to format. Accepts either a UNIX timestamp or a string accepted by strtotime.
 * @return string An HTML representation of the formatted time. 
 */
function html_time($format, $time = NULL){
	$time	=	is_string($time) ? strtotime($time) : ($time ?: time());

	return sprintf('<time datetime="%1$s">%2$s</time>', date('c', $time), date($format, $time));
}



/**
 * Generates an HTML tag with the supplied parameters.
 * 
 * @param string $name - Tag name.
 * @param array $attr - Array of HTML attributes to insert.
 * @param string $content - Content to include between element's open/close tags. Ignored if $name is a self-closing element.
 * @param bool $close - If TRUE (default), the element's closing tag is appended to the returned value.
 * 
 * @return string The compiled markup.
 */
function build_tag($name, $attr = NULL, $content = NULL, $close = TRUE){

	#	Still XHTML-compliant.
	$name	=	strtolower(trim($name));


	#	Start building our HTML tag.
	$output	=	'<' . $name;

	#	Compile HTML attributes
	if($attr) foreach($attr as $key => $value)
		$output	.=	' '.$key.'="'.htmlentities(html_entity_decode($value)).'"';

	#	Ladies and gentlemen, this is a self-closing tag.
	if(in_array($name, explode(' ', 'area base basefont br col command frame hr img input keygen link menuitem meta param source track wbr')))
		return $output.' />';


	#	Close tag, injecting any specified content if need be.
	else return $output . '>'.($content ?: '').($close ? '</'.$name.'>' : '');
}



/**
 * Splits a line-delimited list of key:value pairs into an associative array.
 *
 * Trailing and leading whitespace is stripped from each line, as are blank rows.
 * 
 * @example parse_list('
 *     home: Home
 *     about: About Us
 *     gallery: View our entries
 *     terms-and-conditions: Terms & Conditions
 * ')
 * @param string $input - Block of text, supplied in the format described above.
 * @return array
 */
function parse_list($input){
	$output	=	array();
	
	# Bail early if we weren't passed a valid array.
	if(!$input) return $output;

	$rows	=	explode(PHP_EOL, preg_replace('#(?:^[\x20\t]+)|(?:\n\s*)(?=\n)|\s+$|\n\n#smi', '', $input ?: ''));
	foreach($rows as $row){
		$row				=	preg_split('~\s*:\s*~', $row);
		$output[$row[0]]	=	$row[1];
	}

	# Check if that was an associative array we just parsed
	if(!count(array_filter(array_values($output)))){

		# If so, return a plain indexed array
		$output	=	$rows;
	}
	return $output;
}



/**
 * Replaces any variable declarations found in a string with the accompanying values found in the supplied key/value pair.
 * This provides a safer and more controlled alternative to PHP's native eval function.
 * 
 * @example template('<input type="$type" id="$id" />', array(
{'fraction_non_alphanumeric': 0.14617676266137042, 'fraction_numerical': 0.0015888778550148957, 'mean_word_length': 3.927592954990215, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '4350375', 'n_tokens_mistral': 1607, 'n_tokens_neox': 1519, 'n_words': 588}