<commit_before><?php

Class TemplateParser {
	
	static function get_partial_template($name) {
		# return contents of partial file, or return 'not found' error (as text)
		$partial = Helpers::rglob('./templates/partials*/'.$name.'.{html,json,xml,txt,atom}', GLOB_BRACE);
		return isset($partial[0]) ? file_get_contents($partial[0]) : 'Partial \''.$name.'\' not found';
	}
	
	static function parse($data, $template) {
		# parse template
		if(preg_match('/get[\s]+?["\']\/?(.*?)\/?["\']/', $template)) {
		  $template = self::parse_get($data, $template);
		}
		
		if(preg_match('/foreach[\s]+?([\$\@].+?)\s+?do\s+?([\S\s]+)endforeach/', $template)) {
		  $template = self::parse_foreach($data, $template);
		}
		
		if(preg_match('/if\s*?(!)?\s*?([\$\@].+?)\s+?do\s+?([\S\s]+?)endif/', $template)) {
		  $template = self::parse_if($data, $template);
		}
		
		if(preg_match('/\s:([\w\d_]+?)(\.html)?\b/', $template)) {
		  $template = self::parse_includes($data, $template);
		}
		
		if(preg_match('/\@[\w\d-_]+?/', $template)) {
		  $template = self::parse_vars($data, $template);
		}
		
		return $template;
	}
	
	static function parse_get(&$data, $template) {
	  # match any gets
	  preg_match('/get[\s]+?["\']\/?(.*?)\/?["\']/', $template, $template_parts);
    # strip out the get line
    $template = str_replace($template_parts[0], '', $template);
    # turn route into file path
    $file_path = Helpers::url_to_file_path($template_parts[1]);
    # set data object to match file path
    $data = AssetFactory::get($file_path);
    return $template;
	}
	
	static function parse_foreach($data, $template) {
		# split out the partial into the parts Before, Inside, and After the foreach loop
		preg_match('/([\S\s]*?)foreach[\s]+?([\$\@].+?)\s+?do\s+?([\S\s]+)endforeach([\S\s]*)$/', $template, $template_parts);
		# run the replacements on the pre-"foreach" part of the partial
		$template = self::parse($data, $template_parts[1]);
		<commit_msg>Only replace a get 'route' request if the route it is looking for exists<commit_after><?php

Class TemplateParser {
	
	static function get_partial_template($name) {
		# return contents of partial file, or return 'not found' error (as text)
		$partial = Helpers::rglob('./templates/partials*/'.$name.'.{html,json,xml,txt,atom}', GLOB_BRACE);
		return isset($partial[0]) ? file_get_contents($partial[0]) : 'Partial \''.$name.'\' not found';
	}
	
	static function parse($data, $template) {
		# parse template
		if(preg_match('/get[\s]+?["\']\/?(.*?)\/?["\']/', $template)) {
		  $template = self::parse_get($data, $template);
		}
		
		if(preg_match('/foreach[\s]+?([\$\@].+?)\s+?do\s+?([\S\s]+)endforeach/', $template)) {
		  $template = self::parse_foreach($data, $template);
		}
		
		if(preg_match('/if\s*?(!)?\s*?([\$\@].+?)\s+?do\s+?([\S\s]+?)endif/', $template)) {
		  $template = self::parse_if($data, $template);
		}
		
		if(preg_match('/\s:([\w\d_]+?)(\.html)?\b/', $template)) {
		  $template = self::parse_includes($data, $template);
		}
		
		if(preg_match('/\@[\w\d-_]+?/', $template)) {
		  $template = self::parse_vars($data, $template);
		}
		
		return $template;
	}
	
	static function parse_get(&$data, $template) {
	  # match any gets
	  preg_match('/get[\s]+?["\']\/?(.*?)\/?["\']/', $template, $template_parts);
	  # turn route into file path
    $file_path = Helpers::url_to_file_path($template_parts[1]);
    # if the route exists...
    if(file_exists($file_path)) {
      # strip out the get line
      $template = str_replace($template_parts[0], '', $template);
      # set data object to match file path
      $data = AssetFactory::get($file_path);
    }
    
    return $template;
	}
	
	static function parse_foreach($data, $template) {
		# split out the partial into the parts Before, Inside, and After the foreach loop
		preg_match('/([\S\s]*?)foreach[\s]+?([\$\@].+?)\s+?do\s+?([\S\s]+)endforeach([\S\s]*)$/', $template, $template_parts);
		# run the replacements on the pre-"foreach" part of the partial
		$template = self::parse($data, $template_parts[1]);
		
{'fraction_non_alphanumeric': 0.234375, 'fraction_numerical': 0.00248015873015873, 'mean_word_length': 4.034956304619226, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '6796652', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1448, 'n_words': 280}