[solved] string.gsub

Codea project code:

--# Tabname One
local foo = "bar"
function bar() print(foo) end

--# Tabname Two
local test = ...
function me() print(test) end

Is it possible to get the above code split in separate chunks? I need the tabname and the content of the tab separately.
I tried: string.gsub("%-%-#.+%-%-#") and string.gsub("%-%-#(^[%-%-#])") and couple other ideas, but no one worked. I know I could use for example string.gsub("%b<>") and format the tabs in different way, but this is awkward… Any ideas?

hello @se24vad .Maybe this will help you, i use this function to split tabs:

function Updater:installNewVersion(str)
    pause("---- installing new version ---")
    local a,b,c,d,tabName,tabContent
    local plainText = true
    local start = 0
    local tabMarker = "--".."# " -- this string cannot appear in the code!
    repeat
        a,b = str:find(tabMarker,start+1,plainText)
        c,d = str:find("\
",b+1,plainText)
        tabName = str:sub(b+1,c-1)
        local e,f = str:find(tabMarker,d+1,plainText)
        if e then e = e -1 end
        tabContent = str:sub(d+1,e)
        if tabName == "UserData" and self.userData then tabContent = self.userData end
        if self.projectName then tabName = self.projectName .. ":".. tabName end
        saveProjectTab(tabName, tabContent)
        pause( tabName .. " : installed"  )
        start = e
    until start == nil
end

I will try it out, thank you very much! This seems to be exactly what I asked for! Meanwhile I succeed with splitting the content by each \ and then search for a string starting with --#, which is then the tab name. everything else is considered the tab content. Thank your very much!

so here is my solution:

local txt = [[--#...]] -- content text of the tab

local tabs, latest = {}
txt:gsub("[^\
]*\
", function(str)
    if str:find("^%-%-#") then
        latest = str:sub(5, #str-1)
        tabs[latest] = ''
    else
        tabs[latest] = tabs[latest]..str
    end
end)

then you simply iterate through tabs and save them like:

for name, content in pairs(tabs) do
    saveProjectTab(name, content:sub(1, #content-1))
end

@se24vad i recommend you not to write --# in you code but declare it this way:

local tabMarker = "--".."# " -- this string cannot appear in the code!

if you dont understand why, then copy your project by long-press and paste it in a new project by long-press and… see the tab result in your new project.

Can you describe? I dont notice anything. Everything seems to behave as expected.

@se24vad - Codea uses --# to separate multiple code tabs when you save them, and it uses the same tags to split the code back into their original tabs if you do a long paste back into Codea.

If you have a function which tests for --#, and it includes that exact string, Codea will assume that is the start of a new tab and split your function in two (when pasting code back into Codea).

What Jmv38 has suggested is to artifically split --# into two pieces and join them together, so Codea won’t recognise it as --# when it is searching for tab markers.

@Ignatz Quote: If you have a function which tests for --#, and it includes that exact string, Codea will assume that is the start of a new tab and split your function in two (when pasting code back into Codea).

this can not happen to my approach (from above) because I split my content into separate lines by \ and test if the WHOLE LINE is BEGINNING with --#. What you describe would include at least spaces at the beginning of the line. My approach ignores such fake new tabs, and treats them as comments or just random code…

Thank you for interesting thoughts!

@se24vad - I’m not talking about what your code does. I’m talking about what Codea does.

Think about why the --# is at the top of a tab to start with. It’s there to tell Codea when new tabs start. Right?

So if you paste your code into a new project (what we call a long press, that restores all the separate tabs), Codea looks through the code and makes a new tab every time it sees --#.

When it gets to your function and sees --# in the middle, it thinks this is a new tab and will split your function between two tabs! wrecking your code.

This will only happen if you have code in multiple tabs and if it is pasted into Codea using a long press. It won’t affect your running the code right now. But Jmv38 is correct is suggesting you be careful now, so you don’t run into problems later.

@Ignatz aahh I see. Thank you a lot! I’ll implement that!

Thanks for this bit of code, it comes in handy.

K.I.S.S. backup tool - the result of this discussion :slight_smile:
http://twolivesleft.com/Codea/Talk/discussion/4719/backup-most-simple-backup-tool#Item_1