Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created April 30, 2020 15:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttscoff/0d584d26882f6ee3e5bd2092e02313d0 to your computer and use it in GitHub Desktop.
Save ttscoff/0d584d26882f6ee3e5bd2092e02313d0 to your computer and use it in GitHub Desktop.
Markdown files to Quiver JSON
#!/usr/bin/env ruby
require 'json'
require 'fileutils'
notebook = File.expand_path('~/Desktop/NewSnippets.qvnotebook')
FileUtils.mkdir_p(notebook)
nbmeta = {
"name" => "NewSnippets",
"uuid" => %x{uuidgen}.strip
}
nbmetafile = File.join(notebook, 'meta.json')
File.open(nbmetafile, 'w') {|f|
f.puts JSON.dump(nbmeta)
}
def convert_lang(id)
lang = case id
when 'conf'
'apache_conf'
when 'applescript'
'applescript'
when 'bash'
'sh'
when 'c', 'cpp'
'c_cpp'
when 'coffee'
'coffee'
when 'css'
'css'
when 'diff'
'diff'
when 'gitignore'
'gitignore'
when 'go'
'golang'
when 'haml'
'haml'
when 'handlebars'
'handlebars'
when 'html', 'htm'
'html'
when 'ini'
'ini'
when 'jade'
'jade'
when 'js'
'javascript'
when 'json'
'json'
when 'jsp'
'jsp'
when 'jsx'
'jsx'
when 'latex'
'latex'
when 'less'
'less'
when 'lisp'
'lisp'
when 'lua'
'lua'
when 'makefile'
'makefile'
when 'markdown', 'md', 'txt'
'markdown'
when 'objc', 'm', 'h'
'objectivec'
when 'pl'
'perl'
when 'pgsql'
'pgsql'
when 'php'
'php'
when 'py'
'python'
when 'r'
'r'
when 'rdoc'
'rdoc'
when 'rhtml'
'rhtml'
when 'rb'
'ruby'
when 'ruby'
'ruby'
when 'sass'
'sass'
when 'scala'
'scala'
when 'scss'
'scss'
when 'sql'
'sql'
when 'styl'
'stylus'
when 'svg'
'svg'
when 'swift'
'swift'
when 'tcl'
'tcl'
when 'tex'
'tex'
when 'textile'
'textile'
when 'twig'
'twig'
when 'xml'
'xml'
when 'xquery'
'xquery'
when 'yaml'
'yaml'
else
id
end
lang
end
def split_cells(text, lang)
cells = []
lines = text.split(/[\n\r]/)
cell = {}
cell["type"] = 'markdown'
data = []
tabbed = false
lines.each {|l|
if cell["type"] == 'markdown' && (l =~ /^(#|\/\/|<!|[\/\(]\*)/ || l =~ /^(\t| {4,}).*\S+/)
newcell = {}
newcell["type"] = 'code'
newcell["language"] = lang
unless data.empty?
cell["data"] = data.join("\n")
cells.push cell
end
cell = newcell
data = []
if l =~ /^(\t| {4,})\S+/
tabbed = true
data.push(l.sub(/^(\t| {4})/, ''))
end
elsif cell["type"] == 'code' && tabbed && l =~ /^\S/
newcell = {}
newcell["type"] = 'markdown'
unless data.empty?
cell["data"] = data.join("\n")
cells.push cell
end
cell = newcell
tabbed = false
data = []
data.push(l)
elsif l =~ /^`{3,}(.*)?$/
newcell = {}
blocklang = convert_lang($1) || lang
if cell["type"] == 'markdown'
newcell["type"] = 'code'
newcell["language"] = blocklang
else
newcell["type"] = 'markdown'
end
unless data.empty?
cell["data"] = data.join("\n")
cells.push cell
data = []
end
cell = newcell
else
data.push(tabbed ? l.sub(/^(\t| {4})/, '') : l)
end
}
unless data.empty?
cell["data"] = data.join("\n")
cells.push cell
end
cells
end
Dir.chdir(File.expand_path('~/Desktop/Snippets'))
Dir.glob("*.md").each {|file|
data = file.scan(/(.*?)\.(\w+)\.md$/)[0]
begin
title = data[0]
lang = convert_lang(data[1])
text = IO.read(file)
content = {
'title' => title,
'cells' => split_cells(text, lang)
}
f = File.open(file, 'r')
created = f.stat.mtime.strftime('%s')
updated = f.stat.ctime.strftime('%s')
f.close
uuid = %x{uuidgen}.strip
meta = {
"created_at" => created,
"tags" => [lang],
"title" => title,
"updated_at" => updated,
"uuid" => uuid
}
outfolder = File.join(notebook, title + '.qvnote')
FileUtils.mkdir_p(outfolder)
metafile = File.join(outfolder, 'meta.json')
contentfile = File.join(outfolder, 'content.json')
File.open(metafile, 'w') {|f|
f.puts JSON.dump(meta)
}
File.open(contentfile, 'w') {|f|
f.puts JSON.dump(content)
}
rescue Exception => e
puts "Error processing #{file}"
p e
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment