Skip to content

Instantly share code, notes, and snippets.

@voising
Last active October 21, 2021 21:32
Show Gist options
  • Select an option

  • Save voising/5789806 to your computer and use it in GitHub Desktop.

Select an option

Save voising/5789806 to your computer and use it in GitHub Desktop.

Revisions

  1. voising revised this gist Jun 15, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions how_to.md
    Original file line number Diff line number Diff line change
    @@ -18,8 +18,8 @@ Example

    You could run :

    chmod +x gists_to_dash.rb
    ./gists_to_dash.rb voising Dropbox/Configs/snippets.dash "$" no_comments
    chmod +x gists_to_dash.rb
    ./gists_to_dash.rb voising Dropbox/Configs/snippets.dash "$" no_comments

    More about the char_appended_to_keyword argument
    --------------------------------------------
  2. voising revised this gist Jun 15, 2013. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions how_to.md
    Original file line number Diff line number Diff line change
    @@ -13,6 +13,13 @@ Launch the script
    Launch the script without arguments to see the usage.
    The script will use the keyword to fill the abbreviation field in Dash and fill the snippet with the gist content

    Example
    -------

    You could run :

    chmod +x gists_to_dash.rb
    ./gists_to_dash.rb voising Dropbox/Configs/snippets.dash "$" no_comments

    More about the char_appended_to_keyword argument
    --------------------------------------------
  3. voising revised this gist Jun 15, 2013. 2 changed files with 7 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion gists_to_dash_db.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    #!/usr/bin/env ruby

    if ARGV[0].nil? || ARGV[0].match(/-h/)
    puts "Usage : #{$0} github_username dash_sqlite_db char_appended_to_tag [no_comments]"
    puts "Usage : #{$0} github_username dash_sqlite_db char_appended_to_keyword [no_comments]"
    exit
    end

    7 changes: 6 additions & 1 deletion how_to.md
    Original file line number Diff line number Diff line change
    @@ -13,5 +13,10 @@ Launch the script
    Launch the script without arguments to see the usage.
    The script will use the keyword to fill the abbreviation field in Dash and fill the snippet with the gist content

    The script handles INSERT and UPDATE

    More about the char_appended_to_keyword argument
    --------------------------------------------

    When you write your snippets you may use a key at the end of the abbreviation. That's what this argument stands for.

    If the keyword is #calljquery and the char_appended_to_keyword is "$", the snippet abbreviation will be : calljquery$
  4. voising created this gist Jun 15, 2013.
    96 changes: 96 additions & 0 deletions gists_to_dash_db.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    #!/usr/bin/env ruby

    if ARGV[0].nil? || ARGV[0].match(/-h/)
    puts "Usage : #{$0} github_username dash_sqlite_db char_appended_to_tag [no_comments]"
    exit
    end

    require 'net/http'
    require 'open-uri'
    #require 'awesome_print'
    require 'uri'
    require 'json'
    require 'sqlite3'

    #############
    # Variables
    #############

    url = "https://api.github.com/users/#{ARGV[0]}/gists"
    tag_regex = /#\w+/

    ############
    # Functions
    ############

    def getHttpsContent(url)
    uri = URI.parse(url)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    request = Net::HTTP::Get.new(uri.request_uri)
    http.request(request).body
    end

    #############
    # We get all the public gists
    #############

    puts "\nFetching #{url}...\n"
    gists = JSON.parse getHttpsContent(url)
    #ap gists

    #############
    # We list the gists and detect those we'll update
    #############

    gists_to_update = []
    puts "Gists are :\n"
    gists.each do |gist|
    puts "\t#{gist['description']}\n"
    unless (shortcut = gist['description'].match(tag_regex).to_s).empty?

    #gist['files'].each {|f| ap f}
    files = []
    gist['files'].each {|f| files << {:filename => f[0], :url => f[1]['raw_url']}}

    gists_to_update << {
    :shortcut => shortcut[1..-1],
    :files => files
    }
    end
    end

    puts "\n\nThose Dash snippets will be updated :\n"
    gists_to_update.each {|x| puts "\t#{x[:shortcut]}#{ARGV[2]}"}

    #############
    # We update the database
    #############

    db = SQLite3::Database.new(ARGV[1])
    #db.execute2 'SELECT * FROM snippets;' do |row|
    # ap row
    #end

    gists_to_update.each do |gist|
    content = ""
    gist[:files].each do |f|
    content << "\n\n### #{f[:filename]}\n\n" if ARGV[3].nil?
    open(f[:url]) {|c|
    c.each_line {|line| content << line}
    }
    end

    snippet_id = db.get_first_row( "SELECT sid FROM snippets WHERE title = ?","#{gist[:shortcut]}#{ARGV[2]}")
    if snippet_id.nil?
    db.execute( "INSERT INTO snippets VALUES (null, ?, ?, 'Standard', 0)",
    "#{gist[:shortcut]}#{ARGV[2]}",
    content )
    else
    db.execute( "UPDATE snippets SET body = ? WHERE sid = ?",
    content,
    snippet_id )
    end
    puts "\n#{gist[:shortcut]}#{ARGV[2]} has been updated"
    end
    17 changes: 17 additions & 0 deletions how_to.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    How to
    ======

    Create or edit a gist
    ---------------------

    In your gist description, add a keyword like : #jquery


    Launch the script
    -----------------

    Launch the script without arguments to see the usage.
    The script will use the keyword to fill the abbreviation field in Dash and fill the snippet with the gist content

    The script handles INSERT and UPDATE