Feedback

Konvertiert Code nach Example für XML-Kommentar

Sprache: C#

Dokumentiert ihr euren Code? Klar! Geht es euch auch auf den Zeiger wenn ihr jede Zeile eines BeispielCodes mit /// versehen müßt? Dann ist dieses Snippet für euch! Die Klasse hat nur zwei öffentliche Methoden: ConvertTextToXmlExample und CopyToClipboard. Dem entsprechend einfach ist sie zu handhaben. Hier ist ein Beispiel für das was passiert: [b]Vorher:[/b] [code]using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main() { Console.WriteLine("Hello world"); } } }[/code] [b]Nachher:[/b] [code]/// <example> /// <code> /// <![CDATA[ /// using System; /// using System.Collections.Generic; /// using System.Linq; /// using System.Text; /// /// namespace ConsoleApplication1 /// { /// class Program /// { /// static void Main() /// { /// Console.WriteLine("Hello world"); /// } /// } /// } /// ]]> /// </code> /// </example>[/code] Mit einer kleinen Windows Forms Anwendung heißt es nur noch Copy, Button Click und Paste. Fertig ist die Laube! Hier noch eine Beispielapplikation: [code]using System; using System.Windows.Forms; namespace ConvertCode2HelpExample { public partial class Form1 : Form { public Form1() { InitializeComponent(); FormBorderStyle = FormBorderStyle.Fixed3D; MaximizeBox = false; SizeGripStyle = SizeGripStyle.Hide; } private void Button_Convert_Click(object sender, EventArgs e) { if(!Clipboard.ContainsText()) MessageBox.Show( "Das Clipboard enthält keinen Text.", "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); else { CodeConverter.CopyToClipboard( CodeConverter.ConvertTextToXmlExample( Clipboard.GetText())); } } } }[/code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DotNetExpansions.Forms; // For the fading MessageBox.
// You can get the code at
// http://dotnet-snippets.de/dns/fading-messagebox-ohne-buttons-SID694.aspx
// Or check out a copy of my DotNetExpansions project at // http://cyrons.svn.beanstalkapp.com/general/DotNetExpansions/trunk

namespace ConvertCode2HelpExample
{
   public static class CodeConverter
   {
      // Facade      
      public static string ConvertTextToXmlExample(string text)
      {
         var lines = SplitText2LinesOf(text);
         lines = PrependXmlCommentSignsTo(lines);
         var result = Concatenate(lines);
         result = PrependExampleHeaderTo(result);
         return AppendExampleFooterTo(result);
      }

      private static IEnumerable<string> SplitText2LinesOf(string text)
      {
         char[] splitCharacters = { 'n' };
         if(text == null)
            return null;
         string[] lines = text.Split(splitCharacters);
         var query = lines.Select(value => value.TrimEnd('r'));
         return query;
      }

      public static void CopyToClipboard(string result)
      {
         try
         {
            Clipboard.SetDataObject(result, true);
            IFadingMessageBox fmb = new FadingMessageBox();
            fmb.ShowAndFade("Das Ergebnis wurde im Clipboard gespeichert.",
            "Hinweis", 1.0, FadingMessageBox.FadingMessageBoxIcon.Information);
         }
         catch(Exception)
         {
            if(DialogResult.Retry ==
               MessageBox.Show("Das System ist zur Zeit überlastet." +
                " Der Clip wurde nicht gespeichert. Bitte noch einmal probieren.",
                "FEHLER!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning))
            {
               CopyToClipboard(result);
            }
         }
      }

      private static IEnumerable<string> PrependXmlCommentSignsTo(IEnumerable<string> lines)
      {
         if(lines == null)
            return null;
         if(lines.Count() == 0)
            return null;
         var query = lines.Select(value => "/// " + value);
         return query;
      }

      private static string Concatenate(IEnumerable<string> lines)
      {
         if(lines == null)
            return null;
         if(lines.Count() == 0)
            return null;
         string query = lines.Aggregate((build, line) => build + "rn" + line);
         query += "rn";
         return query;
      }

      private static string PrependExampleHeaderTo(string text)
      {
         const string exampleHeader = "/// <example>rn"
                                      + "/// <code>rn"
                                      + "/// <![CDATA[rn";

         return exampleHeader + text;
      }

      private static string AppendExampleFooterTo(string text)
      {
         const string exampleFooter = "/// ]]>rn"
                                      + "/// </code>rn"
                                      + "/// </example>";
         return text + exampleFooter;
      }
   }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DotNetExpansions.Forms; // For the fading MessageBox.
// You can get the code at
// http://dotnet-snippets.de/dns/fading-messagebox-ohne-buttons-SID694.aspx
// Or check out a copy of my DotNetExpansions project at // http://cyrons.svn.beanstalkapp.com/general/DotNetExpansions/trunk

namespace ConvertCode2HelpExample
{
   public static class CodeConverter
   {
      // Facade      
      public static string ConvertTextToXmlExample(string text)
      {
         var lines = SplitText2LinesOf(text);
         lines = PrependXmlCommentSignsTo(lines);
         var result = Concatenate(lines);
         result = PrependExampleHeaderTo(result);
         return AppendExampleFooterTo(result);
      }

      private static IEnumerable<string> SplitText2LinesOf(string text)
      {
         char[] splitCharacters = { 'n' };
         if(text == null)
            return null;
         string[] lines = text.Split(splitCharacters);
         var query = lines.Select(value => value.TrimEnd('r'));
         return query;
      }

      public static void CopyToClipboard(string result)
      {
         try
         {
            Clipboard.SetDataObject(result, true);
            IFadingMessageBox fmb = new FadingMessageBox();
            fmb.ShowAndFade("Das Ergebnis wurde im Clipboard gespeichert.",
            "Hinweis", 1.0, FadingMessageBox.FadingMessageBoxIcon.Information);
         }
         catch(Exception)
         {
            if(DialogResult.Retry ==
               MessageBox.Show("Das System ist zur Zeit überlastet." +
                " Der Clip wurde nicht gespeichert. Bitte noch einmal probieren.",
                "FEHLER!", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning))
            {
               CopyToClipboard(result);
            }
         }
      }

      private static IEnumerable<string> PrependXmlCommentSignsTo(IEnumerable<string> lines)
      {
         if(lines == null)
            return null;
         if(lines.Count() == 0)
            return null;
         var query = lines.Select(value => "/// " + value);
         return query;
      }

      private static string Concatenate(IEnumerable<string> lines)
      {
         if(lines == null)
            return null;
         if(lines.Count() == 0)
            return null;
         string query = lines.Aggregate((build, line) => build + "rn" + line);
         query += "rn";
         return query;
      }

      private static string PrependExampleHeaderTo(string text)
      {
         const string exampleHeader = "/// <example>rn"
                                      + "/// <code>rn"
                                      + "/// <![CDATA[rn";

         return exampleHeader + text;
      }

      private static string AppendExampleFooterTo(string text)
      {
         const string exampleFooter = "/// ]]>rn"
                                      + "/// </code>rn"
                                      + "/// </example>";
         return text + exampleFooter;
      }
   }
}

1 Kommentar