Last active
March 22, 2024 18:42
This script takes simple Markdown lists and converts them for use in Bike
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
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
# This script takes simple Markdown lists and converts them for use in | |
# Bike (https://www.hogbaysoftware.com/bike/), a Mac outliner app from | |
# Hog Bay Software. | |
# | |
# It doesn't handle nested code blocks or any non-list Markdown. It just | |
# turns simple lists into text Bike will recognize when it's pasted into | |
# a Bike document. Multiple paragraphs in a list item become additional | |
# nodes at the same level. | |
require 'shellwords' | |
require 'fcntl' | |
stdin = $stdin.read.strip if $stdin.stat.size.positive? || $stdin.fcntl(Fcntl::F_GETFL, 0).zero? | |
if !stdin && ARGV.count != 1 | |
exe = File.basename(__FILE__) | |
puts "#{exe} requires one argument" | |
puts "#{exe} [markdown file]" | |
Process.exit 1 | |
end | |
source = ARGV[0] | |
# Read input file | |
content = stdin ? stdin : IO.read(source).strip | |
# Remove spaces from empty lines | |
content.gsub!(/^\s*\n/, "\n") | |
# Replace consecutive empty lines with a single newline | |
content.gsub!(/\n+/, "\n") | |
# Replace 2 or 4 spaces with tabs (based on detected indentation) | |
m = content.match(/^ {2,4}/) # find first indentation | |
indentation = m ? m[0].length : 1 # determine whether first indent is 2 or 4 spaces | |
content.gsub!(/^( {#{indentation}})+(?=\S)/) do |m| # Replace leading spaces with tabs | |
mtch = Regexp.last_match(0) | |
mtch ? "\t" * (mtch.length / indentation) : m # tab * detected indent / detected indentation | |
end | |
# Replace markdown list markers | |
content.gsub!(/(\s*)[+*\-] /, '\1') | |
# Copy result to clibpoard | |
`echo #{Shellwords.escape(content)} | pbcopy` | |
puts "Results in clipboard. Paste into a Bike document." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment