Android Example Simple function to convert Markdown Text to CSBuilder Format

This simple function converts Markdown-formatted text so that it can be used as CSBuilder.

This is particularly useful if, for example, you have created a text in MS Word with bold and italic formatting to present it in an app in formatted form.

You can then convert des MS Word file to Markdown using an online converter (e.g., https://word2md.com/) and use the result as simple txt in the application.

The text is converted using the "Sub ProcessMarkdownToCSBuilder(...)" function.
Sub ProcessMarkdownToCSBuilder(..):
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    Dim markdownText As String = $"This is a **bold** Text, 
    an *italic* Text 
    and a [Link1](http://example1.com) 
    and a [Link2](http://example2.com)."$
    
    Dim cs As CSBuilder = ProcessMarkdownToCSBuilder(markdownText)
    Label1.Text = cs
    cs.EnableClickEvents(Label1)
    
End Sub

Sub ProcessMarkdownToCSBuilder(markdown As String) As CSBuilder
    Dim cs As CSBuilder
    cs.Initialize
    Dim boldPattern As String = "\*\*(.*?)\*\*" ' Regex for **Text**
    Dim italicPattern As String = "\*(.*?)\*" ' Regex for *Text*
    Dim urlPattern As String = "\[(.*?)\]\((.*?)\)" ' Regex for [Text](URL)
    Dim combinedPattern As String = boldPattern & "|" & italicPattern & "|" & urlPattern
    Dim matcher As Matcher = Regex.Matcher(combinedPattern, markdown)
    Dim lastEnd As Int = 0
    Do While matcher.Find
        cs.Append(markdown.SubString2(lastEnd, matcher.GetStart(0)))
        If matcher.Group(1) <> Null Then
            cs.Typeface(Typeface.DEFAULT_BOLD).Append(matcher.Group(1)).PopAll
        Else If matcher.Group(2) <> Null Then
            cs.Typeface(Typeface.CreateNew(Typeface.DEFAULT,Typeface.STYLE_ITALIC)).Append(matcher.Group(2)).PopAll
        Else If matcher.Group(3) <> Null And matcher.Group(4) <> Null Then
            cs.Color(Colors.Blue).Clickable("cs", matcher.Group(4)).Append(matcher.Group(3)).PopAll
        End If
        lastEnd = matcher.GetEnd(0)
    Loop
    cs.Append(markdown.SubString(lastEnd))
    Return cs
End Sub

Sub cs_Click(cstag As Object)
    Log($"Clicked on: ${cstag}"$)
    Label2.Text = Label2.Text  & CRLF & cstag
End Sub

The example project also demonstrates the use of clickable URLs.
24-07-2024_17-34-34.png

The three shown Markdown patterns are just examples. In principle, you can implement any pattern that is common to both CSBuilder and Markdown.

I know there are existing libraries for this, but this example also demonstrates how to use regex for such tasks.
 

Attachments

  • md2cs.zip
    9.6 KB · Views: 222

fredo

Well-Known Member
Licensed User
Longtime User
...you can implement any pattern that is common...

This is what the function looks like when you add “underline”:

B4X:
Sub ProcessMarkdownToCSBuilder(markdown As String) As CSBuilder
    Dim cs As CSBuilder
    cs.Initialize

    Dim boldPattern As String = "\*\*(.*?)\*\*" ' Regex for **Text**
    Dim italicPattern As String = "\*(.*?)\*" ' Regex for *Text*
    Dim underlinePattern As String = "\_(.*?)\_" ' Regex for _Text_
    Dim urlPattern As String = "\[(.*?)\]\((.*?)\)" ' Regex for [Text](URL)

    Dim combinedPattern As String = boldPattern & "|" & italicPattern & "|" & underlinePattern & "|" & urlPattern

    Dim matcher As Matcher = Regex.Matcher(combinedPattern, markdown)
    Dim lastEnd As Int = 0
    Do While matcher.Find
        cs.Append(markdown.SubString2(lastEnd, matcher.GetStart(0)))
        If matcher.Group(1) <> Null Then
            cs.Typeface(Typeface.DEFAULT_BOLD).Append(matcher.Group(1)).PopAll
        Else If matcher.Group(2) <> Null Then
            cs.Typeface(Typeface.CreateNew(Typeface.DEFAULT, Typeface.STYLE_ITALIC)).Append(matcher.Group(2)).PopAll
        Else If matcher.Group(3) <> Null Then
            cs.Underline.Append(matcher.Group(3)).PopAll
        Else If matcher.Group(4) <> Null And matcher.Group(5) <> Null Then
            cs.Color(Colors.Blue).Clickable("cs", matcher.Group(5)).Append(matcher.Group(4)).PopAll
        End If
        lastEnd = matcher.GetEnd(0)
    Loop
    cs.Append(markdown.SubString(lastEnd))
    Return cs
End Sub

25-07-2024_08-16-45.png
 

Attachments

  • md2cs_ext.zip
    9.7 KB · Views: 139
Top