Skip to content

Instantly share code, notes, and snippets.

@rbq
Last active Apr 27, 2020
Embed
What would you like to do?
Convert notes from Snippets.me to Quiver
source 'https://rubygems.org'
gem 'data_mapper'
gem 'dm-sqlite-adapter'
#!/usr/bin/env ruby
# Author: rbq
# Source: https://gist.github.com/rbq/a42eaf490ead1399d3c2
# Version: 2018-11-29
# Usage:
# * `./snippetsme-to-quiver.rb` (requires recent Ruby/Bundler version)
# * Quiver: File -> Open Shared Notebook... -> Snippets.qvnotebook
SNIPPETS_DB_FILENAME = "#{Dir.home}/Library/Application Support/Snippets/Snippets.sqlite"
QUIVER_BOOK_FILENAME = 'Snippets.qvnotebook'
require 'fileutils'
require 'json'
require 'digest/sha1'
require 'bundler/setup'
Bundler.require
DataMapper.setup :default, "sqlite3://#{SNIPPETS_DB_FILENAME}"
DataMapper.repository(:default).adapter.field_naming_convention = lambda { |value| value.name.to_s }
class Snippet
include DataMapper::Resource
storage_names[:default] = 'Snippet'
belongs_to :language, child_key: [:HighlightKey]
has n, :labelings, child_key: [:SnippetKey]
has n, :labels, through: :labelings
property :id, String, field: 'Key', key: true
property :name, String, field: 'Name'
property :code, String, field: 'SourceCode'
property :comment, String, field: 'Comment'
property :author, String, field: 'Author'
property :url, String, field: 'RelatedUrl'
property :filename, String, field: 'ExportFileName'
property :created, Integer, field: 'DateAdded'
property :removed, Integer, field: 'DateRemoved'
def language_name
return 'plain_text' unless language && language.name
{
'C' => 'c_cpp',
'C++' => 'c_cpp',
'C#' => 'csharp',
'Cold Fusion' => 'coldfusion',
'Objective-C' => 'objectivec',
'Shell Script' => 'sh',
}.fetch(language.name) {
language.name.gsub(' ', '_').downcase
}
end
end
class Language
include DataMapper::Resource
storage_names[:default] = 'Highlight'
property :id, String, field: 'Key', key: true
property :name, String, field: 'Name'
end
class Labeling
include DataMapper::Resource
storage_names[:default] = 'SnippetLabels'
belongs_to :snippet, child_key: [:SnippetKey]
belongs_to :label, child_key: [:LabelKey]
property :id, String, field: 'rowid', key: true
end
class Label
include DataMapper::Resource
storage_names[:default] = 'Label'
property :id, String, field: 'Key', key: true
property :name, String, field: 'Name'
end
DataMapper.finalize
quiver_book_metadata = { name: 'Snippets.me', uuid: 'Snippets' }
FileUtils::mkdir_p QUIVER_BOOK_FILENAME
quiver_book_meta_filename = File.join(QUIVER_BOOK_FILENAME, 'meta.json')
File.open(quiver_book_meta_filename, 'w') do
|file| file.write JSON.pretty_generate(quiver_book_metadata)
end
Snippet.all(:name.not => nil, :removed => nil).each do |snippet|
quiver_note_uuid = Digest::SHA1.hexdigest "Snippets.me #{snippet.id}"
quiver_note_meta_data = {
uuid: quiver_note_uuid,
title: snippet.name,
created_at: snippet.created,
updated_at: snippet.created,
tags: snippet.labels.map(&:name).to_a,
}
quiver_note_content_data = {
title: snippet.name,
cells: [
snippet.comment ? { type: 'text', data: snippet.comment } : nil,
snippet.code ? { type: 'code', language: snippet.language_name, data: snippet.code } : nil,
].compact
}
quiver_note_filename = File.join(QUIVER_BOOK_FILENAME, "#{quiver_note_uuid}.qvnote")
quiver_note_meta_filename = File.join(quiver_note_filename, 'meta.json')
quiver_note_content_filename = File.join(quiver_note_filename, 'content.json')
FileUtils::mkdir_p quiver_note_filename
File.open(quiver_note_meta_filename, 'w') do |file|
file.write JSON.pretty_generate(quiver_note_meta_data)
end
FileUtils::mkdir_p quiver_note_filename
File.open(quiver_note_content_filename, 'w') do |file|
file.write JSON.pretty_generate(quiver_note_content_data)
end
end
@19Rafa
Copy link

19Rafa commented Mar 24, 2020

la casa amarilla

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment