Ejemplo n.º 1
0
        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);
        }
Ejemplo 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);
            }
        }
Ejemplo n.º 3
0
        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();
        }
Ejemplo n.º 4
0
        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();
        }
Ejemplo n.º 5
0
        public void StyleDefaults_ClassConfiguredForEachTokenType()
        {
            var fmt = new HtmlFormatter();
            var sd = fmt.GetStyleDefaults("");

            _output.WriteLine(sd);

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

            _output.WriteLine(sd);

            Check.That(sd).StartsWith(".highlight");
        }
Ejemplo n.º 7
0
        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>");
        }