[Gambas-user] [gb.eval.highlight] Spaces and syntax highlighting

Benoît Minisini gambas at ...1...
Sun Jun 17 19:59:32 CEST 2012


Le 17/06/2012 15:49, Adrien Prokopowicz a écrit :
> Le dimanche 17 juin 2012 15:38:22 Benoît Minisini a écrit :
>> Le 12/06/2012 23:52, Adrien Prokopowicz a écrit :
>>> Hi,
>>>
>>> I noticed that the Higlight.Analyze() method doesn't like when the Gambas code
>>> hasn't spaces between operators, commas and a few other ones.
>>>
>>> There is a good example of that problem here :
>>> http://www.gambasforge.org/wiki/testHighlight
>>>
>>> Maybe this behaviour is intended, in order to keep Gambas code clean, but I
>>> haven't found any function to add these spaces, like in the IDE.
>>> Do you know where I can find this ?
>>>
>>> Regards,
>>> Adrien.
>>>
>>
>> Do you have some code to show? Maybe you don't use the Analyze() method
>> correctly...
>>
>>
>
> Here is the function that makes the conversion Gambas string ->  html :
>
> Public Sub GambasToHtml(sValue As String) As XmlElement
>
>    Dim sLignes, s, sCode As String
>    Dim i, nbLignes, lenligne As Integer
>    Dim sTypes As String[] = ["", "normal", "keyword", "function", "operator", "symbol", "number", "string", "comment", "", "", "datatype", "", "", "", "", "error", "help", "preprocessor"]
>    Dim Positions, Types, Lengths As New Integer[]
>    Dim lignes As String[]
>    Dim tableau As New HtmlTable
>    Dim elmt As XmlElement
>    Dim conteneur As New XmlElement("code")
>
>    If Not sValue Then Return
>    'sret = "<table class=\"code\"><tr><td class=\"numlignes\">"
>    tableau.ClassName = "code"
>    tableau[0, 0].ClassName = "numlignes"
>    tableau[0, 1].ClassName = "code"
>    tableau[0, 1].AppendChild(conteneur)
>    sValue = Replace$(sValue, "\r\n", "\n") 'Compatibilité win/mac/linux
>    sValue = Trim$(sValue) 'Espaces indésirables
>    lignes = Split(sValue, "\r\n", "", False)
>    For Each s In lignes
>
>      Highlight.Analyze(s)
>
>      If Highlight.Positions.Count = 0 Then
>        conteneur.AppendText(s)
>        conteneur.AppendFromText("<br />")
>        'sCode&= s&  "<br />"
>        Inc nbLignes
>        Continue
>      Endif
>      Positions = Highlight.Positions
>      Types = Highlight.Types
>      Lengths.Clear()
>
>      For i = 0 To Highlight.Symbols.Max
>        Lengths.Push(Len(Highlight.Symbols[i]))
>      Next
>
>      lenligne = Len(s)
>
>      For i = 1 To lenligne
>        If Positions.Count<= 0
>          'sCode&= String.Mid(s, i, 1)
>          conteneur.AppendText(Mid(s, i, 1))
>        Else If i = (Positions[0] + 1)
>          'sCode&= "<span class=\""&  sTypes[Types[0]]&  "\">"&  String.Mid$(s, Positions[0] + 1, Lengths[0])&  "</span>"
>          elmt = New XmlElement("span")
>          elmt.ClassName = sTypes[Types[0]]
>          elmt.TextContent = Mid$(s, Positions[0] + 1, Lengths[0])
>          conteneur.AppendChild(elmt)
>          i += (Lengths[0] - 1)
>          Positions.Extract(0)
>          Lengths.Extract(0)
>          Types.Extract(0)
>        ' Else If Mid$(s, i, 1) = " "
>        '   'sCode&= " "
>        '   tableau[0, 1].AppendFromText(" ")
>        Else
>          'sCode&= String.Mid$(s, i, 1)
>          conteneur.AppendText(String.Mid(s, i, 1))
>        Endif
>      Next
>      'sCode&= "<br />"
>      conteneur.AppendFromText("<br />")
>      Inc nbLignes
>
>    Next
>
>    For i = 1 To nbLignes
>      sLignes&= (i&  "<br />\n")
>    Next
>
>    tableau[0, 0].AppendFromText(sLignes)
>
>    ' sCode = Replace$(sCode, "&","&")
>    ' sCode = Replace$(sCode, "<","<")
>    ' sCode = Replace$(sCode, ">",">")
>    ' sCode = RegexpReplace(sCode, "<(/?(span|br).*)>", "<&1>")
>    ' sCode = Replace$(sCode, "&nbsp;", " ")
>    ' sRet&= "</td><td class=\"code\">"
>    ' sret&= sCode
>    ' sRet&= "</td></tr></table>"
>    '   Return XmlElement.FromText(sRet)[0]
>    elmt = New XmlElement("div")
>    elmt.ClassName = "code"
>    elmt.AppendChild(tableau)
>    Return elmt
>
> End
>
>
> The full code is available  here : (svn checkout)
> https://gambasforge.svn.sourceforge.net/svnroot/gambasforge/trunk
>
>
> Regards,
> Adrien.

The Positions are relative to the string returned by 
Highlight.TextAfter. In other words, you just have to add a "s = 
Highlight.TextAfter" line just after the Highlight.Analyze call.

Beware that there was a bug: the value returned by Highlight.TextAfter 
was the original string when the Rewrite argument of Highlight.Analyze() 
was FALSE. Use revision #4844 that fixes that bug.

Otherwise, I suggest you set the Rewrite argument.

Regards,

-- 
Benoît Minisini




More information about the User mailing list