Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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();
        }
Exemplo n.º 3
0
        public void TestTranslations()
        {
            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.Translation
            }));
            Assert.That(tokens.Select(t => t.Value).ToList(), Is.EqualTo(new List <string> {
                "abc"
            }));

            tokens = parser.Parse("abc\n\ncde");
            Assert.That(tokens.Select(t => t.Type).ToList(), Is.EqualTo(new List <TokenType> {
                TokenType.Translation, TokenType.NewLine, TokenType.NewLine, TokenType.Translation
            }));
            Assert.That(tokens.Select(t => t.Value).ToList(), Is.EqualTo(new List <string> {
                "abc", "\n", "\n", "cde"
            }));
        }