#!/usr/bin/env ruby $:.unshift File.expand_path("../../lib", __FILE__) require "joplin" require 'thor' require "joplin/token" DIVIDER = %Q( ) class MyCLI < Thor class_option :token, :type => :string #, required: true class_option :help, :type => :boolean class_option :version, :type => :boolean map ["-v", "--version"] => :version map ["-h", "--help"] => :help option :token option :'dry-run', desc: "dry-run", aliases: '-n' desc :clean, "clean unused resources" def clean Joplin::token = options[:token] || Joplin::Token.get puts "Please wait, this can take while." Joplin::Resource.orphaned.map { |r| r.delete if not options['dry-run'] would = "would have " if options['dry-run'] puts "#{would}deleted #{r.id}" } end method_options :force => :boolean desc "version", "get version of program" def version puts Joplin::VERSION end desc :nb2n, "concate all notes in a notebook to one note. Possible PDF export" long_desc "The idea is to make a big note from all the notes in a notebook. PDF export or whatever export can happen from that. The notes are concatenated with a separator." option :token option :type, :type => :string def nb2n(query) Joplin::token = options[:token] || Joplin::Token.get results = Joplin.search(query, { type: 'folder' }) nb = results[0]; if not (nb and nb['title'] == query) abort "notebook #{query} not found" end notebook = Joplin::Notebook.new nb['id'] notes = notebook.notes new_note = Joplin::Note.new new_note.title = query new_note.body = notes.map { |n| "\# #{n.title}\n\n#{n.body}" }.join(DIVIDER) new_note.save! puts "Saved: #{new_note.title} with id: #{new_note.id}" end desc :build, "build a note from a note containing references" long_desc %Q(Takes a note and looks up all the referenced notes and makes a new note from those. The notes are concatenated with a separator.\n\nIf you have a line with 'title: a title' then it will be used for the title of the built email.) def build(id) token note = Joplin::Note.new id notes = [] title = nil note.body.each_line { |line| title = $1 if line =~ /title: (.*)$/ line =~ /\(:\/([^)]+)\)/ notes.push Joplin::Note.new $1 if $1 } new_note = Joplin::Note.new new_note.title = title || "built: #{note.title}" new_note.body = notes.map { |n| "\# #{n.title}\n\n#{n.body}" }.join(DIVIDER) new_note.save! puts "Saved: #{new_note.title} with id: #{new_note.id}" end private def token() Joplin::token = options[:token] || Joplin::Token.get raise "Couldn't find token in local database and it wasn't passed as an option. You better check yourself!" if not Joplin::token end end MyCLI.start(ARGV)