<commit_before>import string

def render(template,env):
	return string.Template(template).safe_substitute(env)

### NEW ### EXPERIMENTAL
import re
from util import join, flag

# TODO  $$x  ${x|y|z}???
def render2(template,env):
	# variables
	varnames = re.findall('[$]{?(\w+)}?',template)
	vre = join(set(varnames),"|","(?<![$])[$]{{?{0}}}?")
	def mapper(x):
		s = x.group()
		name = s.replace('$','').replace('{','').replace('}','') # TODO FIX performance
		return str(env.get(name,s))
	out = re.sub(vre,mapper,template)
	
	# conditional blocks
	cre = '[$]start-(\w+) (.*?) [$]end-\\1'
	conditionals = re.findall(cre,out,re.X)
	def mapper(x):
		name = x.group(1)
		text = x.group(2)
		return text if flag(env.get(name,'yes')) else ''
	out = re.sub(cre,mapper,out,re.X)
	return out

if __name__=="__main__":
	template = "to jest $aa test $bb mechanizmu $cc ok ${aa} $$bb $start-zz :) $end-zz"
	out = render(template,dict(aa=12,bb=34))
	out2 = render2(template,dict(aa=12,bb=34,zz=1))
	print(out)
	print(out2)<commit_msg>handle 6932 without var name in experimental renderer<commit_after>import string

def render(template,env):
	return string.Template(template).safe_substitute(env)

### NEW ### EXPERIMENTAL
import re
from util import join, flag

def render2(template,env):
	# variables
	varnames = re.findall('[$]{?(\w+)}?',template)
	vre = join(set(varnames+['']),"|","[$]?[$]{{?{0}}}?") # +[''] for handling $$ without var name
	def mapper(x):
		s = x.group()
		if s.startswith('$$'):
			return s[1:] # reduce one dollar sign
		name = s[1:].replace('{','').replace('}','') # TODO FIX performance
		return str(env.get(name,s))
	out = re.sub(vre,mapper,template)
	
	# conditional blocks
	cre = '[$]start-(\w+) (.*?) [$]end-\\1'
	conditionals = re.findall(cre,out,re.X)
	def mapper(x):
		name = x.group(1)
		text = x.group(2)
		return text if flag(env.get(name,'yes')) else ''
	out = re.sub(cre,mapper,out,re.X)
	return out

if __name__=="__main__":
	template = "$$ to jest $aa test $bb mechanizmu $cc ok ${aa} $$bb $start-zz :) $end-zz"
	out = render(template,dict(aa=12,bb=34))
	out2 = render2(template,dict(aa=12,bb=34,zz=1))
	print(out)
	print(out2)
{'fraction_non_alphanumeric': 0.22376973073351902, 'fraction_numerical': 0.018570102135561744, 'mean_word_length': 5.104815864022663, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 3, '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': '4382244', 'n_tokens_mistral': 899, 'n_tokens_neox': 859, 'n_words': 178}