Added line trimming in jinja2 environment, and fixed encoding in template generation.