3

I am working on a project on both Mac and Windows. If I build my project on the Mac, Handlebars templates result in translated line-breaks to strings containing LF (line-feed) characters. If I perform the same step on Windows it translates line-breaks to strings containing CRLF (carriage-return and line-feed) character strings.

It is not clear where the source of the problem is whether it is Handlebars, Node, Cygwin, Cake or Git. Intuitively I might expect that Handlebars is based on the precise nature of the templates and Git is automatically converting new lines for the templates to CRLF or LF depending on which machine I'm on.

For example, difference in Handlebars output on the two machines:

Mac (LF):

function program1(depth0,data) 
{
   var buffer = "", stack1;
   buffer += "\n  ";
   stack1 = helpers.each.call(depth0, depth0.produces, {hash:{},inverse:self.noop,fn:self.program(2, program2, data),data:data});
   if(stack1 || stack1 === 0) { buffer += stack1; }
   buffer += "\n";
   return buffer;
}

Windows (CRLF):

function program1(depth0,data) 
{
   var buffer = "", stack1;
   buffer += "\r\n  ";
   stack1 = helpers.each.call(depth0, depth0.produces, {hash:{},inverse:self.noop,fn:self.program(2, program2, data),data:data});
   if(stack1 || stack1 === 0) { buffer += stack1; }
   buffer += "\r\n";
   return buffer;
}

How do I control this behavior so that it is consistent and not platform dependent?

Coder Guy
  • 1,843
  • 1
  • 15
  • 21

2 Answers2

0

Try this, havent tested it myself:

document.write(String.fromCharCode(11));

See http://nemesis.lonestar.org/reference/telecom/codes/ascii.html

CR is 13, LF is 10.

floriank
  • 25,546
  • 9
  • 42
  • 66
0

I had the same issue, working with front-end developers who are on Macs while I am on Windows. Handlebars respected the CR LF in the templates and passed them on, which caused problems in node modules such as highlight.js that didn't handle the CR characters properly.

The problem is the default setting on git is to convert LF line endings to CR LF line endings when doing git operations on Windows. When I pulled code from my Mac friends, the conversion would take place and mess me up.

The solution is to change the git config setting using:

git config --global core.autocrlf input

so the conversion no longer takes place (see https://stackoverflow.com/a/5834094/117797 for more on this).

I also had to fix my current repo state by clearing and re-establishing the git index:

//clear git index
git rm --cached -r .

//re-write git index
git reset --hard

See https://stackoverflow.com/a/29888735/117797 for more.

Community
  • 1
  • 1
Zodman
  • 3,178
  • 2
  • 32
  • 38