-
-
Save ognjenio/1935b4e3b81bd7412d4849e2ed875866 to your computer and use it in GitHub Desktop.
Script for quick adding notes to Dendron
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## To add extract the open graph things | |
require 'optparse' | |
require 'metainspector' | |
def note(title, url, tags, content, desc = nil) | |
return %Q|--- | |
title: #{title} | |
desc: #{desc if !desc.to_s.empty?} | |
#{"source: #{url if !url.to_s.empty?}"} | |
updated: #{Time.now.to_i * 1000} | |
created: #{Time.now.to_i * 1000} | |
--- | |
#{content if !content.to_s.empty?} | |
#{tags.map{|x| "[[tags/#{x}.md]]"}.join("\n") if tags.to_a.any?}| | |
end | |
def pa(string) | |
separator = "-" | |
# Replace accented chars with their ASCII equivalents. | |
parameterized_string = string.dup.downcase | |
# Turn unwanted chars into the separator. | |
parameterized_string.gsub!(/[^a-z0-9\-_]+/, separator) | |
re_duplicate_separator = /-{2,}/ | |
re_leading_trailing_separator = /^-|-$/ | |
# No more than one of the separator in a row. | |
parameterized_string.gsub!(re_duplicate_separator, separator) | |
# Remove leading/trailing separator. | |
parameterized_string.gsub!(re_leading_trailing_separator, "".freeze) | |
parameterized_string | |
end | |
url = title = path = tags = content = desc = nil | |
force = false | |
OptionParser.new do |opts| | |
opts.banner = "Usage: d.rb [options]" | |
opts.on("-tTITLE", "--title=TITLE", "Title") do |v| | |
title = v | |
end | |
opts.on("-uURL", "--url=URL", "URL, will be used to retrieve meta") do |v| | |
url = v | |
end | |
opts.on("-f", "--force", "Force overwrite if file already exists") do |v| | |
force = true | |
end | |
opts.on("-aTAGS", "--tags=TAGS", "Tags, space sparated") do |v| | |
tags = v.split(" ") | |
end | |
opts.on("-dDESC", "--desc=DESC", "Description") do |v| | |
desc = v | |
end | |
opts.on("-pPATH", "--path=PATH", "Path, default 'resources/article'") do |v| | |
path = v | |
end | |
opts.on("-cCONTENT", "--content=CONTENT", "The body of the note") do |v| | |
content = v | |
end | |
opts.on("-h", "--help", "Prints this help") do | |
puts opts | |
exit | |
end | |
end.parse! | |
if title.to_s.empty? and url.to_s.empty? | |
return | |
end | |
if !url.empty? | |
page = MetaInspector.new(url) | |
url = page.untracked_url | |
if title.to_s.empty? | |
title = page.title | |
end | |
if title.to_s.empty? | |
title = url.split("/").last.split("?").first.gsub("-", " ").split.map(&:capitalize).join(' ') | |
end | |
desc = page.best_description | |
end | |
path = 'resources/articles' if !path | |
BASE = "" | |
fp = "#{BASE}#{path}/" + pa(title) + '.md' | |
if !File.exists?(fp) or force | |
File.open(fp, 'w') do |f| | |
f.write note(title, url, tags, content, desc) | |
end | |
puts "[CREATED] #{fp}" | |
else | |
puts "[ALREADY EXISTS] #{fp}" | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment