public void StrippedHtmlIsSameAsInput()
        {
            var input = SampleFile.Load("csharp-sample.txt");
            var tokens = new CSharpLexer()
                .GetTokens(input)
                .ToArray();

            var subject = new HtmlFormatter(new HtmlFormatterOptions()
            {
                NoWrap = true
            });

            var htmlOut = new StringWriter();
            subject.Format(tokens, htmlOut);

            File.WriteAllText("output.html", htmlOut.ToString());

            var txtOut = new StringWriter();
            new NullFormatter().Format(tokens, txtOut);

            var strippedHtml = Regex.Replace(htmlOut.ToString(),
                @"<.*?>", "")
                .Trim();
            var escapedText = HtmlFormatter.EscapeHtml(txtOut.ToString()).Trim();

            Check.That(strippedHtml).IsEqualTo(escapedText);
        }
Exemplo n.º 2
0
        private void btnOpen_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog();
            var result = dialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                var filename = dialog.FileName;

                var lexer = LexerLocator.FindByFilename(filename);

                if (lexer == null)
                {
                    MessageBox.Show(@"I couldn't find a lexer for this file", @"No Lexer Available", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                var tokens = lexer.GetTokens(File.ReadAllText(filename)).ToArray();

                var formatter = new HtmlFormatter(new HtmlFormatterOptions() {Full = true});
                var buffer = new StringWriter();
                formatter.Format(tokens, buffer);

                ShowHtml(buffer.ToString());
                ShowTokens(tokens);
            }
        }
        public void TestLineNumbers()
        {
            var options = new HtmlFormatterOptions()
            {
                LineNumbers = LineNumberStyle.Table
            };
            var input = SampleFile.Load("csharp-sample.txt");
            var tokens = new CSharpLexer()
                .GetTokens(input);

            var subject = new HtmlFormatter(options);
            var output = new StringWriter();
            subject.Format(tokens, output);

            var html = output.ToString();

            Check.That(Regex.IsMatch(html, @"<pre>\s+1\s+2\s+3"))
                .IsTrue();
        }
        public void TestLineAnchors()
        {
            var options = new HtmlFormatterOptions()
            {
                LineAnchors = "foo",
                AnchorLineNumbers = true
            };
            var input = SampleFile.Load("csharp-sample.txt");
            var tokens = new CSharpLexer()
                .GetTokens(input);

            var subject = new HtmlFormatter(options);
            var output = new StringWriter();
            subject.Format(tokens, output);

            var html = output.ToString();

            Check.That(Regex.IsMatch(html, "<a name=\"foo-1\">"))
                .IsTrue();
        }
        public void StyleDefaults_ClassConfiguredForEachTokenType()
        {
            var fmt = new HtmlFormatter();
            var sd = fmt.GetStyleDefaults("");

            _output.WriteLine(sd);

            Check.That(sd).Contains(".c1");
        }
        public void StyleDefaults_SpacedToHighlight()
        {
            var fmt = new HtmlFormatter();
            var sd = fmt.GetStyleDefaults();

            _output.WriteLine(sd);

            Check.That(sd).StartsWith(".highlight");
        }
        public void Full()
        {
            var options = new HtmlFormatterOptions()
            {
                Full = true,
                Title = "My Source Code"
            };
            var input = SampleFile.Load("csharp-sample.txt");
            var tokens = new CSharpLexer()
                .GetTokens(input);

            var subject = new HtmlFormatter(options);
            var output = new StringWriter();
            subject.Format(tokens, output);

            var html = output.ToString();
            File.WriteAllText("output.html", html);

            Check.That(html).Contains("<html>", "<head>", "<title>My Source Code</title>");
        }