Downloading and Uploading Gists

@Juce do you have any plan to port your upload-web trick into your program? It would be really nice to have a stand-alone program, rather than one that depends on the availability of your web site…

Another option would be to use CC repo instead of github for storing the raw project file. I think they dont use Json format. And CC repo is hosted by TLL. What do you think of this idea @ignatz, @briarfox, @Toffer? That would be quite simple for you to do, compared to what you have achieved with CC?

@juce Awesome tool for gists! Will make sharing code much easier.

@Jmv38 Great idea!

@juce i’ve made a couple changes to your code:

  • saving gist url to clipboard is automatic.
  • i make a backup copy of the link in tab ‘myGists’, this may be useful, cause the gists are anonymous…
  • added some prints.

--# Main
-- Gist uploader/downloader

local function iter(obj)
    if not obj.name then
        return
    end
    local data = obj.data
    local name, pos = obj.name, obj.pos
    local s, e, next = data:find("\
[-][-]# ([%w_]+)[^\
]*\
.", pos)
    obj.name, obj.pos = next, e
    return name, data:sub(pos, s)
end

function tabs(data)
    local s, e, name = data:find("^%s*[-][-]# ([%w_]+)[^\
]*\
.")
    local obj = {
        pos = e or 1, 
        name = name or "Main",
        data = data,
    }
    return iter, obj
end

function saveLink(link)
    local name = "myGists"
    local tab = readProjectTab(name) or ""
    tab = tab .. "-- " .. link .. "  -- project: \
"
    saveProjectTab(name,tab)
end

function setup()
    url, c = "", color(203, 209, 60, 255)
    -- Download gist via link in pasteboard
    parameter.action("Paste gist url", function()
        url = pasteboard.text
        parameter.clear()
        parameter.action("Download", function()
            if not url:match("/raw") then
                url = url .. "/raw"
            end
            http.request(url, function(data)
                msg = "Downloaded. Splitting into tabs ..."
                tween.delay(1, function()
                    for tabname,tabdata in tabs(data) do
                        saveProjectTab(tabname, tabdata)
                    end
                    msg, c = "Success!", color(96, 181, 47, 255)
                    parameter.action("Quit", function()
                        close()
                    end)
                end)
            end, function(err)
                msg, c = err, color(177, 49, 49, 255)
                parameter.action("Quit", function()
                    close()
                end)
            end)
        end)
    end)
    -- Upload data from pasteboard to gist
    parameter.action("Upload new gist", function()
        data = pasteboard.text
        parameter.clear()
        print("starting upload...")
        http.request('http://gist-proxy.aws.mapote.com:8888/gists', function(link)
                msg, c = "Success!\
" .. link, color(96, 181, 47, 255)
                pasteboard.copy(link)
                saveLink(link)
                print("link copied in the pasteboard and in tab myGists")
                parameter.action("Quit", function()
                    close()
            end)
        end, function(err)
            msg, c = err, color(177, 49, 49, 255)
            parameter.action("Quit", function()
                close()
            end)
        end,
        { method = 'POST', data = data })
    end)
end

function draw()
    background(18, 18, 19, 255)
    fill(c)
    textWrapWidth(WIDTH-100)
    fontSize(35)
    if msg then
        text(msg, WIDTH/2, HEIGHT/2)
    else
        text(url, WIDTH/2, HEIGHT/2)
    end
end



--# myGists
-- https://gist.github.com/anonymous/3066455ed5cc8433f9ca  -- project: juce gister modified

Nice Project !

Here is my little contribution : https://gist.github.com/anonymous/60058db98c5a010cb0f3

→ auto download of dkjson and use real gists api, so no proxy.

→ use just id for download (like 60058db98c5a010cb0f3)

@HyroVitalyProtago - does your code include Jmv38’s changes? It’s getting a bit confusing.

oh wow, all this activity, while i was away :slight_smile:

@Jmv38, to respond to your earlier post: i was a bit on the fence with the web-service vs included json lib. Didn’t want the project to be too big, where it no longer fits on a forum page… On the other hand, you were right that it is better to have it be self-contained, not relying on my service being up and running.

So i was thinking about how to include dkjson, in a “clever” way… And then @HyroVitalyProtago showed an interesting idea of downloading dkjson on-demand into global data storage. It’s a nice trick…

@Jmv38, myGists tab is a nice touch… although, it then needs to be removed, when a gist is being downloaded into the current project (duplicated from Gist).

@Ignatz, i believe Jmv38’s changes are included in HyroVitalyProtago’s version, but there are other things that are missing. So, yeah it is getting all confusing.

@HyroVitalyProtago, it’s a cool trick with dkjson library. Nicely done there! I have to say though that i don’t like the idea of using just the gist id. The full url is much easier to copy, and also explains what this id is. The id just by itself could refer to anything. Also, i think the whole idea of duplicating the project, downloading the project gist and splitting it into tabs - kinda got lost in your version. Not saying it’s necessarily bad, just completely different, and not what the initial code was trying to do…

One last random note: in my latest version, i had actually made a small tweak to tab-splitting logic - to fix the issue with an extra newline being added to the tabs after splitting.

@juce - I suggest you ask contributors to mark their code changes clearly in some way, or else use version control (although that is probably going a little overboard at this stage).

@Ignatz, fair point on source control, but yes, this is probably still small enough that we can get away with just exchanging gists

@juce all your remarks make a lot of sense.
It is important to keep the original spirit of your code.
I didnt think about removing myGist before downloading. This is quite easy to add.
But i wont touch the code before you tell me which version to start from.
Let me know what you decide to do.

@Ignatz : I have write my code from the @Jmv38 version.

@juce : sorry about remove tabs identification ! I have read the code too quickly and miss the feature ! My code split into tabs different files in a Gists. And I understand that’s not the spirit of original code.

I understand know why there is a --#myGists at the end of file and not into an another tab…

About the id or URL, when you copy https://gist.github.com/anonymous/60058db98c5a010cb0f3, the id is 60058db98c5a010cb0f3. If you prefer URL, just replace at line 13 pasteboard.text by
pasteboard.text:gsub(".*/", "") for catch the id at the end of URL.

So, keep what you want or just ignore my version and have a nice day !

I updated first post to have the new version - 2.0. I think i got the merge of all the features correctly - from both of you guys :slight_smile: Check it out, should still work.

@Jmv38, no problem with myGists - i added the removal. As you said, it’s pretty simple. One thing where i had to modify your saveLink function slightly: to use listProjectTabs instead of direct readProjectTab(“myGists”), because readProjectTab throws an error, when the tab doesn’t exist (yet).

@HyroVitalyProtago, i used your idea of automatically downloading Dkjson library and storing it in global data. I think it is brilliant. Thanks!

Oh, and the gist for version 2.0:
https://gist.github.com/anonymous/d37a0931ba9f4e42ee22

What are gists?

@aurumcoder2624 http://lmgtfy.com/?q=gists :wink:

@juce great job!
i’ve used previous version to load the new one.
And used the new one to load… the new one again. Seemed to work fine.

@Juce hello! i have a small problem: the project creates this gist:
https://gist.githubusercontent.com/anonymous/d312a327e7c72af8eaff
but if i pull it i get the whole github page, and not the code :frowning:
To get the code i must get this adress, from github site:
https://gist.githubusercontent.com/anonymous/d312a327e7c72af8eaff/raw/407210261ebe435baf9b4247aab0aef9ccf220af/Project.lua
Any idea on how to get the real good adress automatically?
Thanks!

[EDIT] i am getting a bit lost. Now i seems to work…

[EDIT] Ah, my mistake, sorry, your code is fine. I was calling the wrong function in my code.

@Jmv38, no problem… Yeah, both links worked ok for me - maybe it was some weird glitch at github?

@Juce no, it was just my fault (stupid me…). Forget it.