Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
TDocumentation dotNet - DocumentationCreator.vb

DocumentationCreator.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports System.Text.RegularExpressions
  2.  
  3. Namespace Parsing
  4.     Public Class CreationOption
  5.         Public Enum DocumentFormat
  6.             Html
  7.             Txt
  8.         End Enum
  9.  
  10.         Private _OneSourceOneDoc As Boolean = False
  11.         Private _IncludePrivates As Boolean = False
  12.         Private _ShowUndocumented As Boolean = True
  13.         Private _Format As DocumentFormat = DocumentFormat.Html
  14.  
  15.         Public Property OneSourceOneDoc() As Boolean
  16.             Get
  17.                 Return _OneSourceOneDoc
  18.             End Get
  19.             Set(ByVal Value As Boolean)
  20.                 _OneSourceOneDoc = Value
  21.             End Set
  22.         End Property
  23.  
  24.         Public Property IncludePrivates() As Boolean
  25.             Get
  26.                 Return _IncludePrivates
  27.             End Get
  28.             Set(ByVal Value As Boolean)
  29.                 _IncludePrivates = Value
  30.             End Set
  31.         End Property
  32.  
  33.         Public Property ShowUndocumented() As Boolean
  34.             Get
  35.                 Return _ShowUndocumented
  36.             End Get
  37.             Set(ByVal Value As Boolean)
  38.                 _ShowUndocumented = Value
  39.             End Set
  40.         End Property
  41.  
  42.         Public Property Format() As DocumentFormat
  43.             Get
  44.                 Return _Format
  45.             End Get
  46.             Set(ByVal value As DocumentFormat)
  47.                 _Format = value
  48.             End Set
  49.         End Property
  50.     End Class
  51.  
  52.     Public Class DocumentationCreator
  53.         Private _Options As CreationOption
  54.         Private _DocumentTitle As String
  55.         Private _Sources As List(Of String)
  56.  
  57.         Public ReadOnly Property Options() As CreationOption
  58.             Get
  59.                 Return _Options
  60.             End Get
  61.         End Property
  62.  
  63.         Public ReadOnly Property Sources() As List(Of String)
  64.             Get
  65.                 Return _Sources
  66.             End Get
  67.         End Property
  68.  
  69.         Public Property DocumentTitle() As String
  70.             Get
  71.                 Return _DocumentTitle
  72.             End Get
  73.             Set(ByVal value As String)
  74.                 _DocumentTitle = value
  75.             End Set
  76.         End Property
  77.  
  78.         Sub New()
  79.             _Options = New CreationOption
  80.             _Sources = New List(Of String)
  81.             _DocumentTitle = "Progetto 1"
  82.         End Sub
  83.  
  84.         Private Function AccomplishOptions(ByVal El As Entity) As Boolean
  85.             Dim IsPrivate As Boolean = (El.Declaration.Contains("Private") Or El.Declaration.Contains("Protected"))
  86.             Dim IsUndocumented As Boolean = El.Documentation.Summary.Contains("Documentazione assente")
  87.             Dim AccomplishPrivate As Boolean
  88.             Dim AccomplishUndocumented As Boolean
  89.  
  90.             If (Me.Options.IncludePrivates = False) And IsPrivate Then
  91.                 AccomplishPrivate = False
  92.             Else
  93.                 AccomplishPrivate = True
  94.             End If
  95.  
  96.             If (Me.Options.ShowUndocumented = False) And IsUndocumented Then
  97.                 AccomplishUndocumented = False
  98.             Else
  99.                 AccomplishUndocumented = True
  100.             End If
  101.  
  102.             Return (AccomplishPrivate And AccomplishUndocumented)
  103.         End Function
  104.  
  105.         Private Sub AppendComment(ByVal S As System.Text.StringBuilder, ByVal Value As String, ByVal Type As String, ByVal DisplayedName As String)
  106.             If String.IsNullOrEmpty(Value) Then
  107.                 Exit Sub
  108.             End If
  109.  
  110.             S.AppendLine("<tr>")
  111.             S.AppendFormat("<td class='{0}' width='20%'>{1}</td>{2}", Type, DisplayedName, vbCrLf)
  112.             S.AppendFormat("<td class='{0}'>{1}</td>{2}", Type, Value, vbCrLf)
  113.             S.AppendLine("</tr>")
  114.         End Sub
  115.  
  116.         Private Sub AppendParams(ByVal S As System.Text.StringBuilder, ByVal Value As Comments.ParamCommentList, ByVal Type As String, ByVal DisplayedName As String)
  117.             If Value Is Nothing OrElse Value.Count = 0 Then
  118.                 Exit Sub
  119.             End If
  120.  
  121.             S.AppendLine("<tr>")
  122.             S.AppendFormat("<td class='{0}' width='20%'>{1}</td>{2}", Type, DisplayedName, vbCrLf)
  123.             S.AppendFormat("<td class='{0}'>", Type)
  124.  
  125.             For Each Param As Comments.ParamComment In Value
  126.                 S.AppendFormat("<b>{0}</b><br>{1}<br>{2}{2}", Param.ParameterName, Param.ParameterDescription, vbCrLf)
  127.             Next
  128.  
  129.             S.AppendLine("</td>")
  130.             S.AppendLine("</tr>")
  131.         End Sub
  132.  
  133.         Private Function ReplaceSpecialChar(ByVal S As String) As String
  134.             S = S.Replace("à", "à")
  135.             S = S.Replace("è", "è")
  136.             S = S.Replace("é", "é")
  137.             S = S.Replace("ì", "ì")
  138.             S = S.Replace("ò", "ò")
  139.             S = S.Replace("ù", "ù")
  140.             Return S
  141.         End Function
  142.  
  143.         Private Sub AppendMemberList(ByVal S As System.Text.StringBuilder, ByVal El As Entity, ByVal Links As List(Of String))
  144.             Dim Text As String = El.Name & " <font style='font-size:8pt;'>(" & CodeParser.GetNameFromType(El.Type) & ")</font>"
  145.  
  146.             Do While Links.Contains(El.Id)
  147.                 El.Id &= "D"
  148.             Loop
  149.  
  150.             Links.Add(El.Id)
  151.  
  152.             If El.IsContainer Then
  153.                 S.AppendFormat("<li> <a href='#{0}'>{1}</a> </li>{2}", El.Id, Text, vbCrLf)
  154.                 S.AppendLine("<ul>")
  155.                 For Each Child As Entity In El.Children
  156.                     If AccomplishOptions(Child) Then
  157.                         AppendMemberList(S, Child, Links)
  158.                     End If
  159.                 Next
  160.                 S.AppendLine("</ul>")
  161.             Else
  162.                 S.AppendFormat("<li> <a href='#{0}'>{1}</a> </li>{2}", El.Id, Text, vbCrLf)
  163.             End If
  164.         End Sub
  165.  
  166.         Private Sub AppendMemberDocumentation(ByVal S As System.Text.StringBuilder, ByVal El As Entity)
  167.             Dim Doc As Comments.DocumentationComment = El.Documentation
  168.             Dim Header As String
  169.  
  170.             If El.IsContainer Then
  171.                 Header = "class"
  172.                 S.AppendLine("<div class='container'>")
  173.             Else
  174.                 Header = "member"
  175.             End If
  176.  
  177.             S.AppendFormat("<div class='{0}'><a name='{1}'>{2}</a></div>{3}", Header, El.Id, El.Name & " <font style='font-size:11pt;'>(" & CodeParser.GetNameFromType(El.Type) & ")</font>", vbCrLf)
  178.             S.AppendLine("<table border='0' width='99%' align='center'>")
  179.             AppendComment(S, El.Declaration, "signature", "Signature")
  180.             AppendComment(S, Doc.Summary, "summary", "Descrizione")
  181.             AppendComment(S, Doc.Remarks, "remarks", "Dettagli")
  182.             AppendComment(S, Doc.See, "see", "Vedi anche")
  183.             AppendComment(S, Doc.SeeAlso, "see", "Vedi anche")
  184.  
  185.             If El.GetCommentType() <> EntityCommentType.Generic Then
  186.                 Dim DetailedDoc As Comments.MethodComment = CType(Doc, Comments.MethodComment)
  187.                 AppendParams(S, DetailedDoc.Params, "params", "Parametri")
  188.                 AppendParams(S, DetailedDoc.TypeParams, "typeparams", "Parametri Generics")
  189.                 AppendParams(S, DetailedDoc.Exceptions, "exception", "Eccezioni")
  190.                 AppendComment(S, DetailedDoc.Returns, "returns", "Valore restituito")
  191.             End If
  192.             S.AppendLine("</table>")
  193.             S.AppendLine("<br><br>")
  194.  
  195.             If El.IsContainer Then
  196.                 For Each Child As Entity In El.Children
  197.                     If AccomplishOptions(Child) Then
  198.                         AppendMemberDocumentation(S, Child)
  199.                     End If
  200.                 Next
  201.                 S.AppendLine("</div>")
  202.                 S.AppendLine("<br>")
  203.             End If
  204.         End Sub
  205.  
  206.         Private Sub CreateHtml(ByVal Result As ParserResult, ByVal FileName As String, ByVal SourceName As String, Optional ByVal IsFirstFile As Boolean = True)
  207.             Dim Builder As New System.Text.StringBuilder()
  208.             Dim Links As New List(Of String)
  209.  
  210.             If IsFirstFile Then
  211.                 Builder.Append(My.Resources.ReportTemplate.Replace("|||Doc|||", Me.DocumentTitle))
  212.             End If
  213.  
  214.             With Builder
  215.                 If IsFirstFile Then
  216.                     .AppendLine("<div class='doctitle'>Documentazione " & Me.DocumentTitle & "</div>")
  217.                 End If
  218.                 .AppendLine("<div class='filename'>" & SourceName & "</div>")
  219.                 .AppendLine("<div class='subtitle'>Generata con TDocumentation dotNet v1.0</div><br>")
  220.                 .AppendLine("Sintesi del progetto:")
  221.                 .AppendLine("<ul>")
  222.                 For Each El As Entity In Result
  223.                     If AccomplishOptions(El) Then
  224.                         AppendMemberList(Builder, El, Links)
  225.                     End If
  226.                 Next
  227.                 .AppendLine("</ul>")
  228.                 Links.Clear()
  229.  
  230.                 For Each El As Entity In Result
  231.                     If AccomplishOptions(El) Then
  232.                         AppendMemberDocumentation(Builder, El)
  233.                     End If
  234.                 Next
  235.  
  236.                 .AppendLine("</body>")
  237.                 .AppendLine("</html>")
  238.             End With
  239.  
  240.             IO.File.WriteAllText(FileName, ReplaceSpecialChar(Builder.ToString))
  241.         End Sub
  242.  
  243.         Private Sub CreateHtml(ByVal Folder As String)
  244.             Dim Parser As New CodeParser()
  245.             Dim Result As ParserResult
  246.             Dim CreatedFiles As New List(Of String)
  247.             Dim PutHeader As Boolean = True
  248.  
  249.             For Each Source As String In Me.Sources
  250.                 Dim File As String = Folder & "\Documentazione " & Me.DocumentTitle & " (" & IO.Path.GetFileName(Source) & ").html"
  251.                 Parser.Load(Source)
  252.                 Result = Parser.ParseCode
  253.                 Me.CreateHtml(Result, File, IO.Path.GetFileName(Source), PutHeader)
  254.                 CreatedFiles.Add(File)
  255.                 If Me.Options.OneSourceOneDoc Then
  256.                     PutHeader = False
  257.                 End If
  258.             Next
  259.  
  260.             If Me.Options.OneSourceOneDoc Then
  261.                 Dim FinalDocumentText As New System.Text.StringBuilder
  262.                 For Each File As String In CreatedFiles
  263.                     FinalDocumentText.AppendLine(IO.File.ReadAllText(File))
  264.                 Next
  265.                 IO.File.WriteAllText(Folder & "\Documentazione  " & Me.DocumentTitle & ".html", FinalDocumentText.ToString)
  266.             End If
  267.         End Sub
  268.  
  269.         Public Sub Create(ByVal Folder As String)
  270.             If Me.Options.Format = CreationOption.DocumentFormat.Html Then
  271.                 Me.CreateHtml(Folder)
  272.             End If
  273.         End Sub
  274.     End Class
  275. End Namespace