public void TestNewLines() { ArticleLexer parser = new ArticleLexer(); List<Token> tokens; tokens = parser.Parse("\r\n"); Assert.That(tokens.Select(t => t.Type).ToList(), Is.EqualTo(new List<TokenType> {TokenType.NewLine})); Assert.That(tokens.Select(t => t.Text).ToList(), Is.EqualTo(new List<string> {"\r\n"})); Assert.That(tokens.Select(t => t.Value).ToList(), Is.EqualTo(new List<string> {"\n"})); tokens = parser.Parse("\r\n\r\n"); Assert.That(tokens.Select(t => t.Type).ToList(), Is.EqualTo(new List<TokenType> {TokenType.NewLine, TokenType.NewLine})); Assert.That(tokens.Select(t => t.Text).ToList(), Is.EqualTo(new List<string> {"\r\n", "\r\n"})); Assert.That(tokens.Select(t => t.Value).ToList(), Is.EqualTo(new List<string> {"\n", "\n"})); tokens = parser.Parse("abc\ncde"); Assert.That(tokens.Select(t => t.Type).ToList(), Is.EqualTo(new List<TokenType> {TokenType.Translation, TokenType.NewLine, TokenType.Translation})); Assert.That(tokens.Select(t => t.Text).ToList(), Is.EqualTo(new List<string> {"abc", "\n", "cde"})); Assert.That(tokens.Select(t => t.Value).ToList(), Is.EqualTo(new List<string> {"abc", "\n", "cde"})); tokens = parser.Parse("abc\rcde"); Assert.That(tokens.Select(t => t.Type).ToList(), Is.EqualTo(new List<TokenType> {TokenType.Translation, TokenType.NewLine, TokenType.Translation})); Assert.That(tokens.Select(t => t.Text).ToList(), Is.EqualTo(new List<string> {"abc", "\r", "cde"})); Assert.That(tokens.Select(t => t.Value).ToList(), Is.EqualTo(new List<string> {"abc", "\n", "cde"})); tokens = parser.Parse("abc\r\ncde"); Assert.That(tokens.Select(t => t.Type).ToList(), Is.EqualTo(new List<TokenType> {TokenType.Translation, TokenType.NewLine, TokenType.Translation})); Assert.That(tokens.Select(t => t.Text).ToList(), Is.EqualTo(new List<string> {"abc", "\r\n", "cde"})); Assert.That(tokens.Select(t => t.Value).ToList(), Is.EqualTo(new List<string> {"abc", "\n", "cde"})); tokens = parser.Parse("\nabc\n"); Assert.That(tokens.Select(t => t.Type).ToList(), Is.EqualTo(new List<TokenType> {TokenType.NewLine, TokenType.Translation, TokenType.NewLine})); Assert.That(tokens.Select(t => t.Text).ToList(), Is.EqualTo(new List<string> {"\n", "abc", "\n"})); Assert.That(tokens.Select(t => t.Value).ToList(), Is.EqualTo(new List<string> {"\n", "abc", "\n"})); }
private static FlowDocument FormatWord(Word word) { FlowDocument document = new FlowDocument(); if (word == null) { return document; } Paragraph paragraph = FlowDocumentStyles.CreateParagraph(); document.Blocks.Add(paragraph); { Run run = new Run(word.Name); paragraph.Inlines.Add(run); FlowDocumentStyles.FormatWord(run); paragraph.Inlines.Add(new LineBreak()); } ArticleLexer parser = new ArticleLexer(); List<Token> tokens = parser.Parse(word.Description); foreach (Token token in tokens) { string text = token.Value; if (token.Type == TokenType.Translation) { text = "\u2022 " + text; } if (token.Type == TokenType.Example) { text = "Example: " + text; } Run run = new Run(text); paragraph.Inlines.Add(run); FormatText(run, token.Type); } //todo: handle different number of new lines in the end of the artcle if (!string.IsNullOrEmpty(word.Tags)) { Paragraph tagsParagraph = new Paragraph(); document.Blocks.Add(tagsParagraph); tagsParagraph.Margin = new Thickness(0, 5, 0, 0); Run run = new Run("Tags: "); tagsParagraph.Inlines.Add(run); run.FontWeight = FontWeights.Bold; run = new Run(word.Tags); tagsParagraph.Inlines.Add(run); } return document; }
public void TestWordForms() { ArticleLexer parser = new ArticleLexer(); List<Token> tokens; tokens = parser.Parse("#abc"); Assert.That(tokens.Select(t => t.Type).ToList(), Is.EqualTo(new List<TokenType> {TokenType.WordForm})); Assert.That(tokens.Select(t => t.Value).ToList(), Is.EqualTo(new List<string> {"abc"})); tokens = parser.Parse("#abc\ncde"); Assert.That(tokens.Select(t => t.Type).ToList(), Is.EqualTo(new List<TokenType> {TokenType.WordForm, TokenType.NewLine, TokenType.Translation})); Assert.That(tokens.Select(t => t.Value).ToList(), Is.EqualTo(new List<string> {"abc", "\n", "cde"})); tokens = parser.Parse("abc\n#cde"); Assert.That(tokens.Select(t => t.Type).ToList(), Is.EqualTo(new List<TokenType> {TokenType.Translation, TokenType.NewLine, TokenType.WordForm})); Assert.That(tokens.Select(t => t.Value).ToList(), Is.EqualTo(new List<string> {"abc", "\n", "cde"})); tokens = parser.Parse("#abc\n#cde"); Assert.That(tokens.Select(t => t.Type).ToList(), Is.EqualTo(new List<TokenType> {TokenType.WordForm, TokenType.NewLine, TokenType.WordForm})); Assert.That(tokens.Select(t => t.Value).ToList(), Is.EqualTo(new List<string> {"abc", "\n", "cde"})); }
private void WriteWordForm(HtmlTextWriter writer, WordForm wordForm) { writer.AddAttribute(HtmlTextWriterAttribute.Class, CssWordArticle); if (wordForm.Name == wordForm.WordInfo.Word.Name) { writer.AddAttribute("id", CreateId(WordIdPrefix, wordForm.WordInfo.Word.Name)); } writer.RenderBeginTag(HtmlTextWriterTag.Div); writer.AddAttribute(HtmlTextWriterAttribute.Class, CssWordForm); writer.RenderBeginTag(HtmlTextWriterTag.Div); writer.WriteLine(wordForm.Title); writer.RenderEndTag(); if (wordForm.Name != wordForm.WordInfo.Word.Name) { writer.AddAttribute(HtmlTextWriterAttribute.Class, CssWordRef); writer.RenderBeginTag(HtmlTextWriterTag.Div); writer.Write("see: "); writer.AddAttribute(HtmlTextWriterAttribute.Href, "#" + CreateId(WordIdPrefix, wordForm.WordInfo.Word.Name)); writer.RenderBeginTag(HtmlTextWriterTag.A); writer.Write(wordForm.WordInfo.Word.Name); writer.RenderEndTag(); writer.RenderEndTag(); } else { ArticleLexer parser = new ArticleLexer(); List<Token> tokens = parser.Parse(wordForm.WordInfo.Word.Description); bool afterNewLine = false; foreach (Token token in tokens) { if (token.Type == TokenType.NewLine) { if (afterNewLine) { writer.WriteLine("<br/>"); } afterNewLine = true; } else { afterNewLine = false; WriteToken(writer, token); } } } writer.RenderEndTag(); }