' Gambas module file Public Struct MarkupList sType As String iIndent As Integer End Struct Public EnableCode As Integer Private $aMarkup As String[] Private $cLink As Collection Private $aIndex As String[] Private $aTable As String[] Private $aTableClass As String[] Private $aTablePos As Integer[] Private $hMarkdown As Markdown Private $hLink As New MarkdownLink Private $bComment As Boolean Private Sub GetIndent(sLine As String) As Integer Return Len(sLine) - Len(LTrim(sLine)) End Public Sub Convert(sMarkup As String, hMarkdown As Markdown) As String $hMarkdown = hMarkdown $cLink = New Collection $aIndex = New String[] EnableCode = 0 Return ConvertMarkup(Split(sMarkup, "\n")) End Private Sub IsWordLimit(sCar As String) As Boolean If IsLetter(sCar) Or If IsDigit(sCar) Or If Len(sCar) >= 2 Then Return Return True End Private Sub ConvertMarkup(aLine As String[], Optional bDoNotSetLine As Boolean) As String Dim iLine As Integer Dim aResult As String[] Dim sLine As String Dim sText As String Dim I As Integer Dim bCode As Boolean Dim iBlockQuote As Integer Dim sCar As String Dim bInsidePar As Boolean Dim bIgnorePar As Boolean Dim bAddPar As Boolean Dim iIndent, iCurrentIndent As Integer Dim hMarkupList As MarkupList Dim bJustList As Boolean Dim sLink As String Dim iPos As Integer Dim aList As New MarkupList[] Dim sTable As String Dim iIndexPos As Integer = -1 Dim aCommand As String[] Dim aSaveMarkup As String[] Dim aSaveTable As String[] Dim aSaveTablePos As Integer[] Dim aSaveTableClass As String[] Dim sClass As String aResult = New String[] aSaveMarkup = $aMarkup $aMarkup = New String[] aSaveTable = $aTable $aTable = New String[] aSaveTableClass = $aTableClass $aTableClass = New String[] aSaveTablePos = $aTablePos $aTablePos = New Integer[] ' iLine = 0 ' Do ' If iLine >= aLine.Count Then Break ' sLine = LTrim(aLine[iLine]) ' If Left(sLine) = "%" Then ' If sLine Begins "%IF " Then ' Else If sLine Begins "%ELSE " Then ' ' Endif ' Endif ' Inc iLine ' Loop For iLine = 0 To aLine.Max sLine = LTrim(aLine[iLine]) If Not sLine Then Continue If Left(sLine) <> "[" Then Continue I = InStr(sLine, "]:") If I = 0 Then Continue If I >= 3 Then sLink = Trim(Mid$(sLine, I + 2)) $cLink[Mid$(sLine, 2, I - 2)] = sLink If Left(sLink) = "#" Then iPos = InStr(sLink, " ") If iPos Then sLink = Left(sLink, iPos - 1) aLine[iLine] = "" Continue Endif Endif aLine[iLine] = "" Next iLine = -1 While iLine < aLine.Max Inc iLine sLine = aLine[iLine] If Not bDoNotSetLine Then $hMarkdown.Line = iLine $hMarkdown.Current = sLine Endif 'If $aMarkup.Count Then ' aResult.Add(ConvertLine(sLine)) ' Continue 'Endif sLine = RTrim(sLine) If Not sLine Then sLine = aLine[iLine] If $bComment Then iPos = InStr(sLine, "-->") If iPos = 0 Then Continue $bComment = False sLine = Mid$(sLine, iPos + 4) Endif ' Special command If Left(sLine, 2) = "@{" And If Right(sLine) = "}" Then If sLine = "@{index}" Then iIndexPos = aResult.Count aResult.Add(sLine) Else aCommand = $hMarkdown.Command(Mid$(sLine, 3, -1)) If aCommand And If aCommand.Count Then aLine.Insert(aCommand, iLine + 1) Endif Endif Continue Endif ' Blockquote I = 0 Do If Left(sLine) <> ">" Then Break sCar = Mid$(sLine, 2, 1) If sCar <> " " And If sCar <> gb.Tab Then Break Inc I sLine = LTrim(Mid$(sLine, 3)) Loop If I > iBlockQuote Then While I > iBlockQuote aResult.Add("
") Inc iBlockQuote Wend Else If I < iBlockQuote Then While I < iBlockQuote aResult.Add("
") Dec iBlockQuote Wend Endif ' Lists If Trim(sLine) Then iIndent = GetIndent(sLine) GoSub CLOSE_LIST sLine = Mid$(sLine, iCurrentIndent + 1) ' Horizontal lines If Left(sLine) = "*" And If Right(sLine) = "*" Then sText = Replace(sLine, " ", "") If Len(sText) >= 3 And If sText = String$(Len(sText), "*") Then GoSub CLOSE_PARA aResult.Add("
") Continue Endif Endif If Left(sLine) = "-" And If Right(sLine) = "-" Then sText = Replace(sLine, " ", "") If Len(sText) >= 3 And If sText = String$(Len(sText), "-") Then GoSub CLOSE_PARA aResult.Add("
") Continue Endif Endif If sLine Begins "* " Or If sLine Begins "- " Then hMarkupList = New MarkupList hMarkupList.sType = "ul" hMarkupList.iIndent = iIndent + 1 + GetIndent(Mid$(sLine, 2)) aList.Add(hMarkupList) bJustList = True If aResult.Count And If Trim(aResult[aResult.Max]) = "" Then If Not Trim(aLine[iLine - 1]) Then If iLine < aLine.Max And If GetIndent(aLine[iLine + 1]) >= hMarkupList.iIndent Then bJustList = False Endif aResult[aResult.Max - 1] = "

\n" & aResult[aResult.Max - 1] Endif aResult.Remove(aResult.Max) Else GoSub CLOSE_PARA aResult.Add(Space$(iCurrentIndent) & "