Imports System.Text.RegularExpressions
Imports System.Text
Imports TallComponents.PDF.Layout
Imports System.Web
Namespace CodeFormatting
Public Structure ColoredFont
Dim Font As Font
Dim Color As Color
End Structure
Public Structure KeywordRegex
Dim Reg As Regex
Dim IsKeyword As Boolean
Sub New(ByVal Reg As Regex, ByVal IsKeyword As Boolean)
Me.Reg = Reg
Me.IsKeyword = IsKeyword
End Sub
End Structure
Public MustInherit Class BaseConverter
Private _FileName, _Source As String
Private _Expressions As New List(Of Regex)
Private _CommentExpression As Regex
Private _Code, _Comment, _Keyword As ColoredFont
Public Property FileName() As String
Get
Return _FileName
End Get
Set(ByVal Value As String)
If Value <> "" Then
_FileName = Value
End If
End Set
End Property
Public Property Code() As ColoredFont
Get
Return _Code
End Get
Set(ByVal Value As ColoredFont)
_Code = Value
End Set
End Property
Public Property Comment() As ColoredFont
Get
Return _Comment
End Get
Set(ByVal Value As ColoredFont)
_Comment = Value
End Set
End Property
Public Property Keyword() As ColoredFont
Get
Return _Keyword
End Get
Set(ByVal Value As ColoredFont)
_Keyword = Value
End Set
End Property
Public Property Source() As String
Get
Return _Source
End Get
Set(ByVal Value As String)
If Value <> "" Then
_Source = Value
End If
End Set
End Property
Public Property CommentExpression() As Regex
Get
Return _CommentExpression
End Get
Set(ByVal Value As Regex)
_CommentExpression = Value
End Set
End Property
Public ReadOnly Property Expressions() As List(Of Regex)
Get
Return _Expressions
End Get
End Property
Sub New(ByVal Code As ColoredFont, ByVal Comment As ColoredFont, ByVal Keyword As ColoredFont)
Me.Code = Code
Me.Comment = Comment
Me.Keyword = Keyword
End Sub
Protected Function ToHexColor(ByVal C As Color) As String
Return Hex(C.R).PadLeft(2, "0") & Hex(C.G).PadLeft(2, "0") & Hex(C.B).PadLeft(2, "0")
End Function
Public Sub LoadCCL(ByVal CCLFile As String)
Dim CCL As New IO.StreamReader(CCLFile)
Dim Line As String
Dim Temp As Regex
Line = CCL.ReadLine
Me.CommentExpression = New Regex(Line)
While Not CCL.EndOfStream
Line = CCL.ReadLine
If Line.Contains("rem") Then
Temp = New Regex(Line)
Else
Temp = New Regex(Line)
End If
Me.Expressions.Add(Temp)
End While
CCL.Close()
End Sub
Public MustOverride Sub Create(ByVal FileName As String)
Public MustOverride Sub Write()
Public MustOverride Sub Close()
Public MustOverride Sub Convert(ByVal ProgBar As ProgressBar)
End Class
Public Class HtmlConverter
Inherits BaseConverter
Private Writer As IO.StreamWriter
Private IsOpen As Boolean = False
Sub New(ByVal Code As ColoredFont, ByVal Comment As ColoredFont, ByVal Keyword As ColoredFont)
MyBase.New(Code, Comment, Keyword)
End Sub
Private Function GetTag(ByVal SelFont As ColoredFont, ByVal BaseTag As String) As String
Dim Tag As String = BaseTag
With SelFont
Tag = "<font face='" & .Font.Name & "' color='#" & ToHexColor(.Color) & "'>" & Tag & "</font>"
If .Font.Bold Then
Tag = "<b>" & Tag & "</b>"
End If
If .Font.Italic Then
Tag = "<i>" & Tag & "</i>"
End If
End With
Return Tag
End Function
Public Overrides Sub Close()
If Not IsOpen Then
Throw New IO.IOException
End If
With Writer
.WriteLine("</font>")
.WriteLine("</body>")
.WriteLine("</html>")
.Close()
End With
IsOpen = False
End Sub
Public Overrides Sub Convert(ByVal ProgBar As ProgressBar)
If Me.Source Is Nothing Then
Throw New NullReferenceException
End If
Dim Index As Int32 = 0
Dim Lines() As String = Me.Source.Split(vbCrLf)
Dim Temp As New StringBuilder
For Each Line As String In Lines
ProgBar.Value = Index * 100 / Lines.Length
Application.DoEvents()
If Me.CommentExpression.IsMatch(Line) Then
Line = Me.CommentExpression.Replace(Line, "${sp1}" & GetTag(Me.Comment, "${initrem}${rem}"), RegexOptions.IgnoreCase Or RegexOptions.Multiline)
Else
For Each Reg As Regex In Me.Expressions
Line = Reg.Replace(Line, "${sp1}" & GetTag(Me.Keyword, "${key}") & "${sp2}", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
Next
End If
Temp.AppendLine(Line)
Index += 1
Next
Me.Source = Temp.ToString
End Sub
Public Overrides Sub Create(ByVal FileName As String)
Writer = New IO.StreamWriter(FileName)
IsOpen = True
Me.FileName = FileName
With Writer
.WriteLine("<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"">")
.WriteLine("<html>" & vbCrLf)
.WriteLine("<head>")
.WriteLine(" <title>Codice sorgente generato da Code Converter (Written by Totem)")
.WriteLine("</head>" & vbCrLf)
.WriteLine("<body>")
.WriteLine("<font face='" & Me.Code.Font.Name & "' size='" & Me.Code.Font.SizeInPoints / 3 & "' color='#" & _
ToHexColor(Me.Code.Color) & "'>")
End With
Me.Source = Me.Source.Replace("<", "<")
Me.Source = Me.Source.Replace(">", ">")
End Sub
Public Overrides Sub Write()
If Not IsOpen Then
Throw New IO.IOException
End If
Dim Space As New Regex("\t", RegexOptions.Multiline)
Me.Source = Me.Source.Replace(vbCrLf, "<br>" & vbCrLf)
Me.Source = Space.Replace(Me.Source, " ")
Me.Source = Me.Source.Replace(" ", " ")
Me.Source = Me.Source.Replace(" ", " ")
Me.Source = Me.Source.Replace("à", "à")
Me.Source = Me.Source.Replace("è", "è")
Me.Source = Me.Source.Replace("ì", "ì")
Me.Source = Me.Source.Replace("ò", "ò")
Me.Source = Me.Source.Replace("ù", "ù")
Writer.Write(Me.Source)
End Sub
End Class
Public Class HtmlCssConverter
Inherits BaseConverter
Private Writer As IO.StreamWriter
Private IsOpen As Boolean = False
Private _CssFileName As String
Public Property CssFileName() As String
Get
Return _CssFileName
End Get
Set(ByVal Value As String)
If Value <> "" Then
_CssFileName = Value
End If
End Set
End Property
Sub New(ByVal Code As ColoredFont, ByVal Comment As ColoredFont, ByVal Keyword As ColoredFont)
MyBase.New(Code, Comment, Keyword)
End Sub
Private Function GetCssDescription(ByVal Font As ColoredFont) As String
Dim Description As New System.Text.StringBuilder
If Font.Font.Bold Then
Description.Append(" font-weight : bold;")
Else
Description.Append(" font-weight : normal;")
End If
Description.AppendLine()
If Font.Font.Italic Then
Description.Append(" font-style : italic;")
Else
Description.Append(" font-style : normal;")
End If
Description.AppendLine()
Description.AppendFormat(" font-family : {0};{1}", Font.Font.Name, vbCrLf)
Description.AppendFormat(" font-size : {0}pt;{1}", Font.Font.SizeInPoints, vbCrLf)
Description.AppendFormat(" color : #{0};{1}", ToHexColor(Font.Color), vbCrLf)
Return Description.ToString
End Function
Public Overrides Sub Close()
If Not IsOpen Then
Throw New IO.IOException
End If
With Writer
.WriteLine("</div>")
.WriteLine("</body>")
.WriteLine("</html>")
.Close()
End With
IsOpen = False
End Sub
Public Overrides Sub Convert(ByVal ProgBar As ProgressBar)
If Me.Source Is Nothing Then
Throw New NullReferenceException
End If
Dim Index As Int32 = 0
Dim Lines() As String = Me.Source.Split(vbCrLf)
Dim Temp As New StringBuilder
For Each Line As String In Lines
ProgBar.Value = Index * 100 / Lines.Length
Application.DoEvents()
If Me.CommentExpression.IsMatch(Line) Then
Line = Me.CommentExpression.Replace(Line, "${sp1}<c>${initrem}${rem}</c>", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
Else
For Each Reg As Regex In Me.Expressions
Line = Reg.Replace(Line, "${sp1}<k>${key}</k>${sp2}", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
Next
End If
Temp.Append(Line)
Index += 1
Next
Me.Source = Temp.ToString
End Sub
Public Overrides Sub Create(ByVal FileName As String)
Dim CssWriter As IO.StreamWriter
IsOpen = True
Me.FileName = FileName
Me.CssFileName = IO.Path.GetFileNameWithoutExtension(FileName) & ".css"
Writer = New IO.StreamWriter(Me.FileName)
CssWriter = New IO.StreamWriter(IO.Path.GetDirectoryName(FileName) & "\" & Me.CssFileName)
With CssWriter
.WriteLine(".code")
.WriteLine("{")
.WriteLine(GetCssDescription(Me.Code))
.WriteLine(" white-space : pre;")
.WriteLine("}")
.WriteLine()
.WriteLine("k")
.WriteLine("{")
.WriteLine(GetCssDescription(Me.Keyword))
.WriteLine("}")
.WriteLine()
.WriteLine("c")
.WriteLine("{")
.WriteLine(GetCssDescription(Me.Comment))
.WriteLine("}")
.Close()
End With
With Writer
.WriteLine("<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.01 Transitional//EN"">")
.WriteLine("<html>" & vbCrLf)
.WriteLine("<head>")
.WriteLine(" <title>Codice sorgente generato da Code Converter (Written by Totem)")
.WriteLine(" <link rel='stylesheet' type='text/css' href='" & Me.CssFileName & "'>")
.WriteLine("</head>" & vbCrLf)
.WriteLine("<body>")
.WriteLine("<div class='code'>")
End With
Me.Source = Me.Source.Replace("<", "<")
Me.Source = Me.Source.Replace(">", ">")
End Sub
Public Overrides Sub Write()
If Not IsOpen Then
Throw New IO.IOException
End If
Me.Source = Me.Source.Replace("à", "à")
Me.Source = Me.Source.Replace("è", "è")
Me.Source = Me.Source.Replace("ì", "ì")
Me.Source = Me.Source.Replace("ò", "ò")
Me.Source = Me.Source.Replace("ù", "ù")
Writer.Write(Me.Source)
End Sub
End Class
Public Class RtfConverter
Inherits BaseConverter
Private Writer As IO.StreamWriter
Private IsOpen As Boolean = False
Private _OpenWithMSWord As Boolean = True
Sub New(ByVal Code As ColoredFont, ByVal Comment As ColoredFont, ByVal Keyword As ColoredFont)
MyBase.New(Code, Comment, Keyword)
End Sub
Public Property OpenWithMSWord() As Boolean
Get
Return _OpenWithMSWord
End Get
Set(ByVal Value As Boolean)
_OpenWithMSWord = Value
End Set
End Property
Private Function GetTagInfo(ByVal StartTag As String, ByVal ZeroBasedIndex As Int32) As String
Dim Index As Int32 = ZeroBasedIndex
If Me.OpenWithMSWord Then
Index += 1
End If
Return StartTag & Index
End Function
Private Function GetTag(ByVal IsKeyword As Boolean, ByVal BaseTag As String) As String
Dim Tag As String = BaseTag
If IsKeyword Then
Tag = "{\f2 " & Tag & "}"
Tag = GetTagInfo("{\cf", 1) & Tag & "}"
Tag = "{\fs" & Me.Keyword.Font.SizeInPoints * 2 & Tag & "}"
If Me.Keyword.Font.Bold Then
Tag = "{\b " & Tag & "}"
End If
If Me.Keyword.Font.Bold Then
Tag = "{\i " & Tag & "}"
End If
Else
Tag = "{\f3 " & Tag & "}"
Tag = GetTagInfo("{\cf", 2) & Tag & "}"
Tag = "{\fs" & Me.Comment.Font.SizeInPoints * 2 & Tag & "}"
If Me.Comment.Font.Bold Then
Tag = "{\b " & Tag & "}"
End If
If Me.Comment.Font.Bold Then
Tag = "{\i " & Tag & "}"
End If
End If
Return Tag
End Function
Public Overrides Sub Close()
If Not IsOpen Then
Throw New IO.IOException
End If
Writer.Write("}}}}")
Writer.Close()
End Sub
Public Overrides Sub Convert(ByVal ProgBar As System.Windows.Forms.ProgressBar)
If Me.Source Is Nothing Then
Throw New NullReferenceException
End If
Dim Index As Int32 = 0
Dim Lines() As String = Me.Source.Split(vbCrLf)
Dim Temp As New StringBuilder
For Each Line As String In Lines
ProgBar.Value = Index * 100 / Lines.Length
Application.DoEvents()
If Me.CommentExpression.IsMatch(Line) Then
Line = Me.CommentExpression.Replace(Line, "${sp1}" & GetTag(False, "${initrem}${rem}"), RegexOptions.IgnoreCase Or RegexOptions.Multiline)
Else
For Each Reg As Regex In Me.Expressions
Line = Reg.Replace(Line, "${sp1}" & GetTag(True, "${key}") & "${sp2}", RegexOptions.IgnoreCase Or RegexOptions.Multiline)
Next
End If
Temp.Append(Line)
Index += 1
Next
Me.Source = Temp.ToString
End Sub
Public Overrides Sub Create(ByVal FileName As String)
IsOpen = True
Me.FileName = FileName
Writer = New IO.StreamWriter(Me.FileName)
With Writer
.WriteLine("{\rtf1 \ansi \ansicpg1252 \deff0 \deflang1040")
.WriteLine("{\fonttbl")
.WriteLine(" {\f1 \fnil \fcharset0 " & Me.Code.Font.Name & ";}")
.WriteLine(" {\f2 \fnil \fcharset0 " & Me.Keyword.Font.Name & ";}")
.WriteLine(" {\f3 \fnil \fcharset0 " & Me.Comment.Font.Name & ";}")
.WriteLine("}")
.Write("{\colortbl ")
.Write("\red{0}\green{1}\blue{2}; ", Me.Code.Color.R, Me.Code.Color.G, Me.Code.Color.B)
.Write("\red{0}\green{1}\blue{2}; ", Me.Keyword.Color.R, Me.Keyword.Color.G, Me.Keyword.Color.B)
.Write("\red{0}\green{1}\blue{2};", Me.Comment.Color.R, Me.Comment.Color.G, Me.Comment.Color.B)
.WriteLine("}")
.WriteLine()
.WriteLine("{\f1 {\fs" & Me.Code.Font.SizeInPoints * 2 & " " & GetTagInfo("{\cf", 0) & " ")
End With
End Sub
Public Overrides Sub Write()
If Not IsOpen Then
Throw New IO.IOException
End If
Me.Source = Me.Source.Replace(Chr(10), "\line" & vbCrLf)
Writer.Write(Me.Source)
End Sub
End Class
Public Class PdfConverter
Inherits BaseConverter
Private Writer As Document
Private MainParagraph As Paragraphs.TextParagraph
Sub New(ByVal Code As ColoredFont, ByVal Comment As ColoredFont, ByVal Keyword As ColoredFont)
MyBase.New(Code, Comment, Keyword)
End Sub
Public Overrides Sub Close()
End Sub
Public Overrides Sub Convert(ByVal ProgBar As System.Windows.Forms.ProgressBar)
If Me.Source Is Nothing Then
Throw New NullReferenceException
End If
End Sub
Public Overrides Sub Create(ByVal FileName As String)
End Sub
Public Overrides Sub Write()
End Sub
End Class
End Namespace