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
Code Converter - CodeFormatting.vb

CodeFormatting.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports System.Text.RegularExpressions
  2. Imports System.Text
  3. Imports TallComponents.PDF.Layout
  4. Imports System.Web
  5. Namespace CodeFormatting
  6.     Public Structure ColoredFont
  7.         Dim Font As Font
  8.         Dim Color As Color
  9.     End Structure
  10.  
  11.     Public Structure KeywordRegex
  12.         Dim Reg As Regex
  13.         Dim IsKeyword As Boolean
  14.         Sub New(ByVal Reg As Regex, ByVal IsKeyword As Boolean)
  15.             Me.Reg = Reg
  16.             Me.IsKeyword = IsKeyword
  17.         End Sub
  18.     End Structure
  19.  
  20.     Public MustInherit Class BaseConverter
  21.         Private _FileName, _Source As String
  22.         Private _Expressions As New List(Of Regex)
  23.         Private _CommentExpression As Regex
  24.         Private _Code, _Comment, _Keyword As ColoredFont
  25.  
  26.         Public Property FileName() As String
  27.             Get
  28.                 Return _FileName
  29.             End Get
  30.             Set(ByVal Value As String)
  31.                 If Value <> "" Then
  32.                     _FileName = Value
  33.                 End If
  34.             End Set
  35.         End Property
  36.  
  37.         Public Property Code() As ColoredFont
  38.             Get
  39.                 Return _Code
  40.             End Get
  41.             Set(ByVal Value As ColoredFont)
  42.                 _Code = Value
  43.             End Set
  44.         End Property
  45.  
  46.         Public Property Comment() As ColoredFont
  47.             Get
  48.                 Return _Comment
  49.             End Get
  50.             Set(ByVal Value As ColoredFont)
  51.                 _Comment = Value
  52.             End Set
  53.         End Property
  54.  
  55.         Public Property Keyword() As ColoredFont
  56.             Get
  57.                 Return _Keyword
  58.             End Get
  59.             Set(ByVal Value As ColoredFont)
  60.                 _Keyword = Value
  61.             End Set
  62.         End Property
  63.  
  64.         Public Property Source() As String
  65.             Get
  66.                 Return _Source
  67.             End Get
  68.             Set(ByVal Value As String)
  69.                 If Value <> "" Then
  70.                     _Source = Value
  71.                 End If
  72.             End Set
  73.         End Property
  74.  
  75.         Public Property CommentExpression() As Regex
  76.             Get
  77.                 Return _CommentExpression
  78.             End Get
  79.             Set(ByVal Value As Regex)
  80.                 _CommentExpression = Value
  81.             End Set
  82.         End Property
  83.  
  84.         Public ReadOnly Property Expressions() As List(Of Regex)
  85.             Get
  86.                 Return _Expressions
  87.             End Get
  88.         End Property
  89.  
  90.         Sub New(ByVal Code As ColoredFont, ByVal Comment As ColoredFont, ByVal Keyword As ColoredFont)
  91.             Me.Code = Code
  92.             Me.Comment = Comment
  93.             Me.Keyword = Keyword
  94.         End Sub
  95.  
  96.         Protected Function ToHexColor(ByVal C As Color) As String
  97.             Return Hex(C.R).PadLeft(2, "0") & Hex(C.G).PadLeft(2, "0") & Hex(C.B).PadLeft(2, "0")
  98.         End Function
  99.  
  100.         Public Sub LoadCCL(ByVal CCLFile As String)
  101.             Dim CCL As New IO.StreamReader(CCLFile)
  102.             Dim Line As String
  103.             Dim Temp As Regex
  104.  
  105.             Line = CCL.ReadLine
  106.             Me.CommentExpression = New Regex(Line)
  107.             While Not CCL.EndOfStream
  108.                 Line = CCL.ReadLine
  109.                 If Line.Contains("rem") Then
  110.                     Temp = New Regex(Line)
  111.                 Else
  112.                     Temp = New Regex(Line)
  113.                 End If
  114.                 Me.Expressions.Add(Temp)
  115.             End While
  116.  
  117.             CCL.Close()
  118.         End Sub
  119.  
  120.         Public MustOverride Sub Create(ByVal FileName As String)
  121.         Public MustOverride Sub Write()
  122.         Public MustOverride Sub Close()
  123.         Public MustOverride Sub Convert(ByVal ProgBar As ProgressBar)
  124.     End Class
  125.  
  126.     Public Class HtmlConverter
  127.         Inherits BaseConverter
  128.  
  129.         Private Writer As IO.StreamWriter
  130.         Private IsOpen As Boolean = False
  131.  
  132.         Sub New(ByVal Code As ColoredFont, ByVal Comment As ColoredFont, ByVal Keyword As ColoredFont)
  133.             MyBase.New(Code, Comment, Keyword)
  134.         End Sub
  135.  
  136.         Private Function GetTag(ByVal SelFont As ColoredFont, ByVal BaseTag As String) As String
  137.             Dim Tag As String = BaseTag
  138.  
  139.             With SelFont
  140.                 Tag = "<font face='" & .Font.Name & "' color='#" & ToHexColor(.Color) & "'>" & Tag & "</font>"
  141.                 If .Font.Bold Then
  142.                     Tag = "<b>" & Tag & "</b>"
  143.                 End If
  144.                 If .Font.Italic Then
  145.                     Tag = "<i>" & Tag & "</i>"
  146.                 End If
  147.             End With
  148.  
  149.             Return Tag
  150.         End Function
  151.  
  152.         Public Overrides Sub Close()
  153.             If Not IsOpen Then
  154.                 Throw New IO.IOException
  155.             End If
  156.  
  157.             With Writer
  158.                 .WriteLine("</font>")
  159.                 .WriteLine("</body>")
  160.                 .WriteLine("</html>")
  161.                 .Close()
  162.             End With
  163.             IsOpen = False
  164.         End Sub
  165.  
  166.         Public Overrides Sub Convert(ByVal ProgBar As ProgressBar)
  167.             If Me.Source Is Nothing Then
  168.                 Throw New NullReferenceException
  169.             End If
  170.  
  171.             Dim Index As Int32 = 0
  172.             Dim Lines() As String = Me.Source.Split(vbCrLf)
  173.             Dim Temp As New StringBuilder
  174.  
  175.             For Each Line As String In Lines
  176.                 ProgBar.Value = Index * 100 / Lines.Length
  177.                 Application.DoEvents()
  178.  
  179.                 If Me.CommentExpression.IsMatch(Line) Then
  180.                     Line = Me.CommentExpression.Replace(Line, "${sp1}" & GetTag(Me.Comment, "${initrem}${rem}"), RegexOptions.IgnoreCase Or RegexOptions.Multiline)
  181.                 Else
  182.                     For Each Reg As Regex In Me.Expressions
  183.                         Line = Reg.Replace(Line, "${sp1}" & GetTag(Me.Keyword, "${key}") & "${sp2}", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
  184.                     Next
  185.                 End If
  186.  
  187.                 Temp.AppendLine(Line)
  188.                 Index += 1
  189.             Next
  190.  
  191.             Me.Source = Temp.ToString
  192.         End Sub
  193.  
  194.         Public Overrides Sub Create(ByVal FileName As String)
  195.             Writer = New IO.StreamWriter(FileName)
  196.             IsOpen = True
  197.             Me.FileName = FileName
  198.  
  199.             With Writer
  200.                 .WriteLine("<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"">")
  201.                 .WriteLine("<html>" & vbCrLf)
  202.                 .WriteLine("<head>")
  203.                 .WriteLine("  <title>Codice sorgente generato da Code Converter (Written by Totem)")
  204.                 .WriteLine("</head>" & vbCrLf)
  205.                 .WriteLine("<body>")
  206.                 .WriteLine("<font face='" & Me.Code.Font.Name & "' size='" & Me.Code.Font.SizeInPoints / 3 & "' color='#" & _
  207.                 ToHexColor(Me.Code.Color) & "'>")
  208.             End With
  209.  
  210.             Me.Source = Me.Source.Replace("<", "<")
  211.             Me.Source = Me.Source.Replace(">", ">")
  212.         End Sub
  213.  
  214.         Public Overrides Sub Write()
  215.             If Not IsOpen Then
  216.                 Throw New IO.IOException
  217.             End If
  218.  
  219.             Dim Space As New Regex("\t", RegexOptions.Multiline)
  220.  
  221.             Me.Source = Me.Source.Replace(vbCrLf, "<br>" & vbCrLf)
  222.             Me.Source = Space.Replace(Me.Source, "    ")
  223.             Me.Source = Me.Source.Replace("  ", "  ")
  224.             Me.Source = Me.Source.Replace("  ", "  ")
  225.             Me.Source = Me.Source.Replace("à", "à")
  226.             Me.Source = Me.Source.Replace("è", "è")
  227.             Me.Source = Me.Source.Replace("ì", "ì")
  228.             Me.Source = Me.Source.Replace("ò", "ò")
  229.             Me.Source = Me.Source.Replace("ù", "ù")
  230.  
  231.             Writer.Write(Me.Source)
  232.         End Sub
  233.     End Class
  234.  
  235.     Public Class HtmlCssConverter
  236.         Inherits BaseConverter
  237.  
  238.         Private Writer As IO.StreamWriter
  239.         Private IsOpen As Boolean = False
  240.         Private _CssFileName As String
  241.  
  242.         Public Property CssFileName() As String
  243.             Get
  244.                 Return _CssFileName
  245.             End Get
  246.             Set(ByVal Value As String)
  247.                 If Value <> "" Then
  248.                     _CssFileName = Value
  249.                 End If
  250.             End Set
  251.         End Property
  252.  
  253.         Sub New(ByVal Code As ColoredFont, ByVal Comment As ColoredFont, ByVal Keyword As ColoredFont)
  254.             MyBase.New(Code, Comment, Keyword)
  255.         End Sub
  256.  
  257.         Private Function GetCssDescription(ByVal Font As ColoredFont) As String
  258.             Dim Description As New System.Text.StringBuilder
  259.  
  260.             If Font.Font.Bold Then
  261.                 Description.Append("  font-weight : bold;")
  262.             Else
  263.                 Description.Append("  font-weight : normal;")
  264.             End If
  265.             Description.AppendLine()
  266.             If Font.Font.Italic Then
  267.                 Description.Append("  font-style : italic;")
  268.             Else
  269.                 Description.Append("  font-style : normal;")
  270.             End If
  271.             Description.AppendLine()
  272.             Description.AppendFormat("  font-family : {0};{1}", Font.Font.Name, vbCrLf)
  273.             Description.AppendFormat("  font-size : {0}pt;{1}", Font.Font.SizeInPoints, vbCrLf)
  274.             Description.AppendFormat("  color : #{0};{1}", ToHexColor(Font.Color), vbCrLf)
  275.            
  276.             Return Description.ToString
  277.         End Function
  278.  
  279.         Public Overrides Sub Close()
  280.             If Not IsOpen Then
  281.                 Throw New IO.IOException
  282.             End If
  283.  
  284.             With Writer
  285.                 .WriteLine("</div>")
  286.                 .WriteLine("</body>")
  287.                 .WriteLine("</html>")
  288.                 .Close()
  289.             End With
  290.             IsOpen = False
  291.         End Sub
  292.  
  293.         Public Overrides Sub Convert(ByVal ProgBar As ProgressBar)
  294.             If Me.Source Is Nothing Then
  295.                 Throw New NullReferenceException
  296.             End If
  297.  
  298.             Dim Index As Int32 = 0
  299.             Dim Lines() As String = Me.Source.Split(vbCrLf)
  300.             Dim Temp As New StringBuilder
  301.  
  302.             For Each Line As String In Lines
  303.                 ProgBar.Value = Index * 100 / Lines.Length
  304.                 Application.DoEvents()
  305.  
  306.                 If Me.CommentExpression.IsMatch(Line) Then
  307.                     Line = Me.CommentExpression.Replace(Line, "${sp1}<c>${initrem}${rem}</c>", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
  308.                 Else
  309.                     For Each Reg As Regex In Me.Expressions
  310.                         Line = Reg.Replace(Line, "${sp1}<k>${key}</k>${sp2}", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
  311.                     Next
  312.                 End If
  313.  
  314.                 Temp.Append(Line)
  315.                 Index += 1
  316.             Next
  317.  
  318.             Me.Source = Temp.ToString
  319.         End Sub
  320.  
  321.         Public Overrides Sub Create(ByVal FileName As String)
  322.             Dim CssWriter As IO.StreamWriter
  323.  
  324.             IsOpen = True
  325.             Me.FileName = FileName
  326.             Me.CssFileName = IO.Path.GetFileNameWithoutExtension(FileName) & ".css"
  327.             Writer = New IO.StreamWriter(Me.FileName)
  328.             CssWriter = New IO.StreamWriter(IO.Path.GetDirectoryName(FileName) & "\" & Me.CssFileName)
  329.  
  330.             With CssWriter
  331.                 .WriteLine(".code")
  332.                 .WriteLine("{")
  333.                 .WriteLine(GetCssDescription(Me.Code))
  334.                 .WriteLine("  white-space : pre;")
  335.                 .WriteLine("}")
  336.                 .WriteLine()
  337.                 .WriteLine("k")
  338.                 .WriteLine("{")
  339.                 .WriteLine(GetCssDescription(Me.Keyword))
  340.                 .WriteLine("}")
  341.                 .WriteLine()
  342.                 .WriteLine("c")
  343.                 .WriteLine("{")
  344.                 .WriteLine(GetCssDescription(Me.Comment))
  345.                 .WriteLine("}")
  346.                 .Close()
  347.             End With
  348.  
  349.             With Writer
  350.                 .WriteLine("<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"">")
  351.                 .WriteLine("<html>" & vbCrLf)
  352.                 .WriteLine("<head>")
  353.                 .WriteLine("  <title>Codice sorgente generato da Code Converter (Written by Totem)")
  354.                 .WriteLine("  <link rel='stylesheet' type='text/css' href='" & Me.CssFileName & "'>")
  355.                 .WriteLine("</head>" & vbCrLf)
  356.                 .WriteLine("<body>")
  357.                 .WriteLine("<div class='code'>")
  358.             End With
  359.  
  360.             Me.Source = Me.Source.Replace("<", "<")
  361.             Me.Source = Me.Source.Replace(">", ">")
  362.         End Sub
  363.  
  364.         Public Overrides Sub Write()
  365.             If Not IsOpen Then
  366.                 Throw New IO.IOException
  367.             End If
  368.  
  369.             Me.Source = Me.Source.Replace("à", "à")
  370.             Me.Source = Me.Source.Replace("è", "è")
  371.             Me.Source = Me.Source.Replace("ì", "ì")
  372.             Me.Source = Me.Source.Replace("ò", "ò")
  373.             Me.Source = Me.Source.Replace("ù", "ù")
  374.  
  375.             Writer.Write(Me.Source)
  376.         End Sub
  377.     End Class
  378.  
  379.     Public Class RtfConverter
  380.         Inherits BaseConverter
  381.  
  382.         Private Writer As IO.StreamWriter
  383.         Private IsOpen As Boolean = False
  384.         Private _OpenWithMSWord As Boolean = True
  385.  
  386.         Sub New(ByVal Code As ColoredFont, ByVal Comment As ColoredFont, ByVal Keyword As ColoredFont)
  387.             MyBase.New(Code, Comment, Keyword)
  388.         End Sub
  389.  
  390.         Public Property OpenWithMSWord() As Boolean
  391.             Get
  392.                 Return _OpenWithMSWord
  393.             End Get
  394.             Set(ByVal Value As Boolean)
  395.                 _OpenWithMSWord = Value
  396.             End Set
  397.         End Property
  398.  
  399.         Private Function GetTagInfo(ByVal StartTag As String, ByVal ZeroBasedIndex As Int32) As String
  400.             Dim Index As Int32 = ZeroBasedIndex
  401.  
  402.             If Me.OpenWithMSWord Then
  403.                 Index += 1
  404.             End If
  405.  
  406.             Return StartTag & Index
  407.         End Function
  408.  
  409.         Private Function GetTag(ByVal IsKeyword As Boolean, ByVal BaseTag As String) As String
  410.             Dim Tag As String = BaseTag
  411.  
  412.             If IsKeyword Then
  413.                 Tag = "{\f2 " & Tag & "}"
  414.                 Tag = GetTagInfo("{\cf", 1) & Tag & "}"
  415.                 Tag = "{\fs" & Me.Keyword.Font.SizeInPoints * 2 & Tag & "}"
  416.                 If Me.Keyword.Font.Bold Then
  417.                     Tag = "{\b " & Tag & "}"
  418.                 End If
  419.                 If Me.Keyword.Font.Bold Then
  420.                     Tag = "{\i " & Tag & "}"
  421.                 End If
  422.             Else
  423.                 Tag = "{\f3 " & Tag & "}"
  424.                 Tag = GetTagInfo("{\cf", 2) & Tag & "}"
  425.                 Tag = "{\fs" & Me.Comment.Font.SizeInPoints * 2 & Tag & "}"
  426.                 If Me.Comment.Font.Bold Then
  427.                     Tag = "{\b " & Tag & "}"
  428.                 End If
  429.                 If Me.Comment.Font.Bold Then
  430.                     Tag = "{\i " & Tag & "}"
  431.                 End If
  432.             End If
  433.  
  434.             Return Tag
  435.         End Function
  436.  
  437.         Public Overrides Sub Close()
  438.             If Not IsOpen Then
  439.                 Throw New IO.IOException
  440.             End If
  441.  
  442.             Writer.Write("}}}}")
  443.             Writer.Close()
  444.         End Sub
  445.  
  446.         Public Overrides Sub Convert(ByVal ProgBar As System.Windows.Forms.ProgressBar)
  447.             If Me.Source Is Nothing Then
  448.                 Throw New NullReferenceException
  449.             End If
  450.  
  451.             Dim Index As Int32 = 0
  452.             Dim Lines() As String = Me.Source.Split(vbCrLf)
  453.             Dim Temp As New StringBuilder
  454.  
  455.             For Each Line As String In Lines
  456.                 ProgBar.Value = Index * 100 / Lines.Length
  457.                 Application.DoEvents()
  458.  
  459.                 If Me.CommentExpression.IsMatch(Line) Then
  460.                     Line = Me.CommentExpression.Replace(Line, "${sp1}" & GetTag(False, "${initrem}${rem}"), RegexOptions.IgnoreCase Or RegexOptions.Multiline)
  461.                 Else
  462.                     For Each Reg As Regex In Me.Expressions
  463.                         Line = Reg.Replace(Line, "${sp1}" & GetTag(True, "${key}") & "${sp2}", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
  464.                     Next
  465.                 End If
  466.  
  467.                 Temp.Append(Line)
  468.                 Index += 1
  469.             Next
  470.  
  471.             Me.Source = Temp.ToString
  472.         End Sub
  473.  
  474.         Public Overrides Sub Create(ByVal FileName As String)
  475.             IsOpen = True
  476.             Me.FileName = FileName
  477.             Writer = New IO.StreamWriter(Me.FileName)
  478.  
  479.             With Writer
  480.                 .WriteLine("{\rtf1 \ansi \ansicpg1252 \deff0 \deflang1040")
  481.  
  482.                 .WriteLine("{\fonttbl")
  483.                 .WriteLine("  {\f1 \fnil \fcharset0 " & Me.Code.Font.Name & ";}")
  484.                 .WriteLine("  {\f2 \fnil \fcharset0 " & Me.Keyword.Font.Name & ";}")
  485.                 .WriteLine("  {\f3 \fnil \fcharset0 " & Me.Comment.Font.Name & ";}")
  486.                 .WriteLine("}")
  487.  
  488.                 .Write("{\colortbl ")
  489.                 .Write("\red{0}\green{1}\blue{2}; ", Me.Code.Color.R, Me.Code.Color.G, Me.Code.Color.B)
  490.                 .Write("\red{0}\green{1}\blue{2}; ", Me.Keyword.Color.R, Me.Keyword.Color.G, Me.Keyword.Color.B)
  491.                 .Write("\red{0}\green{1}\blue{2};", Me.Comment.Color.R, Me.Comment.Color.G, Me.Comment.Color.B)
  492.                 .WriteLine("}")
  493.                 .WriteLine()
  494.  
  495.                 .WriteLine("{\f1 {\fs" & Me.Code.Font.SizeInPoints * 2 & " " & GetTagInfo("{\cf", 0) & " ")
  496.             End With
  497.         End Sub
  498.  
  499.         Public Overrides Sub Write()
  500.             If Not IsOpen Then
  501.                 Throw New IO.IOException
  502.             End If
  503.  
  504.             Me.Source = Me.Source.Replace(Chr(10), "\line" & vbCrLf)
  505.  
  506.             Writer.Write(Me.Source)
  507.         End Sub
  508.     End Class
  509.  
  510.     Public Class PdfConverter
  511.         Inherits BaseConverter
  512.  
  513.         Private Writer As Document
  514.         Private MainParagraph As Paragraphs.TextParagraph
  515.  
  516.         Sub New(ByVal Code As ColoredFont, ByVal Comment As ColoredFont, ByVal Keyword As ColoredFont)
  517.             MyBase.New(Code, Comment, Keyword)
  518.         End Sub
  519.  
  520.         Public Overrides Sub Close()
  521.  
  522.         End Sub
  523.  
  524.         Public Overrides Sub Convert(ByVal ProgBar As System.Windows.Forms.ProgressBar)
  525.             If Me.Source Is Nothing Then
  526.                 Throw New NullReferenceException
  527.             End If
  528.         End Sub
  529.  
  530.         Public Overrides Sub Create(ByVal FileName As String)
  531.            
  532.         End Sub
  533.  
  534.         Public Overrides Sub Write()
  535.            
  536.         End Sub
  537.     End Class
  538. End Namespace