/// <summary> /// Skips some characters in the input that won't be used in the output. /// You can also set the type of the text that is skipped. /// </summary> /// <param name="length">The characters to skip</param> /// <param name="painting">The painting of the skipped text</param> public void SkipInputText(int length, NHamlTokenType painting) { if (painting != NHamlTokenType.PlainText) { _painting.Add(new _SOURCEPAINTING { start = _inputPosition, end = _inputPosition + length, color = (int)painting } ); } _inputPosition += length; }
void HandleLine(string line) { var match = Regex.Match(line, @"^( *)([%.#-=/_])([^ \r\n]*)([^\r\n]*)"); if (match.Success) { NHamlTokenType type = NHamlTokenType.PlainText; int spaceLength = match.Groups[1].Length; // initial whitespace string capturedChar = match.Groups[2].ToString(); // determining character string definition = match.Groups[3].ToString(); // rest of the tag definiton (like 'tag' in '%tag') string text = match.Groups[4].ToString(); // rest of the line while ((openedBraces.Count > 0) && (spaceLength <= openedBraces.Peek())) { openedBraces.Pop(); codeMapper.AppendOutputText("}"); } bool skip = false; switch (capturedChar) { case "%": case ".": case "#": codeMapper.SkipInputText(spaceLength, NHamlTokenType.PlainText); foreach (Match m in Regex.Matches(capturedChar + definition, "([%.#])([^%.#]*)")) { char char1 = m.Groups[1].ToString()[0]; string def = m.Groups[2].ToString(); switch (char1) { case '%': type = NHamlTokenType.TagDefinition; break; case '.': type = NHamlTokenType.ClassDefinition; break; case '#': type = NHamlTokenType.IdDefinition; break; } codeMapper.SkipInputText(1 + def.Length, type); } type = NHamlTokenType.PlainText; break; case "-": type = NHamlTokenType.CodeDefinition; if (definition == "//") { type = NHamlTokenType.CommentDefinition; } else { openedBraces.Push(spaceLength); } break; case "=": type = NHamlTokenType.EquationDefinition; break; case "/": type = NHamlTokenType.CommentDefinition; break; case "_": type = NHamlTokenType.PartialDefinition; break; default: codeMapper.SkipInputText(line.Length, NHamlTokenType.PlainText); skip = true; break; //throw new InvalidOperationException(); } if (!skip) { if (type != NHamlTokenType.PlainText) { codeMapper.SkipInputText(spaceLength + 1 + definition.Length, type); } if ((type == NHamlTokenType.EquationDefinition) || (definition.Length > 0 && definition[definition.Length - 1] == '=')) { codeMapper.AppendOutputText("System.Console.WriteLine("); codeMapper.AddMappedSection(text.Length); codeMapper.AppendOutputText(");" + System.Environment.NewLine); } if ((type == NHamlTokenType.CodeDefinition)) { codeMapper.AddMappedSection(text.Length); codeMapper.AppendOutputText(" {"); codeMapper.AppendOutputText(System.Environment.NewLine); } } } else { var spaceLength = line.Length - line.TrimStart(' ').Length; while ((openedBraces.Count > 0) && (spaceLength <= openedBraces.Peek())) { openedBraces.Pop(); codeMapper.AppendOutputText("}"); } codeMapper.SkipInputText(line.Length, NHamlTokenType.PlainText); } }
/// <summary> /// Skips some characters in the input that won't be used in the output. /// You can also set the type of the text that is skipped. /// </summary> /// <param name="length">The characters to skip</param> /// <param name="painting">The painting of the skipped text</param> public void SkipInputText(int length, NHamlTokenType painting) { if (painting != NHamlTokenType.PlainText) { _painting.Add(new _SOURCEPAINTING { start = _inputPosition, end = _inputPosition+length, color = (int)painting } ); } _inputPosition += length; }